Skip to content

Commit 205c9b3

Browse files
committed
Remove the first adjacent placeholder when accepting suggestions
1 parent da44c76 commit 205c9b3

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Core/Sources/SuggestionInjector/SuggestionInjector.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,25 @@ public struct SuggestionInjector {
233233

234234
// then check how many characters are not in the suffix of the suggestion.
235235
guard let splitIndex else { return 0 }
236-
237-
let suffix = lastRemovedLineCleaned[splitIndex...]
236+
237+
var suffix = String(lastRemovedLineCleaned[splitIndex...])
238238
if last.hasSuffix(suffix) { return 0 }
239239

240+
// remove the first adjacent placeholder in suffix which looks like `<#Hello#>`
241+
242+
let regex = try! NSRegularExpression(pattern: "<#.*?#>")
243+
244+
if let firstPlaceholderRange = regex.firstMatch(
245+
in: suffix,
246+
options: [],
247+
range: NSRange(suffix.startIndex..., in: suffix)
248+
)?.range,
249+
firstPlaceholderRange.location <= 1,
250+
let r = Range(firstPlaceholderRange, in: suffix)
251+
{
252+
suffix.removeSubrange(r)
253+
}
254+
240255
let lastInsertingLine = toBeInserted[toBeInserted.endIndex - 1]
241256
.droppedLineBreak()
242257
.appending(suffix)

Core/Tests/SuggestionInjectorTests/AcceptSuggestionTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,40 @@ final class AcceptSuggestionTests: XCTestCase {
541541
542542
""")
543543
}
544+
545+
func test_remove_the_first_adjacent_placeholder_in_the_last_line(
546+
) async throws {
547+
let content = """
548+
apiKeyName: <#T##value: BinaryInteger##BinaryInteger#> <#Hello#>,
549+
"""
550+
551+
let suggestion = CodeSuggestion(
552+
text: "apiKeyName: azureOpenAIAPIKeyName",
553+
position: .init(line: 0, character: 12),
554+
uuid: "",
555+
range: .init(
556+
start: .init(line: 0, character: 0),
557+
end: .init(line: 0, character: 12)
558+
),
559+
displayText: ""
560+
)
561+
562+
var lines = content.breakIntoEditorStyleLines()
563+
var extraInfo = SuggestionInjector.ExtraInfo()
564+
var cursor = CursorPosition(line: 5, character: 34)
565+
SuggestionInjector().acceptSuggestion(
566+
intoContentWithoutSuggestion: &lines,
567+
cursorPosition: &cursor,
568+
completion: suggestion,
569+
extraInfo: &extraInfo
570+
)
571+
572+
XCTAssertEqual(cursor, .init(line: 0, character: 33))
573+
XCTAssertEqual(lines.joined(separator: ""), """
574+
apiKeyName: azureOpenAIAPIKeyName <#Hello#>,
575+
576+
""")
577+
}
544578
}
545579

546580
extension String {

0 commit comments

Comments
 (0)