Example #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;
  }
Example #2
0
  /**
   * @return a tuple with the refactoring request, the processor and a boolean indicating if all
   *     pre-conditions succedded.
   * @throws MisconfigurationException
   */
  @Override
  protected MarkOccurrencesRequest createRequest(
      BaseEditor baseEditor, IDocumentProvider documentProvider, IProgressMonitor monitor)
      throws BadLocationException, OperationCanceledException, CoreException,
          MisconfigurationException {
    if (!MarkOccurrencesPreferencesPage.useMarkOccurrences()) {
      return new PyMarkOccurrencesRequest(false, null, null);
    }
    PyEdit pyEdit = (PyEdit) baseEditor;

    // ok, the editor is still there wit ha document... move on
    PyRefactorAction pyRefactorAction = getRefactorAction(pyEdit);

    final RefactoringRequest req =
        getRefactoringRequest(pyEdit, pyRefactorAction, PySelection.fromTextSelection(this.ps));

    if (req == null
        || !req.nature
            .getRelatedInterpreterManager()
            .isConfigured()) { // we check if it's configured because it may still be a stub...
      return new PyMarkOccurrencesRequest(false, null, null);
    }

    PyReferenceSearcher searcher = new PyReferenceSearcher(req);
    // to see if a new request was not created in the meantime (in which case this one will be
    // cancelled)
    if (monitor.isCanceled()) {
      return new PyMarkOccurrencesRequest(false, null, null);
    }

    try {
      searcher.prepareSearch(req);
      if (monitor.isCanceled()) {
        return new PyMarkOccurrencesRequest(false, null, null);
      }
      searcher.search(req);
      if (monitor.isCanceled()) {
        return new PyMarkOccurrencesRequest(false, null, null);
      }
      // Ok, search succeeded.
      return new PyMarkOccurrencesRequest(true, req, searcher);
    } catch (PyReferenceSearcher.SearchException e) {
      // Suppress search failures.
      return new PyMarkOccurrencesRequest(false, null, null);
    } catch (Throwable e) {
      throw new RuntimeException(
          "Error in occurrences while analyzing modName:"
              + req.moduleName
              + " initialName:"
              + req.initialName
              + " line (start at 0):"
              + req.ps.getCursorLine(),
          e);
    }
  }