From 47df72b32c18f55811768442c29a43330ae069ce Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Fri, 19 Sep 2014 16:03:02 +0400 Subject: [PATCH] Proper word selection inside string literals in JSON --- .../JsonBasicWordSelectionFilter.java | 15 +++++++ .../JsonStringLiteralSelectionHandler.java | 39 +++++++++++++++++++ .../intellij/json/JsonWordSelectionTest.java | 17 ++++++++ .../selectWord/EscapeAwareness/after1.json | 3 ++ .../selectWord/EscapeAwareness/after2.json | 3 ++ .../selectWord/EscapeAwareness/after3.json | 3 ++ .../selectWord/EscapeAwareness/before.json | 3 ++ .../src/META-INF/JsonPlugin.xml | 2 + 8 files changed, 85 insertions(+) create mode 100644 json/src/com/intellij/json/editor/selection/JsonBasicWordSelectionFilter.java create mode 100644 json/src/com/intellij/json/editor/selection/JsonStringLiteralSelectionHandler.java create mode 100644 json/tests/test/com/intellij/json/JsonWordSelectionTest.java create mode 100644 json/tests/testData/selectWord/EscapeAwareness/after1.json create mode 100644 json/tests/testData/selectWord/EscapeAwareness/after2.json create mode 100644 json/tests/testData/selectWord/EscapeAwareness/after3.json create mode 100644 json/tests/testData/selectWord/EscapeAwareness/before.json diff --git a/json/src/com/intellij/json/editor/selection/JsonBasicWordSelectionFilter.java b/json/src/com/intellij/json/editor/selection/JsonBasicWordSelectionFilter.java new file mode 100644 index 000000000000..6c098ed9e842 --- /dev/null +++ b/json/src/com/intellij/json/editor/selection/JsonBasicWordSelectionFilter.java @@ -0,0 +1,15 @@ +package com.intellij.json.editor.selection; + +import com.intellij.json.JsonParserDefinition; +import com.intellij.openapi.util.Condition; +import com.intellij.psi.PsiElement; + +/** + * @author Mikhail Golubev + */ +public class JsonBasicWordSelectionFilter implements Condition { + @Override + public boolean value(PsiElement element) { + return !(JsonParserDefinition.STRING_LITERALS.contains(element.getNode().getElementType())); + } +} diff --git a/json/src/com/intellij/json/editor/selection/JsonStringLiteralSelectionHandler.java b/json/src/com/intellij/json/editor/selection/JsonStringLiteralSelectionHandler.java new file mode 100644 index 000000000000..13a2a3a5a27c --- /dev/null +++ b/json/src/com/intellij/json/editor/selection/JsonStringLiteralSelectionHandler.java @@ -0,0 +1,39 @@ +package com.intellij.json.editor.selection; + +import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase; +import com.intellij.codeInsight.editorActions.SelectWordUtil; +import com.intellij.json.JsonParserDefinition; +import com.intellij.json.psi.JsonStringLiteral; +import com.intellij.lexer.StringLiteralLexer; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.ElementManipulators; +import com.intellij.psi.PsiElement; +import com.intellij.psi.tree.IElementType; + +import java.util.ArrayList; +import java.util.List; + +import static com.intellij.json.JsonElementTypes.SINGLE_QUOTED_STRING; + +/** + * @author Mikhail Golubev + */ +public class JsonStringLiteralSelectionHandler extends ExtendWordSelectionHandlerBase { + @Override + public boolean canSelect(PsiElement e) { + return JsonParserDefinition.STRING_LITERALS.contains(e.getNode().getElementType()); + } + + @Override + public List select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { + final PsiElement parent = e.getParent(); + assert parent instanceof JsonStringLiteral; + final IElementType type = e.getNode().getElementType(); + final StringLiteralLexer lexer = new StringLiteralLexer(type == SINGLE_QUOTED_STRING ? '\'' : '"', type, false, "/", false, false); + final List result = new ArrayList(); + SelectWordUtil.addWordHonoringEscapeSequences(editorText, e.getTextRange(), cursorOffset, lexer, result); + result.add(ElementManipulators.getValueTextRange(parent).shiftRight(parent.getTextOffset())); + return result; + } +} diff --git a/json/tests/test/com/intellij/json/JsonWordSelectionTest.java b/json/tests/test/com/intellij/json/JsonWordSelectionTest.java new file mode 100644 index 000000000000..5378cf1a6e28 --- /dev/null +++ b/json/tests/test/com/intellij/json/JsonWordSelectionTest.java @@ -0,0 +1,17 @@ +package com.intellij.json; + +import com.intellij.testFramework.fixtures.CodeInsightTestUtil; + +/** + * @author Mikhail Golubev + */ +public class JsonWordSelectionTest extends JsonTestCase { + + private void doTest() { + CodeInsightTestUtil.doWordSelectionTestOnDirectory(myFixture, "selectWord/" + getTestName(false), "json"); + } + + public void testEscapeAwareness() { + doTest(); + } +} diff --git a/json/tests/testData/selectWord/EscapeAwareness/after1.json b/json/tests/testData/selectWord/EscapeAwareness/after1.json new file mode 100644 index 000000000000..a6772fb99b98 --- /dev/null +++ b/json/tests/testData/selectWord/EscapeAwareness/after1.json @@ -0,0 +1,3 @@ +[ + "\tree \node" +] \ No newline at end of file diff --git a/json/tests/testData/selectWord/EscapeAwareness/after2.json b/json/tests/testData/selectWord/EscapeAwareness/after2.json new file mode 100644 index 000000000000..574d839ae12e --- /dev/null +++ b/json/tests/testData/selectWord/EscapeAwareness/after2.json @@ -0,0 +1,3 @@ +[ + "\tree \node" +] \ No newline at end of file diff --git a/json/tests/testData/selectWord/EscapeAwareness/after3.json b/json/tests/testData/selectWord/EscapeAwareness/after3.json new file mode 100644 index 000000000000..b2c26e285533 --- /dev/null +++ b/json/tests/testData/selectWord/EscapeAwareness/after3.json @@ -0,0 +1,3 @@ +[ + "\tree \node" +] \ No newline at end of file diff --git a/json/tests/testData/selectWord/EscapeAwareness/before.json b/json/tests/testData/selectWord/EscapeAwareness/before.json new file mode 100644 index 000000000000..fb6a50b39bc8 --- /dev/null +++ b/json/tests/testData/selectWord/EscapeAwareness/before.json @@ -0,0 +1,3 @@ +[ + "\tree \node" +] \ No newline at end of file diff --git a/platform/platform-resources/src/META-INF/JsonPlugin.xml b/platform/platform-resources/src/META-INF/JsonPlugin.xml index d18285e592cb..0604be44c8ed 100644 --- a/platform/platform-resources/src/META-INF/JsonPlugin.xml +++ b/platform/platform-resources/src/META-INF/JsonPlugin.xml @@ -40,6 +40,8 @@ + +