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