Merge pull request #18089 from desktop/fix-select-last-line

Fix diff parser when last line of file has "no newline" marker before the change
This commit is contained in:
Sergio Padrino 2024-01-29 15:06:52 +01:00 committed by GitHub
commit c2a964f80d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

View file

@ -311,7 +311,6 @@ export class DiffParser {
let diffLineNumber = linesConsumed
while ((c = this.parseLinePrefix(this.peek()))) {
const line = this.readLine()
diffLineNumber++
if (!line) {
throw new Error('Expected unified diff line but reached end of diff')
@ -338,6 +337,12 @@ export class DiffParser {
continue
}
// We must increase `diffLineNumber` only when we're certain that the line
// is not a "no newline" marker. Otherwise, we'll end up with a wrong
// `diffLineNumber` for the next line. This could happen if the last line
// in the file doesn't have a newline before the change.
diffLineNumber++
let diffLine: DiffLine
if (c === DiffPrefixAdd) {

View file

@ -256,6 +256,7 @@ index 1910281..257cc56 100644
expect(lines[i].type).toBe(DiffLineType.Delete)
expect(lines[i].oldLineNumber).toBe(1)
expect(lines[i].newLineNumber).toBeNull()
expect(lines[i].originalLineNumber).toBe(1)
expect(lines[i].noTrailingNewLine).toBe(true)
i++
@ -263,6 +264,7 @@ index 1910281..257cc56 100644
expect(lines[i].type).toBe(DiffLineType.Add)
expect(lines[i].oldLineNumber).toBeNull()
expect(lines[i].newLineNumber).toBe(1)
expect(lines[i].originalLineNumber).toBe(2)
expect(lines[i].noTrailingNewLine).toBe(false)
i++
})
@ -305,6 +307,7 @@ index 1910281..ba0e162 100644
expect(lines[i].type).toBe(DiffLineType.Delete)
expect(lines[i].oldLineNumber).toBe(1)
expect(lines[i].newLineNumber).toBeNull()
expect(lines[i].originalLineNumber).toBe(1)
expect(lines[i].noTrailingNewLine).toBe(true)
i++
@ -312,6 +315,7 @@ index 1910281..ba0e162 100644
expect(lines[i].type).toBe(DiffLineType.Add)
expect(lines[i].oldLineNumber).toBeNull()
expect(lines[i].newLineNumber).toBe(1)
expect(lines[i].originalLineNumber).toBe(2)
expect(lines[i].noTrailingNewLine).toBe(true)
i++
})