Skip to content

Commit

Permalink
fix(util): fix diffLines, fix #596
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Apr 28, 2019
1 parent 65c4fe2 commit 01b0969
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/modules/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ describe('diff lines', () => {
replacement: []
})
})

it('should diff removed line', () => {
let res = diffLines('a\n\n\nb', 'a\n\nb')
expect(res).toEqual({
start: 2,
end: 3,
replacement: []
})
})
})

describe('patch line', () => {
Expand Down
12 changes: 5 additions & 7 deletions src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class Document {
private env: Env) {
this.fireContentChanges = debounce(() => {
this._fireContentChanges()
}, 50)
}, 200)
this.fetchContent = debounce(() => {
this._fetchContent().catch(e => {
logger.error(`Error on fetch content:`, e)
Expand Down Expand Up @@ -221,6 +221,7 @@ export default class Document {
rangeLength: change.end - change.start,
text: change.newText
}]
logger.debug('changes:', JSON.stringify(changes, null, 2))
this._onDocumentChange.fire({
textDocument: { version, uri },
contentChanges: changes
Expand Down Expand Up @@ -268,12 +269,9 @@ export default class Document {

public async applyEdits(_nvim: Neovim, edits: TextEdit[], sync = true): Promise<void> {
if (edits.length == 0) return
let orig = this.lines.join('\n')
let textDocument = TextDocument.create(this.uri, this.filetype, 1, orig + (this.eol ? '\n' : ''))
let orig = this.lines.join('\n') + (this.eol ? '\n' : '')
let textDocument = TextDocument.create(this.uri, this.filetype, 1, orig)
let content = TextDocument.applyEdits(textDocument, edits)
if (this.eol && content.endsWith('\n')) {
content = content.slice(0, -1)
}
// could be equal sometimes
if (orig === content) {
this.createDocument()
Expand All @@ -285,7 +283,7 @@ export default class Document {
strictIndexing: false
})
// can't wait vim sync buffer
this.lines = content.split('\n')
this.lines = (this.eol && content.endsWith('\n') ? content.slice(0, -1) : content).split('\n')
if (sync) this.forceSync()
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/util/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ export function diffLines(from: string, to: string): ChangedLines {
}
}
if (start != newLines.length) {
for (let j = oldLen; j >= 0; j--) {
if (j < start) {
end = start
let maxRemain = Math.min(end - start, len - start)
for (let j = 0; j < maxRemain; j++) {
if (oldLines[oldLen - j - 1] != newLines[len - j - 1]) {
break
}
if (oldLines[j - 1] !== newLines[len - (oldLen - j) - 1]) {
end = j
break
}
if (j == 0) {
end = 0
}
end = end - 1
}
}
return {
Expand Down

0 comments on commit 01b0969

Please sign in to comment.