下面列出了怎么用org.eclipse.jface.text.source.ISourceViewer的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
ContentAssistant ca = new ContentAssistant();
JsonContentAssistProcessor processor = createContentAssistProcessor(ca);
ca.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
ca.setInformationControlCreator(getInformationControlCreator(sourceViewer));
ca.enableAutoInsert(false);
ca.enablePrefixCompletion(false);
ca.enableAutoActivation(true);
ca.setAutoActivationDelay(100);
ca.enableColoredLabels(true);
ca.setShowEmptyList(true);
ca.setRepeatedInvocationMode(true);
ca.addCompletionListener(processor);
ca.setStatusLineVisible(true);
return ca;
}
@Override
protected void setCaretPosition(final int position) {
final ISourceViewer viewer= getSourceViewer();
final StyledText text= viewer.getTextWidget();
if (text != null && !text.isDisposed()) {
final Point selection= text.getSelection();
final int caret= text.getCaretOffset();
final int offset= modelOffset2WidgetOffset(viewer, position);
if (caret == selection.x)
text.setSelectionRange(selection.y, offset - selection.y);
else
text.setSelectionRange(selection.x, offset - selection.x);
}
}
public static void updatePreview(ISourceViewer viewer, URL previewContent, String[] substitutions,
IScriptFormatterFactory factory, Map<String, String> preferences)
{
String content = null;
if (previewContent != null)
{
try
{
final String c = new String(Util.getInputStreamAsCharArray(previewContent.openConnection()
.getInputStream(), -1, ENCODING));
content = Util.concatenate(Util.splitLines(c), LINE_SEPARATOR);
if (content != null && substitutions != null && substitutions.length > 0)
{
content = substitute(content, substitutions);
}
}
catch (IOException e)
{
IdeLog.logError(FormatterUIEplPlugin.getDefault(), e, IDebugScopes.DEBUG);
disablePreview(viewer);
return;
}
}
updatePreview(viewer, content, substitutions, factory, preferences);
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
if (xtextEditor != null) {
ITextSelection selection = (ITextSelection) xtextEditor.getSelectionProvider().getSelection();
IRegion region = new Region(selection.getOffset(), selection.getLength());
ISourceViewer internalSourceViewer = xtextEditor.getInternalSourceViewer();
IHyperlink[] hyperlinks = getDetector().detectHyperlinks(internalSourceViewer, region, false);
if (hyperlinks != null && hyperlinks.length > 0) {
IHyperlink hyperlink = hyperlinks[0];
hyperlink.open();
}
}
return null;
}
@Override
protected void doSetInput(IEditorInput input) throws CoreException {
ISourceViewer sourceViewer= getSourceViewer();
if (!(sourceViewer instanceof ISourceViewerExtension2)) {
setPreferenceStore(createCombinedPreferenceStore(input));
internalDoSetInput(input);
return;
}
// uninstall & unregister preference store listener
getSourceViewerDecorationSupport(sourceViewer).uninstall();
((ISourceViewerExtension2)sourceViewer).unconfigure();
setPreferenceStore(createCombinedPreferenceStore(input));
// install & register preference store listener
sourceViewer.configure(getSourceViewerConfiguration());
getSourceViewerDecorationSupport(sourceViewer).install(getPreferenceStore());
internalDoSetInput(input);
}
public IContentFormatter getContentFormatter( ISourceViewer sourceViewer){
ContentFormatter formatter = new ContentFormatter();
XMLFormattingStrategy formattingStrategy = new XMLFormattingStrategy();
DefaultFormattingStrategy defaultStrategy = new DefaultFormattingStrategy();
TextFormattingStrategy textStrategy = new TextFormattingStrategy();
DocTypeFormattingStrategy doctypeStrategy = new DocTypeFormattingStrategy();
PIFormattingStrategy piStrategy = new PIFormattingStrategy();
formatter.setFormattingStrategy(defaultStrategy,
IDocument.DEFAULT_CONTENT_TYPE);
formatter.setFormattingStrategy(textStrategy,
XMLPartitionScanner.XML_TEXT);
formatter.setFormattingStrategy(doctypeStrategy,
XMLPartitionScanner.XML_DOCTYPE);
formatter.setFormattingStrategy(piStrategy, XMLPartitionScanner.XML_PI);
formatter.setFormattingStrategy(textStrategy,
XMLPartitionScanner.XML_CDATA);
formatter.setFormattingStrategy(formattingStrategy,
XMLPartitionScanner.XML_START_TAG);
formatter.setFormattingStrategy(formattingStrategy,
XMLPartitionScanner.XML_END_TAG);
return formatter;
}
@Override
public IReconciler getReconciler(ISourceViewer sourceViewer) {
if (fPreferenceStore == null || !fPreferenceStore.getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED)) {
return null;
}
SpellingService spellingService = EditorsUI.getSpellingService();
if (spellingService.getActiveSpellingEngineDescriptor(fPreferenceStore) == null) {
return null;
}
//Overridden (just) to return a PyReconciler!
IReconcilingStrategy strategy = new PyReconciler(sourceViewer, spellingService);
MonoReconciler reconciler = new MonoReconciler(strategy, false);
reconciler.setIsIncrementalReconciler(false);
reconciler.setProgressMonitor(new NullProgressMonitor());
reconciler.setDelay(500);
return reconciler;
}
@Override
protected ISourceViewer createTypeScriptSourceViewer(Composite parent, IVerticalRuler verticalRuler,
IOverviewRuler overviewRuler, boolean isOverviewRulerVisible, int styles, IPreferenceStore store) {
return new AdaptedSourceViewer(parent, verticalRuler, overviewRuler, isOverviewRulerVisible, styles,
store) {
@Override
protected void handleDispose() {
super.handleDispose();
// dispose the compilation unit adapter
dispose();
fEditor.remove(this);
if (fEditor.isEmpty()) {
fEditor = null;
fSite = null;
}
fSourceViewer.remove(this);
if (fSourceViewer.isEmpty())
fSourceViewer = null;
}
};
}
@Override
protected void installTabsToSpacesConverter() {
ISourceViewer sourceViewer= getSourceViewer();
SourceViewerConfiguration config= getSourceViewerConfiguration();
if (config != null && sourceViewer instanceof ITextViewerExtension7) {
int tabWidth= config.getTabWidth(sourceViewer);
TabsToSpacesConverter tabToSpacesConverter= new TabsToSpacesConverter();
tabToSpacesConverter.setNumberOfSpacesPerTab(tabWidth);
IDocumentProvider provider= getDocumentProvider();
if (provider instanceof ICompilationUnitDocumentProvider) {
ICompilationUnitDocumentProvider cup= (ICompilationUnitDocumentProvider) provider;
tabToSpacesConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
} else
tabToSpacesConverter.setLineTracker(new DefaultLineTracker());
((ITextViewerExtension7)sourceViewer).setTabsToSpacesConverter(tabToSpacesConverter);
updateIndentPrefixes();
}
}
/**
* @since 2.3
*/
protected void selectAndRevealQuickfix(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations, List<ICompletionProposal> completionProposals) {
if (completionProposals.isEmpty()) {
return;
}
if (!(invocationContext instanceof QuickAssistInvocationContext && ((QuickAssistInvocationContext) invocationContext).isSuppressSelection())) {
ISourceViewer sourceViewer = invocationContext.getSourceViewer();
IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
Iterator<Annotation> iterator = applicableAnnotations.iterator();
while (iterator.hasNext()) {
Position pos = annotationModel.getPosition(iterator.next());
if (pos != null) {
sourceViewer.setSelectedRange(pos.getOffset(), pos.getLength());
sourceViewer.revealRange(pos.getOffset(), pos.getLength());
break;
}
}
}
}
@Override
public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
// create a content assistant:
PyCorrectionAssistant assistant = new PyCorrectionAssistant();
// next create a content assistant processor to populate the completions window
IQuickAssistProcessor processor = new PythonCorrectionProcessor(this.getEdit());
// Correction assist works on all
assistant.setQuickAssistProcessor(processor);
assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
//delay and auto activate set on PyContentAssistant constructor.
return assistant;
}
private void handleVerifyKeyPressed(VerifyEvent event) {
if (!event.doit)
return;
if (event.stateMask != SWT.MOD1)
return;
switch (event.character) {
case ' ':
fPatternEditor.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
event.doit = false;
break;
// CTRL-Z
case 'z' - 'a' + 1:
fPatternEditor.doOperation(ITextOperationTarget.UNDO);
event.doit = false;
break;
}
}
@Override
public void createPartControl(Composite parent) {
super.createPartControl(parent);
IPreferenceStore preferenceStore= getPreferenceStore();
boolean closeBrackets= preferenceStore.getBoolean(CLOSE_BRACKETS);
boolean closeStrings= preferenceStore.getBoolean(CLOSE_STRINGS);
boolean closeAngularBrackets= JavaCore.VERSION_1_5.compareTo(preferenceStore.getString(JavaCore.COMPILER_SOURCE)) <= 0;
fBracketInserter.setCloseBracketsEnabled(closeBrackets);
fBracketInserter.setCloseStringsEnabled(closeStrings);
fBracketInserter.setCloseAngularBracketsEnabled(closeAngularBrackets);
ISourceViewer sourceViewer= getSourceViewer();
if (sourceViewer instanceof ITextViewerExtension)
((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(fBracketInserter);
if (isMarkingOccurrences())
installOccurrencesFinder(false);
}
@Override
public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
return new HTMLAnnotationHover(true) {
@Override
protected boolean isIncluded(Annotation annotation) {
return isShowInOverviewRuler(annotation);
}
};
}
public String[] getConfiguredContentTypes( ISourceViewer sourceViewer )
{
return new String[]{
IDocument.DEFAULT_CONTENT_TYPE,
XMLPartitionScanner.XML_COMMENT,
XMLPartitionScanner.XML_TAG
};
}
@Override
protected void setContentAssistProcessor(ContentAssistant assistant, SourceViewerConfiguration configuration,
ISourceViewer sourceViewer) {
super.setContentAssistProcessor(assistant, configuration, sourceViewer);
assistant.setContentAssistProcessor(jsDocContentAssistProcessor, TokenTypeToPartitionMapper.JS_DOC_PARTITION);
assistant.setContentAssistProcessor(null, TokenTypeToPartitionMapper.REG_EX_PARTITION);
assistant.setContentAssistProcessor(null, TokenTypeToPartitionMapper.TEMPLATE_LITERAL_PARTITION);
assistant.setContentAssistProcessor(null, TerminalsTokenTypeToPartitionMapper.SL_COMMENT_PARTITION);
assistant.setContentAssistProcessor(null, TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION);
}
/**
* {@inheritDoc} Code copied from parent. Override required to run in UI because of getSourceViewer, which creates a new Shell.
*/
@Override
public ContentAssistProcessorTestBuilder assertMatchString(final String matchString) throws Exception {
BadLocationException exception = UiThreadDispatcher.dispatchAndWait(new Function<BadLocationException>() {
@Override
public BadLocationException run() {
String currentModelToParse = getModel();
final XtextResource xtextResource = loadHelper.getResourceFor(new StringInputStream(currentModelToParse));
final IXtextDocument xtextDocument = getDocument(xtextResource, currentModelToParse);
XtextSourceViewerConfiguration configuration = get(XtextSourceViewerConfiguration.class);
Shell shell = new Shell();
try {
ISourceViewer sourceViewer = getSourceViewer(shell, xtextDocument, configuration);
IContentAssistant contentAssistant = configuration.getContentAssistant(sourceViewer);
String contentType = xtextDocument.getContentType(currentModelToParse.length());
if (contentAssistant.getContentAssistProcessor(contentType) != null) {
ContentAssistContext.Factory factory = get(ContentAssistContext.Factory.class);
ContentAssistContext[] contexts = factory.create(sourceViewer, currentModelToParse.length(), xtextResource);
for (ContentAssistContext context : contexts) {
Assert.assertTrue("matchString = '" + matchString + "', actual: '" + context.getPrefix() + "'", "".equals(context.getPrefix())
|| matchString.equals(context.getPrefix()));
}
} else {
Assert.fail("No content assistant for content type " + contentType);
}
} catch (BadLocationException e) {
return e;
} finally {
shell.dispose();
}
return null;
}
});
if (exception != null) {
throw exception;
}
return this;
}
protected void setupPresentationReconciler(PresentationReconciler reconciler, ISourceViewer sourceViewer) {
// Must be called from UI thread
assertTrue(Display.getCurrent() != null);
// Create a token registry for given sourceViewer
TokenRegistry tokenRegistry = new TokenRegistry(colorManager, stylingPrefs) {
@Override
protected void handleTokenModified(Token token) {
sourceViewer.invalidateTextPresentation();
}
};
addConfigurationScopedOwned(sourceViewer, tokenRegistry);
ArrayList2<AbstractLangScanner> scanners = new ArrayList2<>();
for(LangPartitionTypes partitionType : getPartitionTypes()) {
String contentType = partitionType.getId();
AbstractLangScanner scanner = createScannerFor(Display.getCurrent(), partitionType, tokenRegistry);
scanners.add(scanner);
DefaultDamagerRepairer dr = getDamagerRepairer(scanner, contentType);
reconciler.setDamager(dr, contentType);
reconciler.setRepairer(dr, contentType);
}
}
public IReconciler getReconciler( ISourceViewer sourceViewer )
{
// Creates an instance of MonoReconciler with the specified strategy,
// and is not incremental.
return new MonoReconciler( new ScriptReconcilingStrategy( sourceViewer ),
false );
}
public void uninstall() {
ISourceViewer sourceViewer= getSourceViewer();
if (sourceViewer != null)
sourceViewer.removeTextInputListener(this);
IDocumentProvider documentProvider= getDocumentProvider();
if (documentProvider != null) {
IDocument document= documentProvider.getDocument(getEditorInput());
if (document != null)
document.removeDocumentListener(this);
}
}
private static void assertNoProposals(IProgressMonitor monitor,
JsniMethodBodyCompletionProposalComputer jcpc,
CompilationUnitEditor cuEditor, ISourceViewer viewer, int offset) {
assertEquals(0, jcpc.computeCompletionProposals(
new JavaContentAssistInvocationContext(viewer, offset, cuEditor),
monitor).size());
}
public ITextHover getTextHover( ISourceViewer sourceViewer,
String contentType, int stateMask )
{
if ( !( JSPartitionScanner.JS_COMMENT.equals( contentType )
//|| JSPartitionScanner.JS_KEYWORD.equals( contentType )
|| JSPartitionScanner.JS_STRING.equals( contentType ) ) )
{
return new ScriptDebugHover( );
}
return super.getTextHover( sourceViewer, contentType, stateMask );
}
public void setupPresentationReconciler(PresentationReconciler reconciler, ISourceViewer sourceViewer)
{
DTDSourceConfiguration.getDefault().setupPresentationReconciler(reconciler, sourceViewer);
DefaultDamagerRepairer dr = new ThemeingDamagerRepairer(getXMLScanner());
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setDamager(dr, DEFAULT);
reconciler.setRepairer(dr, DEFAULT);
dr = new ThemeingDamagerRepairer(getPreProcessorScanner());
reconciler.setDamager(dr, PRE_PROCESSOR);
reconciler.setRepairer(dr, PRE_PROCESSOR);
dr = new ThemeingDamagerRepairer(getCDATAScanner());
reconciler.setDamager(dr, CDATA);
reconciler.setRepairer(dr, CDATA);
dr = new ThemeingDamagerRepairer(getXMLTagScanner());
reconciler.setDamager(dr, TAG);
reconciler.setRepairer(dr, TAG);
dr = new ThemeingDamagerRepairer(getCommentScanner());
reconciler.setDamager(dr, COMMENT);
reconciler.setRepairer(dr, COMMENT);
}
private ICompletionProposal[] computeCompletionProposals(
final IXtextDocument xtextDocument, int cursorPosition, Shell shell)
throws BadLocationException {
XtextSourceViewerConfiguration configuration = get(
XtextSourceViewerConfiguration.class);
ISourceViewer sourceViewer = getSourceViewer(shell, xtextDocument,
configuration);
return computeCompletionProposals(xtextDocument, cursorPosition,
configuration, sourceViewer);
}
@Override
public IPresentationReconciler getPresentationReconciler(
ISourceViewer sourceViewer) {
PresentationReconciler reconciler = (PresentationReconciler) super.getPresentationReconciler(sourceViewer);
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(jsniScanner);
reconciler.setDamager(dr, GWTPartitions.JSNI_METHOD);
reconciler.setRepairer(dr, GWTPartitions.JSNI_METHOD);
return reconciler;
}
private RenameRefactoringInfo getRenameRefactoringInfo() {
ITextSelection textSelection = WorkbenchUtils.getActiveTextSelection();
if (textSelection == null) {
return null;
}
IEditorPart editor = WorkbenchUtils.getActiveEditor(false);
if (editor == null) {
return null;
}
ISourceViewer textViewer = (ISourceViewer)editor.getAdapter(ISourceViewer.class);
SourceCodeTextEditor textEditor = null;
if (editor instanceof SourceCodeTextEditor) {
textEditor = (SourceCodeTextEditor) editor;
}
else{
return null;
}
PstLeafNode pstLeafNode = ModulaEditorSymbolUtils.getIdentifierPstLeafNode(textEditor, textSelection.getOffset());
IModulaSymbol symbol = ModulaEditorSymbolUtils.getModulaSymbol(
textViewer.getDocument(), pstLeafNode);
WordAndRegion wordUnderCursor = SelectionUtils.getWordUnderCursor(false);
if (symbol == null || wordUnderCursor == null) {
return null;
}
IFile ifileEdited = WorkbenchUtils.getIFileFrom(editor.getEditorInput());
if (ifileEdited == null) {
return null;
}
return new RenameRefactoringInfo(ifileEdited, wordUnderCursor.word, symbol);
}
/**
* Finds the previous position before the given position.
*
* @param position the current position
* @return the previous position
*/
protected int findPreviousPosition(int position) {
ISourceViewer viewer = getSourceViewer();
int widget = -1;
int previous = position;
while (previous != BreakIterator.DONE && widget == -1) { // XXX: optimize
previous = fIterator.preceding(previous);
if (previous != BreakIterator.DONE) {
widget = modelOffset2WidgetOffset(viewer, previous);
}
}
IDocument document = viewer.getDocument();
LinkedModeModel model = LinkedModeModel.getModel(document, position);
if (model != null && previous != BreakIterator.DONE) {
LinkedPosition linkedPosition = model.findPosition(new LinkedPosition(document, position, 0));
if (linkedPosition != null) {
int linkedPositionOffset = linkedPosition.getOffset();
if (position != linkedPositionOffset && previous < linkedPositionOffset) {
previous = linkedPositionOffset;
}
} else {
LinkedPosition previousLinkedPosition = model
.findPosition(new LinkedPosition(document, previous, 0));
if (previousLinkedPosition != null) {
int previousLinkedPositionEnd = previousLinkedPosition.getOffset()
+ previousLinkedPosition.getLength();
if (position != previousLinkedPositionEnd && previous < previousLinkedPositionEnd) {
previous = previousLinkedPositionEnd;
}
}
}
}
return previous;
}
private void updateVisibility() {
if (fPopup != null && !fPopup.isDisposed() && fDelayJobFinished) {
boolean visible= false;
//TODO: Check for visibility of linked position, not whether popup is outside of editor?
if (fRenameLinkedMode.isCaretInLinkedPosition()) {
StyledText textWidget= fEditor.getViewer().getTextWidget();
Rectangle eArea= Geometry.toDisplay(textWidget, textWidget.getClientArea());
Rectangle pBounds= fPopup.getBounds();
pBounds.x-= GAP;
pBounds.y-= GAP;
pBounds.width+= 2 * GAP;
pBounds.height+= 2 * GAP;
if (eArea.intersects(pBounds)) {
visible= true;
}
}
if (visible && ! fPopup.isVisible()) {
ISourceViewer viewer= fEditor.getViewer();
if (viewer instanceof IWidgetTokenOwnerExtension) {
IWidgetTokenOwnerExtension widgetTokenOwnerExtension= (IWidgetTokenOwnerExtension) viewer;
visible= widgetTokenOwnerExtension.requestWidgetToken(this, WIDGET_PRIORITY);
}
} else if (! visible && fPopup.isVisible()) {
releaseWidgetToken();
}
fPopup.setVisible(visible);
}
}
private void setRedraw(ISourceViewer view, boolean value) {
if (view != null) {
try {
// some viewers may not have a text widget
view.getTextWidget().setRedraw(value);
} catch (Exception e) {}
}
}
@Override
public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
return new String[] {
IDocument.DEFAULT_CONTENT_TYPE,
ImpexDocumentPartitioner.IMPEX_COMMENT,
ImpexDocumentPartitioner.IMPEX_DATA,
ImpexDocumentPartitioner.IMPEX_HEADER,
ImpexDocumentPartitioner.IMPEX_INSTRUCTION };
}