Skip to content

Commit c4c1f42

Browse files
committed
Fix extract code when selecting the whole file
1 parent a963c39 commit c4c1f42

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

Core/Sources/XPCShared/Models.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,16 @@ func selectedCode(in selection: EditorContent.Selection, for lines: [String]) ->
6868
guard startPosition.line >= 0, startPosition.line < lines.count else { return "" }
6969
guard startPosition.character >= 0,
7070
startPosition.character < lines[startPosition.line].count else { return "" }
71-
guard endPosition.line >= 0, endPosition.line < lines.count else { return "" }
71+
guard endPosition.line >= 0,
72+
endPosition.line < lines.count
73+
|| (endPosition.line == lines.count && endPosition.character == -1)
74+
else { return "" }
7275
guard endPosition.line >= startPosition.line else { return "" }
73-
guard endPosition.character >= 0,
74-
endPosition.character < lines[endPosition.line].count else { return "" }
76+
guard endPosition.character >= -1 else { return "" }
77+
78+
if endPosition.line < lines.endIndex {
79+
guard endPosition.character < lines[endPosition.line].count else { return "" }
80+
}
7581

7682
var code = ""
7783
if startPosition.line == endPosition.line {
@@ -94,9 +100,11 @@ func selectedCode(in selection: EditorContent.Selection, for lines: [String]) ->
94100
}
95101
}
96102

97-
let endLine = lines[endPosition.line]
98-
let endIndex = endLine.index(endLine.startIndex, offsetBy: endPosition.character)
99-
code += String(endLine[...endIndex])
103+
if endPosition.character >= 0, endPosition.line < lines.endIndex {
104+
let endLine = lines[endPosition.line]
105+
let endIndex = endLine.index(endLine.startIndex, offsetBy: endPosition.character)
106+
code += String(endLine[...endIndex])
107+
}
100108
}
101109

102110
return code

0 commit comments

Comments
 (0)