@@ -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 }
0 commit comments