Skip to content

Commit ddd23b4

Browse files
committed
Update UpdatedContent to return a selection range
1 parent 2633668 commit ddd23b4

File tree

7 files changed

+55
-38
lines changed

7 files changed

+55
-38
lines changed

Core/Sources/CopilotModel/ExportedFromLSP.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ public extension CursorPosition {
99

1010
public extension CursorRange {
1111
static var outOfScope: CursorRange { .init(start: .outOfScope, end: .outOfScope) }
12+
static func cursor(_ position: CursorPosition) -> CursorRange {
13+
return .init(start: position, end: position)
14+
}
1215
}

Core/Sources/Service/SuggestionCommandHandler/CommentBaseCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct CommentBaseCommandHandler: SuggestionCommandHandler {
103103

104104
return .init(
105105
content: String(lines.joined(separator: "")),
106-
newCursor: cursorPosition,
106+
newSelection: .cursor(cursorPosition),
107107
modifications: extraInfo.modifications
108108
)
109109
}

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ struct PseudoCommandHandler {
108108
.presentErrorMessage("Fail to set editor content.")
109109
}
110110

111-
if let cursor = result.newCursor {
111+
#warning("MUSTDO: handle range")
112+
if let cursor = result.newSelection?.start {
112113
var range = convertCursorPositionToRange(cursor, in: result.content)
113114
if let value = AXValueCreate(.cfRange, &range) {
114115
AXUIElementSetAttributeValue(

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -136,47 +136,55 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
136136
func acceptSuggestion(editor: EditorContent) async throws -> UpdatedContent? {
137137
presenter.markAsProcessing(true)
138138
defer { presenter.markAsProcessing(false) }
139-
139+
140140
let fileURL = try await Environment.fetchCurrentFileURL()
141141
let (workspace, _) = try await Workspace.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
142142

143-
let result: (suggestion: CopilotCompletion, cleanup: () -> Void)? = {
143+
let result: (
144+
suggestion: CopilotCompletion,
145+
cleanup: () -> Void,
146+
startPosition: CursorPosition?
147+
)? = {
144148
if let service = WidgetDataSource.shared.promptToCodes[fileURL]?.promptToCodeService {
145-
return (CopilotCompletion(
146-
text: service.code,
147-
position: service.selectionRange.start,
148-
uuid: UUID().uuidString,
149-
range: service.selectionRange,
150-
displayText: service.code
151-
), {
152-
WidgetDataSource.shared.removePromptToCode(for: fileURL)
153-
presenter.closePromptToCode(fileURL: fileURL)
154-
})
149+
return (
150+
CopilotCompletion(
151+
text: service.code,
152+
position: service.selectionRange.start,
153+
uuid: UUID().uuidString,
154+
range: service.selectionRange,
155+
displayText: service.code
156+
),
157+
{
158+
WidgetDataSource.shared.removePromptToCode(for: fileURL)
159+
presenter.closePromptToCode(fileURL: fileURL)
160+
},
161+
service.selectionRange.start
162+
)
155163
}
156-
164+
157165
if let acceptedSuggestion = workspace.acceptSuggestion(
158166
forFileAt: fileURL,
159167
editor: editor
160168
) {
161-
return (acceptedSuggestion, {
162-
presenter.discardSuggestion(fileURL: fileURL)
163-
})
169+
return (
170+
acceptedSuggestion,
171+
{
172+
presenter.discardSuggestion(fileURL: fileURL)
173+
},
174+
nil
175+
)
164176
}
165-
177+
166178
return nil
167179
}()
168-
180+
169181
guard let result else { return nil }
170-
182+
171183
let injector = SuggestionInjector()
172184
var lines = editor.lines
173185
var cursorPosition = editor.cursorPosition
174186
var extraInfo = SuggestionInjector.ExtraInfo()
175-
injector.rejectCurrentSuggestions(
176-
from: &lines,
177-
cursorPosition: &cursorPosition,
178-
extraInfo: &extraInfo
179-
)
187+
180188
injector.acceptSuggestion(
181189
intoContentWithoutSuggestion: &lines,
182190
cursorPosition: &cursorPosition,
@@ -185,11 +193,16 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
185193
)
186194

187195
result.cleanup()
188-
196+
189197
return .init(
190198
content: String(lines.joined(separator: "")),
191-
newCursor: cursorPosition,
192-
modifications: extraInfo.modifications
199+
newSelection: {
200+
if let startPosition = result.startPosition {
201+
return .init(start: startPosition, end: cursorPosition)
202+
}
203+
return .cursor(cursorPosition)
204+
}(),
205+
modifications : extraInfo.modifications
193206
)
194207
}
195208

Core/Sources/Service/SuggestionPresenter/PresentInCommentSuggestionPresenter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct PresentInCommentSuggestionPresenter {
2525
guard let completion = await filespace.presentingSuggestion else {
2626
return .init(
2727
content: originalContent,
28-
newCursor: cursorPosition,
28+
newSelection: .cursor(cursorPosition),
2929
modifications: extraInfo.modifications
3030
)
3131
}
@@ -40,7 +40,7 @@ struct PresentInCommentSuggestionPresenter {
4040

4141
return .init(
4242
content: String(lines.joined(separator: "")),
43-
newCursor: cursorPosition,
43+
newSelection: .cursor(cursorPosition),
4444
modifications: extraInfo.modifications
4545
)
4646
}
@@ -65,7 +65,7 @@ struct PresentInCommentSuggestionPresenter {
6565

6666
return .init(
6767
content: String(lines.joined(separator: "")),
68-
newCursor: cursorPosition,
68+
newSelection: .cursor(cursorPosition),
6969
modifications: extraInfo.modifications
7070
)
7171
}

Core/Sources/XPCShared/Models.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public struct EditorContent: Codable {
4747
}
4848

4949
public struct UpdatedContent: Codable {
50-
public init(content: String, newCursor: CursorPosition? = nil, modifications: [Modification]) {
50+
public init(content: String, newSelection: CursorRange? = nil, modifications: [Modification]) {
5151
self.content = content
52-
self.newCursor = newCursor
52+
self.newSelection = newSelection
5353
self.modifications = modifications
5454
}
5555

5656
public var content: String
57-
public var newCursor: CursorPosition?
57+
public var newSelection: CursorRange?
5858
public var modifications: [Modification]
5959
}
6060

EditorExtension/Helpers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ extension XCSourceEditorCommandInvocation {
1919
}
2020

2121
func accept(_ updatedContent: UpdatedContent) {
22-
if let newCursor = updatedContent.newCursor {
22+
if let newSelection = updatedContent.newSelection {
2323
mutateCompleteBuffer(
2424
modifications: updatedContent.modifications,
2525
restoringSelections: false
2626
)
2727
buffer.selections.removeAllObjects()
2828
buffer.selections.add(XCSourceTextRange(
29-
start: .init(line: newCursor.line, column: newCursor.character),
30-
end: .init(line: newCursor.line, column: newCursor.character)
29+
start: .init(line: newSelection.start.line, column: newSelection.start.character),
30+
end: .init(line: newSelection.end.line, column: newSelection.end.character)
3131
))
3232
} else {
3333
mutateCompleteBuffer(

0 commit comments

Comments
 (0)