Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 329883

Summary: Add Occurrences Marking
Product: [Modeling] TMF Reporter: Christoph Zwicker <zwc>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: jan, sebastian.zarnekow
Version: unspecifiedFlags: sebastian.zarnekow: indigo+
Target Milestone: M6   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Java Source none

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