Skip to content

Commit 865d850

Browse files
committed
Do not inject suggestion comment if it's only appending spaces and line breaks
1 parent 05ea34d commit 865d850

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

Core/Sources/SuggestionInjector/SuggestionInjector.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct SuggestionInjector {
2727
var ranges = [ClosedRange<Int>]()
2828
var suggestionStartIndex = -1
2929

30+
// find ranges of suggestion comments
3031
for (index, line) in content.enumerated() {
3132
if line.hasPrefix(suggestionStart) {
3233
suggestionStartIndex = index
@@ -42,6 +43,7 @@ public struct SuggestionInjector {
4243
extraInfo.modifications.append(contentsOf: reversedRanges.map(Modification.deleted))
4344
extraInfo.didChangeContent = !ranges.isEmpty
4445

46+
// remove the lines from bottom to top
4547
for range in reversedRanges {
4648
for i in stride(from: range.upperBound, through: range.lowerBound, by: -1) {
4749
if i <= cursorPosition.line, cursorPosition.line >= 0 {
@@ -65,13 +67,17 @@ public struct SuggestionInjector {
6567
count: Int,
6668
extraInfo: inout ExtraInfo
6769
) {
70+
// assemble suggestion comment
6871
let start = completion.range.start
6972
let startText = "\(suggestionStart) \(index + 1)/\(count)"
7073
var lines = [startText + "\n"]
7174
lines.append(contentsOf: completion.text.breakLines(appendLineBreakToLastLine: true))
7275
lines.append(suggestionEnd + "\n")
73-
if lines.count <= 2 { return }
7476

77+
// if suggestion is empty, returns without modifying the code
78+
guard lines.count > 2 else { return }
79+
80+
// replace the common prefix of the first line with space and carrot
7581
let existedLine = start.line < content.endIndex ? content[start.line] : nil
7682
let commonPrefix = longestCommonPrefix(of: lines[1], and: existedLine ?? "")
7783

@@ -87,6 +93,9 @@ public struct SuggestionInjector {
8793
with: String(repeating: " ", count: commonPrefix.count - 1) + "^"
8894
)
8995
}
96+
97+
// if the suggestion is only appeding new lines and spaces, return without modification
98+
if completion.text.dropFirst(commonPrefix.count).allSatisfy({ $0.isWhitespace || $0.isNewline }) { return }
9099

91100
let lineIndex = start.line + {
92101
guard let existedLine else { return 0 }

Core/Tests/SuggestionInjectorTests/ProposeSuggestionTests.swift

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,48 @@ final class ProposeSuggestionTests: XCTestCase {
186186
}
187187

188188
// swiftformat:enable all
189-
189+
190+
func test_propose_suggestion_overlap_one_line_adding_only_spaces() async throws {
191+
let content = """
192+
if true {
193+
print("hello")
194+
} else {
195+
print("world")
196+
}
197+
"""
198+
let text = "} else {\n"
199+
let suggestion = CopilotCompletion(
200+
text: text,
201+
position: .init(line: 2, character: 0),
202+
uuid: "",
203+
range: .init(
204+
start: .init(line: 2, character: 0),
205+
end: .init(line: 2, character: 8)
206+
),
207+
displayText: ""
208+
)
209+
var extraInfo = SuggestionInjector.ExtraInfo()
210+
var lines = content.breakLines()
211+
SuggestionInjector().proposeSuggestion(
212+
intoContentWithoutSuggestion: &lines,
213+
completion: suggestion,
214+
index: 0,
215+
count: 10,
216+
extraInfo: &extraInfo
217+
)
218+
XCTAssertFalse(extraInfo.didChangeContent)
219+
XCTAssertFalse(extraInfo.didChangeCursorPosition)
220+
XCTAssertNil(extraInfo.suggestionRange)
221+
XCTAssertEqual(lines, content.breakLines().applying(extraInfo.modifications))
222+
XCTAssertEqual(lines.joined(separator: ""), """
223+
if true {
224+
print("hello")
225+
} else {
226+
print("world")
227+
}
228+
""")
229+
}
230+
190231
func test_propose_suggestion_partial_overlap() async throws {
191232
let content = "func quickSort() {}}\n"
192233
let text = """

0 commit comments

Comments
 (0)