Ejemplo n.º 1
0
  /**
   * This method applies the code-formatting to the document in the PySelection
   *
   * @param pyEdit used to restore the selection
   * @param ps the selection used (contains the document that'll be changed)
   * @param regionsToFormat if null or empty, the whole document will be formatted, otherwise, only
   *     the passed ranges will be formatted.
   * @throws SyntaxErrorException
   */
  public void applyFormatAction(
      PyEdit pyEdit, PySelection ps, int[] regionsToFormat, boolean throwSyntaxError)
      throws BadLocationException, SyntaxErrorException {
    final IFormatter participant = getFormatter();
    final IDocument doc = ps.getDoc();
    final SelectionKeeper selectionKeeper = new SelectionKeeper(ps);

    DocumentRewriteSession session = null;
    try {

      if (regionsToFormat == null || regionsToFormat.length == 0) {
        if (doc instanceof IDocumentExtension4) {
          IDocumentExtension4 ext = (IDocumentExtension4) doc;
          session = ext.startRewriteSession(DocumentRewriteSessionType.STRICTLY_SEQUENTIAL);
        }
        participant.formatAll(doc, pyEdit, null, true, throwSyntaxError);

      } else {
        if (doc instanceof IDocumentExtension4) {
          IDocumentExtension4 ext = (IDocumentExtension4) doc;
          session = ext.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
        }
        participant.formatSelection(doc, regionsToFormat, pyEdit, ps);
      }

    } finally {
      if (session != null) {
        ((IDocumentExtension4) doc).stopRewriteSession(session);
      }
    }

    selectionKeeper.restoreSelection(pyEdit.getSelectionProvider(), doc);
  }
Ejemplo n.º 2
0
  /**
   * This method applies the code-formatting to the document in the PySelection
   *
   * @param pyEdit used to restore the selection
   * @param ps the selection used (contains the document that'll be changed)
   * @param forceFormatAll whether the full formatting (and not the selection formatting) should be
   *     applied.
   * @param throwSyntaxError
   * @throws SyntaxErrorException
   */
  public void applyFormatAction(
      PyEdit pyEdit, PySelection ps, boolean forceFormatAll, boolean throwSyntaxError)
      throws BadLocationException, SyntaxErrorException {
    final IFormatter participant = getFormatter();
    final IDocument doc = ps.getDoc();
    final ITextSelection selection = ps.getTextSelection();
    final int startLine = ps.getStartLineIndex();
    final SelectionKeeper selectionKeeper = new SelectionKeeper(ps);

    if (selection.getLength() == 0 || forceFormatAll) {
      participant.formatAll(doc, pyEdit, true, throwSyntaxError);
    } else {
      participant.formatSelection(doc, startLine, ps.getEndLineIndex(), pyEdit, ps);
    }

    selectionKeeper.restoreSelection(pyEdit.getSelectionProvider(), doc);
  }