ResolveLineNumbers populates StartLine/EndLine on each comment by matching the ExistingCode against the corresponding file's diff hunks (primary), or falling back to scanning the full new-file content line-by-line.
(comments []model.LlmComment, diffs []model.Diff)
| 10 | // the ExistingCode against the corresponding file's diff hunks (primary), or |
| 11 | // falling back to scanning the full new-file content line-by-line. |
| 12 | func ResolveLineNumbers(comments []model.LlmComment, diffs []model.Diff) []model.LlmComment { |
| 13 | if len(comments) == 0 || len(diffs) == 0 { |
| 14 | return comments |
| 15 | } |
| 16 | |
| 17 | // Build lookup: newPath -> *Diff |
| 18 | diffByPath := make(map[string]*model.Diff, len(diffs)) |
| 19 | for i := range diffs { |
| 20 | d := &diffs[i] |
| 21 | if d.NewPath != "/dev/null" && d.NewPath != "" { |
| 22 | diffByPath[d.NewPath] = d |
| 23 | } |
| 24 | if d.OldPath != "/dev/null" && d.OldPath != "" { |
| 25 | diffByPath[d.OldPath] = d |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | result := make([]model.LlmComment, len(comments)) |
| 30 | copy(result, comments) |
| 31 | |
| 32 | for i := range result { |
| 33 | cm := &result[i] |
| 34 | if cm.StartLine > 0 || cm.EndLine > 0 { |
| 35 | continue |
| 36 | } |
| 37 | if cm.ExistingCode == "" { |
| 38 | continue |
| 39 | } |
| 40 | d, ok := diffByPath[cm.Path] |
| 41 | if !ok { |
| 42 | continue |
| 43 | } |
| 44 | |
| 45 | // Primary: try matching from deleted/context lines in diff hunks |
| 46 | if resolveFromHunk(d, cm) { |
| 47 | continue |
| 48 | } |
| 49 | |
| 50 | // Fallback: scan the new file content for consecutive matches |
| 51 | resolveFromFileContent(d, cm) |
| 52 | } |
| 53 | |
| 54 | return result |
| 55 | } |
| 56 | |
| 57 | // ResolveComment attempts to resolve StartLine/EndLine for a single comment |
| 58 | // by matching ExistingCode against the diff. Returns true on success. |