Java Code Examples for org.eclipse.jface.text.IDocument#getPositions()

The following examples show how to use org.eclipse.jface.text.IDocument#getPositions() . 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: 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 2
Source File: PartitionCodeReader.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Note: this just gets the positions in the document. To cover for holes, use
 * StringUtils.sortAndMergePositions with the result of this call.
 */
public static Position[] getDocumentTypedPositions(IDocument document, String defaultContentType) {
    if (ALL_CONTENT_TYPES_AVAILABLE.equals(defaultContentType)) {
        //Consider the whole document
        return new Position[] { new TypedPosition(0, document.getLength(), defaultContentType) };
    }
    Position[] positions;
    try {
        IDocumentPartitionerExtension2 partitioner = (IDocumentPartitionerExtension2) document
                .getDocumentPartitioner();
        String[] managingPositionCategories = partitioner.getManagingPositionCategories();
        Assert.isTrue(managingPositionCategories.length == 1);
        positions = document.getPositions(managingPositionCategories[0]);
        if (positions == null) {
            positions = new Position[] { new TypedPosition(0, document.getLength(), defaultContentType) };
        }
    } catch (Exception e) {
        Log.log("Unable to get positions for: " + defaultContentType, e); //Shouldn't happen, but if it does, consider the whole doc.
        positions = new Position[] { new TypedPosition(0, document.getLength(), defaultContentType) };
    }
    return positions;
}
 
Example 3
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 4
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 5
Source File: SemanticHighlightingReconciler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public List<HighlightedPositionCore> reconciled(IDocument document, ASTNode ast, boolean forced, IProgressMonitor progressMonitor) throws BadPositionCategoryException {
	// ensure at most one thread can be reconciling at any time
	synchronized (fReconcileLock) {
		if (fIsReconciling) {
			return emptyList();
		} else {
			fIsReconciling= true;
		}
	}
	fJobPresenter= fPresenter;
	fJobSemanticHighlightings= fSemanticHighlightings;
	fJobHighlightings= fHighlightings;

	try {
		if (ast == null) {
			return emptyList();
		}

		ASTNode[] subtrees= getAffectedSubtrees(ast);
		if (subtrees.length == 0) {
			return emptyList();
		}

		startReconcilingPositions();

		if (!fJobPresenter.isCanceled()) {
			fJobDeprecatedMemberHighlighting= null;
			for (int i= 0, n= fJobSemanticHighlightings.length; i < n; i++) {
				SemanticHighlightingCore semanticHighlighting= fJobSemanticHighlightings[i];
				if (semanticHighlighting instanceof DeprecatedMemberHighlighting) {
					fJobDeprecatedMemberHighlighting = fJobHighlightings.get(i);
					break;
				}
			}
			reconcilePositions(subtrees);
		}

		// We need to manually install the position category
		if (!document.containsPositionCategory(fPresenter.getPositionCategory())) {
			document.addPositionCategory(fPresenter.getPositionCategory());
		}

		updatePresentation(document, fAddedPositions, fRemovedPositions);
		stopReconcilingPositions();
		Position[] positions = document.getPositions(fPresenter.getPositionCategory());
		for (Position position : positions) {
			Preconditions.checkState(position instanceof HighlightedPositionCore);
		}
		return FluentIterable.from(Arrays.asList(positions)).filter(HighlightedPositionCore.class).toList();
	} finally {
		fJobPresenter= null;
		fJobSemanticHighlightings= null;
		fJobHighlightings= null;
		fJobDeprecatedMemberHighlighting= null;
		synchronized (fReconcileLock) {
			fIsReconciling= false;
		}
	}
}
 
Example 6
Source File: DocumentScopeManager.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private String getTokenScopeFragments(ITextViewer viewer, IDocument document, int offset)
{
	if (!(viewer instanceof ISourceViewer))
	{
		return null;
	}

	try
	{
		// Force adding the category in case it doesn't exist yet...
		document.addPositionCategory(ICommonConstants.SCOPE_CATEGORY);
		Position[] scopes = document.getPositions(ICommonConstants.SCOPE_CATEGORY);
		int index = document.computeIndexInCategory(ICommonConstants.SCOPE_CATEGORY, offset);

		if (scopes == null || scopes.length == 0)
		{
			return null;
		}
		if (index >= scopes.length)
		{
			index = scopes.length - 1;
		}
		Position scope = scopes[index];
		if (scope == null)
		{
			return null;
		}
		if (!scope.includes(offset))
		{
			if (index > 0)
			{
				scope = scopes[--index];
				if (scope == null || !scope.includes(offset))
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}
		if (scope instanceof TypedPosition)
		{
			TypedPosition pos = (TypedPosition) scope;
			return pos.getType();
		}
	}
	catch (Exception e)
	{
		IdeLog.logError(CommonEditorPlugin.getDefault(), e);
	}
	return null;
}