Java Code Examples for com.intellij.openapi.editor.Document#getCharsSequence()

The following examples show how to use com.intellij.openapi.editor.Document#getCharsSequence() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventLog.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void removeJavaNewLines(Document document, List<RangeMarker> lineSeparators, String indent, boolean hasHtml) {
  CharSequence text = document.getCharsSequence();
  int i = 0;
  while (true) {
    i = StringUtil.indexOf(text, '\n', i);
    if (i < 0) break;
    int j = i + 1;
    if (StringUtil.startsWith(text, j, indent)) {
      j += indent.length();
    }
    document.deleteString(i, j);
    if (!hasHtml) {
      lineSeparators.add(document.createRangeMarker(TextRange.from(i, 0)));
    }
  }
}
 
Example 2
Source File: OutlineLocation.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the next word in the document starting at offset.
 * <p>
 * This helper is used to avoid displaying outline guides where it appears
 * that the word at the start of the outline (e.g. the Widget constructor
 * name) has changed since the guide was created. This catches edge cases
 * where RangeMarkers go off the rails and return strange values after
 * running a code formatter or other tool that generates widespread edits.
 */
public static String getCurrentWord(Document document, int offset) {
  final int documentLength = document.getTextLength();
  offset = Math.max(0, offset);
  if (offset < 0 || offset >= documentLength) return "";
  final CharSequence chars = document.getCharsSequence();
  // Clamp the max current word length at 20 to avoid slow behavior if the
  // next "word" in the document happened to be incredibly long.
  final int maxWordEnd = Math.min(documentLength, offset + 20);

  int end = offset;
  while (end < maxWordEnd && Character.isAlphabetic(chars.charAt(end))) {
    end++;
  }
  if (offset == end) return "";
  return chars.subSequence(offset, end).toString();
}
 
Example 3
Source File: PsiFileBreadcrumbsCollector.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Finds first breadcrumb-rendering element, possibly shifting offset backwards, skipping whitespaces and grabbing previous element
 * This logic solves inconsistency with brace matcher. For example,
 * <pre><code>
 *   class Foo {
 *     public void bar() {
 *
 *     } &lt;caret&gt;
 *   }
 * </code></pre>
 * will highlight bar's braces, looking backwards. So it should include it to breadcrumbs, too.
 */
@Nullable
private static PsiElement findStartElement(Document document, int offset, VirtualFile file, Project project, BreadcrumbsProvider defaultInfoProvider, boolean checkSettings) {
  PsiElement middleElement = findFirstBreadcrumbedElement(offset, file, project, defaultInfoProvider, checkSettings);

  // Let's simulate brace matcher logic of searching brace backwards (see `BraceHighlightingHandler.updateBraces`)
  CharSequence chars = document.getCharsSequence();
  int leftOffset = CharArrayUtil.shiftBackward(chars, offset - 1, "\t ");
  leftOffset = leftOffset >= 0 ? leftOffset : offset - 1;

  PsiElement leftElement = findFirstBreadcrumbedElement(leftOffset, file, project, defaultInfoProvider, checkSettings);
  if (leftElement != null && (middleElement == null || PsiTreeUtil.isAncestor(middleElement, leftElement, true))) {
    return leftElement;
  }
  else {
    return middleElement;
  }
}
 
Example 4
Source File: DeleteToWordEndAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static int getWordEndOffset(Editor editor, int offset, boolean camelMode) {
  Document document = editor.getDocument();
  CharSequence text = document.getCharsSequence();
  if(offset >= document.getTextLength() - 1)
    return offset;
  int newOffset = offset + 1;
  int lineNumber = editor.getCaretModel().getLogicalPosition().line;
  int maxOffset = document.getLineEndOffset(lineNumber);
  if(newOffset > maxOffset) {
    if(lineNumber+1 >= document.getLineCount())
      return offset;
    maxOffset = document.getLineEndOffset(lineNumber+1);
  }
  for (; newOffset < maxOffset; newOffset++) {
    if (EditorActionUtil.isWordEnd(text, newOffset, camelMode) ||
        EditorActionUtil.isWordStart(text, newOffset, camelMode)) {
      break;
    }
  }
  return newOffset;
}
 
