Line indent provider: more cases fixed

This commit is contained in:
Rustam Vishnyakov
2016-06-03 14:55:34 +03:00
parent 1285749ae6
commit b795cf38e6
2 changed files with 33 additions and 6 deletions
@@ -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;
@@ -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();
}