LineCounts returns how many lines a line-level diff of oldText → newText adds and removes (PLAN-2348 U2). It counts the way the web DiffView renders jsdiff's diffLines: each changed run is split on newlines after dropping one trailing newline, so a run "a\nb\n" is two lines. Both sides are Myers lin
(oldText, newText string)
| 164 | // equally valid split on unusual inputs, in which case a count can differ from |
| 165 | // the rendered diff by the lines those splits trade. |
| 166 | func LineCounts(oldText, newText string) (added, removed int) { |
| 167 | a, b, lines := dmp.DiffLinesToChars(oldText, newText) |
| 168 | diffs := dmp.DiffCharsToLines(dmp.DiffMain(a, b, false), lines) |
| 169 | for _, d := range diffs { |
| 170 | n := strings.Count(strings.TrimSuffix(d.Text, "\n"), "\n") + 1 |
| 171 | switch d.Type { |
| 172 | case difflib.DiffInsert: |
| 173 | added += n |
| 174 | case difflib.DiffDelete: |
| 175 | removed += n |
| 176 | } |
| 177 | } |
| 178 | return added, removed |
| 179 | } |