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

Function resolveFromFileContent

internal/diff/resolver.go:238–283  ·  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

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