escaping # in verbose regexps is not redundant (PY-6545)

This commit is contained in:
Dmitry Jemerov
2013-01-30 13:50:45 +01:00
parent 9e57727004
commit 6d6da65bdf
4 changed files with 42 additions and 5 deletions

View File

@@ -1,17 +1,18 @@
package com.jetbrains.python.psi.impl;
import com.intellij.lang.ASTNode;
import com.intellij.lang.Language;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.LiteralTextEscaper;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceService;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
import com.intellij.psi.impl.source.tree.LeafElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.regexp.PythonVerboseRegexpLanguage;
import com.jetbrains.python.lexer.PythonHighlightingLexer;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.types.PyType;
@@ -396,9 +397,25 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt
}
public boolean characterNeedsEscaping(char c) {
if (c == '#') {
return isVerboseInjection();
}
return c == ']' || c == '}' || c == '\"' || c == '\'';
}
private boolean isVerboseInjection() {
List<Pair<PsiElement,TextRange>> files = InjectedLanguageManager.getInstance(getProject()).getInjectedPsiFiles(this);
if (files != null) {
for (Pair<PsiElement, TextRange> file : files) {
Language language = file.getFirst().getLanguage();
if (language == PythonVerboseRegexpLanguage.INSTANCE) {
return true;
}
}
}
return false;
}
public boolean supportsPerl5EmbeddedComments() {
return true;
}

View File

@@ -0,0 +1,15 @@
import re
urlparts = re.compile(
r"""
(?P<scheme>http|https)
://
(?P<netloc>(?:(?!/|\?|\#)\S)*)
/?
(?P<path>(?:(?!\?|\#)\S)*)
\??
(?P<query>(?:(?!\#)\S)*)
\#?
(?P<fragment>[\S]*)
""", re.VERBOSE
)

View File

@@ -55,6 +55,10 @@ public class PyRegexpTest extends PyTestCase {
doTestHighlighting();
}
public void testVerboseEscapedHash() { // PY-6545
doTestHighlighting();
}
private void doTestHighlighting() {
myFixture.testHighlighting(true, false, true, "regexp/" + getTestName(true) + ".py");
}

View File

@@ -105,7 +105,8 @@ public class PythonAllTestsSuite {
PyJoinLinesTest.class,
PyStatementListTest.class,
PyChangeSignatureTest.class,
PyCommenterTest.class
PyCommenterTest.class,
PyRegexpTest.class
};
public static TestSuite suite() {