Example 5
Source File: CommentUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static IndentData getMinLineIndent(Document document, int line1, int line2, @Nonnull PsiFile file) {
  CharSequence chars = document.getCharsSequence();
  IndentData minIndent = null;
  for (int line = line1; line <= line2; line++) {
    int lineStart = document.getLineStartOffset(line);
    int textStart = CharArrayUtil.shiftForward(chars, lineStart, " \t");
    if (textStart >= document.getTextLength()) {
      textStart = document.getTextLength();
    }
    else {
      char c = chars.charAt(textStart);
      if (c == '\n' || c == '\r') continue; // empty line
    }
    IndentData indent = IndentData.createFrom(chars, lineStart, textStart, CodeStyle.getIndentOptions(file).TAB_SIZE);
    minIndent = IndentData.min(minIndent, indent);
  }
  if (minIndent == null && line1 == line2 && line1 < document.getLineCount() - 1) {
    return getMinLineIndent(document, line1 + 1, line1 + 1, file);
  }
  return minIndent;
}
 
Example 6
Source File: CodeDocumentationUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method that does the following:
 * <pre>
 * <ol>
 *   <li>Checks if target document line that contains given offset starts with '*';</li>
 *   <li>Returns document text located between the '*' and first non-white space symbols after it if the check above is successful;</li>
 * </ol>
 * </pre>
 *
 * @param document target document
 * @param offset   target offset that identifies line to check and max offset to use during scanning
 * @return
 */
@Nullable
public static String getIndentInsideJavadoc(@Nonnull Document document, int offset) {
  CharSequence text = document.getCharsSequence();
  if (offset >= text.length()) {
    return null;
  }
  int line = document.getLineNumber(offset);
  int lineStartOffset = document.getLineStartOffset(line);
  int lineEndOffset = document.getLineEndOffset(line);
  int i = CharArrayUtil.shiftForward(text, lineStartOffset, " \t");
  if (i > lineEndOffset || text.charAt(i) != '*') {
    return null;
  }

  int start = i + 1;
  int end = CharArrayUtil.shiftForward(text, start, " \t");
  end = Math.min(end, lineEndOffset);
  return end > start ? text.subSequence(start, end).toString() : "";
}
 
Example 7
Source File: SimpleTokenSetQuoteHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasNonClosedLiteral(Editor editor, HighlighterIterator iterator, int offset) {
  int start = iterator.getStart();
  try {
    Document doc = editor.getDocument();
    CharSequence chars = doc.getCharsSequence();
    int lineEnd = doc.getLineEndOffset(doc.getLineNumber(offset));

    while (!iterator.atEnd() && iterator.getStart() < lineEnd) {
      IElementType tokenType = iterator.getTokenType();

      if (myLiteralTokenSet.contains(tokenType)) {
        if (isNonClosedLiteral(iterator, chars)) return true;
      }
      iterator.advance();
    }
  }
  finally {
    while(iterator.atEnd() || iterator.getStart() != start) iterator.retreat();
  }

  return false;
}
 
Example 8
Source File: TailType.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public int processTail(final Editor editor, int tailOffset) {
  Document document = editor.getDocument();
  int textLength = document.getTextLength();
  CharSequence chars = document.getCharsSequence();
  if (tailOffset < textLength - 1 && chars.charAt(tailOffset) == ' ' && chars.charAt(tailOffset + 1) == '='){
    return moveCaret(editor, tailOffset, 2);
  }
  if (tailOffset < textLength && chars.charAt(tailOffset) == '='){
    return moveCaret(editor, tailOffset, 1);
  }
  if (isSpaceAroundAssignmentOperators(editor, tailOffset)) {
    document.insertString(tailOffset, " =");
    tailOffset = moveCaret(editor, tailOffset, 2);
    tailOffset = insertChar(editor, tailOffset, ' ');
  }
  else{
    document.insertString(tailOffset, "=");
    tailOffset = moveCaret(editor, tailOffset, 1);
  }
  return tailOffset;
}
 
