Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 329883 - Add Occurrences Marking
Summary: Add Occurrences Marking
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: M6   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-10 06:03 EST by Christoph Zwicker CLA
Modified: 2017-09-19 18:14 EDT (History)
2 users (show)

See Also:
sebastian.zarnekow: indigo+


Attachments
Java Source (3.58 KB, application/x-zip-compressed)
2010-11-10 06:05 EST, Christoph Zwicker CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Christoph Zwicker CLA 2010-11-10 06:03:19 EST
Build Identifier: 

As discussed with Sebastian

Reproducible: Always
Comment 1 Christoph Zwicker CLA 2010-11-10 06:05:35 EST
Created attachment 182799 [details]
Java Source

Ok to be published under EPL. Basic implementation based on "same text", not reference finding.e
Comment 2 Christoph Zwicker CLA 2010-11-10 06:11:36 EST

  /**
   * 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);
    }

  }
Comment 3 Jan Koehnlein CLA 2011-02-24 12:46:32 EST
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.
Comment 4 Karsten Thoms CLA 2017-09-19 18:04:26 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 5 Karsten Thoms CLA 2017-09-19 18:14:29 EDT
Closing all bugs that were set to RESOLVED before Neon.0