/** * Returns the partitioner's positions. Apparently, this is an array of TypedPosition objects * that partitions the document, ordered from start to end. These TypedPosition objects mark * all the non-TLA+ portions of the document--that is, comments, strings, and PlusCal tokens. * * @return the partitioner's positions * @throws BadPositionCategoryException if getting the positions from the * document fails */ protected final Position[] getPositions() throws BadPositionCategoryException { if (fCachedPositions == null) { fCachedPositions= fDocument.getPositions(fPositionCategory); } else if (CHECK_CACHE_CONSISTENCY) { Position[] positions= fDocument.getPositions(fPositionCategory); int len= Math.min(positions.length, fCachedPositions.length); for (int i= 0; i < len; i++) { if (!positions[i].equals(fCachedPositions[i])) System.err.println("FastPartitioner.getPositions(): cached position is not up to date: from document: " + toString(positions[i]) + " in cache: " + toString(fCachedPositions[i])); //$NON-NLS-1$ //$NON-NLS-2$ } for (int i= len; i < positions.length; i++) System.err.println("FastPartitioner.getPositions(): new position in document: " + toString(positions[i])); //$NON-NLS-1$ for (int i= len; i < fCachedPositions.length; i++) System.err.println("FastPartitioner.getPositions(): stale position in cache: " + toString(fCachedPositions[i])); //$NON-NLS-1$ } return fCachedPositions; }
/** * Returns the partitioners positions. * * @return the partitioners positions * @throws BadPositionCategoryException * if getting the positions from the document fails * @since 2.2 */ protected final Position[] getPositions() throws BadPositionCategoryException { if (fCachedPositions == null) { fCachedPositions = fDocument.getPositions(fPositionCategory); } else if (CHECK_CACHE_CONSISTENCY) { Position[] positions = fDocument.getPositions(fPositionCategory); int len = Math.min(positions.length, fCachedPositions.length); for (int i = 0; i < len; i++) { if (!positions[i].equals(fCachedPositions[i])) System.err .println("FastPartitioner.getPositions(): cached position is not up to date: from document: " + toString(positions[i]) + " in cache: " + toString(fCachedPositions[i])); //$NON-NLS-1$ //$NON-NLS-2$ } for (int i = len; i < positions.length; i++) System.err .println("FastPartitioner.getPositions(): new position in document: " + toString(positions[i])); //$NON-NLS-1$ for (int i = len; i < fCachedPositions.length; i++) System.err .println("FastPartitioner.getPositions(): stale position in cache: " + toString(fCachedPositions[i])); //$NON-NLS-1$ } return fCachedPositions; }
/** * Returns the partitioners positions. * * @return the partitioners positions * @throws BadPositionCategoryException if getting the positions from the document fails */ protected final Position[] getPositions() throws BadPositionCategoryException { if (fCachedPositions == null) { fCachedPositions = fDocument.getPositions(fPositionCategory); } else if (CHECK_CACHE_CONSISTENCY) { Position[] positions = fDocument.getPositions(fPositionCategory); int len = Math.min(positions.length, fCachedPositions.length); for (int i = 0; i < len; i++) { if (!positions[i].equals(fCachedPositions[i])) System.err.println( "FastPartitioner.getPositions(): cached position is not up to date: from document: " + toString(positions[i]) + " in cache: " + toString(fCachedPositions[i])); // $NON-NLS-1$ //$NON-NLS-2$ } for (int i = len; i < positions.length; i++) System.err.println( "FastPartitioner.getPositions(): new position in document: " + toString(positions[i])); // $NON-NLS-1$ for (int i = len; i < fCachedPositions.length; i++) System.err.println( "FastPartitioner.getPositions(): stale position in cache: " + toString(fCachedPositions[i])); // $NON-NLS-1$ } return fCachedPositions; }
/** * Creates a new edition on the document of this factory. * * @param offset the offset of the edition at the point when is created. * @param length the length of the edition (not updated via the position update mechanism) * @param text the text to be replaced on the document * @return an <code>Edit</code> reflecting the edition on the document */ public Edit createEdit(int offset, int length, String text) throws BadLocationException { if (!fDocument.containsPositionCategory(fCategory)) { fDocument.addPositionCategory(fCategory); fUpdater= new DefaultPositionUpdater(fCategory); fDocument.addPositionUpdater(fUpdater); } Position position= new Position(offset); try { fDocument.addPosition(fCategory, position); } catch (BadPositionCategoryException e) { Assert.isTrue(false); } return new Edit(fDocument, length, text, position); }
/** * Get all positions of the given position category * In sexp's these positions are typically skipped * * @param doc * @param pos_names * @return the positions to exclude */ public static List<Position> getExclusions(IDocument doc, String[] pos_names) { List<Position> excludePositions = new LinkedList<Position>(); String cat = getTypeCategory(doc); if (cat != null) { try { Position[] xpos = doc.getPositions(cat); for (int j = 0; j < xpos.length; j++) { if (xpos[j] instanceof TypedPosition) { for (int jj = 0; jj < pos_names.length; jj++) { if (((TypedPosition) xpos[j]).getType().contains(pos_names[jj])) { excludePositions.add(xpos[j]); } } } } } catch (BadPositionCategoryException e) {} } return excludePositions; }
public void partClosed(IWorkbenchPartReference partRef) { if (partRef instanceof IEditorReference) { IEditorPart epart = ((IEditorReference) partRef).getEditor(false); ITextEditor editor = (location != null ? location.getEditor() : null); if (editor == EmacsPlusUtils.getTextEditor(epart, false)) { RegisterLocation loc = location; // we're out of here removeListener(this); IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); // remove position category, if still present if (document.containsPositionCategory(MarkRing.MARK_CATEGORY)) { try { document.removePositionUpdater(MarkRing.updater); document.removePositionCategory(MarkRing.MARK_CATEGORY); } catch (BadPositionCategoryException e) { } } // convert to path loc.setPath(convertToPath(editor)); } } }
public void configureForwardReaderKeepingPositions(int offset, int end) throws IOException, BadPositionCategoryException { if (!fSupportKeepPositions) { throw new AssertionError("configureForwardReader must be called with supportKeepPositions=true."); } fOffset = offset; fStartForwardOffset = offset; fForward = true; fEnd = Math.min(fDocument.getLength(), end); fcurrentPositionI = 0; if (fPositions.length > 0) { fCurrentPosition = fPositions[0]; } else { fCurrentPosition = null; } }
public void configureForwardReader(IDocument document, int offset, int end, boolean supportKeepPositions) throws IOException, BadPositionCategoryException { fSupportKeepPositions = supportKeepPositions; fDocument = document; fOffset = offset; fStartForwardOffset = offset; fForward = true; fEnd = Math.min(fDocument.getLength(), end); fcurrentPositionI = 0; fPositions = createPositions(document); if (fPositions.length > 0) { fCurrentPosition = fPositions[0]; } else { fCurrentPosition = null; } }
private Position[] createPositions(IDocument document) throws BadPositionCategoryException { Position[] positions = getDocumentTypedPositions(document, contentType); List<TypedPosition> typedPositions = PartitionMerger.sortAndMergePositions(positions, document.getLength()); int size = typedPositions.size(); List<Position> list = new ArrayList<Position>(size); for (int i = 0; i < size; i++) { Position position = typedPositions.get(i); if (isPositionValid(position, contentType)) { list.add(position); } } if (!fForward) { Collections.reverse(list); } Position[] ret = list.toArray(new Position[list.size()]); return ret; }
public DocCopy(IDocument document) { this.contents = document.get(); this.document = document; categoryToPos = new HashMap<>(); String[] positionCategories = document.getPositionCategories(); for (String string : positionCategories) { try { categoryToPos.put(string, document.getPositions(string)); } catch (BadPositionCategoryException e) { Log.log(e); } } IDocumentExtension4 doc4 = (IDocumentExtension4) document; modificationStamp = doc4.getModificationStamp(); }
@SuppressWarnings("restriction") private void processIOConsole(IOConsole ioConsole) { IDocument document = ioConsole.getDocument(); try { Position[] positions = document.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY); Arrays.sort(positions, new Comparator<Position>() { @Override public int compare(Position o1, Position o2) { return Integer.compare(o1.getOffset(), o2.getOffset()); } }); if (positions.length > 0) { Position p = positions[positions.length - 1]; if (p instanceof ConsoleHyperlinkPosition) { ConsoleHyperlinkPosition consoleHyperlinkPosition = (ConsoleHyperlinkPosition) p; IHyperlink hyperLink = consoleHyperlinkPosition.getHyperLink(); hyperLink.linkActivated(); } } } catch (BadPositionCategoryException e) { Log.log(e); } }
private void ensurePositionCategoryRemoved(IDocument document) { if (document.containsPositionCategory(getCategory())) { try { document.removePositionCategory(getCategory()); } catch (BadPositionCategoryException e) { // ignore } document.removePositionUpdater(fUpdater); } }
/** * <p> * Returns the first position of a specific category of the given document. * </p> * * @param document the document to get the positions from * @param category the category of the position * * @return a position. If there is none return <code>null</code>. */ public Position getFirstPosition(IDocument document, String category) { try { Position[] positions = document.getPositions(category); if (positions.length > 0) { return positions[0]; } } catch (BadPositionCategoryException e) { } return null; }
/** * Traverses the OutlineNode tree and adds a Position for each * node to Document. * * Also adds the nodes to type lists of the OutlineInput and * calculates the tree depth. * * 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; }
/** * {@inheritDoc} * <p> * May be extended by subclasses. * </p> */ public void disconnect() { Assert.isTrue(fDocument.containsPositionCategory(fPositionCategory)); try { fDocument.removePositionCategory(fPositionCategory); } catch (BadPositionCategoryException x) { // can not happen because of Assert } }
/** * Flushes the active rewrite session. */ protected final void flushRewriteSession() { fActiveRewriteSession= null; // remove all position belonging to the partitioner position category try { fDocument.removePositionCategory(fPositionCategory); } catch (BadPositionCategoryException x) { } fDocument.addPositionCategory(fPositionCategory); fIsInitialized= false; }
/** * For printing out debugging information * (TypedPosition) * @param msg */ public void debugPrint(String msg) { System.out.println("Debug print " + msg); System.out.println("Start comment offset: " + pcalStartCommentOffset + "; Start: (" + pcalStartOffset + ", " + pcalStartLength + "); End comment: (" + pcalEndCommentOffset + ", " + pcalEndCommentLength + ")") ; Position[] positions = null; try { positions = fDocument.getPositions(fPositionCategory); } catch (BadPositionCategoryException e1) { System.out.println("Can't get positions") ; e1.printStackTrace(); return ; } for (int i = 0; i < positions.length; i++) { try { TypedPosition position = (TypedPosition) positions[i] ; System.out.println("Position " + i + ": (" + position.getOffset() + ", " + position.getLength() + ") type: " + position.getType() + (position.isDeleted?" DELETED":"")); System.out.println(" `" + fDocument.get(position.getOffset(), position.getLength()) + "'") ; } catch (Exception e) { System.out.println("Item " + i + " Exception: " + e.getMessage()) ; } } IRegion result = createRegion(); if (result == null) { System.out.println("Returned null"); } else { System.out.println("Returned (" + result.getOffset() + ", " + result.getLength() + ")") ; } }
@Override public Position[] getPositions(String category, int offset, int length, boolean canStartBefore, boolean canEndAfter) throws BadPositionCategoryException { positionsReadLock.lock(); try { return super.getPositions(category, offset, length, canStartBefore, canEndAfter); } finally { positionsReadLock.unlock(); } }
@Override public Position[] getPositions(String category) throws BadPositionCategoryException { positionsReadLock.lock(); try { return super.getPositions(category); } finally { positionsReadLock.unlock(); } }
@Override public void addPosition(String category, Position position) throws BadLocationException, BadPositionCategoryException { positionsWriteLock.lock(); try { super.addPosition(category, position); } finally { positionsWriteLock.unlock(); } }
@Override public void removePosition(String category, Position position) throws BadPositionCategoryException { positionsWriteLock.lock(); try { super.removePosition(category, position); } finally { positionsWriteLock.unlock(); } }
/** * {@inheritDoc} * <p> * May be extended by subclasses. * </p> * * @since 2.2 */ public void disconnect() { Assert.isTrue(fDocument.containsPositionCategory(fPositionCategory)); try { fDocument.removePositionCategory(fPositionCategory); } catch (BadPositionCategoryException x) { // can not happen because of Assert } }
/** * Flushes the active rewrite session. * * @since 2.2 */ protected final void flushRewriteSession() { fActiveRewriteSession = null; // remove all position belonging to the partitioner position category try { fDocument.removePositionCategory(fPositionCategory); } catch (BadPositionCategoryException x) { } fDocument.addPositionCategory(fPositionCategory); fIsInitialized = false; }
/** * Stop managing the given document. * * @param document * The document */ private void releaseDocument(IDocument document) { if (document != null) { document.removeDocumentListener(this); document.removePositionUpdater(fPositionUpdater); try { document.removePositionCategory(getPositionCategory()); } catch (BadPositionCategoryException e) { // Should not happen log.debug(e.getMessage(), e); } } }
/** * {@inheritDoc} * * <p>May be extended by subclasses. */ public void disconnect() { Assert.isTrue(fDocument.containsPositionCategory(fPositionCategory)); try { fDocument.removePositionCategory(fPositionCategory); } catch (BadPositionCategoryException x) { // can not happen because of Assert } }
/** Flushes the active rewrite session. */ protected final void flushRewriteSession() { fActiveRewriteSession = null; // remove all position belonging to the partitioner position category try { fDocument.removePositionCategory(fPositionCategory); } catch (BadPositionCategoryException x) { } fDocument.addPositionCategory(fPositionCategory); fIsInitialized = false; }
/** * Restores any decorated regions and updates the buffer's variable offsets. * * @return the buffer. * @throws MalformedTreeException * @throws BadLocationException */ public TemplateBuffer updateBuffer() throws MalformedTreeException, BadLocationException { checkState(); TemplateVariable[] variables = fBuffer.getVariables(); try { removeRangeMarkers(fPositions, fDocument, variables); } catch (BadPositionCategoryException x) { Assert.isTrue(false); } fBuffer.setContent(fDocument.get(), variables); fDocument = null; return fBuffer; }
/** * 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 org.eclipse.jface.text.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 JavaPlugin.log(e); } }
/** * Called after the document changed occurred. It must be preceded by a call to preReplace(). * * @param document the document on which to track the reference position. * @return offset after the replace */ public int postReplace(IDocument document) { try { document.removePosition(CATEGORY, fPosition); document.removePositionUpdater(fPositionUpdater); document.removePositionCategory(CATEGORY); } catch (BadPositionCategoryException e) { // should not happen JavaPlugin.log(e); } return fPosition.getOffset(); }