Example 9
Source File: SmartEnterProcessor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
protected PsiElement getStatementAtCaret(Editor editor, PsiFile psiFile) {
  int caret = editor.getCaretModel().getOffset();

  final Document doc = editor.getDocument();
  CharSequence chars = doc.getCharsSequence();
  int offset = caret == 0 ? 0 : CharArrayUtil.shiftBackward(chars, caret - 1, " \t");
  if (doc.getLineNumber(offset) < doc.getLineNumber(caret)) {
    offset = CharArrayUtil.shiftForward(chars, caret, " \t");
  }

  return psiFile.findElementAt(offset);
}
 
Example 10
Source File: ORTypedHandler.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public Result beforeCharTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, @NotNull FileType fileType) {
    if (!(fileType instanceof OclFileType || fileType instanceof OclInterfaceFileType)) {
        return Result.CONTINUE;
    }

    // #62 - don't insert a ) when at the end of a comment
    if (c == ')') {
        Document doc = editor.getDocument();
        PsiDocumentManager.getInstance(project).commitDocument(doc);
        CaretModel caretModel = editor.getCaretModel();


        // * <caret> )
        int offsetBefore = caretModel.getOffset();
        if (offsetBefore < doc.getTextLength()) {
            CharSequence charsSequence = doc.getCharsSequence();
            char c1 = charsSequence.charAt(offsetBefore - 1);
            char c2 = charsSequence.charAt(offsetBefore);
            if (c1 == '*' && c2 == ')') {
                PsiElement leaf = file.findElementAt(offsetBefore);
                if (leaf instanceof PsiComment) {
                    caretModel.moveToOffset(offsetBefore + 1);
                    return Result.STOP;
                }
            }
        }
    }

    return super.beforeCharTyped(c, project, editor, file, fileType);
}
 
Example 11
Source File: TargetElementUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int adjustOffset(@Nullable PsiFile file, Document document, final int offset) {
  CharSequence text = document.getCharsSequence();
  int correctedOffset = offset;
  int textLength = document.getTextLength();
  if (offset >= textLength) {
    correctedOffset = textLength - 1;
  }
  else if (!isIdentifierPart(file, text, offset)) {
    correctedOffset--;
  }
  if (correctedOffset < 0 || !isIdentifierPart(file, text, correctedOffset)) return offset;
  return correctedOffset;
}
 
Example 12
Source File: KillToWordEndAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredWriteAction
@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
  CaretModel caretModel = editor.getCaretModel();
  int caretOffset = caretModel.getOffset();
  Document document = editor.getDocument();
  if (caretOffset >= document.getTextLength()) {
    return;
  }

  int caretLine = caretModel.getLogicalPosition().line;
  int lineEndOffset = document.getLineEndOffset(caretLine);
  CharSequence text = document.getCharsSequence();
  boolean camel = editor.getSettings().isCamelWords();
  for (int i = caretOffset + 1; i < lineEndOffset; i++) {
    if (EditorActionUtil.isWordEnd(text, i, camel)) {
      KillRingUtil.cut(editor, caretOffset, i);
      return;
    }
  }
  
  int end = lineEndOffset;
  if (caretLine < document.getLineCount() - 1) {
    // No word end found between the current position and line end, hence, remove line feed sign if possible.
    end++;
  }

  if (end > caretOffset) {
    KillRingUtil.cut(editor, caretOffset, end);
  }
}
 
