SSR: fix surround selection with $

GitOrigin-RevId: f21add58786ef24512cf501e56403ebd135e13d0
This commit is contained in:
Bas Leijdekkers
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent 0a6410c615
commit 842e4283c8
2 changed files with 64 additions and 35 deletions
@@ -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;
@@ -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("<caret>aaa\nbbb\n\n", "$<caret>$aaa\nbbb\n\n");
}
public void testTypeOver() {
doTest("$X<caret>$", "$X$<caret>");
}
public void testMultipleCarets() {
doTest("$c<caret>$ <caret> ", "$c$<caret> $<caret>$ ");
}
public void testSurroundSelection() {
doTest("<selection>bla<caret></selection> no class",
"$<selection>bla<caret></selection>$ no class");
}
public void testCombined() {
doTest("$x<caret>$ y<caret>z <selection>abc<caret></selection>",
"$x$<caret> y$<caret>$z $<selection>abc<caret></selection>$");
}
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);
}
}