@Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { if (oldInput != null) { IDocument document = fDocumentProvider.getDocument(oldInput); if (document != null) { try { document.removePositionCategory(JSON_ELEMENTS); } catch (BadPositionCategoryException x) { } document.removePositionUpdater(fPositionUpdater); } } rootObject = null; if (newInput != null) { IDocument document = fDocumentProvider.getDocument(newInput); if (document != null) { document.addPositionCategory(JSON_ELEMENTS); document.addPositionUpdater(fPositionUpdater); parse(document); } } }
@Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { clearAnnotations(); if (oldInput != null) { IDocument document = documentProvider.getDocument(oldInput); if (document != null) { try { document.removePositionCategory(SEGMENTS); } catch (BadPositionCategoryException e) { // Do Nothing } document.removePositionUpdater(positionUpdater); } } if (newInput != null) { IDocument document = documentProvider.getDocument(newInput); if (document != null) { document.addPositionCategory(SEGMENTS); document.addPositionUpdater(positionUpdater); tree = parser.parse(document.get()); addAnnotations(document.get()); } } }
/** * Traverses the OutlineNode tree and adds a Position for each node to Document. * * <p>Also adds the nodes to type lists of the OutlineInput and calculates the tree depth. * * <p>Old Positions are removed before adding new ones. * * @param rootNodes * @param monitor monitor for the job calling this method */ private void updateDocumentPositions(List<OutlineNode> rootNodes, IProgressMonitor monitor) { TexOutlineInput newOutlineInput = new TexOutlineInput(rootNodes); IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); // remove previous positions try { document.removePositionCategory("__outline"); } catch (BadPositionCategoryException bpce) { // do nothing, the category will be added again next, it does not exists the first time } document.addPositionCategory("__outline"); pollCancel(monitor); // add new positions for nodes and their children int maxDepth = 0; for (Iterator<OutlineNode> iter = rootNodes.iterator(); iter.hasNext(); ) { OutlineNode node = iter.next(); int localDepth = addNodePosition(node, document, 0, newOutlineInput); if (localDepth > maxDepth) { maxDepth = localDepth; } pollCancel(monitor); } pollCancel(monitor); // set the new outline input newOutlineInput.setTreeDepth(maxDepth); this.outlineInput = newOutlineInput; }
@Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { if (oldInput != null) { IDocument document = documentProvider.getDocument(oldInput); if (document != null) { try { document.removePositionCategory(CLASS_POSITIONS); } catch (BadPositionCategoryException x) { } document.removePositionUpdater(positionUpdater); } } editorInput = (IEditorInput) newInput; if (newInput != null) { IDocument document = documentProvider.getDocument(newInput); if (document != null) { document.addPositionCategory(CLASS_POSITIONS); document.addPositionUpdater(positionUpdater); editorPageChanged(viewer); } } viewer.refresh(); }
/** * Remembers and returns the current selection. The saved selection can be restored by calling * <code>restoreSelection()</code>. * * @return the current selection * @see org.eclipse.jface.text.ITextViewer#getSelectedRange() * @since 3.0 */ protected Point rememberSelection() { final ITextSelection selection = (ITextSelection) getSelection(); final IDocument document = getDocument(); if (fSelections.isEmpty()) { fSelectionCategory = _SELECTION_POSITION_CATEGORY + hashCode(); fSelectionUpdater = new NonDeletingPositionUpdater(fSelectionCategory); document.addPositionCategory(fSelectionCategory); document.addPositionUpdater(fSelectionUpdater); } try { final Position position; if (selection instanceof IBlockTextSelection) position = new ColumnPosition( selection.getOffset(), selection.getLength(), ((IBlockTextSelection) selection).getStartColumn(), ((IBlockTextSelection) selection).getEndColumn()); else position = new Position(selection.getOffset(), selection.getLength()); document.addPosition(fSelectionCategory, position); fSelections.push(position); } catch (BadLocationException exception) { // Should not happen } catch (BadPositionCategoryException exception) { // Should not happen } return new Point(selection.getOffset(), selection.getLength()); }
/** * Installs this position manager in the given document. The position manager stays active until * <code>uninstall</code> or <code>dispose</code> is called. * * @param document the document to be installed on */ public void install(IDocument document) { if (document != null && this.fDocument != document) { fDocument = document; fDocument.addPositionCategory(fCategory); fDocument.addPositionUpdater(fPositionUpdater); } }
private void rewriteImports() { if (fImportRewrite == null) return; if (isReadOnly()) return; ICompilationUnit cu = getCompilationUnit(); if (cu == null) return; try { Position position = new Position(getCompletionOffset(), 0); IDocument document = getDocument(); final String category = "__template_position_importer" + System.currentTimeMillis(); // $NON-NLS-1$ IPositionUpdater updater = new DefaultPositionUpdater(category); document.addPositionCategory(category); document.addPositionUpdater(updater); document.addPosition(position); try { JavaModelUtil.applyEdit(cu, fImportRewrite.rewriteImports(null), false, null); setCompletionOffset(position.getOffset()); } catch (CoreException e) { handleException(null, e); } finally { document.removePosition(position); document.removePositionUpdater(updater); document.removePositionCategory(category); } } catch (BadLocationException e) { handleException(null, e); } catch (BadPositionCategoryException e) { handleException(null, e); } }
private void ensurePositionCategoryInstalled(final IDocument document, LinkedModeModel model) { if (!document.containsPositionCategory(getCategory())) { document.addPositionCategory(getCategory()); fUpdater = new InclusivePositionUpdater(getCategory()); document.addPositionUpdater(fUpdater); model.addLinkingListener( new ILinkedModeListener() { /* * @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.eclipse.jface.text.link.LinkedModeModel, int) */ @Override public void left(LinkedModeModel environment, int flags) { ensurePositionCategoryRemoved(document); } @Override public void suspend(LinkedModeModel environment) {} @Override public void resume(LinkedModeModel environment, int flags) {} }); } }
/** * Adds a position with the given offset and length into a document. * * @param document the document to add a position into * @param category the category of this position * @param offset the offset of the position * @param length the length of the position */ public void addPosition( org.eclipse.jface.text.IDocument document, String category, int offset, int length) { try { document.addPositionCategory(category); org.eclipse.jface.text.Position position = new org.eclipse.jface.text.Position(offset, length); document.addPosition(category, position); } catch (org.eclipse.jface.text.BadLocationException e) { } catch (org.eclipse.jface.text.BadPositionCategoryException e) { } }
/** * Called before document changes occur. It must be followed by a call to postReplace(). * * @param document the document on which to track the reference position. * @param offset the offset * @throws BadLocationException if the offset describes an invalid range in this document */ public void preReplace(IDocument document, int offset) throws BadLocationException { fPosition.setOffset(offset); try { document.addPositionCategory(CATEGORY); document.addPositionUpdater(fPositionUpdater); document.addPosition(CATEGORY, fPosition); } catch (BadPositionCategoryException e) { // should not happen DartToolsPlugin.log(e); } }
/* * @see org.eclipse.swt.dnd.DragSourceListener#dragStart(org.eclipse.swt.dnd.DragSourceEvent) */ @Override public void dragStart(DragSourceEvent event) { if (!fIsEnabled) { event.doit = false; return; } // convert screen coordinates to widget offest int offset = getOffsetAtLocation(event.x, event.y, false); // convert further to a document offset offset = getDocumentOffset(offset); Point selection = fViewer.getSelectedRange(); if (selection != null && offset >= selection.x && offset < selection.x + selection.y) { fSelectionPosition = new Position(selection.x, selection.y); if (fViewer instanceof ITextViewerExtension) { ((ITextViewerExtension) fViewer).getRewriteTarget().beginCompoundChange(); } IDocument doc = fViewer.getDocument(); try { // add the drag selection position // the position is used to delete the selection on a DROP_MOVE // and it can be used by the drop target to determine if it should // allow the drop (e.g. if drop location overlaps selection) doc.addPositionCategory(DRAG_SELECTION_CATEGORY); fPositionUpdater = new DefaultPositionUpdater(DRAG_SELECTION_CATEGORY); doc.addPositionUpdater(fPositionUpdater); doc.addPosition(DRAG_SELECTION_CATEGORY, fSelectionPosition); } catch (BadLocationException e) { // should not happen } catch (BadPositionCategoryException e) { // cannot happen } event.doit = true; // this has no effect? event.detail = DND.DROP_COPY; if (isDocumentEditable()) { event.detail |= DND.DROP_MOVE; } } else { event.doit = false; event.detail = DND.DROP_NONE; } }
/** @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent) */ public void verifyKey(VerifyEvent event) { // early pruning to slow down normal typing as little as possible if (!event.doit || !isAutoInsertEnabled() || !isAutoInsertCharacter(event.character)) { return; } IDocument document = textViewer.getDocument(); final Point selection = textViewer.getSelectedRange(); final int offset = selection.x; final int length = selection.y; try { String scope = getScopeAtOffset(document, offset); if (fgCommentSelector.matches(scope)) { return; } if (length > 0) { wrapSelection(event, document, offset, length); return; } // Don't auto-close if next char is a letter or digit if (document.getLength() > offset) { char nextChar = document.getChar(offset); if (Character.isJavaIdentifierPart(nextChar)) { return; } } // Don't auto-close if we have an open pair! if (isUnclosedPair( event, document, offset)) // We have an open string or pair, just insert the single // character, don't do anything special { return; } final char closingCharacter = getPeerCharacter(event.character); // If this is the start char and there's no unmatched close char, insert the close char if (unpairedClose(event.character, closingCharacter, document, offset)) { return; } final StringBuffer buffer = new StringBuffer(); buffer.append(event.character); buffer.append(closingCharacter); if (offset == document.getLength()) { String delim = null; if (document instanceof IDocumentExtension4) { delim = ((IDocumentExtension4) document).getDefaultLineDelimiter(); } if (delim == null) { delim = System.getProperty("line.separator", "\r\n"); // $NON-NLS-1$ //$NON-NLS-2$ } buffer.append(delim); } document.replace(offset, length, buffer.toString()); BracketLevel level = new BracketLevel(); fBracketLevelStack.push(level); LinkedPositionGroup group = new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, offset + 1, 0, LinkedPositionGroup.NO_STOP)); LinkedModeModel model = new LinkedModeModel(); model.addLinkingListener(this); model.addGroup(group); model.forceInstall(); // set up position tracking for our magic peers if (fBracketLevelStack.size() == 1) { document.addPositionCategory(CATEGORY); document.addPositionUpdater(fUpdater); } level.fFirstPosition = new Position(offset, 1); level.fSecondPosition = new Position(offset + 1, 1); document.addPosition(CATEGORY, level.fFirstPosition); document.addPosition(CATEGORY, level.fSecondPosition); level.fUI = new EditorLinkedModeUI(model, textViewer); level.fUI.setSimpleMode(true); level.fUI.setExitPolicy( new ExitPolicy( textViewer, closingCharacter, getEscapeCharacter(closingCharacter), fBracketLevelStack)); level.fUI.setExitPosition(textViewer, offset + 2, 0, Integer.MAX_VALUE); level.fUI.setCyclingMode(LinkedModeUI.CYCLE_NEVER); level.fUI.enter(); IRegion newSelection = level.fUI.getSelectedRegion(); textViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength()); event.doit = false; } catch (BadLocationException e) { CommonEditorPlugin.logError(e); } catch (BadPositionCategoryException e) { CommonEditorPlugin.logError(e); } }