public String apply(String contents) { final TextEdit edit = codeFormatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0, contents.length(), 0, Constants.LINE_SEPARATOR); if (edit == null) { // TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost return contents; } IDocument document = new Document(contents); try { edit.apply(document); } catch (Exception e) { throw new RuntimeException( "Failed to format the generated source code.", e); } return document.get(); } }
public String getHoverInfo(ITextViewer textViewer, IRegion region) { try { IDocument doc = textViewer.getDocument(); String text = doc.get(region.getOffset(), region.getLength()); return "<b>"+text+"</b>"; } catch (Exception e) { return null; } }
@Override public void documentAboutToBeChanged(DocumentEvent event) { if (fActiveRegion != null) { fRememberedPosition= new Position(fActiveRegion.getOffset(), fActiveRegion.getLength()); try { event.getDocument().addPosition(fRememberedPosition); } catch (BadLocationException x) { fRememberedPosition= null; } } }
@Override public void doubleClicked(ITextViewer text) { int offset= text.getSelectedRange().x; if (offset < 0) return; final IDocument document= text.getDocument(); IRegion region= findExtendedDoubleClickSelection(document, offset); if (region == null) region= findWord(document, offset); if (region != null) text.setSelectedRange(region.getOffset(), region.getLength()); }
private void repairRepresentation() { if (fActiveRegion == null) return; int offset= fActiveRegion.getOffset(); int length= fActiveRegion.getLength(); fActiveRegion= null; // Invalidate ==> remove applied text presentation if (fTextViewer instanceof ITextViewerExtension2) ((ITextViewerExtension2) fTextViewer).invalidateTextPresentation(offset, length); else fTextViewer.invalidateTextPresentation(); }
@Override public int findAndSelect(int widgetOffset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch) { int modelOffset= widgetOffset == -1 ? -1 : widgetOffset2ModelOffset(widgetOffset); if (fRange != null) { IRegion range= fRange.getRange(); modelOffset= TextViewer.this.findAndSelectInRange(modelOffset, findString, searchForward, caseSensitive, wholeWord, range.getOffset(), range.getLength(), regExSearch); } else { modelOffset= TextViewer.this.findAndSelect(modelOffset, findString, searchForward, caseSensitive, wholeWord, regExSearch); } widgetOffset= modelOffset == -1 ? -1 : modelOffset2WidgetOffset(modelOffset); return widgetOffset; }
/** * Same as {@link #modelRange2WidgetRange(IRegion)} just for a {@link org.eclipse.jface.text.Position}. * * @param modelPosition the position describing a range in the viewer's document * @return a region describing a range in the viewer's widget * @since 2.1 */ protected IRegion modelRange2WidgetRange(Position modelPosition) { return modelRange2WidgetRange(new Region(modelPosition.getOffset(), modelPosition.getLength())); }
@Override boolean isMultiline(ISelection selection) throws BadLocationException { ITextSelection ts= (ITextSelection)selection; return fDocument.getLineOfOffset(ts.getOffset()) < fDocument.getLineOfOffset(ts.getOffset() + ts.getLength()); }
private /*static*/ final boolean isVisible(ITextViewer viewer, int offset, int length) { if (viewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension= (ITextViewerExtension5) viewer; IRegion overlap= extension.modelRange2WidgetRange(new Region(offset, length)); return overlap != null; } return viewer.overlapsWithVisibleRegion(offset, length); }
/** * Creates a new document with the given initial content. * * @param initialContent the document's initial content */ public Document(String initialContent) { super(); setTextStore(new CopyOnWriteTextStore(new GapTextStore())); setLineTracker(new DefaultLineTracker()); getStore().set(initialContent); getTracker().set(initialContent); completeInitialization(); }
@Override public char getChar(int pos) throws BadLocationException { if ((0 > pos) || (pos >= getLength())) throw new BadLocationException(); return getStore().get(pos); }
@Override public void documentAboutToBeChanged(DocumentEvent e) { if (e.getDocument() == getVisibleDocument()) fWidgetCommand.setEvent(e); handleVisibleDocumentAboutToBeChanged(e); }
/** * Creates a new empty document. */ public Document() { super(); setTextStore(new CopyOnWriteTextStore(new GapTextStore())); setLineTracker(new DefaultLineTracker()); completeInitialization(); }
@Override public void mouseExit(MouseEvent e) { if (!hasInformationControlReplacer() || !canMoveIntoInformationControl(getCurrentInformationControl()) || !inKeepUpZone(e.x, e.y, fSubjectControl, fSubjectArea, false)) { fMouseLostWhileComputing= true; deactivate(); } }
@Override protected void hideInformationControl() { try { super.hideInformationControl(); } finally { if (fTextViewer instanceof IWidgetTokenOwner) { IWidgetTokenOwner owner= (IWidgetTokenOwner) fTextViewer; owner.releaseWidgetToken(this); } } }
@Override void replaceInformationControl(boolean takeFocus) { if (fTextViewer != null) fTextViewer.releaseWidgetToken(this); super.replaceInformationControl(takeFocus); }
@Override public void doubleClicked(ITextViewer text) { int offset= text.getSelectedRange().x; if (offset < 0) return; final IDocument document= text.getDocument(); IRegion region= findExtendedDoubleClickSelection(document, offset); if (region == null) region= findWord(document, offset); if (region != null) text.setSelectedRange(region.getOffset(), region.getLength()); }
/** * Creates a new document with the given initial content. * * @param initialContent the document's initial content */ public Document(String initialContent) { super(); setTextStore(new CopyOnWriteTextStore(new GapTextStore())); setLineTracker(new DefaultLineTracker()); getStore().set(initialContent); getTracker().set(initialContent); completeInitialization(); }
@Override public char getChar(int pos) throws BadLocationException { if ((0 > pos) || (pos >= getLength())) throw new BadLocationException(); return getStore().get(pos); }
@Override public String get(int pos, int length) throws BadLocationException { int myLength= getLength(); if ((0 > pos) || (0 > length) || (pos + length > myLength)) throw new BadLocationException(); return getStore().get(pos, length); }