Auto-indent line with chain-call continuation
(cherry picked from commit fdb4120)
This commit is contained in:
committed by
Nikolay Krasko
parent
3cc3ad2b9e
commit
2b231f7e31
@@ -46,9 +46,7 @@ import com.intellij.util.text.CharArrayUtil;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.kotlin.KtNodeTypes;
|
import org.jetbrains.kotlin.KtNodeTypes;
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||||
import org.jetbrains.kotlin.psi.KtClassOrObject;
|
import org.jetbrains.kotlin.psi.*;
|
||||||
import org.jetbrains.kotlin.psi.KtFile;
|
|
||||||
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(
|
||||||
@@ -230,8 +228,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
|
|||||||
LtGtTypingUtils.handleKotlinAutoCloseLT(editor);
|
LtGtTypingUtils.handleKotlinAutoCloseLT(editor);
|
||||||
return Result.STOP;
|
return Result.STOP;
|
||||||
}
|
}
|
||||||
|
else if (c == '{' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
|
||||||
if (c == '{' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
|
|
||||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||||
|
|
||||||
int offset = editor.getCaretModel().getOffset();
|
int offset = editor.getCaretModel().getOffset();
|
||||||
@@ -242,9 +239,13 @@ public class KotlinTypedHandler extends TypedHandlerDelegate {
|
|||||||
return Result.STOP;
|
return Result.STOP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (c == ':') {
|
||||||
if (c == ':') {
|
if (autoIndentCase(editor, project, file, KtClassOrObject.class)) {
|
||||||
if (autoIndentCase(editor, project, file)) {
|
return Result.STOP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '.') {
|
||||||
|
if (autoIndentCase(editor, project, file, KtQualifiedExpression.class)) {
|
||||||
return Result.STOP;
|
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();
|
int offset = editor.getCaretModel().getOffset();
|
||||||
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
|
||||||
PsiElement currElement = file.findElementAt(offset - 1);
|
PsiElement currElement = file.findElementAt(offset - 1);
|
||||||
if (currElement != null) {
|
if (currElement != null) {
|
||||||
PsiElement parent = currElement.getParent();
|
PsiElement parent = currElement.getParent();
|
||||||
if (parent != null && parent instanceof KtClassOrObject) {
|
if (parent != null && kclass.isInstance(parent)) {
|
||||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, offset - 1);
|
CodeStyleManager.getInstance(project).adjustLineIndent(file, offset - currElement.getText().length());
|
||||||
return true;
|
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() {
|
fun testMoveThroughGT() {
|
||||||
LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List<Set<Int<caret>>>")
|
LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List<Set<Int<caret>>>")
|
||||||
EditorTestUtil.performTypingAction(LightPlatformCodeInsightTestCase.getEditor(), '>')
|
EditorTestUtil.performTypingAction(LightPlatformCodeInsightTestCase.getEditor(), '>')
|
||||||
|
|||||||
Reference in New Issue
Block a user