示例#1
0
  /**
   * @param markOccurrencesRequest
   * @return true if the annotations were removed and added without any problems and false otherwise
   */
  @Override
  protected synchronized Map<Annotation, Position> getAnnotationsToAddAsMap(
      final BaseEditor baseEditor,
      IAnnotationModel annotationModel,
      MarkOccurrencesRequest markOccurrencesRequest,
      IProgressMonitor monitor)
      throws BadLocationException {
    PyEdit pyEdit = (PyEdit) baseEditor;
    PySourceViewer viewer = pyEdit.getPySourceViewer();
    if (viewer == null || monitor.isCanceled()) {
      return null;
    }
    if (viewer.getIsInToggleCompletionStyle() || monitor.isCanceled()) {
      return null;
    }

    PyMarkOccurrencesRequest pyMarkOccurrencesRequest =
        (PyMarkOccurrencesRequest) markOccurrencesRequest;
    Set<ASTEntry> occurrences = pyMarkOccurrencesRequest.getOccurrences();
    if (occurrences == null) {
      if (DEBUG) {
        System.out.println("Occurrences == null");
      }
      return null;
    }

    IDocument doc = pyEdit.getDocument();
    Map<Annotation, Position> toAddAsMap = new HashMap<Annotation, Position>();
    boolean markOccurrencesInStrings = MarkOccurrencesPreferencesPage.useMarkOccurrencesInStrings();

    // get the annotations to add
    for (ASTEntry entry : occurrences) {
      if (!markOccurrencesInStrings) {
        if (entry.node instanceof Name) {
          Name name = (Name) entry.node;
          if (name.ctx == Name.Artificial) {
            continue;
          }
        }
      }

      SimpleNode node = entry.getNameNode();
      IRegion lineInformation = doc.getLineInformation(node.beginLine - 1);

      try {
        Annotation annotation = new Annotation(getOccurrenceAnnotationsType(), false, "occurrence");
        Position position =
            new Position(
                lineInformation.getOffset() + node.beginColumn - 1,
                pyMarkOccurrencesRequest.getInitialName().length());
        toAddAsMap.put(annotation, position);

      } catch (Exception e) {
        Log.log(e);
      }
    }
    return toAddAsMap;
  }
 /** This is the apply that should actually be called! */
 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
   IDocument document = viewer.getDocument();
   if (viewer instanceof PySourceViewer) {
     PySourceViewer pySourceViewer = (PySourceViewer) viewer;
     PyEdit pyEdit = pySourceViewer.getEdit();
     this.indentString = pyEdit.getIndentPrefs().getIndentationString();
   } else {
     // happens on compare editor
     this.indentString = new DefaultIndentPrefs().getIndentationString();
   }
   // If the completion is applied with shift pressed, do a local import. Note that the user is
   // only actually
   // able to do that if the popup menu is focused (i.e.: request completion and do a tab to focus
   // it, instead
   // of having the focus on the editor and just pressing up/down).
   if ((stateMask & SWT.SHIFT) != 0) {
     this.setAddLocalImport(true);
   }
   apply(document, trigger, stateMask, offset);
 }