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

The following examples show how to use org.eclipse.jface.text.source.IAnnotationModel#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: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private Annotation getAnnotation(final int offset, final int length) {
	final IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput());
	if (model == null || length < 0)
		return null;

	Iterator iterator;
	if (model instanceof IAnnotationModelExtension2) {
		iterator = ((IAnnotationModelExtension2) model).getAnnotationIterator(offset, length, true, true);
	} else {
		iterator = model.getAnnotationIterator();
	}

	while (iterator.hasNext()) {
		final Annotation a = (Annotation) iterator.next();
		final Position p = model.getPosition(a);
		if (p != null && p.overlapsWith(offset, length))
			return a;
	}
	return null;
}
 
Example 2
Source File: XtextQuickAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.3
 */
protected void selectAndRevealQuickfix(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations, List<ICompletionProposal> completionProposals) {
       if (completionProposals.isEmpty()) {
       	return;
       }
	if (!(invocationContext instanceof QuickAssistInvocationContext && ((QuickAssistInvocationContext) invocationContext).isSuppressSelection())) {
		ISourceViewer sourceViewer = invocationContext.getSourceViewer();
		IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
		Iterator<Annotation> iterator = applicableAnnotations.iterator();
		while (iterator.hasNext()) {
			Position pos = annotationModel.getPosition(iterator.next());
			if (pos != null) {
				sourceViewer.setSelectedRange(pos.getOffset(), pos.getLength());
				sourceViewer.revealRange(pos.getOffset(), pos.getLength());
				break;
			}
		}
	}
}
 
Example 3
Source File: ModulaSpellingHover.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
private HoverInfoWithSpellingAnnotation getSpellingHover(ITextViewer textViewer, IRegion hoverRegion) {
    IAnnotationModel model= null;
    if (textViewer instanceof ISourceViewerExtension2) {
        model = ((ISourceViewerExtension2)textViewer).getVisualAnnotationModel();
    } else if (textViewer instanceof SourceViewer) {
        model= ((SourceViewer)textViewer).getAnnotationModel();
    }
    if (model != null) {
        @SuppressWarnings("rawtypes")
        Iterator e= model.getAnnotationIterator();
        while (e.hasNext()) {
            Annotation a= (Annotation) e.next();
            if (a instanceof SpellingAnnotation) {
                Position p= model.getPosition(a);
                if (p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
                    return new HoverInfoWithSpellingAnnotation((SpellingAnnotation)a, textViewer, p.getOffset());
                }
            }
        }
    }
    return null;
}
 
