From 5406243e492d57a6b7f3db3857bb71e88a83f60c Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 12 Dec 2018 13:05:39 +0700 Subject: [PATCH] TailTypes#CASE_ARROW: do not add if arrow is present even with different spacing (IDEA-204074) --- .../src/com/intellij/codeInsight/TailTypes.java | 15 ++++++++++----- .../normal/SecondLabelInRuleSwitch.java | 3 ++- .../normal/SecondLabelInRuleSwitch_after.java | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/TailTypes.java b/java/java-impl/src/com/intellij/codeInsight/TailTypes.java index 414b90435946..272c5fd17cc8 100644 --- a/java/java-impl/src/com/intellij/codeInsight/TailTypes.java +++ b/java/java-impl/src/com/intellij/codeInsight/TailTypes.java @@ -21,7 +21,6 @@ import com.intellij.codeInsight.completion.simple.ParenthesesTailType; import com.intellij.codeInsight.completion.simple.RParenthTailType; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiSwitchBlock; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.siyeh.ig.psiutils.SwitchUtils; @@ -142,11 +141,17 @@ public class TailTypes { @Override public boolean isApplicable(@NotNull InsertionContext context) { Document document = context.getDocument(); - int offset = context.getTailOffset(); int length = document.getTextLength(); - int endOffset = offset + ARROW.length(); - if (endOffset > length) return true; - return !document.getText(new TextRange(offset, endOffset)).equals(ARROW); + CharSequence chars = document.getCharsSequence(); + int offset; + for(offset = context.getTailOffset(); offset < length; offset++) { + char c = chars.charAt(offset); + if (c != '\n' && c != ' ' && c != '\t') { + break; + } + } + boolean hasArrow = offset + 2 < length && chars.subSequence(offset, offset + 2).toString().equals("->"); + return !hasArrow; } @Override diff --git a/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch.java b/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch.java index 3aef0000080a..74d7ac740811 100644 --- a/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch.java +++ b/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch.java @@ -3,7 +3,8 @@ public class ConstConfig { void test(X x) { switch (x) { - case BAR, FO -> {} + case BAR, FO -> + System.out.println(); } } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch_after.java b/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch_after.java index edb48dfd3016..73543941571d 100644 --- a/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch_after.java +++ b/java/java-tests/testData/codeInsight/completion/normal/SecondLabelInRuleSwitch_after.java @@ -3,7 +3,8 @@ public class ConstConfig { void test(X x) { switch (x) { - case BAR, FOO -> {} + case BAR, FOO -> + System.out.println(); } } } \ No newline at end of file