Skip to content

Commit 41353e7

Browse files
committed
Limit the suggestion to match the line limit
1 parent 497eacb commit 41353e7

3 files changed

Lines changed: 31 additions & 17 deletions

File tree

Core/Sources/SuggestionService/RawSuggestionPostProcessing/DefaultRawSuggestionPostProcessingStrategy.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,14 @@ protocol RawSuggestionPostProcessingStrategy {
55
func postProcess(rawSuggestion: String, infillPrefix: String, suffix: [String]) -> String
66
}
77

8-
extension RawSuggestionPostProcessingStrategy {
9-
func removeTrailingNewlinesAndWhitespace(from string: String) -> String {
10-
var text = string[...]
11-
while let last = text.last, last.isNewline || last.isWhitespace {
12-
text = text.dropLast(1)
13-
}
14-
return String(text)
15-
}
16-
}
17-
188
struct DefaultRawSuggestionPostProcessingStrategy: RawSuggestionPostProcessingStrategy {
199
let codeWrappingTags: (opening: String, closing: String)?
2010

2111
func postProcess(rawSuggestion: String, infillPrefix: String, suffix: [String]) -> String {
2212
var suggestion = extractSuggestion(from: rawSuggestion)
2313
removePrefix(from: &suggestion, infillPrefix: infillPrefix)
2414
removeSuffix(from: &suggestion, suffix: suffix)
25-
return removeTrailingNewlinesAndWhitespace(from: infillPrefix + suggestion)
15+
return infillPrefix + suggestion
2616
}
2717

2818
func extractSuggestion(from response: String) -> String {

Core/Sources/SuggestionService/RawSuggestionPostProcessing/NoOpRawSuggestionPostProcessingStrategy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22

33
struct NoOpRawSuggestionPostProcessingStrategy: RawSuggestionPostProcessingStrategy {
44
func postProcess(rawSuggestion: String, infillPrefix: String, suffix: [String]) -> String {
5-
removeTrailingNewlinesAndWhitespace(from: infillPrefix + rawSuggestion)
5+
infillPrefix + rawSuggestion
66
}
77
}
88

Core/Sources/SuggestionService/Service.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ actor Service {
5454
case let .chatModel(model):
5555
CodeCompletionLogger.logger.logModel(model)
5656
suggestedCodeSnippets = try await service.getCompletions(
57-
prompt,
57+
prompt,
5858
streamStopStrategy: stopStream,
5959
model: model,
6060
count: 1
@@ -82,13 +82,21 @@ actor Service {
8282
return suggestedCodeSnippets
8383
.filter { !$0.allSatisfy { $0.isWhitespace || $0.isNewline } }
8484
.map {
85-
CodeSuggestion(
86-
id: UUID().uuidString,
87-
text: postProcessor.postProcess(
85+
let suggestionText = postProcessor
86+
.postProcess(
8887
rawSuggestion: $0,
8988
infillPrefix: prompt.suggestionPrefix.prependingValue,
9089
suffix: prompt.suffix
91-
),
90+
)
91+
.keepLines(
92+
count: UserDefaults.shared
93+
.value(for: \.maxNumberOfLinesOfSuggestion)
94+
)
95+
.removeTrailingNewlinesAndWhitespace()
96+
97+
return CodeSuggestion(
98+
id: UUID().uuidString,
99+
text: suggestionText,
92100
position: request.cursorPosition,
93101
range: .init(
94102
start: .init(
@@ -186,3 +194,19 @@ actor Service {
186194
}
187195
}
188196

197+
extension String {
198+
func keepLines(count: Int) -> String {
199+
if count <= 0 { return self }
200+
let lines = breakLines()
201+
return lines.prefix(count).joined()
202+
}
203+
204+
func removeTrailingNewlinesAndWhitespace() -> String {
205+
var text = self[...]
206+
while let last = text.last, last.isNewline || last.isWhitespace {
207+
text = text.dropLast(1)
208+
}
209+
return String(text)
210+
}
211+
}
212+

0 commit comments

Comments
 (0)