forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.ts
More file actions
245 lines (223 loc) · 7.85 KB
/
editor.ts
File metadata and controls
245 lines (223 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import {TextEdit, Position, Range, TextDocument} from 'vscode';
import * as dmp from 'diff-match-patch';
import {EOL} from 'os';
import * as fs from 'fs';
import * as path from 'path';
const tmp = require('tmp');
// Code borrowed from goFormat.ts (Go Extension for VS Code)
const EDIT_DELETE = 0;
const EDIT_INSERT = 1;
const EDIT_REPLACE = 2;
const NEW_LINE_LENGTH = EOL.length;
class Patch {
diffs: dmp.Diff[];
start1: number;
start2: number;
length1: number;
length2: number;
}
class Edit {
action: number;
start: Position;
end: Position;
text: string;
constructor(action: number, start: Position) {
this.action = action;
this.start = start;
this.text = '';
}
apply(): TextEdit {
switch (this.action) {
case EDIT_INSERT:
return TextEdit.insert(this.start, this.text);
case EDIT_DELETE:
return TextEdit.delete(new Range(this.start, this.end));
case EDIT_REPLACE:
return TextEdit.replace(new Range(this.start, this.end), this.text);
}
}
}
export function getTextEditsFromPatch(before: string, patch: string): TextEdit[] {
if (patch.startsWith('---')) {
// Strip the first two lines
patch = patch.substring(patch.indexOf('@@'));
}
if (patch.length === 0) {
return [];
}
// Remove the text added by unified_diff
// # Work around missing newline (http://bugs.python.org/issue2142).
patch = patch.replace(/\\ No newline at end of file[\r\n]/, '');
let d = new dmp.diff_match_patch();
let patches: any[] = patch_fromText.call(d, patch);
if (!Array.isArray(patches) || patches.length === 0) {
throw new Error('Unable to parse Patch string');
}
let textEdits = [];
// Add line feeds
// & build the text edits
patches.forEach(patch => {
patch.diffs.forEach(diff => {
diff[1] += EOL;
});
textEdits = textEdits.concat(getTextEditsInternal(before, patch.diffs, patch.start1));
});
return textEdits;
}
export function getTextEdits(before: string, after: string): TextEdit[] {
let d = new dmp.diff_match_patch();
let diffs = d.diff_main(before, after);
return getTextEditsInternal(before, diffs);
}
function getTextEditsInternal(before: string, diffs: [number, string][], startLine: number = 0): TextEdit[] {
let line = startLine;
let character = 0;
if (line > 0) {
let beforeLines = <string[]>before.split(/\r?\n/g);
beforeLines.filter((l, i) => i < line).forEach(l => character += l.length + NEW_LINE_LENGTH);
}
let edits: TextEdit[] = [];
let edit: Edit = null;
for (let i = 0; i < diffs.length; i++) {
let start = new Position(line, character);
// Compute the line/character after the diff is applied.
for (let curr = 0; curr < diffs[i][1].length; curr++) {
if (diffs[i][1][curr] !== '\n') {
character++;
} else {
character = 0;
line++;
}
}
switch (diffs[i][0]) {
case dmp.DIFF_DELETE:
if (edit == null) {
edit = new Edit(EDIT_DELETE, start);
} else if (edit.action !== EDIT_DELETE) {
throw new Error('cannot format due to an internal error.');
}
edit.end = new Position(line, character);
break;
case dmp.DIFF_INSERT:
if (edit == null) {
edit = new Edit(EDIT_INSERT, start);
} else if (edit.action === EDIT_DELETE) {
edit.action = EDIT_REPLACE;
}
// insert and replace edits are all relative to the original state
// of the document, so inserts should reset the current line/character
// position to the start.
line = start.line;
character = start.character;
edit.text += diffs[i][1];
break;
case dmp.DIFF_EQUAL:
if (edit != null) {
edits.push(edit.apply());
edit = null;
}
break;
}
}
if (edit != null) {
edits.push(edit.apply());
}
return edits;
}
export function getTempFileWithDocumentContents(document: TextDocument): Promise<string> {
return new Promise<string>((resolve, reject) => {
let ext = path.extname(document.uri.fsPath);
let tmp = require('tmp');
tmp.file({ postfix: ext }, function (err, tmpFilePath, fd) {
if (err) {
return reject(err);
}
fs.writeFile(tmpFilePath, document.getText(), ex => {
if (ex) {
return reject(`Failed to create a temporary file, ${ex.message}`);
}
resolve(tmpFilePath);
});
});
});
}
/**
* Parse a textual representation of patches and return a list of Patch objects.
* @param {string} textline Text representation of patches.
* @return {!Array.<!diff_match_patch.patch_obj>} Array of Patch objects.
* @throws {!Error} If invalid input.
*/
function patch_fromText(textline) {
var patches = [];
if (!textline) {
return patches;
}
// Start Modification by Don Jayamanne 24/06/2016 Support for CRLF
var text = textline.split(/[\r\n]/);
// End Modification
var textPointer = 0;
var patchHeader = /^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$/;
while (textPointer < text.length) {
var m = text[textPointer].match(patchHeader);
if (!m) {
throw new Error('Invalid patch string: ' + text[textPointer]);
}
var patch = new (<any>dmp.diff_match_patch).patch_obj();
patches.push(patch);
patch.start1 = parseInt(m[1], 10);
if (m[2] === '') {
patch.start1--;
patch.length1 = 1;
} else if (m[2] == '0') {
patch.length1 = 0;
} else {
patch.start1--;
patch.length1 = parseInt(m[2], 10);
}
patch.start2 = parseInt(m[3], 10);
if (m[4] === '') {
patch.start2--;
patch.length2 = 1;
} else if (m[4] == '0') {
patch.length2 = 0;
} else {
patch.start2--;
patch.length2 = parseInt(m[4], 10);
}
textPointer++;
while (textPointer < text.length) {
var sign = text[textPointer].charAt(0);
try {
//var line = decodeURI(text[textPointer].substring(1));
// For some reason the patch generated by python files don't encode any characters
// And this patch module (code from Google) is expecting the text to be encoded!!
// Temporary solution, disable decoding
// Issue #188
var line = text[textPointer].substring(1);
} catch (ex) {
// Malformed URI sequence.
throw new Error('Illegal escape in patch_fromText: ' + line);
}
if (sign == '-') {
// Deletion.
patch.diffs.push([dmp.DIFF_DELETE, line]);
} else if (sign == '+') {
// Insertion.
patch.diffs.push([dmp.DIFF_INSERT, line]);
} else if (sign == ' ') {
// Minor equality.
patch.diffs.push([dmp.DIFF_EQUAL, line]);
} else if (sign == '@') {
// Start of next patch.
break;
} else if (sign === '') {
// Blank line? Whatever.
} else {
// WTF?
throw new Error('Invalid patch mode "' + sign + '" in: ' + line);
}
textPointer++;
}
}
return patches;
}