org.eclipse.jface.text.BadPositionCategoryException Java Examples

The following examples show how to use org.eclipse.jface.text.BadPositionCategoryException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PartitionCodeReader.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: EmacsPlusUtils.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: DocumentPartitioner.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the partitioners positions.
 * 
 * @return the partitioners positions
 * @throws BadPositionCategoryException
 *             if getting the positions from the document fails
 * @since 2.2
 */
protected synchronized 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;
}
 
Example #4
Source File: TecoRegister.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
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));
		}
	}
}
 
Example #5
Source File: FastPartitioner.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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: " //$NON-NLS-1$
                                + toString(positions[i]) + " in cache: " + toString(fCachedPositions[i])); //$NON-NLS-1$
            }
        }
        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;
}
 
Example #6
Source File: PartitionCodeReader.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
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;
    }
}
 
Example #7
Source File: DocCopy.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
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();
}
 
Example #8
Source File: DocumentLifeCycleHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ICompilationUnit handleOpen(DidOpenTextDocumentParams params) {
	ICompilationUnit unit = super.handleOpen(params);

	if (unit == null || unit.getResource() == null || unit.getResource().isDerived()) {
		return unit;
	}

	try {
		installSemanticHighlightings(unit);
	} catch (JavaModelException | BadPositionCategoryException e) {
		JavaLanguageServerPlugin.logException("Error while opening document. URI: " + params.getTextDocument().getUri(), e);
	}

	return unit;
}
 
Example #9
Source File: BlockCommentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #10
Source File: PartitionCodeReader.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
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;
    }
}
 
Example #11
Source File: TLAFastPartitioner.java    From tlaplus with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #12
Source File: PyOpenLastConsoleHyperlink.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
@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);
    }
}
 
Example #13
Source File: MarkRing.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Add new element to the front of the ring.  If the length is 
 * exceeded, drop the last element 
 * 
 * @param document
 * @param element
 */
void pushElement(IDocument document, IBufferLocation element) {
	addFirst(element);
	if (size() > bufferSize) {
		IBufferLocation old = removeLast();
		try {
			removeMarkPosition(document,((MarkElement)old).getPosition());
		} catch (BadPositionCategoryException e) {
		}
	}
}
 
Example #14
Source File: TLAFastPartitioner.java    From tlaplus with MIT License 5 votes vote down vote up
/**
 * 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() + ")") ;
    }
}
 
Example #15
Source File: TypeScriptCompletionProposal.java    From typescript.java with MIT License 5 votes vote down vote up
private void ensurePositionCategoryRemoved(IDocument document) {
	if (document.containsPositionCategory(getCategory())) {
		try {
			document.removePositionCategory(getCategory());
		} catch (BadPositionCategoryException e) {
			// ignore
		}
		document.removePositionUpdater(fUpdater);
	}
}
 
Example #16
Source File: FastPartitioner.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #17
Source File: FastPartitioner.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * May be extended by subclasses.
 * </p>
 */
@Override
public void disconnect() {

    Assert.isTrue(fDocument.containsPositionCategory(fPositionCategory));

    try {
        fDocument.removePositionCategory(fPositionCategory);
    } catch (BadPositionCategoryException x) {
        // can not happen because of Assert
    }
}
 
Example #18
Source File: DocumentPartitioner.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Flushes the active rewrite session.
 * 
 * @since 2.2
 */
protected synchronized 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;
}
 
Example #19
Source File: TecoRegister.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
private void removePosition(ITextEditor editor, Position position) {
	removeListener();
	IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
	try {
		document.removePosition(MarkRing.MARK_CATEGORY,position);
	} catch (BadPositionCategoryException e) {
	}			
}
 
Example #20
Source File: CommentFormatterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a document with the given content and the given positions
 * registered with the {@link DefaultPositionUpdater}.
 *
 * @param content the content
 * @param positions the positions
 * @return the document
 * @throws IllegalArgumentException
 */
