resolveFromFileContent scans the new file content line-by-line for consecutive matches of the normalized existing_code.
(d *model.Diff, cm *model.LlmComment)
| 170 | // resolveFromFileContent scans the new file content line-by-line for consecutive |
| 171 | // matches of the normalized existing_code. |
| 172 | func resolveFromFileContent(d *model.Diff, cm *model.LlmComment) bool { |
| 173 | if d.NewFileContent == "" { |
| 174 | return false |
| 175 | } |
| 176 | |
| 177 | fileLines := strings.Split(d.NewFileContent, "\n") |
| 178 | targetLines := splitAndNormalize(cm.ExistingCode) |
| 179 | if len(targetLines) == 0 { |
| 180 | return false |
| 181 | } |
| 182 | |
| 183 | // Normalize file lines the same way as target: skip blanks so that |
| 184 | // blank lines in the source don't break the sliding-window match. |
| 185 | // "Consecutive" here means adjacent non-blank lines. |
| 186 | normalizedFileLines := make([]string, 0, len(fileLines)) |
| 187 | fileLineNums := make([]int, 0, len(fileLines)) |
| 188 | for i, line := range fileLines { |
| 189 | n := normalizeLine(strings.TrimRight(line, "\r")) |
| 190 | if n == "" { |
| 191 | continue |
| 192 | } |
| 193 | normalizedFileLines = append(normalizedFileLines, n) |
| 194 | fileLineNums = append(fileLineNums, i+1) |
| 195 | } |
| 196 | |
| 197 | if len(normalizedFileLines) < len(targetLines) { |
| 198 | return false |
| 199 | } |
| 200 | |
| 201 | for i := 0; i <= len(normalizedFileLines)-len(targetLines); i++ { |
| 202 | matched := true |
| 203 | for j, target := range targetLines { |
| 204 | if normalizedFileLines[i+j] != target { |
| 205 | matched = false |
| 206 | break |
| 207 | } |
| 208 | } |
| 209 | if matched { |
| 210 | cm.StartLine = fileLineNums[i] |
| 211 | cm.EndLine = fileLineNums[i+len(targetLines)-1] |
| 212 | return true |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return false |
| 217 | } |
| 218 | |
| 219 | // splitAndNormalize splits code text into lines and normalizes each one. |
| 220 | func splitAndNormalize(code string) []string { |
no test coverage detected