From d0d583e8ee50c90d7990d03398088999b37c1beb Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 14 Dec 2016 15:19:59 +0300 Subject: [PATCH] Do indent adjustment on the fly only for the first element in the line (KT-15123) Don't interfere spacing for dot in range operator. #KT-15123 Fixed --- .../idea/editor/KotlinTypedHandler.java | 26 ++++++++++++++----- .../kotlin/idea/editor/TypedHandlerTest.kt | 12 +++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java index a50f7deedba..92e1d2bb845 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java @@ -33,20 +33,21 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Condition; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.TokenType; +import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.formatter.FormatterUtil; import com.intellij.psi.impl.source.tree.LeafPsiElement; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; +import com.intellij.psi.util.PsiTreeUtil; 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.*; +import org.jetbrains.kotlin.psi.KtClassOrObject; +import org.jetbrains.kotlin.psi.KtFile; +import org.jetbrains.kotlin.psi.KtQualifiedExpression; +import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry; public class KotlinTypedHandler extends TypedHandlerDelegate { private final static TokenSet CONTROL_FLOW_EXPRESSIONS = TokenSet.create( @@ -296,12 +297,25 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { 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) { + + // Should be applied only if there's nothing but the whitespace in line before the element + PsiElement prevLeaf = PsiTreeUtil.prevLeaf(currElement); + if (!(prevLeaf instanceof PsiWhiteSpace && prevLeaf.getText().contains("\n"))) { + return false; + } + PsiElement parent = currElement.getParent(); if (parent != null && kclass.isInstance(parent)) { - CodeStyleManager.getInstance(project).adjustLineIndent(file, offset - currElement.getText().length()); + int curElementLength = currElement.getText().length(); + if (offset < curElementLength) return false; + + CodeStyleManager.getInstance(project).adjustLineIndent(file, offset - curElementLength); + return true; } } diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt index bcac7e20e9b..d0e26ebcb0e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt @@ -545,6 +545,18 @@ class TypedHandlerTest : LightCodeInsightTestCase() { """) } + fun testSpaceAroundRange() { + doCharTypeTest( + '.', + """ + | val test = 1 + """, + """ + | val test = 1 . + """ + ) + } + fun testMoveThroughGT() { LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List>>") EditorTestUtil.performTypingAction(LightPlatformCodeInsightTestCase.getEditor(), '>')