/** * Assumes the given position to specify offset and length of a line to be painted. * * @param position the specification of the line to be painted */ private void drawHighlightLine(Position position) { // Don't draw if highlight current line is turned off if (!fEnabled) { return; } Rectangle rect = getLineRectangle(position); if (rect == null) { return; } if (!fViewer.getTextWidget().isDisposed()) { fViewer.getTextWidget().redraw(rect.x, rect.y, rect.width, rect.height, true); } }
public synchronized Map<Annotation, Position> createNewOccurenceAnnotations( ISourceViewer viewer) { int cursorPosition = viewer.getTextWidget().getCaretOffset(); IChildSelector<ASTElementNode> selector = new IndexASTSelector(cursorPosition, 0); ASTElementNode selectedTreeNode = TreeIterator.select(selector, documentModel.getTreeRepresentation()); IModelElement modelElement = null; IASTElement selectedTreeContents = selectedTreeNode.getElement(); Template template = null; if (selectedTreeContents instanceof ModelASTElement) { modelElement = ((ModelASTElement) selectedTreeContents).getModelElement(); template = selectedTreeContents.getTemplate(); } Map<Annotation, Position> result = new HashMap<Annotation, Position>(); if (modelElement != null && template .getAdapter(IPresentationOptionsProvider.class) .markOccurences( selectedTreeNode, cursorPosition - selectedTreeNode.getAbsoluteOffset(0))) { Collection<Position> positions = documentModel.getOccurences(modelElement); for (Position position : positions) { result.put(new Annotation("hub.sam.tef.occurence", false, "A OCCURENCE"), position); } return result; } else { return result; } }
/* * @see IPainter#paint(int) */ public void paint(int reason) { if (fViewer == null) { return; } if (fViewer.getDocument() == null) { deactivate(false); return; } // initialization if (!fIsActive) { StyledText textWidget = fViewer.getTextWidget(); textWidget.addLineBackgroundListener(this); textWidget.addPaintListener(this); fPositionManager.managePosition(fCurrentLine); fIsActive = true; } // This forces redraw of the line highlight if (updateHighlightLine()) { // clear last line // Fix the background colors for tokens that didn't have the same as line! if (isOpaque() && !fLastLine.isDeleted() && fViewer instanceof ITextViewerExtension2) { ITextViewerExtension2 ext = (ITextViewerExtension2) fViewer; try { ext.invalidateTextPresentation(fLastLine.getOffset(), fLastLine.getLength()); } catch (Exception e) { IdeLog.logError(CommonEditorPlugin.getDefault(), e); } } drawHighlightLine(fLastLine); // draw new line drawHighlightLine(fCurrentLine); } }
/** * Workaround a bug in 64 bit GTK linux that causes the active editor to steal paste insertions * from the omnibox and Glance find UI (dartbug.com/13693). */ public static void addGTKPasteHack(final ISourceViewer viewer) { if (Util.isLinux()) { viewer .getTextWidget() .addVerifyListener( new VerifyListener() { @Override public void verifyText(VerifyEvent e) { Control focusControl = Display.getDefault().getFocusControl(); // If the focus control is not our text we have no business handling insertions. // Redirect to the rightful target if (focusControl != viewer.getTextWidget()) { if (focusControl instanceof Text) { ((Text) focusControl).setText(e.text); e.doit = false; } if (focusControl instanceof Combo) { ((Combo) focusControl).setText(e.text); e.doit = false; } } } }); } }
/** * Returns the location of the caret as offset in the source viewer's input document. * * @return the caret location */ private int getModelCaret() { int widgetCaret = fViewer.getTextWidget().getCaretOffset(); if (fViewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer; return extension.widgetOffset2ModelOffset(widgetCaret); } IRegion visible = fViewer.getVisibleRegion(); return widgetCaret + visible.getOffset(); }
/** * Creates a simple (i.e., not extended) completion context. * * <p>This context may be used for snippet completions. */ public static Optional<JavaContentAssistInvocationContext> newContentAssistInvocationContext( IEditorPart editor) { if (editor instanceof JavaEditor) { JavaEditor ed = (JavaEditor) editor; ISourceViewer viewer = ed.getViewer(); int offset = viewer.getTextWidget().getCaretOffset(); return of(new JavaContentAssistInvocationContext(viewer, offset, ed)); } return absent(); }
/** * Creates an Erlang source preview updater for the given viewer, configuration and preference * store. * * @param viewer the viewer * @param configuration the configuration * @param preferenceStore the preference store */ public ErlangSourceViewerUpdater( final ISourceViewer viewer, final ErlangSourceViewerConfiguration configuration, final IPreferenceStore preferenceStore) { Assert.isNotNull(viewer); Assert.isNotNull(configuration); Assert.isNotNull(preferenceStore); final IPropertyChangeListener fontChangeListener = new IPropertyChangeListener() { /* * @see * org.eclipse.jface.util.IPropertyChangeListener#propertyChange * (org.eclipse.jface.util.PropertyChangeEvent) */ public void propertyChange(final PropertyChangeEvent event) { if (PreferenceConstants.EDITOR_TEXT_FONT.equals(event.getProperty())) { final Font font = JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT); viewer.getTextWidget().setFont(font); } } }; final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() { /* * @see * org.eclipse.jface.util.IPropertyChangeListener#propertyChange * (org.eclipse.jface.util.PropertyChangeEvent) */ public void propertyChange(final PropertyChangeEvent event) { // if (configuration.affectsTextPresentation(event)) { // configuration.handlePropertyChangeEvent(event); // viewer.invalidateTextPresentation(); // } } }; viewer .getTextWidget() .addDisposeListener( new DisposeListener() { /* * @see * org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse * .swt.events.DisposeEvent) */ public void widgetDisposed(final DisposeEvent e) { preferenceStore.removePropertyChangeListener(propertyChangeListener); JFaceResources.getFontRegistry().removeListener(fontChangeListener); } }); JFaceResources.getFontRegistry().addListener(fontChangeListener); preferenceStore.addPropertyChangeListener(propertyChangeListener); }
public void install() { ISourceViewer sourceViewer = editor.getISourceViewer(); 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); }
protected Position getCurrentLinePosition() { Point selection = fViewer.getTextWidget().getSelectionRange(); if (selection.y != 0) { return null; } try { int line = fViewer.getDocument().getLineOfOffset(selection.x); return new Position(fViewer.getDocument().getLineOffset(line), 0); } catch (BadLocationException e) { return null; } }
private void drawCurrentLine(LineBackgroundEvent event, final IRegion lineRegion) { final StyledText textWidget = fViewer.getTextWidget(); final int offset = event.lineOffset; final RGBa lineHighlight = getCurrentTheme().getLineHighlight(); event.lineBackground = getColorManager().getColor(lineHighlight.toRGB()); // In this case, we should be overriding the bg of the style ranges for the line too! if (textWidget.isDisposed()) { return; } // FIXME Only change bg colors of visible ranges! int replaceLength = 160; if (lineRegion != null) { replaceLength = Math.min(replaceLength, lineRegion.getLength()); } // be safe about offsets int charCount = textWidget.getCharCount(); if (offset + replaceLength > charCount) { replaceLength = charCount - offset; if (replaceLength < 0) { // Just playing safe here replaceLength = 0; } } final StyleRange[] ranges = textWidget.getStyleRanges(offset, replaceLength, true); if (ranges == null || ranges.length == 0) { return; } Color background = textWidget.getBackground(); final int[] positions = new int[ranges.length << 1]; int x = 0; boolean apply = false; for (StyleRange range : ranges) { if (range.background != null) { if (!range.background.equals(background)) { positions[x] = range.start; positions[x + 1] = range.length; x += 2; continue; } apply = true; } range.background = null; positions[x] = range.start; positions[x + 1] = range.length; x += 2; } if (apply) { textWidget.setStyleRanges(offset, replaceLength, positions, ranges); } }
public static RenameLinkedMode getActiveLinkedMode() { if (fgActiveLinkedMode != null) { ISourceViewer viewer = fgActiveLinkedMode.fEditor.getViewer(); if (viewer != null) { StyledText textWidget = viewer.getTextWidget(); if (textWidget != null && !textWidget.isDisposed()) { return fgActiveLinkedMode; } } // make sure we don't hold onto the active linked mode if anything went wrong with canceling: fgActiveLinkedMode = null; } return null; }
/* * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.DocumentCommand) */ @Override public void customizeDocumentCommand(IDocument d, DocumentCommand c) { if (c.doit == false) return; clearCachedValues(); if (!fIsSmartMode) { super.customizeDocumentCommand(d, c); return; } if (!fIsSmartTab && isRepresentingTab(c.text)) return; if (c.length == 0 && c.text != null && isLineDelimiter(d, c.text)) { if (fIsSmartIndentAfterNewline) smartIndentAfterNewLine(d, c); else super.customizeDocumentCommand(d, c); } else if (c.text.length() == 1) smartIndentOnKeypress(d, c); else if (c.text.length() > 1 && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_PASTE)) if (fViewer == null || fViewer.getTextWidget() == null || !fViewer.getTextWidget().getBlockSelection()) smartPaste(d, c); // no smart backspace for paste }
/** * Updates all the cached information about the lines to be painted and to be cleared. Returns * <code>true</code> if the line number of the cursor line has changed. * * @return <code>true</code> if cursor line changed */ private boolean updateHighlightLine() { try { IDocument document = fViewer.getDocument(); int modelCaret = getModelCaret(); int lineNumber = document.getLineOfOffset(modelCaret); Point selection = fViewer.getTextWidget().getSelectionRange(); // redraw if the current line number is different from the last line number we painted // initially fLastLineNumber is -1 if (lineNumber != fLastLineNumber || !overlaps(fCurrentLine, modelCaret) || (selection.y != 0)) { // Handle non-empty selections (turn off highlight line) if (selection.y != 0 && fLastLine.equals(fCurrentLine)) { if (fLastSelection.equals(selection)) // selection didn't change { return false; // don't redraw the highlight line } fLastSelection = selection; return true; // selection changed } fLastSelection = selection; // Update the current and last lines fLastLine.offset = fCurrentLine.offset; fLastLine.length = fCurrentLine.length; fLastLine.isDeleted = fCurrentLine.isDeleted; if (fCurrentLine.isDeleted) { fCurrentLine.isDeleted = false; fPositionManager.managePosition(fCurrentLine); } fCurrentLine.offset = document.getLineOffset(lineNumber); if (lineNumber == document.getNumberOfLines() - 1) { fCurrentLine.length = document.getLength() - fCurrentLine.offset; } else { fCurrentLine.length = document.getLineOffset(lineNumber + 1) - fCurrentLine.offset; } fLastLineNumber = lineNumber; return true; } } catch (BadLocationException e) { } return false; }
/** * Sets the caret to the offset of the given element. * * @param element has to be contained in the resource of this editor. */ public void setCaret(EObject element, String text) { try { if (element == null || text == null || text.equals("")) { return; } ISourceViewer viewer = getSourceViewer(); com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource textResource = (com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource) element.eResource(); com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptLocationMap locationMap = textResource.getLocationMap(); int destination = locationMap.getCharStart(element); int length = locationMap.getCharEnd(element) + 1 - destination; com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextScanner lexer = getResource().getMetaInformation().createLexer(); try { lexer.setText(viewer.getDocument().get(destination, length)); com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextToken token = lexer.getNextToken(); String tokenText = token.getText(); while (tokenText != null) { if (token.getText().equals(text)) { destination += token.getOffset(); break; } token = lexer.getNextToken(); if (token == null) { break; } tokenText = token.getText(); } } catch (BadLocationException e) { } destination = ((ProjectionViewer) viewer).modelOffset2WidgetOffset(destination); if (destination < 0) { destination = 0; } viewer.getTextWidget().setSelection(destination); } catch (Exception e) { com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.ui.RezeptUIPlugin.logError( "Exception in setCaret()", e); } }
protected void setLastRulerMouseLocation(ISourceViewer viewer, int line) { // set last mouse activity in order to get the correct context menu if (fCompositeRuler != null) { StyledText st = viewer.getTextWidget(); if (st != null && !st.isDisposed()) { if (viewer instanceof ITextViewerExtension5) { int widgetLine = ((ITextViewerExtension5) viewer).modelLine2WidgetLine(line); Point loc = st.getLocationAtOffset(st.getOffsetAtLine(widgetLine)); fCompositeRuler.setLocationOfLastMouseButtonActivity(0, loc.y); } else if (viewer instanceof TextViewer) { // TODO remove once TextViewer implements the extension int widgetLine = ((TextViewer) viewer).modelLine2WidgetLine(line); Point loc = st.getLocationAtOffset(st.getOffsetAtLine(widgetLine)); fCompositeRuler.setLocationOfLastMouseButtonActivity(0, loc.y); } } } }
public void install() { final ISourceViewer sourceViewer = erlangEditor.getViewer(); if (sourceViewer == null) { return; } final StyledText text = sourceViewer.getTextWidget(); if (text == null || text.isDisposed()) { return; } sourceViewer.addTextInputListener(this); final IDocument document = sourceViewer.getDocument(); if (document != null) { document.addDocumentListener(this); } }
public void deactivate(boolean redraw) { if (fIsActive) { fIsActive = false; /* * on turning off the feature one has to paint the currently highlighted line with the standard background * color */ if (redraw) drawHighlightLine(fCurrentLine); fViewer.getTextWidget().removeLineBackgroundListener(this); fViewer.getTextWidget().removePaintListener(this); if (fPositionManager != null) fPositionManager.unmanagePosition(fCurrentLine); fLastLineNumber = -1; fCurrentLine.offset = 0; fCurrentLine.length = 0; } }
public void lineGetBackground(LineBackgroundEvent event) { if (fViewer == null) { return; } final StyledText textWidget = fViewer.getTextWidget(); if (textWidget == null) { return; } try { final int offset = event.lineOffset; IDocument document = fViewer.getDocument(); int line = document.getLineOfOffset(offset); final IRegion lineRegion = document.getLineInformation(line); // Handle fully opaque line highlight here. A modified approach from CursorLinePainter. if (fEnabled && isOpaque() && isCurrentLine(line)) { // draw current line drawCurrentLine(event, lineRegion); return; } // Not drawing an opaque line highlight, so we need to do our normal line coloring here. // This extends the bg color out for a given line based on it's end scope. String endOfLineScope = getScopeManager().getScopeAtOffset(document, lineRegion.getLength() + offset); String commonPrefix = getScope(document, line, endOfLineScope); TextAttribute at = getCurrentTheme().getTextAttribute(commonPrefix); // if we have no color we need to extend to end of line, but this used to be the highlight // line, force the // theme bg color if (at.getBackground() == null && isOpaque() && fLastLine.includes(offset)) { event.lineBackground = getColorManager().getColor(getCurrentTheme().getBackground()); } else { event.lineBackground = at.getBackground(); } } catch (BadLocationException e) { IdeLog.logError(CommonEditorPlugin.getDefault(), e); } }
/** * If the given caret location string contains a selection range, select that range in the given * viewer * * @param viewer the viewer to contain the selection * @param caretLocation the location string */ protected int updateCaret(ISourceViewer viewer, String caretLocation) { assertTrue(caretLocation, caretLocation.contains("^")); // $NON-NLS-1$ int caretDelta = caretLocation.indexOf("^"); // $NON-NLS-1$ assertTrue(caretLocation, caretDelta != -1); String text = viewer.getTextWidget().getText(); int length = 0; // String around caret/range without the range and caret marker // characters String caretContext; if (caretLocation.contains("[^")) { // $NON-NLS-1$ caretDelta--; assertTrue(caretLocation, caretLocation.startsWith("[^", caretDelta)); // $NON-NLS-1$ int caretRangeEnd = caretLocation.indexOf(']', caretDelta + 2); assertTrue(caretLocation, caretRangeEnd != -1); length = caretRangeEnd - caretDelta - 2; assertTrue(length > 0); caretContext = caretLocation.substring(0, caretDelta) + caretLocation.substring(caretDelta + 2, caretRangeEnd) + caretLocation.substring(caretRangeEnd + 1); } else { caretContext = caretLocation.substring(0, caretDelta) + caretLocation.substring(caretDelta + 1); // +1: // skip // "^" } int caretContextIndex = text.indexOf(caretContext); assertTrue("Caret content " + caretContext + " not found in file", caretContextIndex != -1); int offset = caretContextIndex + caretDelta; viewer.setSelectedRange(offset, length); return offset; }
private Rectangle getLineRectangle(Position position) { if (position == null) { return null; } // if the position that is about to be drawn was deleted then we can't if (position.isDeleted()) { return null; } int widgetOffset = 0; if (fViewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer; widgetOffset = extension.modelOffset2WidgetOffset(position.getOffset()); if (widgetOffset == -1) { return null; } } else { IRegion visible = fViewer.getVisibleRegion(); widgetOffset = position.getOffset() - visible.getOffset(); if (widgetOffset < 0 || visible.getLength() < widgetOffset) { return null; } } StyledText textWidget = fViewer.getTextWidget(); // check for https://bugs.eclipse.org/bugs/show_bug.cgi?id=64898 // this is a guard against the symptoms but not the actual solution if (0 <= widgetOffset && widgetOffset <= textWidget.getCharCount()) { Point upperLeft = textWidget.getLocationAtOffset(widgetOffset); int width = textWidget.getClientArea().width + textWidget.getHorizontalPixel(); int height = textWidget.getLineHeight(widgetOffset); return new Rectangle(0, upperLeft.y, width, height); } return null; }
private void testTabSize(int tabSize, VjoEditor editor) { try { Class clazz = editor .getClass() .getClassLoader() .loadClass("org.eclipse.ui.texteditor.AbstractTextEditor"); Field method = clazz.getDeclaredField("fSourceViewer"); method.setAccessible(true); ISourceViewer sourceViewer = (ISourceViewer) method.get(editor); int size = sourceViewer.getTextWidget().getTabs(); assertEquals(size, tabSize); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } }
private boolean isEditable(ISourceViewer viewer) { return viewer != null && viewer.getTextWidget().getEditable(); }
/** * Constructor * * @param sourceViewer Source in which to paint brackets. */ public SchemeParenthesisPainter(final ISourceViewer sourceViewer, SchemeEditor editor) { mEditor = editor; mSourceViewer = sourceViewer; mTextWidget = sourceViewer.getTextWidget(); }
public int getCaretOffset() { ISourceViewer viewer = getSourceViewer(); return viewer.getTextWidget().getCaretOffset(); }
public void setCaretOffset(int offset) { ISourceViewer viewer = getSourceViewer(); viewer.getTextWidget().setCaretOffset(offset); }