MCPcopy Create free account
hub / github.com/alibaba/open-code-review / ResolveLineNumbers

Function ResolveLineNumbers

internal/diff/resolver.go:15–58  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Calls 2

resolveFromHunkFunction · 0.85
resolveFromFileContentFunction · 0.85