forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostProcessingSuggestionServiceMiddleware.swift
More file actions
37 lines (31 loc) · 1.07 KB
/
PostProcessingSuggestionServiceMiddleware.swift
File metadata and controls
37 lines (31 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Foundation
import SuggestionModel
public struct PostProcessingSuggestionServiceMiddleware: SuggestionServiceMiddleware {
public init() {}
public func getSuggestion(
_ request: SuggestionRequest,
configuration: SuggestionServiceConfiguration,
next: Next
) async throws -> [CodeSuggestion] {
let suggestions = try await next(request)
return suggestions.compactMap {
var suggestion = $0
Self.removeTrailingWhitespacesAndNewlines(&suggestion)
if suggestion.text.isEmpty { return nil }
return suggestion
}
}
static func removeTrailingWhitespacesAndNewlines(_ suggestion: inout CodeSuggestion) {
var text = suggestion.text[...]
while let last = text.last, last.isNewline || last.isWhitespace {
text = text.dropLast(1)
}
suggestion.text = String(text)
}
static func checkIfSuggestionHasNoEffect(
_ suggestion: CodeSuggestion,
request: SuggestionRequest
) -> Bool {
suggestion.text.isEmpty
}
}