@@ -42,39 +42,7 @@ public struct EditorContent: Codable {
4242 public var usesTabsForIndentation : Bool
4343
4444 public func selectedCode( in selection: Selection ) -> String {
45- let startPosition = selection. start
46- let endPosition = selection. end
47-
48- guard startPosition. line >= 0 , startPosition. line < lines. count else { return " " }
49- guard endPosition. line >= 0 , endPosition. line < lines. count else { return " " }
50- guard endPosition. line >= startPosition. line else { return " " }
51-
52- var code = " "
53- if startPosition. line == endPosition. line {
54- let line = lines [ startPosition. line]
55- let startIndex = line. index ( line. startIndex, offsetBy: startPosition. character)
56- let endIndex = line. index ( line. startIndex, offsetBy: endPosition. character)
57- code = String ( line [ startIndex... endIndex] )
58- } else {
59- let startLine = lines [ startPosition. line]
60- let startIndex = startLine. index (
61- startLine. startIndex,
62- offsetBy: startPosition. character
63- )
64- code += String ( startLine [ startIndex... ] )
65-
66- if startPosition. line + 1 < endPosition. line {
67- for line in lines [ startPosition. line + 1 ... endPosition. line - 1 ] {
68- code += line
69- }
70- }
71-
72- let endLine = lines [ endPosition. line]
73- let endIndex = endLine. index ( endLine. startIndex, offsetBy: endPosition. character)
74- code += String ( endLine [ ... endIndex] )
75- }
76-
77- return code
45+ return XPCShared . selectedCode ( in: selection, for: lines)
7846 }
7947}
8048
@@ -89,3 +57,47 @@ public struct UpdatedContent: Codable {
8957 public var newCursor : CursorPosition ?
9058 public var modifications : [ Modification ]
9159}
60+
61+ func selectedCode( in selection: EditorContent . Selection , for lines: [ String ] ) -> String {
62+ let startPosition = selection. start
63+ var endPosition = CursorPosition (
64+ line: selection. end. line,
65+ character: selection. end. character - 1
66+ )
67+
68+ guard startPosition. line >= 0 , startPosition. line < lines. count else { return " " }
69+ guard startPosition. character >= 0 ,
70+ startPosition. character < lines [ startPosition. line] . count else { return " " }
71+ guard endPosition. line >= 0 , endPosition. line < lines. count else { return " " }
72+ guard endPosition. line >= startPosition. line else { return " " }
73+ guard endPosition. character >= 0 ,
74+ endPosition. character < lines [ endPosition. line] . count else { return " " }
75+
76+ var code = " "
77+ if startPosition. line == endPosition. line {
78+ guard endPosition. character >= startPosition. character else { return " " }
79+ let line = lines [ startPosition. line]
80+ let startIndex = line. index ( line. startIndex, offsetBy: startPosition. character)
81+ let endIndex = line. index ( line. startIndex, offsetBy: endPosition. character)
82+ code = String ( line [ startIndex... endIndex] )
83+ } else {
84+ let startLine = lines [ startPosition. line]
85+ let startIndex = startLine. index (
86+ startLine. startIndex,
87+ offsetBy: startPosition. character
88+ )
89+ code += String ( startLine [ startIndex... ] )
90+
91+ if startPosition. line + 1 < endPosition. line {
92+ for line in lines [ startPosition. line + 1 ... endPosition. line - 1 ] {
93+ code += line
94+ }
95+ }
96+
97+ let endLine = lines [ endPosition. line]
98+ let endIndex = endLine. index ( endLine. startIndex, offsetBy: endPosition. character)
99+ code += String ( endLine [ ... endIndex] )
100+ }
101+
102+ return code
103+ }
0 commit comments