/*** * Returns true if it exists a marker annotation in the given offset and false * otherwise. * * @param textViewer * @param offset * @return true if it exists a marker annotation in the given offset and false * otherwise. */ private static boolean hasProblem(ITextViewer textViewer, int offset) { if (!(textViewer instanceof ISourceViewer)) { return false; } IAnnotationModel annotationModel = ((ISourceViewer) textViewer).getAnnotationModel(); Iterator<Annotation> iter = (annotationModel instanceof IAnnotationModelExtension2) ? ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(offset, 1, true, true) : annotationModel.getAnnotationIterator(); while (iter.hasNext()) { Annotation ann = iter.next(); if (ann instanceof MarkerAnnotation) { return true; } } return false; }
@SuppressWarnings("rawtypes") private Annotation getAnnotation(final int offset, final int length) { final IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput()); if (model == null) 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; }
/** * 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; }
@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; }
protected Set<Annotation> getApplicableAnnotations(final IXtextDocument document, final IAnnotationModel annotationModel, final int offset) throws BadLocationException { final int line = document.getLineOfOffset(offset); final String delim = document.getLineDelimiter(line); final int delimLength = delim != null ? delim.length() : 0; final int lineLength = document.getLineLength(line) - delimLength; final int lineOffset = document.getLineOffset(line); Iterator<?> iterator; if (annotationModel instanceof IAnnotationModelExtension2) iterator = ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(lineOffset, lineLength, true, true); else iterator = annotationModel.getAnnotationIterator(); Set<Annotation> possibleAnnotations = Sets.newHashSet(); Annotation actualAnnotation = null; int nearestAnnotationOffset = Integer.MAX_VALUE; Annotation firstAnnotation = null; int offsetOfFirstAnnotation = Integer.MAX_VALUE; while (iterator.hasNext()) { Object key = iterator.next(); if (!(key instanceof Annotation)) continue; Annotation annotationTemp = (Annotation) key; if (!canFix(annotationTemp)) continue; Position pos = annotationModel.getPosition(annotationTemp); if (pos == null) continue; if (pos.overlapsWith(lineOffset, lineLength)) { possibleAnnotations.add(annotationTemp); if (pos.getOffset() < offsetOfFirstAnnotation) { offsetOfFirstAnnotation = pos.getOffset(); firstAnnotation = annotationTemp; } int distanceToOffset = offset - pos.getOffset(); if (distanceToOffset >= 0 && distanceToOffset < nearestAnnotationOffset) { actualAnnotation = annotationTemp; nearestAnnotationOffset = distanceToOffset; } } } // choose the first annotation if there is no better selection available if (actualAnnotation == null) { actualAnnotation = firstAnnotation; } // Find Annotations with same offset an length Set<Annotation> actualAnnotations = Sets.newHashSet(); for(Annotation possibleAnnotation : possibleAnnotations){ Position possibleAnnotationPosition = annotationModel.getPosition(possibleAnnotation); Position actualAnnotationPosition = annotationModel.getPosition(actualAnnotation); // position may be null due to concurrent operation on the annotation model if(possibleAnnotationPosition != null && possibleAnnotationPosition.equals(actualAnnotationPosition)){ actualAnnotations.add(possibleAnnotation); } } return actualAnnotations; }
@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; }
@Override public ICompletionProposal[] computeQuickAssistProposals( IQuickAssistInvocationContext invocationContext) { ISourceViewer viewer = invocationContext.getSourceViewer(); int documentOffset = invocationContext.getOffset(); int length = viewer != null ? viewer.getSelectedRange().y : 0; IAnnotationModel model = viewer.getAnnotationModel(); if (model == null) return null; List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>(); if (model instanceof IAnnotationModelExtension2) { Iterator iter = ((IAnnotationModelExtension2) model) .getAnnotationIterator(documentOffset, length, true, true); while (iter.hasNext()) { Annotation anno = (Annotation) iter.next(); if (canFix(anno)) { int offset = -1; if (anno instanceof TemporaryAnnotation) { offset = ((TemporaryAnnotation) anno).getPosition() .getOffset(); } else if (anno instanceof MarkerAnnotation) { offset = ((MarkerAnnotation) anno).getMarker() .getAttribute(IMarker.CHAR_START, -1); } if (offset == -1) continue; // add "New file wizard" completion proposal IDOMAttr attr = DOMHelper.getAttrByOffset( viewer.getDocument(), offset); if (attr != null) { IFile file = EditorUtils.getFile(viewer.getDocument()); CreateFileCompletionProposal proposal = new CreateFileCompletionProposal( file, attr); if (!proposals.contains(proposal)) { proposals.add(proposal); } } } } } if (proposals.isEmpty()) return null; return proposals .toArray(new ICompletionProposal[proposals.size()]); }
@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; }