From 842e4283c81641e1f768b2e157897f7b475fcb13 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 19 Jun 2019 19:28:42 +0200 Subject: [PATCH] SSR: fix surround selection with $ GitOrigin-RevId: f21add58786ef24512cf501e56403ebd135e13d0 --- .../ui/StructuralSearchTypedHandler.java | 56 +++++++------------ .../ui/StructuralSearchTypedHandlerTest.java | 43 ++++++++++++++ 2 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 platform/structuralsearch/testSource/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandlerTest.java diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandler.java b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandler.java index 46317320314f..e0aab11eea0e 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandler.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandler.java @@ -4,7 +4,6 @@ package com.intellij.structuralsearch.plugin.ui; import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeInsight.editorActions.TypedHandlerDelegate; import com.intellij.openapi.editor.*; -import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiFile; @@ -33,7 +32,7 @@ public class StructuralSearchTypedHandler extends TypedHandlerDelegate { final SelectionModel selectionModel = editor.getSelectionModel(); final String selectedText = selectionModel.getSelectedText(); if (!StringUtil.isEmpty(selectedText)) { - if (selectedText.contains("$") || CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED) { + if (selectedText.contains("$") || !CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED) { return Result.CONTINUE; } final Document document = editor.getDocument(); @@ -79,40 +78,27 @@ public class StructuralSearchTypedHandler extends TypedHandlerDelegate { } return Result.STOP; } - } - return super.beforeSelectionRemoved(c, project, editor, file); - } - - @NotNull - @Override - public Result beforeCharTyped(char c, - @NotNull Project project, - @NotNull Editor editor, - @NotNull PsiFile file, - @NotNull FileType fileType) { - if (editor.getUserData(SubstitutionShortInfoHandler.CURRENT_CONFIGURATION_KEY) == null) { - return Result.CONTINUE; - } - if (c == '$' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) { - final Document document = editor.getDocument(); - final CaretModel caretModel = editor.getCaretModel(); - final Caret caret = caretModel.getCurrentCaret(); - final LogicalPosition position = caret.getLogicalPosition(); - final int lineStart = document.getLineStartOffset(position.line); - final CharSequence text = document.getCharsSequence(); - final int index = lineStart + position.column; - if (index < text.length() && text.charAt(index) == '$') { - caret.setSelection(index, index + 1); - } - final CharSequence line = text.subSequence(lineStart, lineStart + position.column); - boolean $ = false; - for (int i = 0, max = line.length(); i < max; i++) { - if (line.charAt(i) == '$') { - $ = !$; + else if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) { + final Document document = editor.getDocument(); + final CaretModel caretModel = editor.getCaretModel(); + final Caret caret = caretModel.getCurrentCaret(); + final LogicalPosition position = caret.getLogicalPosition(); + final int lineStart = document.getLineStartOffset(position.line); + final CharSequence text = document.getCharsSequence(); + final int index = lineStart + position.column; + if (index < text.length() && text.charAt(index) == '$') { + caret.setSelection(index, index + 1); + } + final CharSequence line = text.subSequence(lineStart, lineStart + position.column); + boolean $ = false; + for (int i = 0, max = line.length(); i < max; i++) { + if (line.charAt(i) == '$') { + $ = !$; + } + } + if (!$) { + document.insertString(lineStart + position.column, "$"); } - } - if (!$) { - document.insertString(lineStart + position.column, "$"); } } return Result.CONTINUE; diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandlerTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandlerTest.java new file mode 100644 index 000000000000..c4c48dee2b9e --- /dev/null +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/plugin/ui/StructuralSearchTypedHandlerTest.java @@ -0,0 +1,43 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.structuralsearch.plugin.ui; + +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.fileTypes.FileTypes; +import com.intellij.testFramework.fixtures.BasePlatformTestCase; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class StructuralSearchTypedHandlerTest extends BasePlatformTestCase { + + public void testNoSelection() { + doTest("aaa\nbbb\n\n", "$$aaa\nbbb\n\n"); + } + + public void testTypeOver() { + doTest("$X$", "$X$"); + } + + public void testMultipleCarets() { + doTest("$c$ ", "$c$ $$ "); + } + + public void testSurroundSelection() { + doTest("bla no class", + "$bla$ no class"); + } + + public void testCombined() { + doTest("$x$ yz abc", + "$x$ y$$z $abc$"); + } + + private void doTest(@NotNull String before, @NotNull String after) { + myFixture.configureByText(FileTypes.PLAIN_TEXT, before); + final Editor editor = myFixture.getEditor(); + editor.putUserData(SubstitutionShortInfoHandler.CURRENT_CONFIGURATION_KEY, new SearchConfiguration()); + myFixture.type("$"); + myFixture.checkResult(after); + } +} \ No newline at end of file