From f29a942ca1fcd0e5b2fb8a61c125de7be63c1c25 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 1 Mar 2013 10:20:02 +0100 Subject: [PATCH] Select Word handles escape sequences in Python string literals (PY-9014) --- .../selectWord/PyLiteralSelectionHandler.java | 15 +++++++++++++-- .../testData/selectWord/escapeSequence/after1.py | 1 + .../testData/selectWord/escapeSequence/before.py | 1 + .../com/jetbrains/python/PySelectWordTest.java | 4 ++++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 python/testData/selectWord/escapeSequence/after1.py create mode 100644 python/testData/selectWord/escapeSequence/before.py diff --git a/python/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java b/python/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java index da68837950ae..c71e22f6f2c0 100644 --- a/python/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java +++ b/python/src/com/jetbrains/python/editor/selectWord/PyLiteralSelectionHandler.java @@ -1,14 +1,17 @@ package com.jetbrains.python.editor.selectWord; import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandler; +import com.intellij.codeInsight.editorActions.SelectWordUtil; import com.intellij.lang.ASTNode; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.PyTokenTypes; +import com.jetbrains.python.lexer.PyStringLiteralLexer; import com.jetbrains.python.psi.PyStringLiteralExpression; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -26,10 +29,18 @@ public class PyLiteralSelectionHandler implements ExtendWordSelectionHandler { public List select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { final PyStringLiteralExpression literal = PsiTreeUtil.getParentOfType(e, PyStringLiteralExpression.class); if (literal != null) { - for (TextRange stringRange : literal.getStringValueTextRanges()) { + List ranges = literal.getStringValueTextRanges(); + List nodes = literal.getStringNodes(); + for (int i = 0; i < ranges.size(); i++) { + TextRange stringRange = ranges.get(i); TextRange offsetRange = stringRange.shiftRight(literal.getTextRange().getStartOffset()); if (offsetRange.contains(cursorOffset) && offsetRange.getLength() > 1) { - return Collections.singletonList(offsetRange); + List result = new ArrayList(); + SelectWordUtil.addWordHonoringEscapeSequences(editorText, nodes.get(i).getTextRange(), cursorOffset, + new PyStringLiteralLexer(nodes.get(i).getElementType()), + result); + result.add(offsetRange); + return result; } } } diff --git a/python/testData/selectWord/escapeSequence/after1.py b/python/testData/selectWord/escapeSequence/after1.py new file mode 100644 index 000000000000..46db6e0b3918 --- /dev/null +++ b/python/testData/selectWord/escapeSequence/after1.py @@ -0,0 +1 @@ +s = 'Hello\nWorld!' \ No newline at end of file diff --git a/python/testData/selectWord/escapeSequence/before.py b/python/testData/selectWord/escapeSequence/before.py new file mode 100644 index 000000000000..be782b39a712 --- /dev/null +++ b/python/testData/selectWord/escapeSequence/before.py @@ -0,0 +1 @@ +s = 'Hello\nWorld!' \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PySelectWordTest.java b/python/testSrc/com/jetbrains/python/PySelectWordTest.java index 1f7298539ccd..bf34068dfcab 100644 --- a/python/testSrc/com/jetbrains/python/PySelectWordTest.java +++ b/python/testSrc/com/jetbrains/python/PySelectWordTest.java @@ -31,6 +31,10 @@ public class PySelectWordTest extends PyTestCase { doTest(); } + public void testEscapeSequence() { // PY-9014 + doTest(); + } + private void doTest() { @NonNls final String path = "selectWord/" + getTestName(true); myFixture.copyDirectoryToProject(path, path);