Skip to content

Commit 6f0ad43

Browse files
committed
Fix that a wrong suffix was append to the suggestion
1 parent 8630be4 commit 6f0ad43

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

Core/Sources/Service/GUI/WidgetDataSource.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extension WidgetDataSource: SuggestionWidgetDataSource {
2626
suggestionCount: filespace.suggestions.count,
2727
currentSuggestionIndex: filespace.suggestionIndex,
2828
replacingRange: suggestion.range,
29+
replacingLines: suggestion.replacingLines,
2930
descriptions: suggestion.descriptions
3031
)
3132
}

Core/Sources/SuggestionService/SuggestionService.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,16 @@ public extension SuggestionService {
8484
}
8585
}
8686

87-
return try await getSuggestion(request, workspaceInfo)
87+
var result = try await getSuggestion(request, workspaceInfo)
88+
if !request.lines.isEmpty {
89+
for index in result.indices {
90+
let range = result[index].range
91+
let lowerBound = max(0, range.start.line)
92+
let upperBound = max(lowerBound, min(request.lines.count - 1, range.end.line))
93+
result[index].replacingLines = Array(request.lines[lowerBound...upperBound])
94+
}
95+
}
96+
return result
8897
} catch let error as SuggestionServiceError {
8998
throw error
9099
} catch {

Core/Sources/SuggestionWidget/SharedPanelView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ struct SharedPanelView_Both_DisplayingSuggestion_Preview: PreviewProvider {
164164
startLineIndex: 8,
165165
suggestionCount: 2,
166166
currentSuggestionIndex: 0,
167-
replacingRange: .zero
167+
replacingRange: .zero,
168+
replacingLines: [""]
168169
)
169170
),
170171
colorScheme: .dark,

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public struct PresentingCodeSuggestion: Equatable {
1414
public var suggestionCount: Int
1515
public var currentSuggestionIndex: Int
1616
public var replacingRange: CursorRange
17+
public var replacingLines: [String]
1718
public var descriptions: [CodeSuggestion.Description]
1819

1920
public init(
@@ -23,6 +24,7 @@ public struct PresentingCodeSuggestion: Equatable {
2324
suggestionCount: Int,
2425
currentSuggestionIndex: Int,
2526
replacingRange: CursorRange,
27+
replacingLines: [String],
2628
descriptions: [CodeSuggestion.Description] = []
2729
) {
2830
self.code = code
@@ -31,6 +33,7 @@ public struct PresentingCodeSuggestion: Equatable {
3133
self.suggestionCount = suggestionCount
3234
self.currentSuggestionIndex = currentSuggestionIndex
3335
self.replacingRange = replacingRange
36+
self.replacingLines = replacingLines
3437
self.descriptions = descriptions
3538
}
3639
}
@@ -231,8 +234,10 @@ struct CodeBlockSuggestionPanel: View {
231234
originalCode: String,
232235
dimmedCharacterCount: AsyncCodeBlock.DimmedCharacterCount
233236
) {
234-
let range = suggestion.replacingRange
235-
let codeInRange = EditorInformation.code(in: textCursorTracker.content.lines, inside: range)
237+
var range = suggestion.replacingRange
238+
range.end = .init(line: range.end.line - range.start.line, character: range.end.character)
239+
range.start = .init(line: 0, character: range.start.character)
240+
let codeInRange = EditorInformation.code(in: suggestion.replacingLines, inside: range)
236241
let leftover = {
237242
if range.end.line >= 0, range.end.line < textCursorTracker.content.lines.endIndex {
238243
let lastLine = textCursorTracker.content.lines[range.end.line]
@@ -253,7 +258,7 @@ struct CodeBlockSuggestionPanel: View {
253258

254259
let prefix = {
255260
if range.start.line >= 0, range.start.line < textCursorTracker.content.lines.endIndex {
256-
let firstLine = textCursorTracker.content.lines[range.start.line]
261+
let firstLine = suggestion.replacingLines[range.start.line]
257262
if range.start.character < firstLine.utf16.count {
258263
let endIndex = firstLine.utf16.index(
259264
firstLine.utf16.startIndex,
@@ -296,7 +301,8 @@ struct CodeBlockSuggestionPanel: View {
296301
startLineIndex: 8,
297302
suggestionCount: 2,
298303
currentSuggestionIndex: 0,
299-
replacingRange: .outOfScope
304+
replacingRange: .outOfScope,
305+
replacingLines: []
300306
), suggestionDisplayCompactMode: .init(
301307
wrappedValue: false,
302308
"suggestionDisplayCompactMode",
@@ -325,7 +331,8 @@ struct CodeBlockSuggestionPanel: View {
325331
startLineIndex: 8,
326332
suggestionCount: 2,
327333
currentSuggestionIndex: 0,
328-
replacingRange: .outOfScope
334+
replacingRange: .outOfScope,
335+
replacingLines: []
329336
), suggestionDisplayCompactMode: .init(
330337
wrappedValue: true,
331338
"suggestionDisplayCompactMode",
@@ -352,7 +359,8 @@ struct CodeBlockSuggestionPanel: View {
352359
startLineIndex: 8,
353360
suggestionCount: 2,
354361
currentSuggestionIndex: 0,
355-
replacingRange: .outOfScope
362+
replacingRange: .outOfScope,
363+
replacingLines: []
356364
))
357365
.preferredColorScheme(.light)
358366
.frame(width: 450, height: 400)

Core/Sources/SuggestionWidget/SuggestionWidgetDataSource.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ struct MockWidgetDataSource: SuggestionWidgetDataSource {
1818
startLineIndex: 1,
1919
suggestionCount: 3,
2020
currentSuggestionIndex: 0,
21-
replacingRange: .zero
21+
replacingRange: .zero,
22+
replacingLines: []
2223
)
2324
}
2425
}

Tool/Sources/SuggestionBasic/CodeSuggestion.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public struct CodeSuggestion: Codable, Equatable {
2222
text: String,
2323
position: CursorPosition,
2424
range: CursorRange,
25+
replacingLines: [String] = [],
2526
descriptions: [Description] = [],
2627
middlewareComments: [String] = [],
2728
metadata: [String: String] = [:]
@@ -30,6 +31,7 @@ public struct CodeSuggestion: Codable, Equatable {
3031
self.position = position
3132
self.id = id
3233
self.range = range
34+
self.replacingLines = replacingLines
3335
self.descriptions = descriptions
3436
self.middlewareComments = middlewareComments
3537
self.metadata = metadata
@@ -53,6 +55,8 @@ public struct CodeSuggestion: Codable, Equatable {
5355
/// The range of the original code that should be replaced.
5456
public var range: CursorRange
5557
/// Descriptions about this code suggestion
58+
@FallbackDecoding<EmptyArray> public var replacingLines: [String]
59+
/// Descriptions about this code suggestion
5660
@FallbackDecoding<EmptyArray> public var descriptions: [Description]
5761
/// A place to store comments inserted by middleware for debugging use.
5862
@FallbackDecoding<EmptyArray> public var middlewareComments: [String]

0 commit comments

Comments
 (0)