Example 13
Source File: TemplateManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public Map<TemplateImpl, String> findMatchingTemplates(final PsiFile file, Editor editor, @Nullable Character shortcutChar, TemplateSettings templateSettings) {
  final Document document = editor.getDocument();
  CharSequence text = document.getCharsSequence();
  final int caretOffset = editor.getCaretModel().getOffset();

  List<TemplateImpl> candidatesWithoutArgument = findMatchingTemplates(text, caretOffset, shortcutChar, templateSettings, false);

  int argumentOffset = passArgumentBack(text, caretOffset);
  String argument = null;
  if (argumentOffset >= 0) {
    argument = text.subSequence(argumentOffset, caretOffset).toString();
    if (argumentOffset > 0 && text.charAt(argumentOffset - 1) == ' ') {
      if (argumentOffset - 2 >= 0 && Character.isJavaIdentifierPart(text.charAt(argumentOffset - 2))) {
        argumentOffset--;
      }
    }
  }
  List<TemplateImpl> candidatesWithArgument = findMatchingTemplates(text, argumentOffset, shortcutChar, templateSettings, true);

  if (candidatesWithArgument.isEmpty() && candidatesWithoutArgument.isEmpty()) {
    return null;
  }

  candidatesWithoutArgument = filterApplicableCandidates(file, caretOffset, candidatesWithoutArgument);
  candidatesWithArgument = filterApplicableCandidates(file, argumentOffset, candidatesWithArgument);
  Map<TemplateImpl, String> candidate2Argument = new HashMap<>();
  addToMap(candidate2Argument, candidatesWithoutArgument, null);
  addToMap(candidate2Argument, candidatesWithArgument, argument);
  return candidate2Argument;
}
 
Example 14
Source File: BuildEnterHandler.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void removeTrailingWhitespace(Document doc, PsiFile file, int offset) {
  CharSequence chars = doc.getCharsSequence();
  int start = offset;
  while (offset < chars.length() && chars.charAt(offset) == ' ') {
    PsiElement element = file.findElementAt(offset);
    if (element == null || !(element instanceof PsiWhiteSpace)) {
      break;
    }
    offset++;
  }
  if (start != offset) {
    doc.deleteString(start, offset);
  }
}
 
Example 15
Source File: BuildQuoteHandler.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isClosingQuote(HighlighterIterator iterator, int offset) {
  final IElementType tokenType = iterator.getTokenType();
  if (!myLiteralTokenSet.contains(tokenType)) {
    return false;
  }
  int start = iterator.getStart();
  int end = iterator.getEnd();
  if (end - start >= 1 && offset == end - 1) {
    return true; // single quote
  }
  if (end - start < 3 || offset < end - 3) {
    return false;
  }
  // check for triple quote
  Document doc = iterator.getDocument();
  if (doc == null) {
    return false;
  }
  CharSequence chars = doc.getCharsSequence();
  char quote = chars.charAt(start);
  boolean tripleQuote = quote == chars.charAt(start + 1) && quote == chars.charAt(start + 2);
  if (!tripleQuote) {
    return false;
  }
  for (int i = offset; i < Math.min(offset + 2, end); i++) {
    if (quote != chars.charAt(i)) {
      return false;
    }
  }
  return true;
}
 
Example 16
Source File: PostprocessReformattingAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void adjustIndentationInRange(@Nonnull PsiFile file, @Nonnull Document document, @Nonnull TextRange[] indents, final int indentAdjustment) {
  final CharSequence charsSequence = document.getCharsSequence();
  for (final TextRange indent : indents) {
    final String oldIndentStr = charsSequence.subSequence(indent.getStartOffset() + 1, indent.getEndOffset()).toString();
    final int oldIndent = IndentHelperImpl.getIndent(file, oldIndentStr, true);
    final String newIndentStr = IndentHelperImpl.fillIndent(CodeStyle.getIndentOptions(file), Math.max(oldIndent + indentAdjustment, 0));
    document.replaceString(indent.getStartOffset() + 1, indent.getEndOffset(), newIndentStr);
  }
}
 
