RegExp: fix repeated escaped space not replace correctly (IDEA-208559)

This commit is contained in:
Bas Leijdekkers
2019-03-25 15:40:37 +01:00
parent c074d98536
commit 79f0949c92
3 changed files with 25 additions and 23 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.lang.regexp.inspection;
import com.intellij.codeInspection.LocalInspectionTool;
@@ -52,15 +52,17 @@ public class RepeatedSpaceInspection extends LocalInspectionTool {
return;
}
int count = 1;
int length = aChar.getTextLength();
PsiElement next = aChar.getNextSibling();
while (isSpace(next)) {
count++;
length += next.getTextLength();
next = next.getNextSibling();
}
if (count > 1) {
final String message = count + " consecutive spaces in RegExp";
final int offset = aChar.getStartOffsetInParent();
myHolder.registerProblem(parent, new TextRange(offset, offset + count), message, new RepeatedSpaceFix(count));
myHolder.registerProblem(parent, new TextRange(offset, offset + length), message, new RepeatedSpaceFix(count));
}
}
@@ -131,7 +133,7 @@ public class RepeatedSpaceInspection extends LocalInspectionTool {
text.append(injectedLanguageManager.getUnescapedText(child));
}
else if (!inserted) {
text.append(" {").append(range.getLength()).append('}');
text.append(" {").append(myCount).append('}');
inserted = true;
}
child = child.getNextSibling();
@@ -1,23 +1,8 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.lang.regexp.inspection;
import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.openapi.project.Project;
@@ -36,6 +21,10 @@ public abstract class RegExpInspectionTestCase extends LightPlatformCodeInsightF
protected abstract LocalInspectionTool getInspection();
protected void highlightTest(@Language("RegExp") String code) {
highlightTest(code, RegExpFileType.INSTANCE);
}
protected void highlightTest(@Language("RegExp") String code, RegExpFileType fileType) {
final LocalInspectionTool inspection = getInspection();
myFixture.enableInspections(inspection);
final HighlightDisplayKey displayKey = HighlightDisplayKey.find(inspection.getShortName());
@@ -47,12 +36,16 @@ public abstract class RegExpInspectionTestCase extends LightPlatformCodeInsightF
currentProfile.setErrorLevel(displayKey, HighlightDisplayLevel.WARNING, project);
}
}
myFixture.configureByText(RegExpFileType.INSTANCE, code);
myFixture.configureByText(fileType, code);
myFixture.testHighlighting();
}
protected void quickfixTest(@Language("RegExp") String before, @Language("RegExp") String after, String hint) {
highlightTest(before);
quickfixTest(before, after, hint, RegExpFileType.INSTANCE);
}
protected void quickfixTest(@Language("RegExp") String before, @Language("RegExp") String after, String hint, RegExpFileType fileType) {
highlightTest(before, fileType);
myFixture.launchAction(myFixture.findSingleIntention(hint));
myFixture.checkResult(after);
}
@@ -1,13 +1,15 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.lang.regexp.inspection;
import com.intellij.codeInspection.LocalInspectionTool;
import org.intellij.lang.regexp.RegExpFileType;
import org.intellij.lang.regexp.ecmascript.EcmaScriptRegexpLanguage;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
@SuppressWarnings("RegExpRepeatedSpace")
@SuppressWarnings({"RegExpRepeatedSpace", "RegExpRedundantEscape"})
public class RepeatedSpaceInspectionTest extends RegExpInspectionTestCase {
public void testSimple() {
@@ -34,6 +36,11 @@ public class RepeatedSpaceInspectionTest extends RegExpInspectionTestCase {
quickfixTest("\\Q \\E<warning descr=\"3 consecutive spaces in RegExp\"> <caret></warning>", "\\Q \\E {3}", "Replace with ' {3}'");
}
public void testEscapedWhitespace() {
quickfixTest("<warning descr=\"3 consecutive spaces in RegExp\"><caret>\\ </warning>", " {3}", "Replace with ' {3}",
new RegExpFileType(EcmaScriptRegexpLanguage.INSTANCE));
}
@Override
@NotNull
protected LocalInspectionTool getInspection() {