Skip to content

Commit 021925a

Browse files
authored
Merge pull request #2 from sergei-dyshel/master
Update to current version of code in VS Code.
2 parents af3ac69 + f82b57c commit 021925a

39 files changed

+10797
-803
lines changed

.unimportedrc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"entry": [
3+
"src/index.ts"
4+
],
5+
"rootDir": "./src",
6+
"aliases": {
7+
"vs/*": [
8+
"./vs/*"
9+
]
10+
},
11+
"ignoreUnresolved": [],
12+
"ignoreUnused": [],
13+
"ignoreUnimported": [
14+
"src/example.ts"
15+
]
16+
}

README.md

Lines changed: 135 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ The package includes typescript definitions.
1111
npm install vscode-diff --save
1212
```
1313
## Usage
14-
Typescript:
14+
15+
Starting from version 1.71.0 VS Code introduced new diffing algorithm that can also detect code moves. It is currently used by default in VS Code and can be accessed in this library as `AdvancedLinesDiffComputer`.
16+
The legacy algorithm can be accessed as `DiffComputer`.
17+
18+
### Example of legacy algorithm usage:
19+
1520
```typescript
1621
import { DiffComputer, IDiffComputerOpts, ILineChange } from 'vscode-diff';
1722

@@ -28,38 +33,40 @@ let diffComputer = new DiffComputer(originalLines, modifiedLines, options);
2833
let lineChanges: ILineChange[] = diffComputer.computeDiff().changes;
2934

3035
console.log(JSON.stringify(lineChanges, null, 2));
31-
// =>
32-
// [
33-
// {
34-
// "originalStartLineNumber": 2,
35-
// "originalEndLineNumber": 2,
36-
// "modifiedStartLineNumber": 2,
37-
// "modifiedEndLineNumber": 2,
38-
// "charChanges": [
39-
// {
40-
// "originalStartLineNumber": 2,
41-
// "originalStartColumn": 1,
42-
// "originalEndLineNumber": 2,
43-
// "originalEndColumn": 9,
44-
// "modifiedStartLineNumber": 2,
45-
// "modifiedStartColumn": 1,
46-
// "modifiedEndLineNumber": 2,
47-
// "modifiedEndColumn": 9
48-
// }
49-
// ]
50-
// },
51-
// {
52-
// "originalStartLineNumber": 3,
53-
// "originalEndLineNumber": 0,
54-
// "modifiedStartLineNumber": 4,
55-
// "modifiedEndLineNumber": 4
56-
// }
57-
// ]
5836
```
59-
Each element in the produced lineChanges array corresponds to a change from the original lines to the modified lines.
37+
Output:
38+
```json
39+
[
40+
{
41+
"originalStartLineNumber": 2,
42+
"originalEndLineNumber": 2,
43+
"modifiedStartLineNumber": 2,
44+
"modifiedEndLineNumber": 2,
45+
"charChanges": [
46+
{
47+
"originalStartLineNumber": 2,
48+
"originalStartColumn": 1,
49+
"originalEndLineNumber": 2,
50+
"originalEndColumn": 9,
51+
"modifiedStartLineNumber": 2,
52+
"modifiedStartColumn": 1,
53+
"modifiedEndLineNumber": 2,
54+
"modifiedEndColumn": 9
55+
}
56+
]
57+
},
58+
{
59+
"originalStartLineNumber": 3,
60+
"originalEndLineNumber": 0,
61+
"modifiedStartLineNumber": 4,
62+
"modifiedEndLineNumber": 4
63+
}
64+
]
65+
```
66+
Each element in the produced `lineChanges` array corresponds to a change from the original lines to the modified lines.
6067

6168
The column and row indices are 1-based. If a 0 index is present, it means that a row has been added/removed, eg:
62-
```
69+
```json
6370
{
6471
"originalStartLineNumber": 3,
6572
"originalEndLineNumber": 0,
@@ -70,7 +77,7 @@ The column and row indices are 1-based. If a 0 index is present, it means that a
7077
means that the 4th line in the modified text was added after line 3 in the original text.
7178

7279
The opposite:
73-
```
80+
```json
7481
{
7582
"originalStartLineNumber": 4,
7683
"originalEndLineNumber": 4,
@@ -80,8 +87,97 @@ The opposite:
8087
```
8188
means that the 4th line in the original text was removed from after line 3 in the modified text.
8289

