Skip to content

Commit 0551659

Browse files
committed
Don't indent to unrelated closing quote (KT-17894)
#KT-17894 Fixed
1 parent e211980 commit 0551659

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

idea/src/org/jetbrains/kotlin/idea/editor/KotlinMultilineStringEnterHandler.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
4545
class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() {
4646
private var wasInMultilineString: Boolean = false
4747
private var whiteSpaceAfterCaret: String = ""
48+
private var isInBrace = false
4849

4950
override fun preprocessEnter(
5051
file: PsiFile, editor: Editor, caretOffset: Ref<Int>, caretAdvance: Ref<Int>, dataContext: DataContext,
@@ -70,7 +71,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() {
7071

7172
val ch1 = text[offset - 1]
7273
val ch2 = text[offset]
73-
val isInBrace = (ch1 == '(' && ch2 == ')') || (ch1 == '{' && ch2 == '}')
74+
isInBrace = (ch1 == '(' && ch2 == ')') || (ch1 == '{' && ch2 == '}')
7475
if (!isInBrace || !CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) {
7576
return Result.Continue
7677
}
@@ -118,11 +119,8 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() {
118119
val wasSingleLine = literal.text.indexOf("\n") == literal.text.lastIndexOf("\n") // Only one '\n' in the string after insertion
119120
val lines = literal.text.split("\n")
120121

121-
val inBraces = (prevLine.endsWith("{") && nextLine.trim().startsWith("}")) ||
122-
(prevLine.endsWith("(") && nextLine.trim().startsWith(")"))
123-
124122
val literalOffset = literal.textRange.startOffset
125-
if (wasSingleLine || (lines.size == 3 && inBraces)) {
123+
if (wasSingleLine || (lines.size == 3 && isInBrace)) {
126124
val shouldUseTrimIndent = hasTrimIndentCallInChain || (marginChar == null && lines.first().trim() == MULTILINE_QUOTE)
127125
val newMarginChar: Char? = if (shouldUseTrimIndent) null else (marginChar ?: DEFAULT_TRIM_MARGIN_CHAR)
128126

@@ -141,7 +139,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() {
141139
caretMarker.isGreedyToRight = true
142140
}
143141

144-
if (inBraces) {
142+
if (isInBrace) {
145143
// Move closing bracket under same indent
146144
forceIndent(caretOffset() + 1, indentSize, newMarginChar, document, settings)
147145
}
@@ -175,7 +173,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() {
175173

176174
if (marginCharToInsert == null || !currentLine.trimStart().startsWith(marginCharToInsert)) {
177175
val indentLength = when {
178-
inBraces -> settings.indentLength(nextLine)
176+
isInBrace -> settings.indentLength(nextLine)
179177
!isPrevLineFirst -> settings.indentLength(prevLine)
180178
else -> settings.indentLength(prevLine) + settings.marginIndent
181179
}
@@ -192,7 +190,7 @@ class KotlinMultilineStringEnterHandler : EnterHandlerDelegateAdapter() {
192190
// Insert same indent after margin char that previous line has
193191
document.insertString(caretOffset(), wsAfterMargin)
194192

195-
if (inBraces) {
193+
if (isInBrace) {
196194
val nextLineOffset = document.getLineStartOffset(prevLineNumber + 2)
197195
forceIndent(nextLineOffset, 0, null, document, settings)
198196
document.insertString(nextLineOffset, (marginCharToInsert?.toString() ?: "") + wsAfterMargin)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fun test = """
2+
{
3+
abc
4+
abc {<caret>
5+
}
6+
""".trimIndent()
7+
//-----
8+
fun test = """
9+
{
10+
abc
11+
abc {
12+
<caret>
13+
}
14+
""".trimIndent()

idea/tests/org/jetbrains/kotlin/idea/editor/MultiLineStringIndentTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ public void testDontInsertTrimMargin4() throws Exception {
9898
doTest(fileName);
9999
}
100100

101+
@TestMetadata("enterAfterOpenningBrace.kt")
102+
public void testEnterAfterOpenningBrace() throws Exception {
103+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/editor/enterHandler/multilineString/spaces/enterAfterOpenningBrace.kt");
104+
doTest(fileName);
105+
}
106+
101107
@TestMetadata("enterBeforeMarginChar.kt")
102108
public void testEnterBeforeMarginChar() throws Exception {
103109
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/editor/enterHandler/multilineString/spaces/enterBeforeMarginChar.kt");

0 commit comments

Comments
 (0)