From b795cf38e6e6af526c451ed93ebdc534fdb193a1 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Fri, 3 Jun 2016 14:55:00 +0300 Subject: [PATCH] Line indent provider: more cases fixed --- .../codeStyle/SemanticEditorPosition.java | 19 ++++++++++++++++++ .../JavaLikeLangLineIndentProvider.java | 20 +++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/SemanticEditorPosition.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/SemanticEditorPosition.java index 64004c27ae6d..facf920da3e6 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/SemanticEditorPosition.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/SemanticEditorPosition.java @@ -15,6 +15,7 @@ */ package com.intellij.psi.impl.source.codeStyle; +import com.intellij.lang.Language; import com.intellij.openapi.editor.ex.EditorEx; import com.intellij.openapi.editor.highlighter.HighlighterIterator; import com.intellij.psi.tree.IElementType; @@ -28,10 +29,12 @@ import org.jetbrains.annotations.Nullable; public abstract class SemanticEditorPosition { public interface SyntaxElement {} + private final EditorEx myEditor; private final HighlighterIterator myIterator; private final CharSequence myChars; public SemanticEditorPosition(@NotNull EditorEx editor, int offset) { + myEditor = editor; myChars = editor.getDocument().getCharsSequence(); myIterator = editor.getHighlighter().createIterator(offset); } @@ -151,6 +154,22 @@ public abstract class SemanticEditorPosition { return -1; } + public EditorEx getEditor() { + return myEditor; + } + + @Nullable + public Language getLanguage() { + return !myIterator.atEnd() ? myIterator.getTokenType().getLanguage() : null; + } + + public boolean isAtLanguage(@Nullable Language language) { + if (language != null && !myIterator.atEnd()) { + return myIterator.getTokenType().getLanguage().is(language); + } + return false; + } + @Nullable public SyntaxElement getCurrElement() { return !myIterator.atEnd() ? map(myIterator.getTokenType()) : null; diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/lineIndent/JavaLikeLangLineIndentProvider.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/lineIndent/JavaLikeLangLineIndentProvider.java index 1b036f2a0ede..ac4a72b1c116 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/lineIndent/JavaLikeLangLineIndentProvider.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/lineIndent/JavaLikeLangLineIndentProvider.java @@ -90,7 +90,7 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI position -> { position.findLeftParenthesisBackwardsSkippingNested(BlockOpeningBrace, BlockClosingBrace); if (!position.isAtEnd()) { - return getBlockStartOffset(position); + return getBlockStatementStartOffset(position); } return -1; }); @@ -111,8 +111,7 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI else if (getPosition(editor, offset).matchesRule( position -> position.before().isAt(BlockOpeningBrace) )) { - return myFactory.createIndentCalculator(getIndentTypeInBlock(project, language), - JavaLikeLangLineIndentProvider::getBlockStartOffset); + return myFactory.createIndentCalculator(getIndentTypeInBlock(project, language), this::getBlockStatementStartOffset); } else if (getPosition(editor, offset).matchesRule( position -> position.before().isAt(Colon) && position.isAfterOnSameLine(SwitchCase, SwitchDefault) @@ -154,7 +153,8 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI } - private static int getBlockStartOffset(@NotNull SemanticEditorPosition position) { + private int getBlockStatementStartOffset(@NotNull SemanticEditorPosition position) { + Language currLanguage = position.getLanguage(); position.before().beforeOptional(BlockOpeningBrace); if (position.isAt(Whitespace)) { if (position.isAtMultiline()) return position.after().getStartOffset(); @@ -164,8 +164,16 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI position.beforeParentheses(LeftParenthesis, RightParenthesis); } while (!position.isAtEnd()) { - if (position.isAt(Whitespace) && position.isAtMultiline()) { - return position.after().getStartOffset(); + if (position.isAt(Semicolon) || + position.isAt(BlockOpeningBrace) || + position.isAt(BlockClosingBrace) || + !position.isAtLanguage(currLanguage)) { + SemanticEditorPosition statementStart = getPosition(position.getEditor(), position.getStartOffset()); + if (statementStart.after().isAt(Whitespace) && statementStart.isAtMultiline()) { + if (!statementStart.after().isAtEnd()) { + return statementStart.getStartOffset(); + } + } } position.before(); }