private static Document createDocument(String content, Position[] positions) throws IllegalArgumentException {
	Document doc= new Document(content);
	try {
		if (positions != null) {
			final String POS_CATEGORY= "myCategory"; //$NON-NLS-1$

			doc.addPositionCategory(POS_CATEGORY);
			doc.addPositionUpdater(new DefaultPositionUpdater(POS_CATEGORY) {
				protected boolean notDeleted() {
					if (this.fOffset < this.fPosition.offset && (this.fPosition.offset + this.fPosition.length < this.fOffset + this.fLength)) {
						this.fPosition.offset= this.fOffset + this.fLength; // deleted positions: set to end of remove
						return false;
					}
					return true;
				}
			});
			for (int i= 0; i < positions.length; i++) {
				try {
					doc.addPosition(POS_CATEGORY, positions[i]);
				} catch (BadLocationException e) {
					throw new IllegalArgumentException("Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + content.length());   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
				}
			}
		}
	} catch (BadPositionCategoryException cannotHappen) {
		// can not happen: category is correctly set up
	}
	return doc;
}
 
Example #21
Source File: CppStyleMessageConsole.java    From CppStyle with MIT License 5 votes vote down vote up
public FileLink getFileLink(int offset) {
	try {
		IDocument document = getDocument();
		if (document != null) {
			Position[] positions = document.getPositions(ERROR_MARKER_CATEGORY);
			Position position = findPosition(offset, positions);
			if (position instanceof MarkerPosition) {
				return ((MarkerPosition) position).getFileLink();
			}
		}
	} catch (BadPositionCategoryException e) {
	}
	return null;
}
 
Example #22
Source File: SnippetTemplateProposal.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
private void ensurePositionCategoryRemoved(IDocument document)
{
	if (document.containsPositionCategory(getCategory()))
	{
		try
		{
			document.removePositionCategory(getCategory());
		}
		catch (BadPositionCategoryException e)
		{
			// ignore
		}
		document.removePositionUpdater(fUpdater);
	}
}
 
Example #23
Source File: JavaFormatter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #24
Source File: CppStyleConsoleViewer.java    From CppStyle with MIT License 5 votes vote down vote up
public void clearFileLink() {
	try {
		Position[] positions = getDocument().getPositions(CppStyleMessageConsole.ERROR_MARKER_CATEGORY);

		for (Position position : positions) {
			getDocument().removePosition(CppStyleMessageConsole.ERROR_MARKER_CATEGORY, position);
		}
	} catch (BadPositionCategoryException e) {
		CppStyle.log(e);
	}
}
 
Example #25
Source File: ASTRewriteFormatter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Document createDocument(String string, Position[] positions) throws IllegalArgumentException {
	Document doc= new Document(string);
	try {
		if (positions != null) {
			final String POS_CATEGORY= "myCategory"; //$NON-NLS-1$

			doc.addPositionCategory(POS_CATEGORY);
			doc.addPositionUpdater(new DefaultPositionUpdater(POS_CATEGORY) {
				protected boolean notDeleted() {
					int start= this.fOffset;
					int end= start + this.fLength;
					if (start < this.fPosition.offset && (this.fPosition.offset + this.fPosition.length < end)) {
						this.fPosition.offset= end; // deleted positions: set to end of remove
						return false;
					}
					return true;
				}
			});
			for (int i= 0; i < positions.length; i++) {
				try {
					doc.addPosition(POS_CATEGORY, positions[i]);
				} catch (BadLocationException e) {
					throw new IllegalArgumentException("Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + string.length());   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
				}
			}
		}
	} catch (BadPositionCategoryException cannotHappen) {
		// can not happen: category is correctly set up
	}
	return doc;
}
 
Example #26
Source File: SemanticHighlightingPresenter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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
			JavaPlugin.log(e);
		}
	}
}
 
Example #27
Source File: BlockCommentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Releases the position category on the document and uninstalls the position updater.
 * <code>Edit</code>s managed by this factory are not updated after this call.
 */
public void release() {
	if (fDocument != null && fDocument.containsPositionCategory(fCategory)) {
		fDocument.removePositionUpdater(fUpdater);
		try {
			fDocument.removePositionCategory(fCategory);
		} catch (BadPositionCategoryException e) {
			Assert.isTrue(false);
		}
		fDocument= null;
		fUpdater= null;
	}
}
 
Example #28
Source File: ParameterGuessingProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void ensurePositionCategoryRemoved(IDocument document) {
	if (document.containsPositionCategory(getCategory())) {
		try {
			document.removePositionCategory(getCategory());
		} catch (BadPositionCategoryException e) {
			// ignore
		}
		document.removePositionUpdater(fUpdater);
	}
}
 
Example #29
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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
		JavaPlugin.log(e);
	}
}
 
Example #30
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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();
}