Example 4
Source File: ContributionAnnotationManager.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This method is only called if an annotation with length &gt; 1 should be inserted, because if
 * text is pasted at a position that already contains an annotation, the content change on the
 * {@link IDocument} that is performed by the caller of {@link #insertAnnotation} leads to an
 * internal change of the annotation model.
 *
 * <p>The annotation with length &gt; 1 is removed, because the new annotation is subsequently
 * added by {@link #insertAnnotation}
 *
 * <p>Therefore, this methods works against the internal mechanisms of eclipse in order to enforce
 * the invariant of annotations with have a length &lt;= 1.
 *
 * @param model The model that could contain a violation of the invariant.
 */
private void enforceAnnotationsWithLengthOne(IAnnotationModel model) {
  List<ContributionAnnotation> annotationsToRemove = new ArrayList<>();

  Iterator<Annotation> i = model.getAnnotationIterator();
  while (i.hasNext()) {
    Annotation annotation = i.next();

    if (!(annotation instanceof ContributionAnnotation)) continue;

    ContributionAnnotation contrAnnotation = (ContributionAnnotation) annotation;
    Position p = model.getPosition(contrAnnotation);
    if (p.getLength() > 1) {
      annotationsToRemove.add(contrAnnotation);

      // Expect only one annotation with length > 1 created by
      // callers content change.
      break;
    }
  }
  annotationModelHelper.replaceAnnotationsInModel(
      model, annotationsToRemove, Collections.emptyMap());
}
 
Example 5
Source File: CorrectionMarkerResolutionGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IProblemLocation findProblemLocation(IEditorInput input, IMarker marker) {
	IAnnotationModel model= JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getAnnotationModel(input);
	if (model != null) { // open in editor
		Iterator<Annotation> iter= model.getAnnotationIterator();
		while (iter.hasNext()) {
			Annotation curr= iter.next();
			if (curr instanceof JavaMarkerAnnotation) {
				JavaMarkerAnnotation annot= (JavaMarkerAnnotation) curr;
				if (marker.equals(annot.getMarker())) {
					Position pos= model.getPosition(annot);
					if (pos != null) {
						return new ProblemLocation(pos.getOffset(), pos.getLength(), annot);
					}
				}
			}
		}
	} else { // not open in editor
		ICompilationUnit cu= getCompilationUnit(marker);
		return createFromMarker(marker, cu);
	}
	return null;
}
 
Example 6
Source File: JsonQuickAssistProcessor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
protected List<IMarker> getMarkersFor(ISourceViewer sourceViewer, int lineOffset, int lineLength) {
    List<IMarker> result = new ArrayList<>();
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    Iterator<?> annotationIter = annotationModel.getAnnotationIterator();

    while (annotationIter.hasNext()) {
        Object annotation = annotationIter.next();

        if (annotation instanceof MarkerAnnotation) {
            MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation;
            IMarker marker = markerAnnotation.getMarker();
            Position markerPosition = annotationModel.getPosition(markerAnnotation);
            if (markerPosition != null && markerPosition.overlapsWith(lineOffset, lineLength)) {
                result.add(marker);
            }
        }
    }
    return result;
}
 
Example 7
Source File: AnnotationExpandHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void sort(List<Annotation> exact, final IAnnotationModel model) {
	class AnnotationComparator implements Comparator<Annotation> {

		/*
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Annotation a1, Annotation a2) {
			Position p1= model.getPosition(a1);
			Position p2= model.getPosition(a2);

			// annotation order:
			// primary order: by position in line
			// secondary: annotation importance
			if (p1.offset == p2.offset)
				return getOrder(a2) - getOrder(a1);
			return p1.offset - p2.offset;
		}
	}

	Collections.sort(exact, new AnnotationComparator());

}
 
Example 8
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the annotation overlapping with the given range or <code>null</code>.
 *
 * @param offset the region offset
 * @param length the region length
 * @return the found annotation or <code>null</code>
 * @since 3.0
 */
private Annotation getAnnotation(int offset, int length) {
	IAnnotationModel model= getDocumentProvider().getAnnotationModel(getEditorInput());
	if (model == null)
		return null;

	Iterator<Annotation> parent;
	if (model instanceof IAnnotationModelExtension2)
		parent= ((IAnnotationModelExtension2)model).getAnnotationIterator(offset, length, true, true);
	else
		parent= model.getAnnotationIterator();

	Iterator<Annotation> e= new JavaAnnotationIterator(parent, false);
	while (e.hasNext()) {
		Annotation a= e.next();
		Position p= model.getPosition(a);
		if (p != null && p.overlapsWith(offset, length))
			return a;
	}
	return null;
}
 
Example 9
Source File: XtextQuickAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 */
protected List<ICompletionProposal> createQuickfixes(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations) {
   	List<ICompletionProposal> result = Lists.newArrayList();
   	ISourceViewer sourceViewer = invocationContext.getSourceViewer();
	IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
	IXtextDocument xtextDocument = xtextDocumentUtil.getXtextDocument(sourceViewer);
   	for(Annotation annotation : applicableAnnotations) {
		if (annotation instanceof SpellingAnnotation) {
			SpellingProblem spellingProblem = ((SpellingAnnotation) annotation).getSpellingProblem();
			ICompletionProposal[] proposals = spellingProblem.getProposals();
			if (proposals != null) {
				result.addAll(asList(proposals));
			}
		} else {
			final Issue issue = issueUtil.getIssueFromAnnotation(annotation);
			Position pos = annotationModel.getPosition(annotation);
			if (issue != null && pos != null) {
				@SuppressWarnings("deprecation")
				Iterable<IssueResolution> resolutions = getResolutions(issue, xtextDocument);
				if (resolutions.iterator().hasNext()) {
					for (IssueResolution resolution : resolutions) {
						result.add(create(pos, resolution));
					}
				}
			}
		}
	}
   	return result;
   }
 
Example 10
Source File: ContributionAnnotationManager.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Refreshes all contribution annotations in the model by removing and reinserting them.
 *
 * @param model the annotation model that should be refreshed
 */
public void refreshAnnotations(final IAnnotationModel model) {

  final List<Annotation> annotationsToRemove = new ArrayList<Annotation>();
  final Map<Annotation, Position> annotationsToAdd = new HashMap<Annotation, Position>();

  for (final Iterator<Annotation> it = model.getAnnotationIterator(); it.hasNext(); ) {

    final Annotation annotation = it.next();

    if (!(annotation instanceof ContributionAnnotation)) continue;

    final Position position = model.getPosition(annotation);

    if (position == null) {
      log.warn("annotation could not be found in the current model: " + annotation);
      continue;
    }

    /*
     * we rely on the fact the a user object is unique during a running
     * session so that user.equals(user) <=> user == user otherwise just
     * reinserting the annotations would not refresh the colors as the
     * color id of the user has not changed
     */
    annotationsToRemove.add(annotation);

    final ContributionAnnotation annotationToReplace = (ContributionAnnotation) annotation;
    final User source = annotationToReplace.getSource();
    final ContributionAnnotation annotationToAdd = new ContributionAnnotation(source, model);

    annotationsToAdd.put(annotationToAdd, position);

    getHistory(source).replaceInHistory(annotationToReplace, annotationToAdd);
  }

  if (annotationsToRemove.isEmpty()) return;

  annotationModelHelper.replaceAnnotationsInModel(model, annotationsToRemove, annotationsToAdd);
}
 
Example 11
Source File: JavaCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ProblemLocation getProblemLocation(IJavaAnnotation javaAnnotation, IAnnotationModel model) {
	int problemId= javaAnnotation.getId();
	if (problemId != -1) {
		Position pos= model.getPosition((Annotation) javaAnnotation);
		if (pos != null) {
			return new ProblemLocation(pos.getOffset(), pos.getLength(), javaAnnotation); // java problems all handled by the quick assist processors
		}
	}
	return null;
}
 
Example 12
Source File: JavaSelectMarkerRulerAction2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void annotationDefaultSelected(VerticalRulerEvent event) {
	Annotation annotation= event.getSelectedAnnotation();
	IAnnotationModel model= getAnnotationModel();

	if (isOverrideIndicator(annotation)) {
		((OverrideIndicatorManager.OverrideIndicator)annotation).open();
		return;
	}

	if (isBreakpoint(annotation))
		triggerAction(ITextEditorActionConstants.RULER_DOUBLE_CLICK, event.getEvent());

	Position position= model.getPosition(annotation);
	if (position == null)
		return;

	if (isQuickFixTarget(annotation)) {
		ITextOperationTarget operation= (ITextOperationTarget) getTextEditor().getAdapter(ITextOperationTarget.class);
		final int opCode= ISourceViewer.QUICK_ASSIST;
		if (operation != null && operation.canDoOperation(opCode)) {
			getTextEditor().selectAndReveal(position.getOffset(), position.getLength());
			operation.doOperation(opCode);
			return;
		}
	}

	// default:
	super.annotationDefaultSelected(event);
}
 
Example 13
Source File: TLAAnnotationHover.java    From tlaplus with MIT License 5 votes vote down vote up
private String[] getMessagesForLine(final ISourceViewer viewer, final int line) {
	final IAnnotationModel model = viewer.getAnnotationModel();

	if (model == null) {
		return new String[0];
	}

	final Iterator<Annotation> it = model.getAnnotationIterator();
	final IDocument document = viewer.getDocument();
	final ArrayList<String> messages = new ArrayList<>();
	final HashMap<Position, Set<String>> placeMessagesMap = new HashMap<>();
	while (it.hasNext()) {
		final Annotation annotation = it.next();
		if (annotation instanceof MarkerAnnotation) {
			final MarkerAnnotation ma = (MarkerAnnotation) annotation;
			final Position p = model.getPosition(ma);
			if (compareRulerLine(p, document, line)) {
				final IMarker marker = ma.getMarker();
				final String message = marker.getAttribute(IMarker.MESSAGE, null);
				if ((message != null) && (message.trim().length() > 0)) {
					Set<String> priorMessages = placeMessagesMap.get(p);
					if (priorMessages == null) {
						priorMessages = new HashSet<>();
						placeMessagesMap.put(p, priorMessages);
					}
					
					if (!priorMessages.contains(message)) {
						messages.add(message);
						priorMessages.add(message);
					}
				}
			}
		}
	}
	return (String[]) messages.toArray(new String[messages.size()]);
}
 
Example 14
Source File: TexlipseAnnotationUpdater.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if the selection is already annotated
 * @param selection current selection
 * @param model The AnnotationModel
 * @return true, if selection is already annotated
 */
private boolean testSelection (ISelection selection, IAnnotationModel model) {
    if (selection instanceof ITextSelection) {
        final ITextSelection textSelection = (ITextSelection) selection;
        //Iterate over all existing annotations
        for (Iterator<Annotation> iter = fOldAnnotations.iterator(); iter.hasNext();) {
            Annotation anno = iter.next();
            Position p = model.getPosition(anno);
            if (p != null && p.offset <= textSelection.getOffset() && p.offset+p.length >= textSelection.getOffset()) { 
                return true;
            }
        }
    }
    return false;
}
 
Example 15
Source File: AnnotationExpandHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected Object getHoverInfoForLine(ISourceViewer viewer, int line) {
	IAnnotationModel model= viewer.getAnnotationModel();
	IDocument document= viewer.getDocument();

	if (model == null)
		return null;

	List<Annotation> exact= new ArrayList<Annotation>();
	HashMap<Position, Object> messagesAtPosition= new HashMap<Position, Object>();

	Iterator<Annotation> e= model.getAnnotationIterator();
	while (e.hasNext()) {
		Annotation annotation= e.next();
		Position position= model.getPosition(annotation);
		if (position == null)
			continue;

		if (compareRulerLine(position, document, line) == 1) {
			if (isDuplicateMessage(messagesAtPosition, position, annotation.getText()))
				continue;

			exact.add(annotation);
		}
	}

	if (exact.size() < 1)
		return null;

	sort(exact, model);

	if (exact.size() > 0)
		setLastRulerMouseLocation(viewer, line);

	AnnotationHoverInput input= new AnnotationHoverInput();
	input.fAnnotations= exact.toArray(new Annotation[0]);
	input.fViewer= viewer;
	input.fRulerInfo= fCompositeRuler;
	input.fAnnotationListener= fgListener;
	input.fDoubleClickListener= fDblClickListener;
	input.model= model;

	return input;
}
 
Example 16
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Overrides the default implementation to handle {@link IJavaAnnotation}.
 * </p>
 *
 * @param offset the region offset
 * @param length the region length
 * @param forward <code>true</code> for forwards, <code>false</code> for backward
 * @param annotationPosition the position of the found annotation
 * @return the found annotation
 * @since 3.2
 */
@Override
protected Annotation findAnnotation(final int offset, final int length, boolean forward, Position annotationPosition) {

	Annotation nextAnnotation= null;
	Position nextAnnotationPosition= null;
	Annotation containingAnnotation= null;
	Position containingAnnotationPosition= null;
	boolean currentAnnotation= false;

	IDocument document= getDocumentProvider().getDocument(getEditorInput());
	int endOfDocument= document.getLength();
	int distance= Integer.MAX_VALUE;

	IAnnotationModel model= getDocumentProvider().getAnnotationModel(getEditorInput());
	if (model == null)
		return null;

	Iterator<Annotation> e= new JavaAnnotationIterator(model.getAnnotationIterator(), true);
	while (e.hasNext()) {
		Annotation a= e.next();
		if ((a instanceof IJavaAnnotation) && ((IJavaAnnotation)a).hasOverlay() || !isNavigationTarget(a))
			continue;

		Position p= model.getPosition(a);
		if (p == null)
			continue;

		if (forward && p.offset == offset || !forward && p.offset + p.getLength() == offset + length) {// || p.includes(offset)) {
			if (containingAnnotation == null || (forward && p.length >= containingAnnotationPosition.length || !forward && p.length >= containingAnnotationPosition.length)) {
				containingAnnotation= a;
				containingAnnotationPosition= p;
				currentAnnotation= p.length == length;
			}
		} else {
			int currentDistance= 0;

			if (forward) {
				currentDistance= p.getOffset() - offset;
				if (currentDistance < 0)
					currentDistance= endOfDocument + currentDistance;

				if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
					distance= currentDistance;
					nextAnnotation= a;
					nextAnnotationPosition= p;
				}
			} else {
				currentDistance= offset + length - (p.getOffset() + p.length);
				if (currentDistance < 0)
					currentDistance= endOfDocument + currentDistance;

				if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
					distance= currentDistance;
					nextAnnotation= a;
					nextAnnotationPosition= p;
				}
			}
		}
	}
	if (containingAnnotationPosition != null && (!currentAnnotation || nextAnnotation == null)) {
		annotationPosition.setOffset(containingAnnotationPosition.getOffset());
		annotationPosition.setLength(containingAnnotationPosition.getLength());
		return containingAnnotation;
	}
	if (nextAnnotationPosition != null) {
		annotationPosition.setOffset(nextAnnotationPosition.getOffset());
		annotationPosition.setLength(nextAnnotationPosition.getLength());
	}

	return nextAnnotation;
}
 
Example 17
Source File: JavaExpandHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected Object getHoverInfoForLine(final ISourceViewer viewer, final int line) {
	final boolean showTemporaryProblems= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
	IAnnotationModel model= viewer.getAnnotationModel();
	IDocument document= viewer.getDocument();

	if (model == null)
		return null;

	List<Annotation> exact= new ArrayList<Annotation>();
	HashMap<Position, Object> messagesAtPosition= new HashMap<Position, Object>();

	Iterator<Annotation> e= model.getAnnotationIterator();
	while (e.hasNext()) {
		Annotation annotation= e.next();

		if (fAnnotationAccess instanceof IAnnotationAccessExtension)
			if (!((IAnnotationAccessExtension)fAnnotationAccess).isPaintable(annotation))
				continue;

		if (annotation instanceof IJavaAnnotation && !isIncluded((IJavaAnnotation)annotation, showTemporaryProblems))
			continue;

		AnnotationPreference pref= fLookup.getAnnotationPreference(annotation);
		if (pref != null) {
			String key= pref.getVerticalRulerPreferenceKey();
			if (key != null && !fStore.getBoolean(key))
				continue;
		}

		Position position= model.getPosition(annotation);
		if (position == null)
			continue;

		if (compareRulerLine(position, document, line) == 1) {

			if (isDuplicateMessage(messagesAtPosition, position, annotation.getText()))
				continue;

			exact.add(annotation);
		}
	}

	sort(exact, model);

	if (exact.size() > 0)
		setLastRulerMouseLocation(viewer, line);

	if (exact.size() > 0) {
		Annotation first= exact.get(0);
		if (!isBreakpointAnnotation(first))
			exact.add(0, new NoBreakpointAnnotation());
	}

	if (exact.size() <= 1)
		return null;

	AnnotationHoverInput input= new AnnotationHoverInput();
	input.fAnnotations= exact.toArray(new Annotation[0]);
	input.fViewer= viewer;
	input.fRulerInfo= fCompositeRuler;
	input.fAnnotationListener= fgListener;
	input.fDoubleClickListener= fDblClickListener;
	input.redoAction= new AnnotationExpansionControl.ICallback() {

		public void run(IInformationControlExtension2 control) {
			control.setInput(getHoverInfoForLine(viewer, line));
		}

	};
	input.model= model;

	return input;
}
 
Example 18
Source File: AbstractAnnotationHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
	IPath path;
	IAnnotationModel model;
	if (textViewer instanceof ISourceViewer) {
		path= null;
		model= ((ISourceViewer)textViewer).getAnnotationModel();
	} else {
		// Get annotation model from file buffer manager
		path= getEditorInputPath();
		model= getAnnotationModel(path);
	}
	if (model == null)
		return null;

	try {
		Iterator<Annotation> parent;
		if (model instanceof IAnnotationModelExtension2)
			parent= ((IAnnotationModelExtension2)model).getAnnotationIterator(hoverRegion.getOffset(), hoverRegion.getLength(), true, true);
		else
			parent= model.getAnnotationIterator();
		Iterator<Annotation> e= new JavaAnnotationIterator(parent, fAllAnnotations);

		int layer= -1;
		Annotation annotation= null;
		Position position= null;
		while (e.hasNext()) {
			Annotation a= e.next();

			AnnotationPreference preference= getAnnotationPreference(a);
			if (preference == null || !(preference.getTextPreferenceKey() != null && fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
				continue;

			Position p= model.getPosition(a);

			int l= fAnnotationAccess.getLayer(a);

			if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
				String msg= a.getText();
				if (msg != null && msg.trim().length() > 0) {
					layer= l;
					annotation= a;
					position= p;
				}
			}
		}
		if (layer > -1)
			return createAnnotationInfo(annotation, position, textViewer);

	} finally {
		try {
			if (path != null) {
				ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
				manager.disconnect(path, LocationKind.NORMALIZE, null);
			}
		} catch (CoreException ex) {
			JavaPlugin.log(ex.getStatus());
		}
	}

	return null;
}
 
Example 19
Source File: AbstractAnnotationHover.java    From typescript.java with MIT License 4 votes vote down vote up
@Override
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
	IPath path;
	IAnnotationModel model;
	if (textViewer instanceof ISourceViewer) {
		path = null;
		model = ((ISourceViewer) textViewer).getAnnotationModel();
	} else {
		// Get annotation model from file buffer manager
		path = getEditorInputPath();
		model = getAnnotationModel(path);
	}
	if (model == null)
		return null;

	try {
		Iterator<Annotation> parent;
		if (model instanceof IAnnotationModelExtension2)
			parent = ((IAnnotationModelExtension2) model).getAnnotationIterator(hoverRegion.getOffset(),
					hoverRegion.getLength() > 0 ? hoverRegion.getLength() : 1, true, true);
		else
			parent = model.getAnnotationIterator();
		Iterator<Annotation> e = new TypeScriptAnnotationIterator(parent, fAllAnnotations);

		int layer = -1;
		Annotation annotation = null;
		Position position = null;
		while (e.hasNext()) {
			Annotation a = e.next();

			AnnotationPreference preference = getAnnotationPreference(a);
			if (preference == null || !(preference.getTextPreferenceKey() != null
			/*
			 * && fStore.getBoolean(preference.getTextPreferenceKey()) ||
			 * (preference.getHighlightPreferenceKey() != null &&
			 * fStore.getBoolean(preference.getHighlightPreferenceKey()))
			 */))
				continue;

			Position p = model.getPosition(a);

			int l = fAnnotationAccess.getLayer(a);

			if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
				String msg = a.getText();
				if (msg != null && msg.trim().length() > 0) {
					layer = l;
					annotation = a;
					position = p;
				}
			}
		}
		if (layer > -1)
			return createAnnotationInfo(annotation, position, textViewer);

	} finally {
		try {
			if (path != null) {
				ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
				manager.disconnect(path, LocationKind.NORMALIZE, null);
			}
		} catch (CoreException ex) {
			TypeScriptUIPlugin.log(ex.getStatus());
		}
	}

	return null;
}
 
