Community
Participate
Working Groups
Build Identifier: As discussed with Sebastian Reproducible: Always
Created attachment 182799 [details] Java Source Ok to be published under EPL. Basic implementation based on "same text", not reference finding.e
/** * Reacts so changed text selections. * * @param event * Event to react to */ protected final void selectionChanged(final SelectionChangedEvent event) { ISelection selection = event.getSelection(); if (!selection.isEmpty() && selection instanceof ITextSelection) { replaceMarkers(((ITextSelection) selection).getOffset(), ((ITextSelection) selection).getLength()); } } /** * Marks all occurrences of the selected token. * * @param selectionOffset * Text selection offset * @param selectionLength * Text selection length */ private void replaceMarkers(final int selectionOffset, final int selectionLength) { CompositeNode rootNode = getDocument().readOnly(new IUnitOfWork<CompositeNode, XtextResource>() { @Override public final CompositeNode exec(final XtextResource state) { return state.getParseResult().getRootNode(); } }); // Check if selection has changed LeafNode selectedLeafNode = parseTreeUtil.findSingleSemanticNode(rootNode, selectionOffset, selectionLength); if (selectedLeafNode != currentSelection) { // NOPMD (check for object instance, not content) currentSelection = selectedLeafNode; } else { return; } final IAnnotationModel annotationModel = getAnnotationModel(); if (annotationModel == null) { return; } // Remove existing annotations for (Annotation annotation : occurrenceAnnotations.values()) { annotationModel.removeAnnotation(annotation); } if (selectedLeafNode == null) { return; } // Add annotations to annotation model occurrenceAnnotations = occurrencesMarker.createMarkers(rootNode, selectedLeafNode); for (Position position : occurrenceAnnotations.keySet()) { annotationModel.addAnnotation(occurrenceAnnotations.get(position), position); } }
Thanks a lot for the patch. It provided a bunch of useful pointers. The implementation I just pushed looks a bit more complicated: - it is based on references instead of text - it uses IReferenceFinder to find references. For performance reasons, annotation data is calculated in a background job - it doesn't affect the XtextEditor code, but rather connects by means of an IActionContribution - it is connected to the preference store and uses a single setting for all editors of a language - annotations are replaced in a single step rather than removed and added for performance reasons - it marks declarations and references differently Implementation pushed to MASTER.
Closing all bugs that were set to RESOLVED before Neon.0