From c70a49fab3498d76e7522492462c573dca2dd739 Mon Sep 17 00:00:00 2001 From: Sergey Rostov Date: Tue, 21 May 2013 16:26:40 +0400 Subject: [PATCH] KT-3575, KT-2297 Inserting pair for "${": reverting to use KotlinTypeHandler (with disabling JetPairMatcher for this case) --- .../org/jetbrains/jet/plugin/JetPairMatcher.java | 8 ++++++-- .../jet/plugin/editor/KotlinTypedHandler.java | 14 ++++++++++++++ .../org/jetbrains/jet/editor/TypedHandlerTest.java | 12 ++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/JetPairMatcher.java b/idea/src/org/jetbrains/jet/plugin/JetPairMatcher.java index a97fdee0200..9bf05aa432b 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetPairMatcher.java +++ b/idea/src/org/jetbrains/jet/plugin/JetPairMatcher.java @@ -39,8 +39,12 @@ public class JetPairMatcher implements PairedBraceMatcher { @Override public boolean isPairedBracesAllowedBeforeType(@NotNull IElementType lbraceType, @Nullable IElementType contextType) { - return lbraceType.equals(JetTokens.LONG_TEMPLATE_ENTRY_START) - || JetTokens.WHITE_SPACE_OR_COMMENT_BIT_SET.contains(contextType) + if (lbraceType.equals(JetTokens.LONG_TEMPLATE_ENTRY_START)) { + // KotlinTypedHandler insert paired brace in this case + return false; + } + + return JetTokens.WHITE_SPACE_OR_COMMENT_BIT_SET.contains(contextType) || contextType == JetTokens.SEMICOLON || contextType == JetTokens.COMMA || contextType == JetTokens.RPAR diff --git a/idea/src/org/jetbrains/jet/plugin/editor/KotlinTypedHandler.java b/idea/src/org/jetbrains/jet/plugin/editor/KotlinTypedHandler.java index b35d929710d..baedb696788 100644 --- a/idea/src/org/jetbrains/jet/plugin/editor/KotlinTypedHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/editor/KotlinTypedHandler.java @@ -57,6 +57,20 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { JetLtGtTypingUtils.handleJetAutoCloseLT(editor); return Result.STOP; } + + if (!(file instanceof JetFile) || !CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) { + return Result.CONTINUE; + } + if (c == '{') { + PsiDocumentManager.getInstance(project).commitAllDocuments(); + int offset = editor.getCaretModel().getOffset(); + PsiElement previousElement = file.findElementAt(offset - 1); + if (previousElement instanceof LeafPsiElement + && ((LeafPsiElement) previousElement).getElementType() == JetTokens.LONG_TEMPLATE_ENTRY_START) { + editor.getDocument().insertString(offset, "}"); + } + return Result.STOP; + } return Result.CONTINUE; } } diff --git a/idea/tests/org/jetbrains/jet/editor/TypedHandlerTest.java b/idea/tests/org/jetbrains/jet/editor/TypedHandlerTest.java index 5ac75e43c72..998a88535cf 100644 --- a/idea/tests/org/jetbrains/jet/editor/TypedHandlerTest.java +++ b/idea/tests/org/jetbrains/jet/editor/TypedHandlerTest.java @@ -26,6 +26,18 @@ public class TypedHandlerTest extends LightCodeInsightTestCase { checkResultByText("val x = \"${}\""); } + public void testTypeStringTemplateStartWithCloseBraceAfter() throws Exception { + configureFromFileText("a.kt", "fun foo() { \"$\" }"); + EditorTestUtil.performTypingAction(getEditor(), '{'); + checkResultByText("fun foo() { \"${}\" }"); + } + + public void testTypeStringTemplateStartBeforeString() throws Exception { + configureFromFileText("a.kt", "fun foo() { \"$something\" }"); + EditorTestUtil.performTypingAction(getEditor(), '{'); + checkResultByText("fun foo() { \"${}something\" }"); + } + public void testKT3575() throws Exception { configureFromFileText("a.kt", "val x = \"$]\""); EditorTestUtil.performTypingAction(getEditor(), '{');