Java Code Examples for org.eclipse.jface.text.source.projection.ProjectionAnnotationModel#getPosition()

The following examples show how to use org.eclipse.jface.text.source.projection.ProjectionAnnotationModel#getPosition() . 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: RepositionHandler.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * A semi-hack... This uses stuff that may change at any time in Eclipse.  
 * In the java editor, the projection annotation model contains the collapsible regions which correspond to methods (and other areas
 * such as import groups).
 * 
 * This may work in other editor types as well... TBD
 */
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection,
		ExecutionEvent event) throws BadLocationException {

	ITextViewerExtension viewer = MarkUtils.getITextViewer(editor);
	if (viewer instanceof ProjectionViewer) {
		ProjectionAnnotationModel projection = ((ProjectionViewer)viewer).getProjectionAnnotationModel();
		Iterator<Annotation> pit = projection.getAnnotationIterator();
		while (pit.hasNext()) {
			Position p = projection.getPosition(pit.next());
			if (p.includes(currentSelection.getOffset())) {
				if (isUniversalPresent()) {
					// Do this here to prevent subsequent scrolling once range is revealed
					MarkUtils.setSelection(editor, new TextSelection(document, p.offset, 0));
				}
				// the viewer is pretty much guaranteed to be a TextViewer
				if (viewer instanceof TextViewer) {
					((TextViewer)viewer).revealRange(p.offset, p.length);
				}
				break;
			}
		}
	}
	return NO_OFFSET;		
}
 
Example 2
Source File: PyFoldingAction.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param element
 * @param elements
 * @param model
 * @return
 */
protected boolean isInsideLast(PyProjectionAnnotation element, List elements, ProjectionAnnotationModel model) {
    if (elements.size() == 0) {
        return false;
    }

    PyProjectionAnnotation top = (PyProjectionAnnotation) elements.get(elements.size() - 1);
    Position p1 = model.getPosition(element);
    Position pTop = model.getPosition(top);

    int p1Offset = p1.getOffset();

    int pTopoffset = pTop.getOffset();
    int pTopLen = pTopoffset + pTop.getLength();

    if (p1Offset > pTopoffset && p1Offset < pTopLen) {
        return true;
    }
    return false;
}
 
Example 3
Source File: DefaultFoldingStructureProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Annotation[] mergeFoldingRegions(Collection<FoldedPosition> foldedPositions,
		ProjectionAnnotationModel projectionAnnotationModel) {
	List<Annotation> deletions = new ArrayList<Annotation>();
	for (Iterator<Annotation> iterator = projectionAnnotationModel.getAnnotationIterator(); iterator.hasNext();) {
		Annotation annotation = iterator.next();
		if (annotation instanceof ProjectionAnnotation) {
			Position position = projectionAnnotationModel.getPosition(annotation);
			if (!foldedPositions.remove(position)) {
				deletions.add(annotation);
			}
		}
	}
	return deletions.toArray(new Annotation[deletions.size()]);
}
 
Example 4
Source File: AbstractFoldingEditor.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public void updateFoldingStructure(Map<ProjectionAnnotation, Position> annotations)
{
	synchronized (lockUpdateFoldingStructure)
	{
		List<Annotation> deletions = new ArrayList<Annotation>();
		Collection<Position> additions = annotations.values();
		ProjectionAnnotationModel currentModel = getAnnotationModel();
		if (currentModel == null)
		{
			return;
		}
		for (@SuppressWarnings("rawtypes")
		Iterator iter = currentModel.getAnnotationIterator(); iter.hasNext();)
		{
			Object annotation = iter.next();
			if (annotation instanceof ProjectionAnnotation)
			{
				Position position = currentModel.getPosition((Annotation) annotation);
				if (additions.contains(position))
				{
					additions.remove(position);
				}
				else
				{
					deletions.add((Annotation) annotation);
				}
			}
		}
		if (annotations.size() != 0 || deletions.size() != 0)
		{
			currentModel.modifyAnnotations(deletions.toArray(new Annotation[deletions.size()]), annotations, null);
		}
	}
}
 
Example 5
Source File: PyCollapse.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run(IAction action) {
    PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(getTextEditor());

    ProjectionAnnotationModel model = getTextEditor().getAdapter(
            ProjectionAnnotationModel.class);
    try {
        if (model != null) {
            //put annotations in array list.
            Iterator iter = model.getAnnotationIterator();
            while (iter != null && iter.hasNext()) {
                PyProjectionAnnotation element = (PyProjectionAnnotation) iter.next();
                Position position = model.getPosition(element);

                int line = ps.getDoc().getLineOfOffset(position.offset);

                int start = ps.getStartLineIndex();
                int end = ps.getEndLineIndex();

                for (int i = start; i <= end; i++) {
                    if (i == line) {
                        model.collapse(element);
                        break;
                    }
                }
            }

        }
    } catch (BadLocationException e) {
        Log.log(IStatus.ERROR, "Unexpected error collapsing", e);
    }
}
 
Example 6
Source File: CodeFoldingSetter.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * We have to be careful not to remove existing annotations because if this happens, previous code folding is not correct.
 */
private Tuple<ProjectionAnnotation, Position> getAnnotationToAdd(Position position, FoldingEntry node,
        ProjectionAnnotationModel model, List<Annotation> existing) {
    for (Iterator<Annotation> iter = existing.iterator(); iter.hasNext();) {
        Annotation element = iter.next();
        Position existingPosition = model.getPosition(element);
        if (existingPosition.equals(position)) {
            //ok, do nothing to this annotation (neither remove nor add, as it already exists in the correct place).
            existing.remove(element);
            return null;
        }
    }
    return new Tuple<ProjectionAnnotation, Position>(new PyProjectionAnnotation(node.getAstEntry(),
            node.isCollapsed), position);
}