mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Select Word handles escape sequences in Python string literals (PY-9014)
This commit is contained in:
@@ -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<TextRange> 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<TextRange> ranges = literal.getStringValueTextRanges();
|
||||
List<ASTNode> 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<TextRange> result = new ArrayList<TextRange>();
|
||||
SelectWordUtil.addWordHonoringEscapeSequences(editorText, nodes.get(i).getTextRange(), cursorOffset,
|
||||
new PyStringLiteralLexer(nodes.get(i).getElementType()),
|
||||
result);
|
||||
result.add(offsetRange);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
s = 'Hello\n<selection>World</selection>!'
|
||||
@@ -0,0 +1 @@
|
||||
s = 'Hello\nWo<caret>rld!'
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user