public void install() { ISourceViewer sourceViewer= getSourceViewer(); if (sourceViewer == null) return; StyledText text= sourceViewer.getTextWidget(); if (text == null || text.isDisposed()) return; sourceViewer.addTextInputListener(this); IDocument document= sourceViewer.getDocument(); if (document != null) document.addDocumentListener(this); }
/** * Retrieves the annotation model from the given source viewer. * * @param sourceViewer the source viewer * @return the source viewer's annotation model or <code>null</code> if none can be found * @since 3.0 */ protected IAnnotationModel findAnnotationModel(ISourceViewer sourceViewer) { if(sourceViewer != null) return sourceViewer.getAnnotationModel(); return null; }
@Override public void verifyKey(VerifyEvent event) { if (matched && event.keyCode == 27) { Point range = sourceViewer.getSelectedRange(); try { sourceViewer.getDocument().replace(range.x - longForm.length(), longForm.length(), shortcut); sourceViewer.setSelectedRange(range.x - longForm.length() + shortcut.length(), 0); matched = false; } catch (BadLocationException e) { handleBadLocationException(e); } } }
private void restorePosition() { if (storedPosition != null && !storedPosition.isDeleted() && viewer.getDocument() != null) { viewer.setSelectedRange(storedPosition.offset, storedPosition.length); viewer.revealRange(storedPosition.offset, storedPosition.length); } storedPosition = null; }
public void setInformation(String content) { super.setInformation(content); IDocument doc= getViewer().getDocument(); if (doc == null) return; // ensure that we can scroll enough ensureScrollable(); String start= null; if (IJavaPartitions.JAVA_DOC.equals(fPartition)) { start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$ } else if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(fPartition)) { start= "/*" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$ } if (start != null) { try { doc.replace(0, 0, start); int startLen= start.length(); getViewer().setDocument(doc, startLen, doc.getLength() - startLen); } catch (BadLocationException e) { // impossible Assert.isTrue(false); } } getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel); }
/** * Selects and reveals the specified ranges in this text editor. * * @param selectionStart the offset of the selection * @param selectionLength the length of the selection * @param revealStart the offset of the revealed range * @param revealLength the length of the revealed range * @since 3.0 */ protected void selectAndReveal(int selectionStart, int selectionLength, int revealStart, int revealLength) { if (fSourceViewer == null) return; ISelection selection= getSelectionProvider().getSelection(); if (selection instanceof ITextSelection) { ITextSelection textSelection= (ITextSelection) selection; if (textSelection.getOffset() != 0 || textSelection.getLength() != 0) markInNavigationHistory(); } StyledText widget= fSourceViewer.getTextWidget(); widget.setRedraw(false); { adjustHighlightRange(revealStart, revealLength); fSourceViewer.revealRange(revealStart, revealLength); fSourceViewer.setSelectedRange(selectionStart, selectionLength); markInNavigationHistory(); } widget.setRedraw(true); }
protected XtextEditor openEditor(String string) throws Exception { int cursor = string.indexOf('|'); String fileExtension = getFileExtension(); IFile file = createFile("foo/myfile" + files.size() + "." + fileExtension, string.replace("|", "")); files.add(file); XtextEditor editor = openEditor(file); editor.getInternalSourceViewer().setSelectedRange(cursor, 0); editor.getInternalSourceViewer().getTextWidget().setFocus(); return editor; }
ISourceViewer sourceViewer = getSourceViewer(); // Get your SourceViewer StyledText styledText = sourceViewer.getTextWidget(); int docOffset = 0; if (sourceViewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension = (ITextViewerExtension5)sourceViewer; docOffset = extension.widgetOffset2ModelOffset(styledText.getCaretOffset()); } else { int offset = sourceViewer.getVisibleRegion().getOffset(); docOffset = offset + styledText.getCaretOffset(); } IDocument document = sourceViewer.getDocument(); int currentLine = document.getLineOfOffset(docOffset);
/** * Returns the {@link StyledText} widget where the annotation must be drawn. * * @return the {@link StyledText} widget where the annotation must be drawn. */ public StyledText getTextWidget() { return fViewer.getTextWidget(); }
/** * @see org.eclipse.jface.text.reconciler.IReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument) */ public void setDocument( IDocument document ) { if ( sourceViewer.getAnnotationModel() == null ) { IAnnotationModel model = new AnnotationModel(); sourceViewer.setDocument( sourceViewer.getDocument(), model ); } // add annotation painter if ( paintManager == null && sourceViewer.getAnnotationModel() instanceof IAnnotationModelExtension ) { AnnotationPainter ap = new AnnotationPainter( sourceViewer, null ); ap.addAnnotationType( "DEFAULT" ); //$NON-NLS-1$ ap.setAnnotationTypeColor( "DEFAULT", BrowserCommonActivator.getDefault().getColor( new RGB( 255, 0, 0 ) ) ); //$NON-NLS-1$ sourceViewer.getAnnotationModel().addAnnotationModelListener( ap ); FilterCharacterPairMatcher cpm = new FilterCharacterPairMatcher( sourceViewer, parser ); MatchingCharacterPainter mcp = new MatchingCharacterPainter( sourceViewer, cpm ); mcp.setColor( BrowserCommonActivator.getDefault().getColor( new RGB( 159, 159, 159 ) ) ); paintManager = new PaintManager( sourceViewer ); paintManager.addPainter( ap ); paintManager.addPainter( mcp ); } }
private boolean isMasked(int offset) { IDocument document= getSourceViewer().getDocument(); try { return fEscapeCharacter == document.getChar(offset - 1); } catch (BadLocationException e) { } return false; } }
private static final IRegion getSignedSelection(ISourceViewer sourceViewer) { Point viewerSelection= sourceViewer.getSelectedRange(); StyledText text= sourceViewer.getTextWidget(); Point selection= text.getSelectionRange(); if (text.getCaretOffset() == selection.x) { viewerSelection.x= viewerSelection.x + viewerSelection.y; viewerSelection.y= -viewerSelection.y; } return new Region(viewerSelection.x, viewerSelection.y); }
fSourceViewer.setDocument(document, model); fSourceViewer.setEditable(isEditable()); fSourceViewer.showAnnotations(model != null); fInitialCaret= fSourceViewer.getTextWidget().getCaret(); fSourceViewer.getTextWidget().invokeAction(ST.TOGGLE_OVERWRITE); handleInsertModeChanged();
/** * @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; } } } }
public void assistSessionStarted(ContentAssistEvent event) { Point selectedRange = viewer.getSelectedRange(); storePosition(selectedRange.x, selectedRange.y); }
/** * Returns for the widget line number for the given document line number. * * @param line the absolute line number * @return the line number relative to the viewer's visible region * @throws BadLocationException if <code>line</code> is not valid in the viewer's document */ private int getWidgetLineNumber(int line) throws BadLocationException { if (fSourceViewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension= (ITextViewerExtension5) fSourceViewer; return extension.modelLine2WidgetLine(line); } IRegion region= fSourceViewer.getVisibleRegion(); int firstLine= fSourceViewer.getDocument().getLineOfOffset(region.getOffset()); return line - firstLine; }
/** * Perform the next completion. */ private void completeNext() { try { fDocument.replace(fLastCompletion.startOffset, fLastCompletion.length, fLastCompletion.next()); //next() will already advance } catch (BadLocationException e) { // we should never get here. different from other places to notify the user. log(e); clearState(); return; } // move the caret to the insertion point ISourceViewer sourceViewer= ((AbstractTextEditor) getTextEditor()).getSourceViewer(); sourceViewer.setSelectedRange(fLastCompletion.startOffset + fLastCompletion.length, 0); sourceViewer.revealRange(fLastCompletion.startOffset, fLastCompletion.length); fExitStrategy.arm(((AbstractTextEditor) getTextEditor()).getSourceViewer()); }
@Override public void propertyChange(PropertyChangeEvent event) { if (colorPreferenceKeys.contains(event.getProperty())) { if (getSourceViewer() instanceof SourceViewer) { ((SourceViewer) getSourceViewer()).unconfigure(); initializeEditor(); getSourceViewer().configure(sourceViewerConfiguration); } } } };
/** Configures annotation model for spell checking. */ private void configureAsEditor(ISourceViewer viewer, Document document) { IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess(); final SourceViewerDecorationSupport support = new SourceViewerDecorationSupport(viewer, null, annotationAccess, EditorsUI.getSharedTextColors()); Iterator<?> e = new MarkerAnnotationPreferences().getAnnotationPreferences().iterator(); while (e.hasNext()) { support.setAnnotationPreference((AnnotationPreference) e.next()); } support.install(EditorsUI.getPreferenceStore()); viewer.getTextWidget().addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { support.uninstall(); } }); AnnotationModel annotationModel = new AnnotationModel(); viewer.setDocument(document, annotationModel); }
protected ISourceViewer getSourceViewer(Shell shell, final IXtextDocument xtextDocument, XtextSourceViewerConfiguration configuration) { XtextSourceViewer.Factory factory = get(XtextSourceViewer.Factory.class); ISourceViewer sourceViewer = factory.createSourceViewer(shell, null, null, false, 0); sourceViewer.configure(configuration); sourceViewer.setDocument(xtextDocument); return sourceViewer; }