Skip to content

Commit 0ee5824

Browse files
committed
Support sending back multiple selections to editor extension
1 parent ffd9d43 commit 0ee5824

4 files changed

Lines changed: 26 additions & 20 deletions

File tree

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ extension PseudoCommandHandler {
459459

460460
// recover selection range
461461

462-
if let selection = result.newSelection {
462+
if let selection = result.newSelections.first {
463463
var range = SourceEditor.convertCursorRangeToRange(selection, in: result.content)
464464
if let value = AXValueCreate(.cfRange, &range) {
465465
AXUIElementSetAttributeValue(

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
241241

242242
return .init(
243243
content: String(lines.joined(separator: "")),
244-
newSelection: {
245-
if ranges.isEmpty {
246-
.init(start: cursorPosition, end: cursorPosition)
247-
} else {
248-
ranges.last
249-
}
250-
}(),
244+
newSelections: ranges,
251245
modifications: extraInfo.modifications
252246
)
253247
}

EditorExtension/Helpers.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import SuggestionBasic
21
import Foundation
2+
import SuggestionBasic
33
import XcodeKit
44
import XPCShared
55

@@ -19,16 +19,21 @@ extension XCSourceEditorCommandInvocation {
1919
}
2020

2121
func accept(_ updatedContent: UpdatedContent) {
22-
if let newSelection = updatedContent.newSelection {
22+
if !updatedContent.newSelections.isEmpty {
2323
mutateCompleteBuffer(
2424
modifications: updatedContent.modifications,
2525
restoringSelections: false
2626
)
2727
buffer.selections.removeAllObjects()
28-
buffer.selections.add(XCSourceTextRange(
29-
start: .init(line: newSelection.start.line, column: newSelection.start.character),
30-
end: .init(line: newSelection.end.line, column: newSelection.end.character)
31-
))
28+
for newSelection in updatedContent.newSelections {
29+
buffer.selections.add(XCSourceTextRange(
30+
start: .init(
31+
line: newSelection.start.line,
32+
column: newSelection.start.character
33+
),
34+
end: .init(line: newSelection.end.line, column: newSelection.end.character)
35+
))
36+
}
3237
} else {
3338
mutateCompleteBuffer(
3439
modifications: updatedContent.modifications,
@@ -47,17 +52,17 @@ extension EditorContent {
4752
uti: buffer.contentUTI,
4853
cursorPosition: ((buffer.selections.lastObject as? XCSourceTextRange)?.end).map {
4954
CursorPosition(line: $0.line, character: $0.column)
50-
} ?? CursorPosition(line: 0, character: 0),
55+
} ?? CursorPosition(line: 0, character: 0),
5156
cursorOffset: -1,
5257
selections: buffer.selections.map {
5358
let sl = ($0 as? XCSourceTextRange)?.start.line ?? 0
5459
let sc = ($0 as? XCSourceTextRange)?.start.column ?? 0
5560
let el = ($0 as? XCSourceTextRange)?.end.line ?? 0
5661
let ec = ($0 as? XCSourceTextRange)?.end.column ?? 0
57-
62+
5863
return Selection(
59-
start: CursorPosition( line: sl, character: sc ),
60-
end: CursorPosition( line: el, character: ec )
64+
start: CursorPosition(line: sl, character: sc),
65+
end: CursorPosition(line: el, character: ec)
6166
)
6267
},
6368
tabSize: buffer.tabWidth,
@@ -96,3 +101,4 @@ extension Task where Failure == Error {
96101
private struct TimeoutError: LocalizedError {
97102
var errorDescription: String? = "Task timed out before completion"
98103
}
104+

Tool/Sources/XPCShared/Models.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@ public struct EditorContent: Codable {
5353
public struct UpdatedContent: Codable {
5454
public init(content: String, newSelection: CursorRange? = nil, modifications: [Modification]) {
5555
self.content = content
56-
self.newSelection = newSelection
56+
self.newSelections = if let newSelection { [newSelection] } else { [] }
57+
self.modifications = modifications
58+
}
59+
60+
public init(content: String, newSelections: [CursorRange], modifications: [Modification]) {
61+
self.content = content
62+
self.newSelections = newSelections
5763
self.modifications = modifications
5864
}
5965

6066
public var content: String
61-
public var newSelection: CursorRange?
67+
public var newSelections: [CursorRange]
6268
public var modifications: [Modification]
6369
}
6470

0 commit comments

Comments
 (0)