Don't insert {} after 'else' and 'try' in infinished statements
This commit is contained in:
committed by
Nikolay Krasko
parent
5301f3eb46
commit
9e0e3aaebb
@@ -52,8 +52,16 @@ import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry;
|
|||||||
public class KotlinTypedHandler extends TypedHandlerDelegate {
|
public class KotlinTypedHandler extends TypedHandlerDelegate {
|
||||||
private final static TokenSet CONTROL_FLOW_EXPRESSIONS = TokenSet.create(
|
private final static TokenSet CONTROL_FLOW_EXPRESSIONS = TokenSet.create(
|
||||||
KtNodeTypes.IF,
|
KtNodeTypes.IF,
|
||||||
|
KtNodeTypes.ELSE,
|
||||||
KtNodeTypes.FOR,
|
KtNodeTypes.FOR,
|
||||||
KtNodeTypes.WHILE);
|
KtNodeTypes.WHILE,
|
||||||
|
KtNodeTypes.TRY);
|
||||||
|
|
||||||
|
private final static TokenSet SUPPRESS_AUTO_INSERT_CLOSE_BRACE_AFTER = TokenSet.create(
|
||||||
|
KtTokens.RPAR,
|
||||||
|
KtTokens.ELSE_KEYWORD,
|
||||||
|
KtTokens.TRY_KEYWORD
|
||||||
|
);
|
||||||
|
|
||||||
private boolean kotlinLTTyped;
|
private boolean kotlinLTTyped;
|
||||||
|
|
||||||
@@ -90,10 +98,12 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
|
|||||||
iterator.retreat();
|
iterator.retreat();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iterator.atEnd() || iterator.getTokenType() != KtTokens.RPAR) {
|
if (iterator.atEnd() || !(SUPPRESS_AUTO_INSERT_CLOSE_BRACE_AFTER.contains(iterator.getTokenType()))) {
|
||||||
return Result.CONTINUE;
|
return Result.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int tokenBeforeBraceOffset = iterator.getStart();
|
||||||
|
|
||||||
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
||||||
|
|
||||||
PsiElement leaf = file.findElementAt(offset);
|
PsiElement leaf = file.findElementAt(offset);
|
||||||
@@ -101,9 +111,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
|
|||||||
PsiElement parent = leaf.getParent();
|
PsiElement parent = leaf.getParent();
|
||||||
if (parent != null && CONTROL_FLOW_EXPRESSIONS.contains(parent.getNode().getElementType())) {
|
if (parent != null && CONTROL_FLOW_EXPRESSIONS.contains(parent.getNode().getElementType())) {
|
||||||
ASTNode nonWhitespaceSibling = FormatterUtil.getPreviousNonWhitespaceSibling(leaf.getNode());
|
ASTNode nonWhitespaceSibling = FormatterUtil.getPreviousNonWhitespaceSibling(leaf.getNode());
|
||||||
if (nonWhitespaceSibling != null && nonWhitespaceSibling.getText().equals(")")) {
|
if (nonWhitespaceSibling != null && nonWhitespaceSibling.getStartOffset() == tokenBeforeBraceOffset) {
|
||||||
// Check that ')' belongs to same parent
|
|
||||||
|
|
||||||
EditorModificationUtil.insertStringAtCaret(editor, "{", false, true);
|
EditorModificationUtil.insertStringAtCaret(editor, "{", false, true);
|
||||||
indentBrace(project, editor, '{');
|
indentBrace(project, editor, '{');
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,12 @@ import com.intellij.testFramework.LightCodeInsightTestCase
|
|||||||
import com.intellij.testFramework.LightPlatformCodeInsightTestCase
|
import com.intellij.testFramework.LightPlatformCodeInsightTestCase
|
||||||
|
|
||||||
public class TypedHandlerTest : LightCodeInsightTestCase() {
|
public class TypedHandlerTest : LightCodeInsightTestCase() {
|
||||||
val amp = '$'
|
val dollar = '$'
|
||||||
|
|
||||||
public fun testTypeStringTemplateStart(): Unit = doCharTypeTest(
|
public fun testTypeStringTemplateStart(): Unit = doCharTypeTest(
|
||||||
'{',
|
'{',
|
||||||
"""val x = "$<caret>" """,
|
"""val x = "$<caret>" """,
|
||||||
"""val x = "$amp{}" """
|
"""val x = "$dollar{}" """
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun testAutoIndentRightOpenBrace(): Unit = doCharTypeTest(
|
public fun testAutoIndentRightOpenBrace(): Unit = doCharTypeTest(
|
||||||
@@ -56,19 +56,19 @@ public class TypedHandlerTest : LightCodeInsightTestCase() {
|
|||||||
public fun testTypeStringTemplateStartWithCloseBraceAfter(): Unit = doCharTypeTest(
|
public fun testTypeStringTemplateStartWithCloseBraceAfter(): Unit = doCharTypeTest(
|
||||||
'{',
|
'{',
|
||||||
"""fun foo() { "$<caret>" }""",
|
"""fun foo() { "$<caret>" }""",
|
||||||
"""fun foo() { "$amp{}" }"""
|
"""fun foo() { "$dollar{}" }"""
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun testTypeStringTemplateStartBeforeString(): Unit = doCharTypeTest(
|
public fun testTypeStringTemplateStartBeforeString(): Unit = doCharTypeTest(
|
||||||
'{',
|
'{',
|
||||||
"""fun foo() { "$<caret>something" }""",
|
"""fun foo() { "$<caret>something" }""",
|
||||||
"""fun foo() { "$amp{}something" }"""
|
"""fun foo() { "$dollar{}something" }"""
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun testKT3575(): Unit = doCharTypeTest(
|
public fun testKT3575(): Unit = doCharTypeTest(
|
||||||
'{',
|
'{',
|
||||||
"""val x = "$<caret>]" """,
|
"""val x = "$<caret>]" """,
|
||||||
"""val x = "$amp{}]" """
|
"""val x = "$dollar{}]" """
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun testAutoCloseBraceInFunctionDeclaration(): Unit = doCharTypeTest(
|
public fun testAutoCloseBraceInFunctionDeclaration(): Unit = doCharTypeTest(
|
||||||
@@ -112,6 +112,54 @@ public class TypedHandlerTest : LightCodeInsightTestCase() {
|
|||||||
"}"
|
"}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedElseSurroundOnSameLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" if(true) {} else <caret>foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" if(true) {} else {foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedTryOnSameLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try <caret>foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try {foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedCatchOnSameLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try {} catch (e: Exception) <caret>foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try {} catch (e: Exception) {foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedFinallyOnSameLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try {} catch (e: Exception) finally <caret>foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try {} catch (e: Exception) finally {foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
public fun testDoNotAutoCloseBraceInUnfinishedWhileSurroundOnSameLine(): Unit = doCharTypeTest(
|
public fun testDoNotAutoCloseBraceInUnfinishedWhileSurroundOnSameLine(): Unit = doCharTypeTest(
|
||||||
'{',
|
'{',
|
||||||
|
|
||||||
@@ -154,6 +202,34 @@ public class TypedHandlerTest : LightCodeInsightTestCase() {
|
|||||||
"}"
|
"}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedElseSurroundOnOtherLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" if(true) {} else <caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" if(true) {} else {<caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedTryOnOtherLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try <caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try {<caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
public fun testDoNotAutoCloseBraceInUnfinishedIfSurroundOnNewLine(): Unit = doCharTypeTest(
|
public fun testDoNotAutoCloseBraceInUnfinishedIfSurroundOnNewLine(): Unit = doCharTypeTest(
|
||||||
'{',
|
'{',
|
||||||
|
|
||||||
@@ -170,6 +246,38 @@ public class TypedHandlerTest : LightCodeInsightTestCase() {
|
|||||||
"}"
|
"}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedElseSurroundOnNewLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" if(true) {} else\n" +
|
||||||
|
" <caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" if(true) {} else\n" +
|
||||||
|
" {<caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
|
public fun testDoNotAutoCloseBraceInUnfinishedTryOnNewLine(): Unit = doCharTypeTest(
|
||||||
|
'{',
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try\n" +
|
||||||
|
" <caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}",
|
||||||
|
|
||||||
|
"fun foo() {\n" +
|
||||||
|
" try\n" +
|
||||||
|
" {<caret>\n" +
|
||||||
|
" foo()\n" +
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
public fun testAutoCloseBraceInsideFor(): Unit = doCharTypeTest(
|
public fun testAutoCloseBraceInsideFor(): Unit = doCharTypeTest(
|
||||||
'{',
|
'{',
|
||||||
|
|
||||||
@@ -224,8 +332,8 @@ public class TypedHandlerTest : LightCodeInsightTestCase() {
|
|||||||
|
|
||||||
public fun testAutoInsertParenInStringLiteral(): Unit = doCharTypeTest(
|
public fun testAutoInsertParenInStringLiteral(): Unit = doCharTypeTest(
|
||||||
'(',
|
'(',
|
||||||
"""fun f() { println("$amp{f<caret>}") }""",
|
"""fun f() { println("$dollar{f<caret>}") }""",
|
||||||
"""fun f() { println("$amp{f(<caret>)}") }"""
|
"""fun f() { println("$dollar{f(<caret>)}") }"""
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun testAutoInsertParenInCode(): Unit = doCharTypeTest(
|
public fun testAutoInsertParenInCode(): Unit = doCharTypeTest(
|
||||||
@@ -257,9 +365,9 @@ public class TypedHandlerTest : LightCodeInsightTestCase() {
|
|||||||
|
|
||||||
public fun testSplitStringByEnter_BeforeSubstitution(): Unit = doCharTypeTest(
|
public fun testSplitStringByEnter_BeforeSubstitution(): Unit = doCharTypeTest(
|
||||||
'\n',
|
'\n',
|
||||||
"""val s = "foo<caret>${amp}bar"""",
|
"""val s = "foo<caret>${dollar}bar"""",
|
||||||
"val s = \"foo\" +\n" +
|
"val s = \"foo\" +\n" +
|
||||||
" \"${amp}bar\""
|
" \"${dollar}bar\""
|
||||||
)
|
)
|
||||||
|
|
||||||
public fun testSplitStringByEnter_AddParentheses(): Unit = doCharTypeTest(
|
public fun testSplitStringByEnter_AddParentheses(): Unit = doCharTypeTest(
|
||||||
|
|||||||
Reference in New Issue
Block a user