resolveFromFileContent scans the new file content line-by-line for consecutive matches of the normalized existing_code.
(d *model.Diff, cm *model.LlmComment)
| 249 | // resolveFromFileContent scans the new file content line-by-line for consecutive |
| 250 | // matches of the normalized existing_code. |
| 251 | func resolveFromFileContent(d *model.Diff, cm *model.LlmComment) bool { |
| 252 | if d.NewFileContent == "" { |
| 253 | return false |
| 254 | } |
| 255 | |
| 256 | fileLines := strings.Split(d.NewFileContent, "\n") |
| 257 | targetLines := splitAndNormalize(cm.ExistingCode) |
| 258 | if len(targetLines) == 0 { |
| 259 | return false |
| 260 | } |
| 261 | |
| 262 | // Normalize file lines the same way as target: skip blanks so that |
| 263 | // blank lines in the source don't break the sliding-window match. |
| 264 | // "Consecutive" here means adjacent non-blank lines. |
| 265 | normalizedFileLines := make([]string, 0, len(fileLines)) |
| 266 | fileLineNums := make([]int, 0, len(fileLines)) |
| 267 | for i, line := range fileLines { |
| 268 | n := normalizeLine(strings.TrimRight(line, "\r")) |
| 269 | if n == "" { |
| 270 | continue |
| 271 | } |
| 272 | normalizedFileLines = append(normalizedFileLines, n) |
| 273 | fileLineNums = append(fileLineNums, i+1) |
| 274 | } |
| 275 | |
| 276 | if len(normalizedFileLines) < len(targetLines) { |
| 277 | return false |
| 278 | } |
| 279 | |
| 280 | for i := 0; i <= len(normalizedFileLines)-len(targetLines); i++ { |
| 281 | matched := true |
| 282 | for j, target := range targetLines { |
| 283 | if normalizedFileLines[i+j] != target { |
| 284 | matched = false |
| 285 | break |
| 286 | } |
| 287 | } |
| 288 | if matched { |
| 289 | cm.StartLine = fileLineNums[i] |
| 290 | cm.EndLine = fileLineNums[i+len(targetLines)-1] |
| 291 | return true |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return false |
| 296 | } |
| 297 | |
| 298 | // splitAndNormalize splits code text into lines and normalizes each one. |
| 299 | func splitAndNormalize(code string) []string { |
no test coverage detected