Example 17
Source File: BaseIndentEnterHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected String getNewIndent(
        @Nonnull final PsiFile file,
        @Nonnull final Document document,
        @Nonnull final CharSequence oldIndent)
{
  CharSequence nonEmptyIndent = oldIndent;
  final CharSequence editorCharSequence = document.getCharsSequence();
  final int nLines = document.getLineCount();
  for (int line = 0; line < nLines && nonEmptyIndent.length() == 0; ++line) {
    final int lineStart = document.getLineStartOffset(line);
    final int indentEnd = EditorActionUtil.findFirstNonSpaceOffsetOnTheLine(document, line);
    if (lineStart < indentEnd) {
      nonEmptyIndent = editorCharSequence.subSequence(lineStart, indentEnd);
    }
  }

  final boolean usesSpacesForIndentation = nonEmptyIndent.length() > 0 && nonEmptyIndent.charAt(nonEmptyIndent.length() - 1) == ' ';
  final boolean firstIndent = nonEmptyIndent.length() == 0;

  final CodeStyleSettings currentSettings = CodeStyleSettingsManager.getSettings(file.getProject());
  final CommonCodeStyleSettings.IndentOptions indentOptions = currentSettings.getIndentOptions(file.getFileType());
  if (firstIndent && indentOptions.USE_TAB_CHARACTER || !firstIndent && !usesSpacesForIndentation) {
    int nTabsToIndent = indentOptions.INDENT_SIZE / indentOptions.TAB_SIZE;
    if (indentOptions.INDENT_SIZE % indentOptions.TAB_SIZE != 0) {
      ++nTabsToIndent;
    }
    return oldIndent + StringUtil.repeatSymbol('\t', nTabsToIndent);
  }
  return oldIndent + StringUtil.repeatSymbol(' ', indentOptions.INDENT_SIZE);
}
 
Example 18
Source File: ArrangementUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to build a text range on the given arguments basis. Expands to the line start/end if possible.
 * <p/>
 * This method is expected to be used in a situation when we want to arrange complete rows.
 * Example:
 * <pre>
 *   class Test {
 *        void test() {
 *        }
 *      int i;
 *   }
 * </pre>
 * Suppose, we want to locate fields before methods. We can move the exact field and method range then but indent will be broken,
 * i.e. we'll get the result below:
 * <pre>
 *   class Test {
 *        int i;
 *      void test() {
 *        }
 *   }
 * </pre>
 * We can expand field and method range to the whole lines and that would allow to achieve the desired result:
 * <pre>
 *   class Test {
 *      int i;
 *        void test() {
 *        }
 *   }
 * </pre>
 * However, this method is expected to just return given range if there are multiple distinct elements at the same line:
 * <pre>
 *   class Test {
 *     void test1(){} void test2() {} int i;
 *   }
 * </pre>
 *
 * @param initialRange  anchor range
 * @param document      target document against which the ranges are built
 * @return              expanded range if possible; <code>null</code> otherwise
 */
@Nonnull
public static TextRange expandToLineIfPossible(@Nonnull TextRange initialRange, @Nonnull Document document) {
  CharSequence text = document.getCharsSequence();
  String ws = " \t";

  int startLine = document.getLineNumber(initialRange.getStartOffset());
  int lineStartOffset = document.getLineStartOffset(startLine);
  int i = CharArrayUtil.shiftBackward(text, lineStartOffset + 1, initialRange.getStartOffset() - 1, ws);
  if (i != lineStartOffset) {
    return initialRange;
  }

  int endLine = document.getLineNumber(initialRange.getEndOffset());
  int lineEndOffset = document.getLineEndOffset(endLine);
  i = CharArrayUtil.shiftForward(text, initialRange.getEndOffset(), lineEndOffset, ws);

  return i == lineEndOffset ? TextRange.create(lineStartOffset, lineEndOffset) : initialRange;
}
 
Example 19
Source File: EnterAfterUnmatchedBraceHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Formats the code block between caret and inserted braces.
 *
 * @param file                target PSI file
 * @param document            target document
 * @param caretOffset         target caret offset
 * @param rBracesInsertOffset target position to insert
 * @param generatedRBraces    string of '}' to insert
 */
