Esempio n. 1
0
  @Override
  protected void runInternal(
      ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory)
      throws BadLocationException, BadPartitioningException {

    if (!(docExtension instanceof IDocument)) return;

    List<Edit> edits = new LinkedList<Edit>();

    ITypedRegion firstPartition =
        docExtension.getPartition(ICPartitions.C_PARTITIONING, selection.getOffset(), false);
    ITypedRegion lastPartition =
        docExtension.getPartition(
            ICPartitions.C_PARTITIONING, selection.getOffset() + selection.getLength() - 1, false);

    int commentAreaStart = selection.getOffset();
    int commentAreaEnd = selection.getOffset() + selection.getLength();
    // Include special partitions fully in the comment area
    if (isSpecialPartition(firstPartition.getType())) {
      commentAreaStart = firstPartition.getOffset();
    }
    if (isSpecialPartition(lastPartition.getType())) {
      commentAreaEnd = lastPartition.getOffset() + lastPartition.getLength();
    }
    Region estimatedCommentArea = new Region(commentAreaStart, commentAreaEnd - commentAreaStart);

    Region commentArea =
        handleEnclosingPartitions(
            estimatedCommentArea, lastPartition, (IDocument) docExtension, factory, edits);

    handleInteriorPartition(commentArea, firstPartition, docExtension, factory, edits);

    executeEdits(edits);
  }
Esempio n. 2
0
  /**
   * Make all inside partitions join in one comment, in particular remove all enclosing comment
   * tokens of the inside partitions.
   *
   * @param commentArea comment area region
   * @param partition first partition
   * @param docExtension document
   * @param factory EditFactory
   * @param List of edits to update the document
   * @throws BadLocationException
   * @throws BadPartitioningException
   */
  private void handleInteriorPartition(
      IRegion commentArea,
      ITypedRegion partition,
      IDocumentExtension3 docExtension,
      Edit.EditFactory factory,
      List<Edit> edits)
      throws BadLocationException, BadPartitioningException {

    int commentAreaEnd = commentArea.getOffset() + commentArea.getLength();
    int prevPartitionEnd = -1;
    int partitionEnd = partition.getOffset() + partition.getLength();

    final int startCommentTokenLength = getCommentStart().length();
    final int endCommentTokenLength = getCommentEnd().length();

    while (partitionEnd <= commentAreaEnd) {
      if (partition.getType() == ICPartitions.C_MULTI_LINE_COMMENT
          || partition.getType() == ICPartitions.C_MULTI_LINE_DOC_COMMENT) {
        // already in a comment - remove start/end tokens
        edits.add(
            factory.createEdit(partition.getOffset(), startCommentTokenLength, "")); // $NON-NLS-1$
        edits.add(
            factory.createEdit(
                partitionEnd - endCommentTokenLength, endCommentTokenLength, "")); // $NON-NLS-1$
      }
      // advance to next partition
      prevPartitionEnd = partitionEnd;
      partition = docExtension.getPartition(ICPartitions.C_PARTITIONING, partitionEnd, false);
      partitionEnd = partition.getOffset() + partition.getLength();

      // break the loop if we get stuck and no advance was made
      if (partitionEnd <= prevPartitionEnd) break;
    }
  }
Esempio n. 3
0
 private boolean isUnclosedPair(VerifyEvent event, IDocument document, int offset)
     throws BadLocationException {
   final char closingCharacter = getPeerCharacter(event.character);
   // This doesn't matter if the user is not typing an "end" character.
   if (closingCharacter != event.character) return false;
   char c = event.character;
   int beginning = 0;
   // Don't check from very beginning of the document! Be smarter/quicker and check from beginning
   // of
   // partition if we can
   if (document instanceof IDocumentExtension3) {
     try {
       IDocumentExtension3 ext = (IDocumentExtension3) document;
       ITypedRegion region =
           ext.getPartition(IDocumentExtension3.DEFAULT_PARTITIONING, offset, false);
       beginning = region.getOffset();
     } catch (BadPartitioningException e) {
       // ignore
     }
   }
   // Now check leading source and see if we're an unclosed pair.
   String previous = document.get(beginning, offset - beginning);
   boolean open = false;
   int index = -1;
   while ((index = previous.indexOf(c, index + 1)) != -1) {
     if (fgCommentSelector.matches(getScopeAtOffset(document, beginning + index))) continue;
     open = !open;
     if (open) {
       c = closingCharacter;
     } else {
       c = event.character;
     }
   }
   return open;
 }
  /**
   * Handles partition boundaries within the selection. The end of the current partition and the
   * start of the next partition are examined for whether they contain comment tokens that interfere
   * with the created comment.
   *
   * <p>Comment tokens are removed from interior multi-line comments. Javadoc comments are left as
   * is; instead, multi-line comment tokens are inserted before and after Javadoc partitions to
   * ensure that the entire selected area is commented.
   *
   * <p>The next partition is returned.
   *
   * @param partition the current partition
   * @param edits the list of edits to add to
   * @param factory the edit factory
   * @param docExtension the document to get the partitions from
   * @return the next partition after the current
   * @throws BadLocationException if accessing the document fails - this can only happen if the
   *     document gets modified concurrently
   * @throws BadPartitioningException if the document does not have a Java partitioning
   */
  private ITypedRegion handleInteriorPartition(
      ITypedRegion partition,
      List edits,
      Edit.EditFactory factory,
      IDocumentExtension3 docExtension)
      throws BadPartitioningException, BadLocationException {

    // end of previous partition
    String partType = partition.getType();
    int partEndOffset = partition.getOffset() + partition.getLength();
    int tokenLength = getCommentStart().length();

    boolean wasJavadoc = false; // true if the previous partition is javadoc

    if (partType == IJavaScriptPartitions.JS_DOC) {

      wasJavadoc = true;

    } else if (partType == IJavaScriptPartitions.JS_COMMENT) {

      // already in a comment - remove ending mark
      edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); // $NON-NLS-1$
    }

    // advance to next partition
    partition =
        docExtension.getPartition(IJavaScriptPartitions.JS_PARTITIONING, partEndOffset, false);
    partType = partition.getType();

    // start of next partition
    if (wasJavadoc) {

      // if previous was javadoc, and the current one is not a comment,
      // then add a block comment start
      if (partType == IDocument.DEFAULT_CONTENT_TYPE || isSpecialPartition(partType)) {
        edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
      }

    } else { // !wasJavadoc

      if (partType == IJavaScriptPartitions.JS_DOC) {
        // if next is javadoc, end block comment before
        edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
      } else if (partType == IJavaScriptPartitions.JS_COMMENT) {
        // already in a comment - remove startToken
        edits.add(
            factory.createEdit(
                partition.getOffset(), getCommentStart().length(), "")); // $NON-NLS-1$
      }
    }

    return partition;
  }
  /*
   * @see
   * org.eclipse.jdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface
   * .text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3,
   * org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
   */
  @Override
  protected void runInternal(
      ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory)
      throws BadLocationException, BadPartitioningException {
    int selectionOffset = selection.getOffset();
    int selectionEndOffset = selectionOffset + selection.getLength();
    List edits = new LinkedList();
    ITypedRegion partition =
        docExtension.getPartition(IJavaScriptPartitions.JS_PARTITIONING, selectionOffset, false);

    handleFirstPartition(partition, edits, factory, selectionOffset);

    while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
      partition = handleInteriorPartition(partition, edits, factory, docExtension);
    }

    handleLastPartition(partition, edits, factory, selectionEndOffset);

    executeEdits(edits);
  }