From 725eebc3d6c4d63bbca5e985a619858633cb6f63 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Tue, 23 Aug 2016 13:55:36 +0300 Subject: [PATCH] Quick indent after semicolon ';' --- .../JavaLikeLangLineIndentProvider.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) 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 923696368c7d..f5fd3f492dc0 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 @@ -56,7 +56,8 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI DocBlockStart, DocBlockEnd, LineComment, - Comma + Comma, + LanguageStartDelimiter } @@ -104,6 +105,14 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI .isAt(BlockClosingBrace))) { return myFactory.createIndentCalculator(getBlockIndentType(project, language), IndentCalculator.LINE_BEFORE); } + else if (getPosition(editor, offset).matchesRule(position -> position.before().isAt(Semicolon))) { + SemanticEditorPosition beforeSemicolon = getPosition(editor, offset).before().beforeOptional(Semicolon); + int statementStart = getStatementStartOffset(beforeSemicolon); + SemanticEditorPosition atStatementStart = getPosition(editor, statementStart); + if (!atStatementStart.isAfterOnSameLine(ForKeyword)) { + return myFactory.createIndentCalculator(NONE, position -> statementStart); + } + } else if (getPosition(editor, offset).matchesRule( position -> position.before().isAt(ArrayOpeningBracket) )) { @@ -161,15 +170,16 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI 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(); position.before(); } - if (position.isAt(RightParenthesis)) { - position.beforeParentheses(LeftParenthesis, RightParenthesis); - } + return getStatementStartOffset(position); + } + + private int getStatementStartOffset(@NotNull SemanticEditorPosition position) { + Language currLanguage = position.getLanguage(); while (!position.isAtEnd()) { if (currLanguage == Language.ANY || currLanguage == null) currLanguage = position.getLanguage(); if (position.isAt(Colon)) { @@ -178,8 +188,18 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI return afterColon.getStartOffset(); } } - if (position.isAtAnyOf(Semicolon, BlockOpeningBrace, BlockClosingBrace, BlockComment, DocBlockEnd, LineComment, LeftParenthesis) || - (position.getLanguage() != Language.ANY) && !position.isAtLanguage(currLanguage)) { + else if (position.isAt(RightParenthesis)) { + position.beforeParentheses(LeftParenthesis, RightParenthesis); + } + else if (position.isAtAnyOf(Semicolon, + BlockOpeningBrace, + BlockClosingBrace, + BlockComment, + DocBlockEnd, + LineComment, + LeftParenthesis, + LanguageStartDelimiter) || + (position.getLanguage() != Language.ANY) && !position.isAtLanguage(currLanguage)) { SemanticEditorPosition statementStart = getPosition(position.getEditor(), position.getStartOffset()); if (!statementStart.after().afterOptional(Whitespace).isAtEnd()) { return statementStart.getStartOffset(); @@ -187,7 +207,7 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI } position.before(); } - return -1; + return 0; }