EA-240119 - taking in account concatenation with empty string

GitOrigin-RevId: 9dd8853c0d19b3b4f887dfdcf39d039f40627636
This commit is contained in:
Ilyas Selimov
2020-10-12 14:46:53 +07:00
committed by intellij-monorepo-bot
parent 100fd1f0f7
commit bdced67aaf
2 changed files with 33 additions and 20 deletions

View File

@@ -3,6 +3,7 @@ package org.intellij.lang.regexp.inspection;
import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLanguageInjectionHost; import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.PsiLiteralValue; import com.intellij.psi.PsiLiteralValue;
@@ -97,20 +98,20 @@ class ShredManager {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return myShredIndex != -1 && extractShredText(myShreds.get(myShredIndex)) != null; return myShredIndex != -1 && findFirstNonEmptyShredIndex() != -1;
} }
@Override @Override
@NotNull @NotNull
public ShredInfo next() { public ShredInfo next() {
if (myShredIndex == -1) throw new IllegalStateException("Iterator doesn't contain any elements"); if (myShredIndex == -1) throw new IllegalStateException("Iterator doesn't contain any shreds");
myShredIndex = findFirstNonEmptyShredIndex();
if (myShredIndex == -1) throw new IllegalStateException("Iterator doesn't contain non-empty shreds");
if (myShredText == null) { if (myShredText == null) {
myShredText = extractShredText(myShreds.get(myShredIndex)); myShredText = extractShredText(myShreds.get(myShredIndex));
if (myShredText == null) throw new IllegalStateException("Iterator#hasNext() must be called before Iterator#next()"); if (StringUtil.isEmpty(myShredText)) throw new IllegalStateException("Current shred text is empty");
if (myShredText.length() > 0) { mySymbolIndex = 0;
mySymbolIndex = 0;
}
} }
int shredIndex = myShredIndex; int shredIndex = myShredIndex;
char shredSymbol = myShredText.charAt(mySymbolIndex); char shredSymbol = myShredText.charAt(mySymbolIndex);
@@ -120,14 +121,21 @@ class ShredManager {
return new ShredInfo(shredIndex, shredSymbol, symbolIndex, shred.getHost()); return new ShredInfo(shredIndex, shredSymbol, symbolIndex, shred.getHost());
} }
private int findFirstNonEmptyShredIndex() {
for (int shredIndex = myShredIndex; shredIndex < myShreds.size(); shredIndex++) {
String shredText = extractShredText(myShreds.get(shredIndex));
if (!StringUtil.isEmpty(shredText)) return shredIndex;
}
return -1;
}
private void updateIndexes() { private void updateIndexes() {
if (mySymbolIndex < myShredText.length() - 1) { if (mySymbolIndex < myShredText.length() - 1) {
mySymbolIndex++; mySymbolIndex++;
} }
else if (myShredIndex < myShreds.size() - 1) { else if (myShredIndex < myShreds.size() - 1) {
mySymbolIndex = 0;
myShredIndex++; myShredIndex++;
myShredText = extractShredText(myShreds.get(myShredIndex)); myShredText = null;
} }
else { else {
myShredIndex = -1; myShredIndex = -1;

View File

@@ -242,32 +242,37 @@ public class RegExpHighlightingTest extends LightJavaCodeInsightFixtureTestCase
} }
public void testConditionalExprNegative1() { public void testConditionalExprNegative1() {
doTestConditionalExpr("c ? \"^Hello$\" : \"^World$\""); doTestUnexpectedAnchor("c ? \"^Hello$\" : \"^World$\"");
} }
public void testBinaryExprPositive1() { public void testBinaryExprPositive1() {
doTestConditionalExpr("\"^good" + warningMarker('$') + "\" + \"" + warningMarker('^') + "luck$\""); doTestUnexpectedAnchor("\"^good" + warningMarker('$') + "\" + \"" + warningMarker('^') + "luck$\"");
} }
public void testConditionalExprPositive1() { public void testConditionalExprPositive1() {
doTestConditionalExpr("c ? \" " + warningMarker('^') + "Hello" + warningMarker('$') + " \" " + doTestUnexpectedAnchor("c ? \" " + warningMarker('^') + "Hello" + warningMarker('$') + " \" " +
": \" " + warningMarker('^') + "World" + warningMarker('$') + " \""); ": \" " + warningMarker('^') + "World" + warningMarker('$') + " \"");
} }
public void testConditionalExprPositive2() { public void testConditionalExprPositive2() {
doTestConditionalExpr("c ? \" " + warningMarker('^') + "Hello$\" " + doTestUnexpectedAnchor("c ? \" " + warningMarker('^') + "Hello$\" " +
": \"^Have" + warningMarker('$') + "\" + \"" + warningMarker('^') + "Fun$\""); ": \"^Have" + warningMarker('$') + "\" + \"" + warningMarker('^') + "Fun$\"");
} }
public void testConditionalExprPositive3() { public void testConditionalExprPositive3() {
doTestConditionalExpr("c ? (c ? (c ? \"^go$\" : \"^od" + warningMarker('$') + "\" + \"" + warningMarker('^') + "lu$\") : \"^ck$\") " + doTestUnexpectedAnchor("c ? (c ? (c ? \"^go$\" : \"^od" + warningMarker('$') + "\" + \"" + warningMarker('^') + "lu$\") : \"^ck$\") " +
": (c ? \"^and" + warningMarker('$') + "\" + \"" + warningMarker('^') + ": (c ? \"^and" + warningMarker('$') + "\" + \"" + warningMarker('^') +
"ha" + warningMarker('$') + "\" + \"" + warningMarker('^') + "ve$\" : \"^fun$\")"); "ha" + warningMarker('$') + "\" + \"" + warningMarker('^') + "ve$\" : \"^fun$\")");
} }
public void testConditionalExprPositive4() { public void testConditionalExprPositive4() {
doTestConditionalExpr("c ? \"^hello" + warningMarker('$') + "\" + getStr() + \"" + warningMarker('^') + "world$\" " + doTestUnexpectedAnchor("c ? \"^hello" + warningMarker('$') + "\" + getStr() + \"" + warningMarker('^') + "world$\" " +
": getStr() + \"" + warningMarker('^') + "yeah" + warningMarker('$') + " \""); ": getStr() + \"" + warningMarker('^') + "yeah" + warningMarker('$') + " \"");
}
public void testConcatenationWithEmptyStrings() {
// "" + " ^" + "" + "$ "
doTestUnexpectedAnchor("\"\" +" + "\" " + warningMarker('^') + "\" + " + "\"\" + " + "\"" + warningMarker('$') + " \"");
} }
private void doTest(@NonNls String code) { private void doTest(@NonNls String code) {
@@ -276,7 +281,7 @@ public class RegExpHighlightingTest extends LightJavaCodeInsightFixtureTestCase
myFixture.testHighlighting(); myFixture.testHighlighting();
} }
private void doTestConditionalExpr(@NonNls String code) { private void doTestUnexpectedAnchor(@NonNls String code) {
myFixture.enableInspections(new UnexpectedAnchorInspection()); myFixture.enableInspections(new UnexpectedAnchorInspection());
myFixture.configureByText(JavaFileType.INSTANCE, "class X {" + myFixture.configureByText(JavaFileType.INSTANCE, "class X {" +
" void test(boolean c) {" + " void test(boolean c) {" +