diff --git a/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java b/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java index 4ae2bfd89a2c..997123351d36 100644 --- a/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java +++ b/python/src/com/jetbrains/python/validation/StringLiteralQuotesAnnotator.java @@ -58,9 +58,17 @@ public class StringLiteralQuotesAnnotator extends PyAnnotator { private boolean checkQuotedString(@NotNull ASTNode stringNode, @NotNull String nodeText) { final char firstQuote = nodeText.charAt(0); - final int lastChar = nodeText.length() - 1; - if (lastChar == 0 || nodeText.charAt(lastChar) != firstQuote || - (nodeText.charAt(lastChar - 1) == '\\' && (lastChar == 1 || nodeText.charAt(lastChar - 2) != '\\'))) { + final char lastChar = nodeText.charAt(nodeText.length() - 1); + int precedingBackslashCount = 0; + for (int i = nodeText.length() - 2; i >= 0; i--) { + if (nodeText.charAt(i) == '\\') { + precedingBackslashCount++; + } + else { + break; + } + } + if (nodeText.length() == 1 || lastChar != firstQuote || precedingBackslashCount % 2 != 0) { getHolder().createErrorAnnotation(stringNode, PyBundle.message("ANN.missing.closing.quote", firstQuote)); return true; } diff --git a/python/testData/highlighting/multipleEscapedBackslashes.py b/python/testData/highlighting/multipleEscapedBackslashes.py new file mode 100644 index 000000000000..4eb30abdcdb4 --- /dev/null +++ b/python/testData/highlighting/multipleEscapedBackslashes.py @@ -0,0 +1,2 @@ +S1 = 'This literal contains even number of backslashes\\\\\\' +S2 = 'This literal contains odd number of backslashes\\\\\\\' \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java b/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java index e1fa67de63e4..73ab49e28659 100644 --- a/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java +++ b/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java @@ -174,6 +174,10 @@ public class PythonHighlightingTest extends PyTestCase { doTest(true, false); } + public void testMultipleEscapedBackslashes() { + doTest(true, false); + } + public void testUnsupportedFeaturesInPython3() { doTest(LanguageLevel.PYTHON30, true, false); }