90+
### Example of new algorithm usage:
91+
```typescript
92+
let advOptions: ILinesDiffComputerOptions = {
93+
ignoreTrimWhitespace: true,
94+
computeMoves: true,
95+
maxComputationTimeMs: 0
96+
}
97+
let advDiffComputer = new AdvancedLinesDiffComputer()
98+
let advLineChanges = advDiffComputer.computeDiff(originalLines, modifiedLines, advOptions).changes;
99+
100+
console.log(JSON.stringify(advLineChanges, null, 2));
101+
```
102+
103+
Output:
104+
```json
105+
[
106+
{
107+
"originalRange": {
108+
"startLineNumber": 2,
109+
"endLineNumberExclusive": 3
110+
},
111+
"modifiedRange": {
112+
"startLineNumber": 2,
113+
"endLineNumberExclusive": 3
114+
},
115+
"innerChanges": [
116+
{
117+
"originalRange": {
118+
"startLineNumber": 2,
119+
"startColumn": 1,
120+
"endLineNumber": 2,
121+
"endColumn": 9
122+
},
123+
"modifiedRange": {
124+
"startLineNumber": 2,
125+
"startColumn": 1,
126+
"endLineNumber": 2,
127+
"endColumn": 9
128+
}
129+
}
130+
]
131+
},
132+
{
133+
"originalRange": {
134+
"startLineNumber": 4,
135+
"endLineNumberExclusive": 4
136+
},
137+
"modifiedRange": {
138+
"startLineNumber": 4,
139+
"endLineNumberExclusive": 5
140+
},
141+
"innerChanges": [
142+
{
143+
"originalRange": {
144+
"startLineNumber": 3,
145+
"startColumn": 6,
146+
"endLineNumber": 3,
147+
"endColumn": 6
148+
},
149+
"modifiedRange": {
150+
"startLineNumber": 3,
151+
"startColumn": 6,
152+
"endLineNumber": 4,
153+
"endColumn": 7
154+
}
155+
}
156+
]
157+
}
158+
]
159+
```
160+
161+
The format of the result mapping is similar to that returned by `DiffComputer` but beware that then ending line number in line range is exclusive, e.g.
162+
```json
163+
"originalRange": {
164+
"startLineNumber": 2,
165+
"endLineNumberExclusive": 3
166+
}
167+
```
168+
169+
as opposed to
170+
```json
171+
"originalStartLineNumber": 2,
172+
"originalEndLineNumber": 2,
173+
```
174+
175+
83176
## Changelog
84177

178+
### 2.1.0
179+
* Update to VS Code 1.82.1 that introduces new diffing algorithm `AdvancedLinesDiffComputer`.
180+
85181
### 2.0.2
86182
* Fix issue [121436](https://github.com/microsoft/vscode/issues/121436)
87183

@@ -105,12 +201,14 @@ Initial release
105201
## Contribute
106202
Since we do not want this package to differ from the original implementation in VS Code, no changes that differs from the [source repository](https://github.com/Microsoft/vscode) will be merged. Any changes that only affect this npm package (like changes to this README) are welcome via pull requests.
107203

108-
If you want to help keep the diff algorithm up to date, you'll find from which commit and what file the code comes from in the top of the file, e.g:
109-
110-
_src/diffComputer.ts_
111-
```javascript
112-
// Updated from commit 46d1426 - vscode/src/vs/editor/common/diff/diffComputer.ts
113-
```
204+
Steps for updating diff algorithm:
205+
* Copy all necessary files from VS Code repo.
206+
* Verify with `npm run build` that all code is self-contained.
207+
* Verify with `npm run unimported` that there are no unused files.
208+
* Run `npm test` to run all the tests.
209+
* Update [src/example.ts] on any API changes.
210+
* Run `npm run example` and update this README with example usage code and output.
211+
* Include VS Code version and commit hash in commit message.
114212

115213
Any help documenting the diff API is very welcome.
116214

0 commit comments

Comments
 (0)