From e60147d8e19e050a07f22211497832e5257e7ec0 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 2 Jan 2012 23:59:19 +0100 Subject: [PATCH] IDEA-65256 (Quick Surround with Quotes/Parentheses improvement) --- .../SelectionQuotingTypedHandler.java | 9 +++++-- .../src/misc/registry.properties | 2 +- .../SelectionQuotingTypedHandlerTest.java | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SelectionQuotingTypedHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SelectionQuotingTypedHandler.java index 308109eb210e..072eec52866c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SelectionQuotingTypedHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/SelectionQuotingTypedHandler.java @@ -63,9 +63,14 @@ public class SelectionQuotingTypedHandler extends TypedHandlerDelegate { final char c2 = getMatchingDelimiter(c); final String newText = String.valueOf(c) + selectedText + c2; EditorModificationUtil.insertStringAtCaret(editor, newText); - if (Registry.is("editor.smarterSelectionQuoting") && !replace) { + if (Registry.is("editor.smarterSelectionQuoting")) { myReplacedTextRange = new TextRange(caretOffset + 1, caretOffset + newText.length() - 1); - myCaretPosition += 1; + if (!replace || myCaretPosition == caretOffset) { + myCaretPosition++; + } + else if (myCaretPosition == caretOffset + newText.length()) { + myCaretPosition--; + } } else { myReplacedTextRange = new TextRange(caretOffset, caretOffset + newText.length()); } diff --git a/platform/platform-resources-en/src/misc/registry.properties b/platform/platform-resources-en/src/misc/registry.properties index b501a5c6579e..619b4d4b42a9 100644 --- a/platform/platform-resources-en/src/misc/registry.properties +++ b/platform/platform-resources-en/src/misc/registry.properties @@ -97,7 +97,7 @@ editor.balloonHints=true editor.mouseSelectionStateResetTimeout=1000 editor.mouseSelectionStateResetDeadzone=4 editor.use.new.tabs=true -editor.smarterSelectionQuoting=false +editor.smarterSelectionQuoting=true ide.tabbedPane.bufferedPaint=true ide.tabbedPane.dragOutMultiplier=1.2 diff --git a/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java b/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java index c9e15ccb0d54..6a5ec5d5ce2d 100644 --- a/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java +++ b/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java @@ -102,6 +102,30 @@ public class SelectionQuotingTypedHandlerTest extends LightPlatformCodeInsightFi doTest('[', "\"aaa\"\nbbb\n\n", "[\"aaa\"]\nbbb\n\n"); } + public void testReplaceBracketAndText() { + doTest("'a", "\"b\"\nbbb\n\n", "'a'\nbbb\n\n"); + } + + public void testTripleEnquote() { + doTest("\"\"\"", "text\nbbb\n\n", "\"\"\"text\"\"\"\nbbb\n\n"); + } + + private void doTest(@NotNull final String cs, @NotNull String before, @NotNull String expected) { + myFixture.configureByText(FileTypes.PLAIN_TEXT, before); + final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction(); + + performAction(myFixture.getProject(), new Runnable() { + @Override + public void run() { + for (int i = 0, max = cs.length(); i < max; i++) { + final char c = cs.charAt(i); + typedAction.actionPerformed(myFixture.getEditor(), c, ((EditorEx)myFixture.getEditor()).getDataContext()); + } + } + }); + myFixture.checkResult(expected); + } + public void testRuby7852ErrantEditor() { myFixture.configureByText(FileTypes.PLAIN_TEXT, "\"aaa\"\nbbb\n\n"); myFixture.getEditor().getCaretModel().moveToOffset(0);