Example 20
Source File: AbstractAnnotationHover.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
@Override
	public Object getHoverInfo(ISourceBuffer sourceBuffer, IRegion hoverRegion, ITextViewer textViewer) {
		
		if (!(textViewer instanceof ISourceViewer)) {
			return null;
		}
		
		ISourceViewer sourceViewer = (ISourceViewer) textViewer;
//		IPath path = null;
		IAnnotationModel model = sourceViewer.getAnnotationModel();
//		if (editor.getSourceViewer_() instanceof ISourceViewer) {
//			path= null;
//			model= editor.getSourceViewer_().getAnnotationModel();
//		} else {
//			// Get annotation model from file buffer manager
//			path= getEditorInputPath();
//			model= getAnnotationModel(path);
//		}
		if (model == null)
			return null;

//		try {
			Iterator<Annotation> parent;
			if (model instanceof IAnnotationModelExtension2)
				parent= ((IAnnotationModelExtension2)model).getAnnotationIterator(hoverRegion.getOffset(), hoverRegion.getLength(), true, true);
			else
				parent= model.getAnnotationIterator();
			Iterator<Annotation> e= new JavaAnnotationIterator(parent, fAllAnnotations);

			int layer= -1;
			Annotation annotation= null;
			Position position= null;
			while (e.hasNext()) {
				Annotation a= e.next();

				AnnotationPreference preference= getAnnotationPreference(a);
				if (preference == null || !(preference.getTextPreferenceKey() != null && fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
					continue;

				Position p= model.getPosition(a);

				int l= fAnnotationAccess.getLayer(a);

				if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
					String msg= a.getText();
					if (msg != null && msg.trim().length() > 0) {
						layer= l;
						annotation= a;
						position= p;
					}
				}
			}
			if (layer > -1)
				return createAnnotationInfo(annotation, position, textViewer);

//		} finally {
//			try {
//				if (path != null) {
//					ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
//					manager.disconnect(path, LocationKind.NORMALIZE, null);
//				}
//			} catch (CoreException ex) {
//				LangUIPlugin.logStatus(ex);
//			}
//		}

		return null;
	}