Skip to content

Commit 477f916

Browse files
committed
Include preceding spaces if the selection starts at first non-space character and the selection is single-line
1 parent bdc88ce commit 477f916

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,21 @@ extension WindowBaseCommandHandler {
323323
guard var selection = editor.selections.last,
324324
selection.start != selection.end
325325
else { return ("", .cursor(editor.cursorPosition)) }
326-
if selection.start.line != selection.end.line {
326+
327+
let isMultipleLine = selection.start.line != selection.end.line
328+
let isSpaceOnlyBeforeStartPositionOnTheSameLine = {
329+
guard selection.start.line >= 0, selection.start.line < editor.lines.count else {
330+
return false
331+
}
332+
let line = editor.lines[selection.start.line]
333+
guard selection.start.character > 0, selection.start.character < line.count else {
334+
return false
335+
}
336+
let substring = line[line.startIndex..<line.index(line.startIndex, offsetBy: selection.start.character)]
337+
return substring.allSatisfy({ $0.isWhitespace })
338+
}()
339+
340+
if isMultipleLine || isSpaceOnlyBeforeStartPositionOnTheSameLine {
327341
// when there are multiple lines start from char 0 so that it can keep the
328342
// indentation.
329343
selection.start = .init(line: selection.start.line, character: 0)

0 commit comments

Comments
 (0)