/** Requests proposals in the last location of the given editor. */
 protected ICompletionProposal[] requestProposals(String mod1Contents, PyEdit editor) {
   IContentAssistant contentAssistant =
       editor.getEditConfiguration().getContentAssistant(editor.getPySourceViewer());
   SimpleAssistProcessor processor =
       (SimpleAssistProcessor)
           contentAssistant.getContentAssistProcessor(IDocument.DEFAULT_CONTENT_TYPE);
   processor
       .doCycle(); // we want to show the default completions in this case (not the simple ones)
   ICompletionProposal[] props =
       processor.computeCompletionProposals(editor.getPySourceViewer(), mod1Contents.length());
   return props;
 }
示例#2
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;
  }
  private void findAndDeleteUnusedImports(
      PySelection ps, PyEdit edit, IDocumentExtension4 doc, IFile f) throws Exception {
    Iterator<MarkerAnnotationAndPosition> it;
    if (edit != null) {
      it = edit.getPySourceViewer().getMarkerIterator();
    } else {

      IMarker markers[] =
          f.findMarkers(IMiscConstants.PYDEV_ANALYSIS_PROBLEM_MARKER, true, IResource.DEPTH_ZERO);
      MarkerAnnotationAndPosition maap[] = new MarkerAnnotationAndPosition[markers.length];
      int ix = 0;
      for (IMarker m : markers) {
        int start = (Integer) m.getAttribute(IMarker.CHAR_START);
        int end = (Integer) m.getAttribute(IMarker.CHAR_END);
        maap[ix++] =
            new MarkerAnnotationAndPosition(
                new MarkerAnnotation(m), new Position(start, end - start));
      }
      it = Arrays.asList(maap).iterator();
    }
    ArrayList<MarkerAnnotationAndPosition> unusedImportsMarkers = getUnusedImports(it);
    sortInReverseDocumentOrder(unusedImportsMarkers);
    deleteImports(doc, ps, unusedImportsMarkers);
  }