-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathDocumentFormatter.ts
497 lines (421 loc) · 17.7 KB
/
DocumentFormatter.ts
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as path from "path";
import vscode = require('vscode');
import {
TextDocument,
TextEdit,
FormattingOptions,
CancellationToken,
DocumentFormattingEditProvider,
DocumentRangeFormattingEditProvider,
OnTypeFormattingEditProvider,
Position,
Range,
TextEditor,
TextLine
} from 'vscode';
import { LanguageClient, RequestType } from 'vscode-languageclient';
import Window = vscode.window;
import { IFeature } from '../feature';
import * as Settings from '../settings';
import * as Utils from '../utils';
import * as AnimatedStatusBar from '../controls/animatedStatusBar';
export namespace ScriptFileMarkersRequest {
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getScriptFileMarkers"; } };
}
export namespace ScriptRegionRequest {
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getScriptRegion"; } };
}
interface ScriptRegionRequestParams {
fileUri: string;
character: string;
line: number;
column: number;
}
interface ScriptRegionRequestResult {
scriptRegion: ScriptRegion;
}
// TODO move some of the common interface to a separate file?
interface ScriptFileMarkersRequestParams {
fileUri: string;
settings: any;
}
interface ScriptFileMarkersRequestResultParams {
markers: ScriptFileMarker[];
}
interface ScriptFileMarker {
message: string;
level: ScriptFileMarkerLevel;
scriptRegion: ScriptRegion;
correction: MarkerCorrection;
}
enum ScriptFileMarkerLevel {
Information = 0,
Warning,
Error
}
interface ScriptRegion {
file: string;
text: string;
startLineNumber: number;
startColumnNumber: number;
startOffset: number;
endLineNumber: number;
endColumnNumber: number;
endOffset: number;
}
interface MarkerCorrection {
name: string;
edits: ScriptRegion[];
}
function toRange(scriptRegion: ScriptRegion): vscode.Range {
return new vscode.Range(
scriptRegion.startLineNumber - 1,
scriptRegion.startColumnNumber - 1,
scriptRegion.endLineNumber - 1,
scriptRegion.endColumnNumber - 1);
}
function toOneBasedPosition(position: Position): Position {
return position.translate({ lineDelta: 1, characterDelta: 1 });
}
function editComparer(leftOperand: ScriptRegion, rightOperand: ScriptRegion): number {
if (leftOperand.startLineNumber < rightOperand.startLineNumber) {
return -1;
} else if (leftOperand.startLineNumber > rightOperand.startLineNumber) {
return 1;
} else {
if (leftOperand.startColumnNumber < rightOperand.startColumnNumber) {
return -1;
}
else if (leftOperand.startColumnNumber > rightOperand.startColumnNumber) {
return 1;
}
else {
return 0;
}
}
}
class DocumentLocker {
private lockedDocuments: Object;
constructor() {
this.lockedDocuments = new Object();
}
isLocked(document: TextDocument): boolean {
return this.isLockedInternal(this.getKey(document));
}
lock(document: TextDocument, unlockWhenDone?: Thenable<any>): void {
this.lockInternal(this.getKey(document), unlockWhenDone);
}
unlock(document: TextDocument): void {
this.unlockInternal(this.getKey(document));
}
unlockAll(): void {
Object.keys(this.lockedDocuments).slice().forEach(documentKey => this.unlockInternal(documentKey));
}
private getKey(document: TextDocument): string {
return document.uri.toString();
}
private lockInternal(documentKey: string, unlockWhenDone?: Thenable<any>): void {
if (!this.isLockedInternal(documentKey)) {
this.lockedDocuments[documentKey] = true;
}
if (unlockWhenDone !== undefined) {
unlockWhenDone.then(() => this.unlockInternal(documentKey));
}
}
private unlockInternal(documentKey: string): void {
if (this.isLockedInternal(documentKey)) {
delete this.lockedDocuments[documentKey];
}
}
private isLockedInternal(documentKey: string): boolean {
return this.lockedDocuments.hasOwnProperty(documentKey);
}
}
class PSDocumentFormattingEditProvider implements
DocumentFormattingEditProvider,
DocumentRangeFormattingEditProvider,
OnTypeFormattingEditProvider {
private static documentLocker = new DocumentLocker();
private static statusBarTracker = new Object();
private languageClient: LanguageClient;
private lineDiff: number;
// The order in which the rules will be executed starting from the first element.
private readonly ruleOrder: string[] = [
"PSPlaceCloseBrace",
"PSPlaceOpenBrace",
"PSUseConsistentWhitespace",
"PSUseConsistentIndentation",
"PSAlignAssignmentStatement"]
// Allows edits to be undone and redone is a single step.
// It is usefuld to have undo stops after every edit while debugging
// hence we keep this as an option but set it true by default.
private aggregateUndoStop: boolean;
private get emptyPromise(): Promise<TextEdit[]> {
return Promise.resolve(TextEdit[0]);
}
constructor(aggregateUndoStop = true) {
this.aggregateUndoStop = aggregateUndoStop;
this.lineDiff = 0;
}
provideDocumentFormattingEdits(
document: TextDocument,
options: FormattingOptions,
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
return this.provideDocumentRangeFormattingEdits(document, null, options, token);
}
provideDocumentRangeFormattingEdits(
document: TextDocument,
range: Range,
options: FormattingOptions,
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
let editor: TextEditor = this.getEditor(document);
if (editor === undefined) {
return this.emptyPromise;
}
// Check if the document is already being formatted.
// If so, then ignore the formatting request.
if (this.isDocumentLocked(document)) {
return this.emptyPromise;
}
let textEdits: Thenable<TextEdit[]> = this.executeRulesInOrder(editor, range, options, 0);
this.lockDocument(document, textEdits);
PSDocumentFormattingEditProvider.showStatusBar(document, textEdits);
return textEdits;
}
provideOnTypeFormattingEdits(
document: TextDocument,
position: Position,
ch: string,
options: FormattingOptions,
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
return this.getScriptRegion(document, position, ch).then(scriptRegion => {
if (scriptRegion === null) {
return this.emptyPromise;
}
return this.provideDocumentRangeFormattingEdits(
document,
toRange(scriptRegion),
options,
token);
});
}
setLanguageClient(languageClient: LanguageClient): void {
this.languageClient = languageClient;
// setLanguageClient is called while restarting a session,
// so this makes sure we clean up the document locker and
// any residual status bars
PSDocumentFormattingEditProvider.documentLocker.unlockAll();
PSDocumentFormattingEditProvider.disposeAllStatusBars();
}
private getScriptRegion(document: TextDocument, position: Position, ch: string): Thenable<ScriptRegion> {
let oneBasedPosition = toOneBasedPosition(position);
return this.languageClient.sendRequest(
ScriptRegionRequest.type,
{
fileUri: document.uri.toString(),
character: ch,
line: oneBasedPosition.line,
column: oneBasedPosition.character
}).then((result: ScriptRegionRequestResult) => {
if (result === null) {
return null;
}
return result.scriptRegion;
});
}
private snapRangeToEdges(range: Range, document: TextDocument): Range {
return range.with({
start: range.start.with({ character: 0 }),
end: document.lineAt(range.end.line).range.end });
}
private getEditor(document: TextDocument): TextEditor {
return Window.visibleTextEditors.find((e, n, obj) => { return e.document === document; });
}
private isDocumentLocked(document: TextDocument): boolean {
return PSDocumentFormattingEditProvider.documentLocker.isLocked(document);
}
private lockDocument(document: TextDocument, unlockWhenDone: Thenable<any>): void {
PSDocumentFormattingEditProvider.documentLocker.lock(document, unlockWhenDone);
}
private executeRulesInOrder(
editor: TextEditor,
range: Range,
options: FormattingOptions,
index: number): Thenable<TextEdit[]> {
if (this.languageClient !== null && index < this.ruleOrder.length) {
let rule: string = this.ruleOrder[index];
let uniqueEdits: ScriptRegion[] = [];
let document: TextDocument = editor.document;
let edits: ScriptRegion[];
return this.languageClient.sendRequest(
ScriptFileMarkersRequest.type,
{
fileUri: document.uri.toString(),
settings: this.getSettings(rule)
})
.then((result: ScriptFileMarkersRequestResultParams) => {
edits = result.markers.map(marker => { return marker.correction.edits[0]; });
// sort in decending order of the edits
edits.sort((left: ScriptRegion, right: ScriptRegion) => {
return -1 * editComparer(left, right);
});
// we need to update the range as the edits might
// have changed the original layout
if (range !== null) {
if (this.lineDiff !== 0) {
range = range.with({ end: range.end.translate({ lineDelta: this.lineDiff }) });
}
// extend the range such that it starts at the first character of the
// start line of the range.
range = this.snapRangeToEdges(range, document);
// filter edits that are contained in the input range
edits = edits.filter(edit => range.contains(toRange(edit).start));
}
// We cannot handle multiple edits at the same point hence we
// filter the markers so that there is only one edit per region
if (edits.length > 0) {
uniqueEdits.push(edits[0]);
for (let edit of edits.slice(1)) {
let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1];
if (lastEdit.startLineNumber !== edit.startLineNumber
|| (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) {
uniqueEdits.push(edit);
}
}
}
// reset line difference to 0
this.lineDiff = 0;
// we do not return a valid array because our text edits
// need to be executed in a particular order and it is
// easier if we perform the edits ourselves
return this.applyEdit(editor, uniqueEdits, 0, index);
})
.then(() => {
// execute the same rule again if we left out violations
// on the same line
let newIndex: number = index + 1;
if (uniqueEdits.length !== edits.length) {
newIndex = index;
}
return this.executeRulesInOrder(editor, range, options, newIndex);
});
} else {
return this.emptyPromise;
}
}
private applyEdit(
editor: TextEditor,
edits: ScriptRegion[],
markerIndex: number,
ruleIndex: number): Thenable<void> {
if (markerIndex >= edits.length) {
return;
}
let undoStopAfter = !this.aggregateUndoStop || (ruleIndex === this.ruleOrder.length - 1 && markerIndex === edits.length - 1);
let undoStopBefore = !this.aggregateUndoStop || (ruleIndex === 0 && markerIndex === 0);
let edit: ScriptRegion = edits[markerIndex];
let editRange: Range = toRange(edit);
// accumulate the changes in number of lines
// get the difference between the number of lines in the replacement text and
// that of the original text
this.lineDiff += this.getNumLines(edit.text) - (editRange.end.line - editRange.start.line + 1);
return editor.edit((editBuilder) => {
editBuilder.replace(
editRange,
edit.text);
},
{
undoStopAfter: undoStopAfter,
undoStopBefore: undoStopBefore
}).then((isEditApplied) => {
return this.applyEdit(editor, edits, markerIndex + 1, ruleIndex);
}); // TODO handle rejection
}
private getNumLines(text: string): number {
return text.split(/\r?\n/).length;
}
private getSettings(rule: string): any {
let psSettings: Settings.ISettings = Settings.load(Utils.PowerShellLanguageId);
let ruleSettings = new Object();
ruleSettings["Enable"] = true;
switch (rule) {
case "PSPlaceOpenBrace":
ruleSettings["OnSameLine"] = psSettings.codeFormatting.openBraceOnSameLine;
ruleSettings["NewLineAfter"] = psSettings.codeFormatting.newLineAfterOpenBrace;
ruleSettings["IgnoreOneLineBlock"] = psSettings.codeFormatting.ignoreOneLineBlock;
break;
case "PSPlaceCloseBrace":
ruleSettings["IgnoreOneLineBlock"] = psSettings.codeFormatting.ignoreOneLineBlock;
ruleSettings["NewLineAfter"] = psSettings.codeFormatting.newLineAfterCloseBrace;
break;
case "PSUseConsistentIndentation":
ruleSettings["IndentationSize"] = vscode.workspace.getConfiguration("editor").get<number>("tabSize");
break;
case "PSUseConsistentWhitespace":
ruleSettings["CheckOpenBrace"] = psSettings.codeFormatting.whitespaceBeforeOpenBrace;
ruleSettings["CheckOpenParen"] = psSettings.codeFormatting.whitespaceBeforeOpenParen;
ruleSettings["CheckOperator"] = psSettings.codeFormatting.whitespaceAroundOperator;
ruleSettings["CheckSeparator"] = psSettings.codeFormatting.whitespaceAfterSeparator;
break;
case "PSAlignAssignmentStatement":
ruleSettings["CheckHashtable"] = psSettings.codeFormatting.alignPropertyValuePairs;
break;
default:
break;
}
let settings: Object = new Object();
settings[rule] = ruleSettings;
return settings;
}
private static showStatusBar(document: TextDocument, hideWhenDone: Thenable<any>): void {
let statusBar = AnimatedStatusBar.showAnimatedStatusBarMessage("Formatting PowerShell document", hideWhenDone);
this.statusBarTracker[document.uri.toString()] = statusBar;
hideWhenDone.then(() => {
this.disposeStatusBar(document.uri.toString());
});
}
private static disposeStatusBar(documentUri: string) {
if (this.statusBarTracker.hasOwnProperty(documentUri)) {
this.statusBarTracker[documentUri].dispose();
delete this.statusBarTracker[documentUri];
}
}
private static disposeAllStatusBars() {
Object.keys(this.statusBarTracker).slice().forEach((key) => this.disposeStatusBar(key));
}
}
export class DocumentFormatterFeature implements IFeature {
private firstTriggerCharacter: string = "}";
private moreTriggerCharacters: string[] = ["\n"];
private formattingEditProvider: vscode.Disposable;
private rangeFormattingEditProvider: vscode.Disposable;
private onTypeFormattingEditProvider: vscode.Disposable;
private languageClient: LanguageClient;
private documentFormattingEditProvider: PSDocumentFormattingEditProvider;
constructor() {
this.documentFormattingEditProvider = new PSDocumentFormattingEditProvider();
this.formattingEditProvider = vscode.languages.registerDocumentFormattingEditProvider(
"powershell",
this.documentFormattingEditProvider);
this.rangeFormattingEditProvider = vscode.languages.registerDocumentRangeFormattingEditProvider(
"powershell",
this.documentFormattingEditProvider);
this.onTypeFormattingEditProvider = vscode.languages.registerOnTypeFormattingEditProvider(
"powershell",
this.documentFormattingEditProvider,
this.firstTriggerCharacter,
...this.moreTriggerCharacters);
}
public setLanguageClient(languageclient: LanguageClient): void {
this.languageClient = languageclient;
this.documentFormattingEditProvider.setLanguageClient(languageclient);
}
public dispose(): any {
this.formattingEditProvider.dispose();
this.rangeFormattingEditProvider.dispose();
this.onTypeFormattingEditProvider.dispose();
}
}