From 79f0949c92cf7c30f8979f0b5f6ee0ed95f1399a Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 25 Mar 2019 11:24:02 +0100 Subject: [PATCH] RegExp: fix repeated escaped space not replace correctly (IDEA-208559) --- .../inspection/RepeatedSpaceInspection.java | 8 +++-- .../inspection/RegExpInspectionTestCase.java | 29 +++++++------------ .../RepeatedSpaceInspectionTest.java | 11 +++++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/RegExpSupport/src/org/intellij/lang/regexp/inspection/RepeatedSpaceInspection.java b/RegExpSupport/src/org/intellij/lang/regexp/inspection/RepeatedSpaceInspection.java index 6bb0cc1ee73f..0b4cd4047ab5 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/inspection/RepeatedSpaceInspection.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/inspection/RepeatedSpaceInspection.java @@ -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(); diff --git a/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpInspectionTestCase.java b/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpInspectionTestCase.java index 06624f932227..1c90e1ea88e1 100644 --- a/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpInspectionTestCase.java +++ b/RegExpSupport/test/org/intellij/lang/regexp/inspection/RegExpInspectionTestCase.java @@ -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); } diff --git a/RegExpSupport/test/org/intellij/lang/regexp/inspection/RepeatedSpaceInspectionTest.java b/RegExpSupport/test/org/intellij/lang/regexp/inspection/RepeatedSpaceInspectionTest.java index 8a35e2a7ae33..1005d134c20f 100644 --- a/RegExpSupport/test/org/intellij/lang/regexp/inspection/RepeatedSpaceInspectionTest.java +++ b/RegExpSupport/test/org/intellij/lang/regexp/inspection/RepeatedSpaceInspectionTest.java @@ -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 ", "\\Q \\E {3}", "Replace with ' {3}'"); } + public void testEscapedWhitespace() { + quickfixTest("\\ ", " {3}", "Replace with ' {3}", + new RegExpFileType(EcmaScriptRegexpLanguage.INSTANCE)); + } + @Override @NotNull protected LocalInspectionTool getInspection() {