From dd2dbd490dade10a28b4f0d1391a3b194b06c97f Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Tue, 31 May 2016 16:24:08 +0300 Subject: [PATCH] - Handle position after 'for' statement - Do not indent before '{' --- .../editorActions/JavaLineIndentProvider.java | 1 + .../codeStyle/JavaLikeLangLineIndentProvider.java | 15 ++++++++++----- .../source/codeStyle/SemanticEditorPosition.java | 13 +++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaLineIndentProvider.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaLineIndentProvider.java index 499d3c481382..feb3ad1db87c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaLineIndentProvider.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaLineIndentProvider.java @@ -46,6 +46,7 @@ public class JavaLineIndentProvider extends JavaLikeLangLineIndentProvider { SYNTAX_MAP.put(JavaTokenType.DEFAULT_KEYWORD, SwitchDefault); SYNTAX_MAP.put(JavaTokenType.IF_KEYWORD, IfKeyword); SYNTAX_MAP.put(JavaTokenType.ELSE_KEYWORD, ElseKeyword); + SYNTAX_MAP.put(JavaTokenType.FOR_KEYWORD, ForKeyword); } @Nullable diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java index 5cd92b2926eb..15c681139c0c 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java @@ -54,7 +54,8 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI SwitchCase, SwitchDefault, ElseKeyword, - IfKeyword + IfKeyword, + ForKeyword } @Nullable @@ -105,11 +106,15 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI else { SemanticEditorPosition position = getPosition(editor, offset); if (position.before().isAt(RightParenthesis)) { + int offsetAfterParen = position.getStartOffset() + 1; position.beforeParentheses(LeftParenthesis, RightParenthesis); if (!position.isAtEnd()) { position.beforeOptional(Whitespace); - if (position.isAt(IfKeyword)) { - return createIndentData(NORMAL, IfKeyword); + if (position.isAt(IfKeyword) || position.isAt(ForKeyword)) { + SyntaxElement element = position.getCurrElement(); + assert element != null; + Type indentType = getPosition(editor, offsetAfterParen).afterOptional(Whitespace).isAt(BlockOpeningBrace) ? NONE : NORMAL; + return createIndentData(indentType, element); } } } @@ -183,7 +188,7 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI if (BlockOpeningBrace.equals(afterElement) && !isOnSeparateLine(editor, afterElement, offset)) { return findStatementStart(editor, afterElement, offset); } - else if (IfKeyword.equals(afterElement)) { + else if (IfKeyword.equals(afterElement) || ForKeyword.equals(afterElement)) { return findStatementStart(editor, null, offset); } return CharArrayUtil.shiftBackward(docChars, offset, " \t\n\r"); @@ -242,7 +247,7 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI } @Nullable - private static Pair createIndentData(@Nullable Type type, @NotNull SyntaxElement element) { + protected static Pair createIndentData(@Nullable Type type, @NotNull SyntaxElement element) { return type != null ? Pair.create(type, element) : null; } 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 4c8f40981770..647a3bf85868 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 @@ -20,6 +20,7 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator; import com.intellij.psi.tree.IElementType; import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Rustam Vishnyakov @@ -56,6 +57,13 @@ public abstract class SemanticEditorPosition { return this; } + public SemanticEditorPosition afterOptional(@NotNull SyntaxElement syntaxElement) { + if (!myIterator.atEnd()) { + if (syntaxElement.equals(map(myIterator.getTokenType()))) myIterator.advance(); + } + return this; + } + public SemanticEditorPosition after() { if (!myIterator.atEnd()) { myIterator.advance(); @@ -116,6 +124,11 @@ public abstract class SemanticEditorPosition { return false; } + @Nullable + public SyntaxElement getCurrElement() { + return !myIterator.atEnd() ? map(myIterator.getTokenType()) : null; + } + public boolean matchesRule(@NotNull Rule rule) { return rule.check(this); }