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

Function resolveFromFileContent

internal/diff/resolver.go:172–217  ·  view source on GitHub ↗

resolveFromFileContent scans the new file content line-by-line for consecutive matches of the normalized existing_code.

(d *model.Diff, cm *model.LlmComment)

Source from the content-addressed store, hash-verified

170// resolveFromFileContent scans the new file content line-by-line for consecutive
171// matches of the normalized existing_code.
172func 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.
220func splitAndNormalize(code string) []string {

Callers 2

ResolveLineNumbersFunction · 0.85
ResolveCommentFunction · 0.85

Calls 2

splitAndNormalizeFunction · 0.70
normalizeLineFunction · 0.70

Tested by

no test coverage detected