Пример #1
0
 private static String getLineDelimiter(IDocument document, int line) throws BadLocationException {
   String newlineChar = document.getLineDelimiter(line);
   if (newlineChar == null && line > 0) {
     return document.getLineDelimiter(line - 1);
   } else {
     return getDefaultLineDelimiter(document);
   }
 }
Пример #2
0
  /**
   * Add enclosing comment tags for the whole area to be commented
   *
   * @param commentArea initial comment area which can be adjusted
   * @param lastPartition last partition
   * @param doc document
   * @param factory Edit factory
   * @param edits List of edits to update the document
   * @return new possibly adjusted comment area
   * @throws BadLocationException
   */
  private Region handleEnclosingPartitions(
      Region commentArea,
      ITypedRegion lastPartition,
      IDocument doc,
      Edit.EditFactory factory,
      List<Edit> edits)
      throws BadLocationException {

    int commentAreaStart = commentArea.getOffset();
    int commentAreaEnd = commentArea.getOffset() + commentArea.getLength();

    String commentStartTag = getCommentStart(); // "/*"
    String commentEndTag = getCommentEnd(); // "*/"

    String startLineEOL = doc.getLineDelimiter(doc.getLineOfOffset(commentAreaStart));
    if (startLineEOL == null) startLineEOL = ""; // $NON-NLS-1$
    String endLineEOL = doc.getLineDelimiter(doc.getLineOfOffset(commentAreaEnd - 1));
    if (endLineEOL == null) endLineEOL = ""; // $NON-NLS-1$

    boolean isLeftEol =
        commentAreaStart < startLineEOL.length()
            || doc.get(commentAreaStart - startLineEOL.length(), startLineEOL.length())
                .equals(startLineEOL);
    boolean isRightEol =
        doc.get(commentAreaEnd - endLineEOL.length(), endLineEOL.length()).equals(endLineEOL);

    if (isLeftEol && isRightEol) {
      // Block of full lines found
      int areaStartLine = doc.getLineOfOffset(commentAreaStart + startLineEOL.length());
      int areaEndLine = doc.getLineOfOffset(commentAreaEnd - endLineEOL.length());
      if (areaStartLine != areaEndLine) {
        // If multiple full lines arrange inserting comment tags on their own lines
        commentStartTag = getCommentStart() + startLineEOL;
        commentEndTag = getCommentEnd() + endLineEOL;
      } else {
        // If one full line insert end comment tag on the same line (before the EOL)
        commentAreaEnd = commentAreaEnd - endLineEOL.length();
      }
    } else {
      if (lastPartition.getType() == ICPartitions.C_SINGLE_LINE_COMMENT
          || lastPartition.getType() == ICPartitions.C_SINGLE_LINE_DOC_COMMENT) {
        // C++ comments "//" partition ends with EOL, insert end comment tag before it
        // on the same line, so we get something like /*// text*/
        commentAreaEnd = commentAreaEnd - endLineEOL.length();
      }
    }

    edits.add(factory.createEdit(commentAreaStart, 0, commentStartTag));
    edits.add(factory.createEdit(commentAreaEnd, 0, commentEndTag));

    return new Region(commentAreaStart, commentAreaEnd - commentAreaStart);
  }
    @Override
    public ICompletionProposal[] computeQuickAssistProposals(
        IQuickAssistInvocationContext invocationContext) {
      IAnnotationModel amodel = invocationContext.getSourceViewer().getAnnotationModel();
      IDocument doc = invocationContext.getSourceViewer().getDocument();

      int offset = invocationContext.getOffset();
      Iterator<?> it = amodel.getAnnotationIterator();
      TreeSet<ICompletionProposal> proposalSet =
          new TreeSet<>(
              new Comparator<Object>() {

                @Override
                public int compare(Object o1, Object o2) {
                  if (o1 instanceof ICompletionProposal && o2 instanceof ICompletionProposal) {
                    ICompletionProposal proposal1 = (ICompletionProposal) o1;
                    ICompletionProposal proposal2 = (ICompletionProposal) o2;
                    return proposal1
                        .getDisplayString()
                        .compareToIgnoreCase(proposal2.getDisplayString());
                  }
                  return 0;
                }
              });
      while (it.hasNext()) {
        Object key = it.next();
        if (!(key instanceof SimpleMarkerAnnotation)) {
          if (key instanceof SpellingAnnotation) {
            SpellingAnnotation annotation = (SpellingAnnotation) key;
            if (amodel.getPosition(annotation).overlapsWith(offset, 1)) {
              ICompletionProposal[] proposals = annotation.getSpellingProblem().getProposals();
              for (ICompletionProposal proposal : proposals) {
                proposalSet.add(proposal);
              }
            }
          }
          continue;
        }

        SimpleMarkerAnnotation annotation = (SimpleMarkerAnnotation) key;
        populateDataModelForAnnotation(annotation);
        IMarker marker = annotation.getMarker();

        IMarkerResolution[] mapping = fResMap.get(marker);
        if (mapping != null) {
          Position pos = amodel.getPosition(annotation);
          try {
            int line = doc.getLineOfOffset(pos.getOffset());
            int start = pos.getOffset();
            String delim = doc.getLineDelimiter(line);
            int delimLength = delim != null ? delim.length() : 0;
            int end = doc.getLineLength(line) + start - delimLength;
            if (offset >= start && offset <= end) {
              for (IMarkerResolution markerResolution : mapping) {
                PDECompletionProposal proposal =
                    new PDECompletionProposal(markerResolution, pos, marker);
                if (!proposalSet.contains(proposal)) {
                  proposalSet.add(proposal);
                }
              }
            }
          } catch (BadLocationException e) {
          }
        }
      }

      return proposalSet.toArray(new ICompletionProposal[proposalSet.size()]);
    }