Skip to content

Commit ae670cb

Browse files
committed
Add cursorOffset to editor content and suggestion request
1 parent b57fc4b commit ae670cb

File tree

9 files changed

+30
-8
lines changed

9 files changed

+30
-8
lines changed

Core/Sources/PromptToCodeService/OpenAIPromptToCodeService.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
3737
content: source.content,
3838
lines: source.lines,
3939
selections: [source.range],
40-
cursorPosition: .outOfScope,
40+
cursorPosition: .outOfScope,
41+
cursorOffset: -1,
4142
lineAnnotations: []
4243
),
4344
selectedContent: code,

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct PseudoCommandHandler {
2323
lines: [],
2424
uti: "",
2525
cursorPosition: .outOfScope,
26+
cursorOffset: -1,
2627
selections: [],
2728
tabSize: 0,
2829
indentSize: 0,
@@ -37,6 +38,7 @@ struct PseudoCommandHandler {
3738
lines: [],
3839
uti: "",
3940
cursorPosition: .outOfScope,
41+
cursorOffset: -1,
4042
selections: [],
4143
tabSize: 0,
4244
indentSize: 0,
@@ -128,6 +130,7 @@ struct PseudoCommandHandler {
128130
lines: [],
129131
uti: "",
130132
cursorPosition: .outOfScope,
133+
cursorOffset: -1,
131134
selections: [],
132135
tabSize: 0,
133136
indentSize: 0,
@@ -148,6 +151,7 @@ struct PseudoCommandHandler {
148151
lines: [],
149152
uti: "",
150153
cursorPosition: .outOfScope,
154+
cursorOffset: -1,
151155
selections: [],
152156
tabSize: 0,
153157
indentSize: 0,
@@ -206,7 +210,7 @@ struct PseudoCommandHandler {
206210
guard let focusElement = application.focusedElement,
207211
focusElement.description == "Source Editor"
208212
else { return }
209-
guard let (content, lines, _, cursorPosition) = await getFileContent(sourceEditor: nil)
213+
guard let (content, lines, _, cursorPosition, cursorOffset) = await getFileContent(sourceEditor: nil)
210214
else {
211215
PresentInWindowSuggestionPresenter()
212216
.presentErrorMessage("Unable to get file content.")
@@ -219,6 +223,7 @@ struct PseudoCommandHandler {
219223
lines: lines,
220224
uti: "",
221225
cursorPosition: cursorPosition,
226+
cursorOffset: cursorOffset,
222227
selections: [],
223228
tabSize: 0,
224229
indentSize: 0,
@@ -261,7 +266,7 @@ struct PseudoCommandHandler {
261266
guard let focusElement = application.focusedElement,
262267
focusElement.description == "Source Editor"
263268
else { return }
264-
guard let (content, lines, _, cursorPosition) = await getFileContent(sourceEditor: nil)
269+
guard let (content, lines, _, cursorPosition, cursorOffset) = await getFileContent(sourceEditor: nil)
265270
else {
266271
PresentInWindowSuggestionPresenter()
267272
.presentErrorMessage("Unable to get file content.")
@@ -274,6 +279,7 @@ struct PseudoCommandHandler {
274279
lines: lines,
275280
uti: "",
276281
cursorPosition: cursorPosition,
282+
cursorOffset: cursorOffset,
277283
selections: [],
278284
tabSize: 0,
279285
indentSize: 0,
@@ -361,7 +367,8 @@ extension PseudoCommandHandler {
361367
content: String,
362368
lines: [String],
363369
selections: [CursorRange],
364-
cursorPosition: CursorPosition
370+
cursorPosition: CursorPosition,
371+
cursorOffset: Int
365372
)?
366373
{
367374
guard let xcode = ActiveApplicationMonitor.shared.activeXcode
@@ -374,7 +381,7 @@ extension PseudoCommandHandler {
374381
let content = focusElement.value
375382
let split = content.breakLines(appendLineBreakToLastLine: false)
376383
let range = convertRangeToCursorRange(selectionRange, in: content)
377-
return (content, split, [range], range.start)
384+
return (content, split, [range], range.start, selectionRange.lowerBound)
378385
}
379386

380387
func getFileURL() async -> URL? {
@@ -410,6 +417,7 @@ extension PseudoCommandHandler {
410417
lines: content.lines,
411418
uti: uti,
412419
cursorPosition: content.cursorPosition,
420+
cursorOffset: content.cursorOffset,
413421
selections: content.selections.map {
414422
.init(start: $0.start, end: $0.end)
415423
},

EditorExtension/Helpers.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ extension EditorContent {
4747
uti: buffer.contentUTI,
4848
cursorPosition: ((buffer.selections.lastObject as? XCSourceTextRange)?.end).map {
4949
CursorPosition(line: $0.line, character: $0.column)
50-
} ?? CursorPosition(line: 0, character: 0),
50+
} ?? CursorPosition(line: 0, character: 0),
51+
cursorOffset: -1,
5152
selections: buffer.selections.map {
5253
let sl = ($0 as? XCSourceTextRange)?.start.line ?? 0
5354
let sc = ($0 as? XCSourceTextRange)?.start.column ?? 0

Pro

Submodule Pro updated from 1795e32 to 34f7970

Tool/Sources/SuggestionModel/EditorInformation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public struct EditorInformation {
1717
public var selections: [CursorRange]
1818
/// The cursor position of the source editor.
1919
public var cursorPosition: CursorPosition
20+
/// The cursor position as offset.
21+
public var cursorOffset: Int
2022
/// Line annotations of the source editor.
2123
public var lineAnnotations: [LineAnnotation]
2224

@@ -41,12 +43,14 @@ public struct EditorInformation {
4143
lines: [String],
4244
selections: [CursorRange],
4345
cursorPosition: CursorPosition,
46+
cursorOffset: Int,
4447
lineAnnotations: [String]
4548
) {
4649
self.content = content
4750
self.lines = lines
4851
self.selections = selections
4952
self.cursorPosition = cursorPosition
53+
self.cursorOffset = cursorOffset
5054
self.lineAnnotations = lineAnnotations.map(EditorInformation.parseLineAnnotation)
5155
}
5256
}

Tool/Sources/SuggestionProvider/SuggestionProvider.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public struct SuggestionRequest {
1111
public var content: String
1212
public var lines: [String]
1313
public var cursorPosition: CursorPosition
14+
public var cursorOffset: Int
1415
public var tabSize: Int
1516
public var indentSize: Int
1617
public var usesTabsForIndentation: Bool
@@ -23,6 +24,7 @@ public struct SuggestionRequest {
2324
content: String,
2425
lines: [String],
2526
cursorPosition: CursorPosition,
27+
cursorOffset: Int,
2628
tabSize: Int,
2729
indentSize: Int,
2830
usesTabsForIndentation: Bool,
@@ -34,6 +36,7 @@ public struct SuggestionRequest {
3436
self.content = content
3537
self.lines = lines
3638
self.cursorPosition = cursorPosition
39+
self.cursorOffset = cursorOffset
3740
self.tabSize = tabSize
3841
self.indentSize = indentSize
3942
self.usesTabsForIndentation = usesTabsForIndentation

Tool/Sources/WorkspaceSuggestionService/Workspace+SuggestionService.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public extension Workspace {
6060
relativePath: fileURL.path.replacingOccurrences(of: projectRootURL.path, with: ""),
6161
content: editor.lines.joined(separator: ""),
6262
lines: editor.lines,
63-
cursorPosition: editor.cursorPosition,
63+
cursorPosition: editor.cursorPosition,
64+
cursorOffset: editor.cursorOffset,
6465
tabSize: editor.tabSize,
6566
indentSize: editor.indentSize,
6667
usesTabsForIndentation: editor.usesTabsForIndentation,

Tool/Sources/XPCShared/Models.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public struct EditorContent: Codable {
1717
lines: [String],
1818
uti: String,
1919
cursorPosition: CursorPosition,
20+
cursorOffset: Int,
2021
selections: [Selection],
2122
tabSize: Int,
2223
indentSize: Int,
@@ -26,6 +27,7 @@ public struct EditorContent: Codable {
2627
self.lines = lines
2728
self.uti = uti
2829
self.cursorPosition = cursorPosition
30+
self.cursorOffset = cursorOffset
2931
self.selections = selections
3032
self.tabSize = tabSize
3133
self.indentSize = indentSize
@@ -37,6 +39,7 @@ public struct EditorContent: Codable {
3739
public var lines: [String]
3840
public var uti: String
3941
public var cursorPosition: CursorPosition
42+
public var cursorOffset: Int
4043
public var selections: [Selection]
4144
public var tabSize: Int
4245
public var indentSize: Int

Tool/Sources/XcodeInspector/SourceEditor.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class SourceEditor {
4949
lines: lines,
5050
selections: selections,
5151
cursorPosition: selections.first?.start ?? .outOfScope,
52+
cursorOffset: selectionRange?.lowerBound ?? 0,
5253
lineAnnotations: lineAnnotations
5354
)
5455
}

0 commit comments

Comments
 (0)