Auto-indent line with chain-call continuation

(cherry picked from commit fdb4120)
This commit is contained in:
Nikolay Krasko
2016-10-19 19:14:41 +03:00
committed by Nikolay Krasko
parent 3cc3ad2b9e
commit 2b231f7e31
2 changed files with 50 additions and 11 deletions
@@ -46,9 +46,7 @@ import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.KtNodeTypes;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.KtClassOrObject;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry;
import org.jetbrains.kotlin.psi.*;
public class KotlinTypedHandler extends TypedHandlerDelegate {
private final static TokenSet CONTROL_FLOW_EXPRESSIONS = TokenSet.create(
@@ -230,8 +228,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
LtGtTypingUtils.handleKotlinAutoCloseLT(editor);
return Result.STOP;
}
if (c == '{' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
else if (c == '{' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
PsiDocumentManager.getInstance(project).commitAllDocuments();
int offset = editor.getCaretModel().getOffset();
@@ -242,9 +239,13 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
return Result.STOP;
}
}
if (c == ':') {
if (autoIndentCase(editor, project, file)) {
else if (c == ':') {
if (autoIndentCase(editor, project, file, KtClassOrObject.class)) {
return Result.STOP;
}
}
else if (c == '.') {
if (autoIndentCase(editor, project, file, KtQualifiedExpression.class)) {
return Result.STOP;
}
}
@@ -293,14 +294,14 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
}
}
private static boolean autoIndentCase(Editor editor, Project project, PsiFile file) {
private static boolean autoIndentCase(Editor editor, Project project, PsiFile file, Class<?> kclass) {
int offset = editor.getCaretModel().getOffset();
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiElement currElement = file.findElementAt(offset - 1);
if (currElement != null) {
PsiElement parent = currElement.getParent();
if (parent != null && parent instanceof KtClassOrObject) {
CodeStyleManager.getInstance(project).adjustLineIndent(file, offset - 1);
if (parent != null && kclass.isInstance(parent)) {
CodeStyleManager.getInstance(project).adjustLineIndent(file, offset - currElement.getText().length());
return true;
}
}
@@ -507,6 +507,44 @@ class TypedHandlerTest : LightCodeInsightTestCase() {
""")
}
fun testChainCallContinueWithDot() {
doCharTypeTest(
'.',
"""
|class Test{ fun test() = this }
|fun some() {
| Test()
| <caret>
|}
""",
"""
|class Test{ fun test() = this }
|fun some() {
| Test()
| .<caret>
|}
""")
}
fun testChainCallContinueWithSafeCall() {
doCharTypeTest(
'.',
"""
|class Test{ fun test() = this }
|fun some() {
| Test()
| ?<caret>
|}
""",
"""
|class Test{ fun test() = this }
|fun some() {
| Test()
| ?.<caret>
|}
""")
}
fun testMoveThroughGT() {
LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List<Set<Int<caret>>>")
EditorTestUtil.performTypingAction(LightPlatformCodeInsightTestCase.getEditor(), '>')