/** * Attaches a coverage annotation model for the given editor if the editor can * be annotated. Does nothing if the model is already attached. * * @param editor * Editor to attach a annotation model to */ public static void attach(ITextEditor editor) { IDocumentProvider provider = editor.getDocumentProvider(); // there may be text editors without document providers (SF #1725100) if (provider == null) return; IAnnotationModel model = provider.getAnnotationModel(editor .getEditorInput()); if (!(model instanceof IAnnotationModelExtension)) return; IAnnotationModelExtension modelex = (IAnnotationModelExtension) model; IDocument document = provider.getDocument(editor.getEditorInput()); CoverageAnnotationModel coveragemodel = (CoverageAnnotationModel) modelex .getAnnotationModel(KEY); if (coveragemodel == null) { coveragemodel = new CoverageAnnotationModel(editor, document); modelex.addAnnotationModel(KEY, coveragemodel); } }
private void ensureAnnotationModelInstalled() { LinkedPositionAnnotations lpa = fCurrentTarget.fAnnotationModel; if (lpa != null) { ITextViewer viewer = fCurrentTarget.getViewer(); if (viewer instanceof ISourceViewer) { ISourceViewer sv = (ISourceViewer) viewer; IAnnotationModel model = sv.getAnnotationModel(); if (model instanceof IAnnotationModelExtension) { IAnnotationModelExtension ext = (IAnnotationModelExtension) model; IAnnotationModel ourModel = ext.getAnnotationModel(getUniqueKey()); if (ourModel == null) { ext.addAnnotationModel(getUniqueKey(), lpa); } } } } }
private void removeOccurrenceAnnotations() { // fMarkOccurrenceModificationStamp= // IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; // fMarkOccurrenceTargetRegion= null; IDocumentProvider documentProvider = getDocumentProvider(); if (documentProvider == null) return; IAnnotationModel annotationModel = documentProvider.getAnnotationModel(getEditorInput()); if (annotationModel == null || fOccurrenceAnnotations == null) return; synchronized (getLockObject(annotationModel)) { if (annotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, null); } else { for (int i = 0, length = fOccurrenceAnnotations.length; i < length; i++) annotationModel.removeAnnotation(fOccurrenceAnnotations[i]); } fOccurrenceAnnotations = null; } }
protected void updateAnnotations(IProgressMonitor monitor, List<Annotation> toBeRemoved, Map<Annotation, Position> annotationToPosition) { if (monitor.isCanceled()) { return; } if (annotationModel instanceof IAnnotationModelExtension) { Annotation[] removedAnnotations = toBeRemoved.toArray(new Annotation[toBeRemoved.size()]); ((IAnnotationModelExtension) annotationModel).replaceAnnotations(removedAnnotations, annotationToPosition); } else { for (Annotation annotation : toBeRemoved) { if (monitor.isCanceled()) { return; } annotationModel.removeAnnotation(annotation); } for (Map.Entry<Annotation, Position> entry : annotationToPosition.entrySet()) { if (monitor.isCanceled()) { return; } annotationModel.addAnnotation(entry.getKey(), entry.getValue()); } } }
void removeOccurrenceAnnotations() { fMarkOccurrenceModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; fMarkOccurrenceTargetRegion= null; IDocumentProvider documentProvider= getDocumentProvider(); if (documentProvider == null) return; IAnnotationModel annotationModel= documentProvider.getAnnotationModel(getEditorInput()); if (annotationModel == null || fOccurrenceAnnotations == null) return; synchronized (getLockObject(annotationModel)) { if (annotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension)annotationModel).replaceAnnotations(fOccurrenceAnnotations, null); } else { for (int i= 0, length= fOccurrenceAnnotations.length; i < length; i++) annotationModel.removeAnnotation(fOccurrenceAnnotations[i]); } fOccurrenceAnnotations= null; } }
public ExternalBreakpointWatcher(IEditorInput input, IDocument document, IAnnotationModel annotationModel) { this.location = EditorUtils.getLocationOrNull(input); this.document = assertNotNull(document); this.annotationModel = assertNotNull(annotationModel); if(annotationModel instanceof IAnnotationModelExtension) { this.annotationModelExt = (IAnnotationModelExtension) annotationModel; } else { this.annotationModelExt = null; } if(location != null) { ResourceUtils.connectResourceListener(resourceListener, this::initializeFromResources, getWorkspaceRoot(), owned); } }
/** * Detaches the coverage annotation model from the given editor. If the editor * does not have a model attached, this method does nothing. * * @param editor * Editor to detach the annotation model from */ public static void detach(ITextEditor editor) { IDocumentProvider provider = editor.getDocumentProvider(); // there may be text editors without document providers (SF #1725100) if (provider == null) return; IAnnotationModel model = provider.getAnnotationModel(editor .getEditorInput()); if (!(model instanceof IAnnotationModelExtension)) return; IAnnotationModelExtension modelex = (IAnnotationModelExtension) model; modelex.removeAnnotationModel(KEY); }
private void uninstallAnnotationModel(LinkedModeUITarget target) { ITextViewer viewer = target.getViewer(); if (viewer instanceof ISourceViewer) { ISourceViewer sv = (ISourceViewer) viewer; IAnnotationModel model = sv.getAnnotationModel(); if (model instanceof IAnnotationModelExtension) { IAnnotationModelExtension ext = (IAnnotationModelExtension) model; ext.removeAnnotationModel(getUniqueKey()); } } }
protected void announceAnnotationChanged(Annotation annotation) { if (annotationModel instanceof XtextResourceMarkerAnnotationModel) ((XtextResourceMarkerAnnotationModel) annotationModel).fireAnnotationChangedEvent(annotation); else { Position position = annotationModel.getPosition(annotation); if (annotationModel instanceof IAnnotationModelExtension) ((IAnnotationModelExtension) annotationModel).modifyAnnotationPosition(annotation, position); else { annotationModel.removeAnnotation(annotation); annotationModel.addAnnotation(annotation, position); } } }
@Override protected IStatus run(IProgressMonitor monitor) { final XtextEditor editor = initialEditor; final boolean isMarkOccurrences = initialIsMarkOccurrences; final ITextSelection selection = initialSelection; final SubMonitor progress = SubMonitor.convert(monitor, 2); if (!progress.isCanceled()) { final Map<Annotation, Position> annotations = (isMarkOccurrences) ? occurrenceComputer.createAnnotationMap(editor, selection, progress.newChild(1)) : Collections.<Annotation, Position>emptyMap(); if (!progress.isCanceled()) { Display.getDefault().asyncExec(new Runnable() { public void run() { if (!progress.isCanceled()) { final IAnnotationModel annotationModel = getAnnotationModel(editor); if (annotationModel instanceof IAnnotationModelExtension) ((IAnnotationModelExtension) annotationModel).replaceAnnotations( getExistingOccurrenceAnnotations(annotationModel), annotations); else if(annotationModel != null) throw new IllegalStateException( "AnnotationModel does not implement IAnnotationModelExtension"); //$NON-NLS-1$ } } }); } } return progress.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS; }
/** * Removes all override indicators from this manager's annotation model. */ void removeAnnotations() { if (fOverrideAnnotations == null) return; synchronized (fAnnotationModelLockObject) { if (fAnnotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension)fAnnotationModel).replaceAnnotations(fOverrideAnnotations, null); } else { for (int i= 0, length= fOverrideAnnotations.length; i < length; i++) fAnnotationModel.removeAnnotation(fOverrideAnnotations[i]); } fOverrideAnnotations= null; } }
private void updateAnnotations() { if (edit == null) { return; } IDocumentProvider provider = edit.getDocumentProvider(); if (provider == null) { return; } IAnnotationModel model = provider.getAnnotationModel(edit.getEditorInput()); if (model == null) { return; } IAnnotationModelExtension modelExtension = (IAnnotationModelExtension) model; List<Annotation> existing = new ArrayList<Annotation>(); Iterator<Annotation> it = model.getAnnotationIterator(); if (it == null) { return; } while (it.hasNext()) { existing.add(it.next()); } IDocument doc = edit.getDocument(); IResource resource = PyMarkerUtils.getResourceForTextEditor(edit); IEditorInput externalFileEditorInput = AbstractBreakpointRulerAction.getExternalFileEditorInput(edit); List<IMarker> markers = AbstractBreakpointRulerAction.getMarkersFromEditorResource(resource, doc, externalFileEditorInput, 0, false, model); Map<Annotation, Position> annotationsToAdd = new HashMap<Annotation, Position>(); for (IMarker m : markers) { Position pos = PyMarkerUtils.getMarkerPosition(doc, m, model); MarkerAnnotation newAnnotation = new MarkerAnnotation(m); annotationsToAdd.put(newAnnotation, pos); } //update all in a single step modelExtension.replaceAnnotations(existing.toArray(new Annotation[0]), annotationsToAdd); }
@Override public Object execute(ExecutionEvent event) throws ExecutionException { final String styleParameter = event.getParameter(PARAMETER_ID); final String annotationType = getAnnotationType(styleParameter); try { final ITextEditor textEditor = AppUtils.getEditor(); Job job = new Job(Messages.RemoveOccurenciesHandler_addStyleJobName) { @SuppressWarnings("unchecked") @Override protected IStatus run(IProgressMonitor monitor) { IDocumentProvider idp = textEditor.getDocumentProvider(); final IAnnotationModel annotationModel = idp.getAnnotationModel(textEditor.getEditorInput()); List<Annotation> removeAnnotations = new LinkedList<Annotation>(); Iterator<Annotation> annotationIterator = annotationModel.getAnnotationIterator(); while (annotationIterator.hasNext()) { Annotation annotation = annotationIterator.next(); if (annotation.getType().equals(annotationType)) { removeAnnotations.add(annotation); } } Annotation[] annotations = removeAnnotations.toArray(new Annotation[removeAnnotations.size()]); ((IAnnotationModelExtension) annotationModel).replaceAnnotations(annotations, null); return Status.OK_STATUS; } }; job.schedule(); } catch (Exception ex) { ex.printStackTrace(); } return null; }
public IStatus run(IProgressMonitor progressMonitor) { fProgressMonitor = progressMonitor; if (isCanceled()) { if (LinkedModeModel.hasInstalledModel(fDocument)) { // Template completion applied, remove occurrences removeOccurrenceAnnotations(); } return Status.CANCEL_STATUS; } ITextViewer textViewer = getViewer(); if (textViewer == null) return Status.CANCEL_STATUS; IDocument document = textViewer.getDocument(); if (document == null) return Status.CANCEL_STATUS; IAnnotationModel annotationModel = getAnnotationModel(); if (annotationModel == null) return Status.CANCEL_STATUS; // Add occurrence annotations int length = fPositions.length; Map annotationMap = new HashMap(length); for (int i = 0; i < length; i++) { if (isCanceled()) return Status.CANCEL_STATUS; String message; Position position = fPositions[i]; // Create & add annotation try { message = document.get(position.offset, position.length); } catch (BadLocationException ex) { // Skip this match continue; } annotationMap.put(new Annotation("org.eclipse.wst.jsdt.ui.occurrences", false, message), //$NON-NLS-1$ position); } if (isCanceled()) return Status.CANCEL_STATUS; synchronized (getLockObject(annotationModel)) { if (annotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap); } else { removeOccurrenceAnnotations(); Iterator iter = annotationMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry mapEntry = (Map.Entry) iter.next(); annotationModel.addAnnotation((Annotation) mapEntry.getKey(), (Position) mapEntry.getValue()); } } fOccurrenceAnnotations = (Annotation[]) annotationMap.keySet() .toArray(new Annotation[annotationMap.keySet().size()]); } return Status.OK_STATUS; }
@Override public IStatus run(IProgressMonitor progressMonitor) { if (isCanceled(progressMonitor)) return Status.CANCEL_STATUS; ITextViewer textViewer= getViewer(); if (textViewer == null) return Status.CANCEL_STATUS; IDocument document= textViewer.getDocument(); if (document == null) return Status.CANCEL_STATUS; IDocumentProvider documentProvider= getDocumentProvider(); if (documentProvider == null) return Status.CANCEL_STATUS; IAnnotationModel annotationModel= documentProvider.getAnnotationModel(getEditorInput()); if (annotationModel == null) return Status.CANCEL_STATUS; // Add occurrence annotations int length= fLocations.length; Map<Annotation, Position> annotationMap= new HashMap<Annotation, Position>(length); for (int i= 0; i < length; i++) { if (isCanceled(progressMonitor)) return Status.CANCEL_STATUS; OccurrenceLocation location= fLocations[i]; Position position= new Position(location.getOffset(), location.getLength()); String description= location.getDescription(); String annotationType= (location.getFlags() == IOccurrencesFinder.F_WRITE_OCCURRENCE) ? "org.eclipse.jdt.ui.occurrences.write" : "org.eclipse.jdt.ui.occurrences"; //$NON-NLS-1$ //$NON-NLS-2$ annotationMap.put(new Annotation(annotationType, false, description), position); } if (isCanceled(progressMonitor)) return Status.CANCEL_STATUS; synchronized (getLockObject(annotationModel)) { if (annotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension)annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap); } else { removeOccurrenceAnnotations(); Iterator<Entry<Annotation, Position>> iter= annotationMap.entrySet().iterator(); while (iter.hasNext()) { Entry<Annotation, Position> mapEntry= iter.next(); annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue()); } } fOccurrenceAnnotations= annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]); } return Status.OK_STATUS; }
/** * Updates the override and implements annotations based * on the given AST. * * @param ast the compilation unit AST * @param progressMonitor the progress monitor * @since 3.0 */ protected void updateAnnotations(CompilationUnit ast, IProgressMonitor progressMonitor) { if (ast == null || progressMonitor.isCanceled()) return; final Map<Annotation, Position> annotationMap= new HashMap<Annotation, Position>(50); ast.accept(new ASTVisitor(false) { /* * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodDeclaration) */ @Override public boolean visit(MethodDeclaration node) { IMethodBinding binding= node.resolveBinding(); if (binding != null) { IMethodBinding definingMethod= Bindings.findOverriddenMethod(binding, true); if (definingMethod != null) { ITypeBinding definingType= definingMethod.getDeclaringClass(); String qualifiedMethodName= definingType.getQualifiedName() + "." + binding.getName(); //$NON-NLS-1$ boolean isImplements= JdtFlags.isAbstract(definingMethod); String text; if (isImplements) text= Messages.format(JavaEditorMessages.OverrideIndicatorManager_implements, BasicElementLabels.getJavaElementName(qualifiedMethodName)); else text= Messages.format(JavaEditorMessages.OverrideIndicatorManager_overrides, BasicElementLabels.getJavaElementName(qualifiedMethodName)); SimpleName name= node.getName(); Position position= new Position(name.getStartPosition(), name.getLength()); annotationMap.put( new OverrideIndicator(isImplements, text, binding.getKey()), position); } } return true; } }); if (progressMonitor.isCanceled()) return; synchronized (fAnnotationModelLockObject) { if (fAnnotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension)fAnnotationModel).replaceAnnotations(fOverrideAnnotations, annotationMap); } else { removeAnnotations(); Iterator<Entry<Annotation, Position>> iter= annotationMap.entrySet().iterator(); while (iter.hasNext()) { Entry<Annotation, Position> mapEntry= iter.next(); fAnnotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue()); } } fOverrideAnnotations= annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]); } }
/** * @param annotationModel */ protected synchronized void removeOccurenceAnnotations(IAnnotationModel annotationModel, BaseEditor pyEdit) { //remove the annotations Map<String, Object> cache = pyEdit.cache; if (cache == null) { return; } //let other threads execute before getting the lock on the annotation model Thread.yield(); Thread thread = Thread.currentThread(); int initiaThreadlPriority = thread.getPriority(); //before getting the lock, let's execute with normal priority, to optimize the time that we'll //retain that object locked (the annotation model is used on lots of places, so, retaining the lock //on it on a minimum priority thread is not a good thing. thread.setPriority(Thread.NORM_PRIORITY); try { synchronized (getLockObject(annotationModel)) { List<Annotation> annotationsToRemove = getOccurrenceAnnotationsInEditor(pyEdit); if (annotationModel instanceof IAnnotationModelExtension) { //replace those ((IAnnotationModelExtension) annotationModel).replaceAnnotations( annotationsToRemove.toArray(new Annotation[annotationsToRemove.size()]), new HashMap<Annotation, Position>()); } else { Iterator<Annotation> annotationIterator = annotationsToRemove.iterator(); while (annotationIterator.hasNext()) { annotationModel.removeAnnotation(annotationIterator.next()); } } cache.put(getOccurrenceAnnotationsCacheKey(), null); } //end remove the annotations } finally { thread.setPriority(initiaThreadlPriority); } }
@Override public void endCollecting() { List<Object> toRemove = new ArrayList<Object>(); Object fLockObject; if (fAnnotationModel instanceof ISynchronizable) { fLockObject = ((ISynchronizable) fAnnotationModel).getLockObject(); } else { fLockObject = new Object(); } //let other threads execute before getting the lock on the annotation model Thread.yield(); Thread thread = Thread.currentThread(); int initiaThreadlPriority = thread.getPriority(); try { //before getting the lock, let's execute with normal priority, to optimize the time that we'll //retain that object locked (the annotation model is used on lots of places, so, retaining the lock //on it on a minimum priority thread is not a good thing. thread.setPriority(Thread.NORM_PRIORITY); Iterator<Annotation> iter; synchronized (fLockObject) { iter = fAnnotationModel.getAnnotationIterator(); while (iter.hasNext()) { Object n = iter.next(); if (n instanceof SpellingAnnotation) { toRemove.add(n); } } iter = null; } Annotation[] annotationsToRemove = toRemove.toArray(new Annotation[toRemove.size()]); //let other threads execute before getting the lock (again) on the annotation model Thread.yield(); synchronized (fLockObject) { if (fAnnotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension) fAnnotationModel).replaceAnnotations(annotationsToRemove, fAddAnnotations); } else { for (int i = 0; i < annotationsToRemove.length; i++) { fAnnotationModel.removeAnnotation(annotationsToRemove[i]); } for (iter = fAddAnnotations.keySet().iterator(); iter.hasNext();) { Annotation annotation = iter.next(); fAnnotationModel.addAnnotation(annotation, fAddAnnotations.get(annotation)); } } } } finally { thread.setPriority(initiaThreadlPriority); } fAddAnnotations = null; }
@Override public Object execute(ExecutionEvent event) throws ExecutionException { final String styleParameter = event.getParameter(PARAMETER_ID); final String annotationType = getAnnotationType(styleParameter); try { final ITextEditor textEditor = AppUtils.getEditor(); final TextSelection selection = AppUtils.getTextSelection(); final String searchTerm = selection.getText(); Job job = new Job("Adding Style") { //$NON-NLS-1$ @Override protected IStatus run(IProgressMonitor monitor) { final IFile file = (IFile) textEditor.getEditorInput().getAdapter(IFile.class); final TextSearchResult textSearchResult = LoganalyserSearchEngine.search(file, searchTerm, monitor); Map<Annotation, Position> annotationMap = new HashMap<Annotation, Position>(); final List<FileMatch> searchResult = LoganalyserSearchEngine.getFileMatches(textSearchResult); for (FileMatch fileMatch : searchResult) { Position position = new Position(fileMatch.getOffset(), fileMatch.getLength()); Annotation annotation = new Annotation(false); annotation.setType(annotationType); annotationMap.put(annotation, position); } IDocumentProvider idp = textEditor.getDocumentProvider(); final IAnnotationModel annotationModel = idp.getAnnotationModel(textEditor.getEditorInput()); ((IAnnotationModelExtension) annotationModel).replaceAnnotations(null, annotationMap); return Status.OK_STATUS; } }; job.setPriority(Job.INTERACTIVE); job.setUser(true); job.schedule(); } catch (Exception ex) { ex.printStackTrace(); } return null; }