Skip to content

Commit

Permalink
chore(diff): implement diffLines without diff package
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Oct 21, 2018
1 parent a6f39e2 commit 21a73b5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -59,7 +59,6 @@
"devDependencies": {
"@chemzqm/tslint-config": "^1.0.17",
"@types/debounce": "^3.0.0",
"@types/diff": "^3.5.1",
"@types/fast-diff": "^1.1.0",
"@types/fb-watchman": "^2.0.0",
"@types/find-up": "^2.1.1",
Expand All @@ -84,7 +83,6 @@
"@chemzqm/neovim": "4.3.24",
"@chemzqm/tsconfig": "0.0.3",
"debounce": "^1.2.0",
"diff": "^3.5.0",
"fast-diff": "^1.2.0",
"fb-watchman": "^2.0.0",
"find-up": "^3.0.0",
Expand Down
11 changes: 9 additions & 2 deletions src/__tests__/modules/diff.test.ts
Expand Up @@ -3,9 +3,9 @@ import { diffLines } from '../../util/diff'
describe('should get diffLines', () => {
it('should get diff for added', () => {
let d = diffLines('1\n2', '1\n2\n3\n4')
expect(d.start).toBe(1)
expect(d.start).toBe(2)
expect(d.end).toBe(2)
expect(d.replacement).toEqual(['2', '3', '4'])
expect(d.replacement).toEqual(['3', '4'])
})

it('should get diff for added #1', () => {
Expand Down Expand Up @@ -42,4 +42,11 @@ describe('should get diffLines', () => {
expect(d.end).toBe(3)
expect(d.replacement).toEqual([])
})

it('should get diff for remove #1', () => {
let d = diffLines('1\n2\n3\n4', '1')
expect(d.start).toBe(1)
expect(d.end).toBe(4)
expect(d.replacement).toEqual([])
})
})
57 changes: 24 additions & 33 deletions src/util/diff.ts
@@ -1,8 +1,5 @@
import { diffLines as DiffLines } from 'diff'
import fastDiff from 'fast-diff'
import { ChangedLines, ChangeItem } from '../types'
import { lastIndex } from './array'
import { trimLast } from './string'
const logger = require('./logger')('util-diff')

interface Change {
Expand All @@ -11,42 +8,36 @@ interface Change {
newText: string
}

function getlines(str: string): string[] {
let s = trimLast(str, '\n')
return s.split('\n')
}

export function diffLines(from: string, to: string): ChangedLines {
let diffs = DiffLines(from, to)
let lastIdx = lastIndex(diffs, o => (o.added || o.removed))
if (lastIdx == -1) return null
let lnum = 0
let start = -1
let end = -1
let lines = []
let idx = 0
for (let diff of diffs) {
if (idx > lastIdx) {
let newLines: string[] = to.split('\n')
let oldLines: string[] = from.split('\n')
let start = 0
let end = oldLines.length
let oldLen = end
let len = newLines.length
for (let i = 0; i <= end; i++) {
if (newLines[i] !== oldLines[i]) {
start = i
break
}
if (start == -1 && (diff.removed || diff.added)) {
start = lnum
end = diff.removed ? start + diff.count : start
if (diff.added) lines.push(...getlines(diff.value))
} else if (diff.removed) {
end = lnum + diff.count
} else if (diff.added) {
end = lnum
lines.push(...getlines(diff.value))
} else if (start != -1) {
lines.push(...getlines(diff.value))
if (i == end) {
start = end
}
if (!diff.added) {
lnum = lnum + diff.count
}
for (let j = oldLen; j >= 0; j--) {
if (oldLines[j - 1] !== newLines[len - (oldLen - j) - 1]) {
end = j
break
}
if (j == 0) {
end = 0
}
idx = idx + 1
}
return { start, end, replacement: lines }
return {
start,
end,
replacement: newLines.slice(start, len - (oldLen - end))
}
}

export function getChangeItem(oldStr: string, newStr: string): ChangeItem {
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Expand Up @@ -130,11 +130,6 @@
resolved "https://registry.yarnpkg.com/@types/debounce/-/debounce-3.0.0.tgz#ea4290f6e2d7ab99464c93b4a69b843d0993b181"
integrity sha512-SbehVj+zL0QFTufW3HP/Xvwr/kFWw1sFMLCvmyJdp5Xegbo81sqA2tByWlyeryvSwWBrY4drpLOtGPh0fkxjCQ==

"@types/diff@^3.5.1":
version "3.5.1"
resolved "https://registry.yarnpkg.com/@types/diff/-/diff-3.5.1.tgz#30253f6e177564ad7da707b1ebe46d3eade71706"
integrity sha512-nkT7bd/YM6QRDQjww8PYf0kOj1MvwxQ/WaCinj2Hm1HlL+JqGTm4cDoQeROfiWX/B3SNI1nyLLhLAQpp5sE3hw==

"@types/events@*":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.2.0.tgz#81a6731ce4df43619e5c8c945383b3e62a89ea86"
Expand Down Expand Up @@ -1024,7 +1019,7 @@ detect-newline@^2.1.0:
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2"
integrity sha1-9B8cEL5LAOh7XxPaaAdZ8sW/0+I=

diff@^3.2.0, diff@^3.5.0:
diff@^3.2.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==
Expand Down

0 comments on commit 21a73b5

Please sign in to comment.