Skip to content

Commit 89839a3

Browse files
Fix incorrect multiline autoindent
1 parent 315f988 commit 89839a3

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationAutoEditStrategy.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,30 @@ && isFollowedBy(doc, command.offset, charPair.close))) {
163163

164164
final var newIndent = registry.getGoodIndentForLine(doc, lineIndex, contentType, IIndentConverter.of(cursorCfg));
165165
if (newIndent != null) {
166+
final var normalizedIndent = cursorCfg.normalizeIndentation(newIndent);
166167
final var lineStartOffset = doc.getLineOffset(lineIndex);
167-
168-
// check if the content was pasted into a line while the cursor was not at the beginning of the line
169-
// but inside or at the end of an existing line indentation
170168
final var offsetInLine = command.offset - lineStartOffset;
171-
if (offsetInLine > 0 && doc.get(lineStartOffset, offsetInLine).isBlank()) {
172-
command.offset = lineStartOffset;
173-
command.length += offsetInLine;
169+
final int firstNewline = command.text.indexOf('\n');
170+
171+
if (firstNewline >= 0) {
172+
final var firstLine = command.text.substring(0, firstNewline + 1);
173+
final var reindentedRest = TextUtils.replaceIndent(
174+
command.text.substring(firstNewline + 1), cursorCfg.indentSize, normalizedIndent, false);
175+
176+
if (offsetInLine > 0 && !doc.get(lineStartOffset, offsetInLine).isBlank()) {
177+
command.text = firstLine + reindentedRest;
178+
} else {
179+
if (offsetInLine > 0) {
180+
command.offset = lineStartOffset;
181+
command.length += offsetInLine;
182+
}
183+
command.text = normalizedIndent + firstLine.stripLeading() + reindentedRest;
184+
}
185+
} else {
186+
command.text = TextUtils
187+
.replaceIndent(command.text, cursorCfg.indentSize, normalizedIndent, false)
188+
.toString();
174189
}
175-
command.text = TextUtils.replaceIndent(command.text, cursorCfg.indentSize,
176-
cursorCfg.normalizeIndentation(newIndent), false).toString();
177190
command.shiftsCaret = true;
178191
}
179192
}

0 commit comments

Comments
 (0)