From 5d992e3b22623531316aba340f6ef4f9b287a155 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 14 Nov 2011 14:00:03 +0100 Subject: [PATCH] escaping single quotes is not redundant in PyCharm (PY-5030) --- .../impl/PyStringLiteralExpressionImpl.java | 42 +++++++++---------- .../regexp/redundantEscapeSingleQuote.py | 3 ++ .../com/jetbrains/python/PyRegexpTest.java | 6 ++- 3 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 python/testData/regexp/redundantEscapeSingleQuote.py diff --git a/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java index fad44951a6f9..15e75cd9af06 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java @@ -181,34 +181,30 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt String octal = escMatcher.group(2); String hex = escMatcher.group(3); String str = null; - if (!raw) { - if (octal != null) { - str = new String(new char[]{(char)Integer.parseInt(octal, 8)}); + if (octal != null) { + str = new String(new char[]{(char)Integer.parseInt(octal, 8)}); - } - else if (hex != null) { - str = new String(new char[]{(char)Integer.parseInt(hex, 16)}); + } + else if (hex != null) { + str = new String(new char[]{(char)Integer.parseInt(hex, 16)}); - } - else { - String toReplace = escMatcher.group(1); - String replacement = escapeMap.get(toReplace); - if (replacement != null) { - str = replacement; - } + } + else { + String toReplace = escMatcher.group(1); + String replacement = escapeMap.get(toReplace); + if (replacement != null) { + str = replacement; } } if (unicode) { - if (!raw) { - String unicodeName = escMatcher.group(4); - String unicode32 = escMatcher.group(6); + String unicodeName = escMatcher.group(4); + String unicode32 = escMatcher.group(6); - if (unicode32 != null) { - str = new String(Character.toChars((int)Long.parseLong(unicode32, 16))); - } - if (unicodeName != null) { - //TOLATER: implement unicode character name escapes - } + if (unicode32 != null) { + str = new String(Character.toChars((int)Long.parseLong(unicode32, 16))); + } + if (unicodeName != null) { + //TOLATER: implement unicode character name escapes } String unicode16 = escMatcher.group(5); if (unicode16 != null) { @@ -372,7 +368,7 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt } public boolean characterNeedsEscaping(char c) { - return c == ']' || c == '}' || c == '\"'; + return c == ']' || c == '}' || c == '\"' || c == '\''; } public boolean supportsPerl5EmbeddedComments() { diff --git a/python/testData/regexp/redundantEscapeSingleQuote.py b/python/testData/regexp/redundantEscapeSingleQuote.py new file mode 100644 index 000000000000..ff917125e871 --- /dev/null +++ b/python/testData/regexp/redundantEscapeSingleQuote.py @@ -0,0 +1,3 @@ +import re + +foo = re.compile(r'[\w\']') diff --git a/python/testSrc/com/jetbrains/python/PyRegexpTest.java b/python/testSrc/com/jetbrains/python/PyRegexpTest.java index 13bf2ce8fee4..845bac244f33 100644 --- a/python/testSrc/com/jetbrains/python/PyRegexpTest.java +++ b/python/testSrc/com/jetbrains/python/PyRegexpTest.java @@ -43,8 +43,12 @@ public class PyRegexpTest extends PyTestCase { PyLexerTestCase.doLexerTest("# abc", lexer, "COMMENT", "COMMENT"); } + public void testRedundantEscapeSingleQuote() { // PY-5027 + doTestHighlighting(); + } + private void doTestHighlighting() { - myFixture.testHighlighting(true, false, false, "regexp/" + getTestName(true) + ".py"); + myFixture.testHighlighting(true, false, true, "regexp/" + getTestName(true) + ".py"); } private void doTestLexer(final String text, String... expectedTokens) {