コード例 #1
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;
 }
コード例 #2
0
ファイル: AddBlockCommentAction.java プロジェクト: zhaog/cdt
  @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);
  }
コード例 #3
0
 /*
  * @see org.eclipse.core.filebuffers.IDocumentSetupParticipant#setup(org.eclipse.jface.text.IDocument)
  */
 public void setup(IDocument document) {
   if (document instanceof IDocumentExtension3) {
     IDocumentExtension3 extension3 = (IDocumentExtension3) document;
     IDocumentPartitioner partitioner =
         new FastPartitioner(
             JavaEditorExamplePlugin.getDefault().getJavaPartitionScanner(),
             JavaPartitionScanner.JAVA_PARTITION_TYPES);
     extension3.setDocumentPartitioner(JavaEditorExamplePlugin.JAVA_PARTITIONING, partitioner);
     partitioner.connect(document);
   }
 }
コード例 #4
0
 public void setup(IDocument document) {
   if (document instanceof IDocumentExtension3) {
     IDocumentExtension3 extension3 = (IDocumentExtension3) document;
     IDocumentPartitioner partitioner =
         new FastPartitioner(
             Activator.getDefault().getResourceFilePartitionScanner(),
             ResourceFilePartitionScanner.PARTITION_TYPES);
     extension3.setDocumentPartitioner(ResourceFilePartitionScanner.PARTITIONING, partitioner);
     ((IDocumentPartitionerExtension3) partitioner).connect(document, false);
   }
 }
コード例 #5
0
  public void setup(IDocument document) {
    IDocumentPartitioner partitioner =
        new FastPartitioner(
            Activator.getDefault().getAdaPartitionScanner(), AdaPartitionScanner.PARTITION_TYPES);

    if (document instanceof IDocumentExtension3) {
      IDocumentExtension3 extension3 = (IDocumentExtension3) document;
      extension3.setDocumentPartitioner(AdaPartitionScanner.ADA_PARTITIONNING, partitioner);
    }

    partitioner.connect(document);
  }
コード例 #6
0
 protected void setupDocumentPartitioner(IDocument document, String partitioning) {
   IDocumentPartitioner partitioner = createDocumentPartitioner();
   if (partitioner != null) {
     partitioner.connect(document);
     if (document instanceof IDocumentExtension3) {
       IDocumentExtension3 extension3 = (IDocumentExtension3) document;
       extension3.setDocumentPartitioner(partitioning, partitioner);
     } else {
       document.setDocumentPartitioner(partitioner);
     }
   }
 }
 public void setup(IDocument document) {
   AutoconfPartitioner partitioner =
       new AutoconfPartitioner(
           new AutoconfPartitionScanner(), AutoconfPartitionScanner.AUTOCONF_PARTITION_TYPES);
   partitioner.connect(document, 1);
   if (document instanceof IDocumentExtension3) {
     IDocumentExtension3 extension3 = (IDocumentExtension3) document;
     extension3.setDocumentPartitioner(AutoconfEditor.AUTOCONF_PARTITIONING, partitioner);
   } else {
     document.setDocumentPartitioner(partitioner);
   }
   //		document.addDocumentListener(this);
 }
コード例 #8
0
ファイル: AddBlockCommentAction.java プロジェクト: zhaog/cdt
  /**
   * 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;
    }
  }
コード例 #9
0
 @Override
 protected void connectPartitioningToElement(IEditorInput input, IDocument document) {
   if (document instanceof IDocumentExtension3) {
     IDocumentExtension3 extension = (IDocumentExtension3) document;
     if (extension.getDocumentPartitioner(IXQueryPartitions.XQUERY_PARTITIONING) == null) {
       XQueryDocumentSetupParticipant participant = new XQueryDocumentSetupParticipant();
       participant.setup(document);
     }
   }
   if (!(input instanceof IFileEditorInput)) {
     // disable folding
     IPreferenceStore store = XQueryUI.getDefault().getPreferenceStore();
     store.setDefault(PreferenceConstants.EDITOR_FOLDING_ENABLED, false);
     store.setDefault(PreferenceConstants.EDITOR_COMMENTS_FOLDING_ENABLED, false);
   }
 }
コード例 #10
0
  /**
   * 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;
  }
コード例 #11
0
  /**
   * Adds two spaces to the indentation of the previous line.
   *
   * @param d the document to work on
   * @param c the command to deal with
   */
  private void autoIndentAfterNewLine(IDocument d, DocumentCommand c) {

    if (c.offset == -1 || d.getLength() == 0) return;

    try {
      String lastPartitionType = null;
      if (d instanceof IDocumentExtension3) {
        IDocumentExtension3 id3 = (IDocumentExtension3) d;
        lastPartitionType =
            id3.getDocumentPartitioner(SassEditor.SASS_PARTITIONING).getContentType(c.offset - 1);
      }

      // find start of line
      int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
      IRegion info = d.getLineInformationOfOffset(p);
      int start = info.getOffset();

      // find white spaces
      int end = findEndOfWhiteSpace(d, start, c.offset);

      // indent if we just finished an element
      StringBuffer buf = new StringBuffer(c.text);
      if (lastPartitionType != null
          && (lastPartitionType.equalsIgnoreCase(SassPartitionScanner.SASS_CLASS)
              || lastPartitionType.equalsIgnoreCase(SassPartitionScanner.SASS_ID))) {
        buf.append("  ");
      }

      if (end > start) {
        // append to input
        buf.append(d.get(start, end - start));
      }

      c.text = buf.toString();

    } catch (BadLocationException excp) {
      // stop work
    }
  }
コード例 #12
0
  /**
   * Installs a document partitioner on the containing editor's document. This partitioner will
   * determine where contained editors should go.
   *
   * @param viewer
   */
  public void installPartitioner(ISourceViewer viewer) {
    FastPartitioner partitioner =
        new FastPartitioner(scanner, new String[] {ContainingEditorScanner.CONTAINED_EDITOR});
    partitioner.connect(doc);

    ((IDocumentExtension3) doc)
        .setDocumentPartitioner(ContainingEditorScanner.CONTAINED_EDITOR, partitioner);

    if (viewer instanceof ITextViewerExtension4) {
      ((ITextViewerExtension4) viewer).addTextPresentationListener(this);
    } else {
      EmbeddedCALPlugin.logWarning(
          "Cannot install Presentation Listener.  " + "The display may not update properly.",
          new RuntimeException());
    }
  }
コード例 #13
0
  /*
   * @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);
  }