resolveFromFileContent scans the new file content line-by-line for consecutive matches of the normalized existing_code.
(d *model.Diff, cm *model.LlmComment)
| 167 | // resolveFromFileContent scans the new file content line-by-line for consecutive |
| 168 | // matches of the normalized existing_code. |
| 169 | func resolveFromFileContent(d *model.Diff, cm *model.LlmComment) bool { |
| 170 | if d.NewFileContent == "" { |
| 171 | return false |
| 172 | } |
| 173 | |
| 174 | fileLines := strings.Split(d.NewFileContent, "\n") |
| 175 | targetLines := splitAndNormalize(cm.ExistingCode) |
| 176 | if len(targetLines) == 0 { |
| 177 | return false |
| 178 | } |
| 179 | |
| 180 | // Normalize file lines the same way as target: skip blanks so that |
| 181 | // blank lines in the source don't break the sliding-window match. |
| 182 | // "Consecutive" here means adjacent non-blank lines. |
| 183 | normalizedFileLines := make([]string, 0, len(fileLines)) |
| 184 | fileLineNums := make([]int, 0, len(fileLines)) |
| 185 | for i, line := range fileLines { |
| 186 | n := normalizeLine(strings.TrimRight(line, "\r")) |
| 187 | if n == "" { |
| 188 | continue |
| 189 | } |
| 190 | normalizedFileLines = append(normalizedFileLines, n) |
| 191 | fileLineNums = append(fileLineNums, i+1) |
| 192 | } |
| 193 | |
| 194 | if len(normalizedFileLines) < len(targetLines) { |
| 195 | return false |
| 196 | } |
| 197 | |
| 198 | for i := 0; i <= len(normalizedFileLines)-len(targetLines); i++ { |
| 199 | matched := true |
| 200 | for j, target := range targetLines { |
| 201 | if normalizedFileLines[i+j] != target { |
| 202 | matched = false |
| 203 | break |
| 204 | } |
| 205 | } |
| 206 | if matched { |
| 207 | cm.StartLine = fileLineNums[i] |
| 208 | cm.EndLine = fileLineNums[i+len(targetLines)-1] |
| 209 | return true |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return false |
| 214 | } |
| 215 | |
| 216 | // splitAndNormalize splits code text into lines and normalizes each one. |
| 217 | func splitAndNormalize(code string) []string { |
no test coverage detected