protected void formatCodeFragmentBetweenBraces(@Nonnull PsiFile file,
                                               @Nonnull Document document,
                                               int caretOffset,
                                               int rBracesInsertOffset,
                                               String generatedRBraces) {
  Project project = file.getProject();
  long stamp = document.getModificationStamp();
  boolean closingBraceIndentAdjusted;
  try {
    PsiDocumentManager.getInstance(project).commitDocument(document);
    CodeStyleManager.getInstance(project).adjustLineIndent(file, new TextRange(caretOffset, rBracesInsertOffset + 2));
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
  finally {
    closingBraceIndentAdjusted = stamp != document.getModificationStamp();
    // do you remember that we insert the '\n'? here we take it back!
    document.deleteString(caretOffset, caretOffset + 1);
  }

  // There is a possible case that formatter was unable to adjust line indent for the closing brace (that is the case for plain text
  // document for example). Hence, we're trying to do the manually.
  if (!closingBraceIndentAdjusted) {
    int line = document.getLineNumber(rBracesInsertOffset);
    StringBuilder buffer = new StringBuilder();
    int start = document.getLineStartOffset(line);
    int end = document.getLineEndOffset(line);
    final CharSequence text = document.getCharsSequence();
    for (int i = start; i < end; i++) {
      char c = text.charAt(i);
      if (c != ' ' && c != '\t') {
        break;
      }
      else {
        buffer.append(c);
      }
    }
    if (buffer.length() > 0) {
      document.insertString(rBracesInsertOffset + 1, buffer);
    }
  }
}
 
Example 20
Source File: BaseIndentEnterHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public Result preprocessEnter(
        @Nonnull final PsiFile file,
        @Nonnull final Editor editor,
        @Nonnull final Ref<Integer> caretOffset,
        @Nonnull final Ref<Integer> caretAdvance,
        @Nonnull final DataContext dataContext,
        final EditorActionHandler originalHandler)
{
  Result res = shouldSkipWithResult(file, editor, dataContext);
  if (res != null) {
    return res;
  }

  final Document document = editor.getDocument();
  int caret = editor.getCaretModel().getOffset();
  final int lineNumber = document.getLineNumber(caret);

  final int lineStartOffset = document.getLineStartOffset(lineNumber);
  final int previousLineStartOffset = lineNumber > 0 ? document.getLineStartOffset(lineNumber - 1) : lineStartOffset;
  final EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
  final HighlighterIterator iterator = highlighter.createIterator(caret - 1);
  final IElementType type = getNonWhitespaceElementType(iterator, lineStartOffset, previousLineStartOffset);

  final CharSequence editorCharSequence = document.getCharsSequence();
  final CharSequence lineIndent =
          editorCharSequence.subSequence(lineStartOffset, EditorActionUtil.findFirstNonSpaceOffsetOnTheLine(document, lineNumber));

  // Enter in line comment
  if (type == myLineCommentType) {
    final String restString = editorCharSequence.subSequence(caret, document.getLineEndOffset(lineNumber)).toString();
    if (!StringUtil.isEmptyOrSpaces(restString)) {
      final String linePrefix = lineIndent + myLineCommentPrefix;
      EditorModificationUtil.insertStringAtCaret(editor, "\n" + linePrefix);
      editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, linePrefix.length()));
      return Result.Stop;
    }
    else if (iterator.getStart() < lineStartOffset) {
      EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
      return Result.Stop;
    }
  }

  if (!myWorksWithFormatter && LanguageFormatting.INSTANCE.forLanguage(myLanguage) != null) {
    return Result.Continue;
  }
  else {
    if (myIndentTokens.contains(type)) {
      final String newIndent = getNewIndent(file, document, lineIndent);
      EditorModificationUtil.insertStringAtCaret(editor, "\n" + newIndent);
      return Result.Stop;
    }

    EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
    editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, calcLogicalLength(editor, lineIndent)));
    return Result.Stop;
  }
}