Skip to content

Commit ad3566f

Browse files
committed
Add logging for document formatting operations
This change adds additional logging for document formatting operations to help diagnose user reported issues with this feature.
1 parent 9f6e02f commit ad3566f

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

src/features/DocumentFormatter.ts

+57-25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from 'vscode-languageclient';
2727
import { TextDocumentIdentifier } from "vscode-languageserver-types";
2828
import Window = vscode.window;
29+
import { Logger } from '../logging';
2930
import { IFeature } from '../feature';
3031
import * as Settings from '../settings';
3132
import * as Utils from '../utils';
@@ -130,22 +131,60 @@ class PSDocumentFormattingEditProvider implements
130131
return Promise.resolve(TextEdit[0]);
131132
}
132133

133-
constructor() {
134+
constructor(private logger: Logger) {
134135
}
135136

136137
provideDocumentFormattingEdits(
137138
document: TextDocument,
138139
options: FormattingOptions,
139140
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
140-
return this.provideDocumentRangeFormattingEdits(document, null, options, token);
141-
}
141+
142+
this.logger.writeVerbose(`Formatting entire document - ${document.uri}...`)
143+
return this.sendDocumentFormatRequest(document, null, options, token);
144+
}
142145

143146
provideDocumentRangeFormattingEdits(
144147
document: TextDocument,
145148
range: Range,
146149
options: FormattingOptions,
147150
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
148151

152+
this.logger.writeVerbose(`Formatting document range ${JSON.stringify(range)} - ${document.uri}...`)
153+
return this.sendDocumentFormatRequest(document, range, options, token);
154+
}
155+
156+
provideOnTypeFormattingEdits(
157+
document: TextDocument,
158+
position: Position,
159+
ch: string,
160+
options: FormattingOptions,
161+
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
162+
163+
this.logger.writeVerbose(`Formatting on type at position ${JSON.stringify(position)} - ${document.uri}...`)
164+
165+
return this.getScriptRegion(document, position, ch).then(scriptRegion => {
166+
if (scriptRegion === null) {
167+
this.logger.writeVerbose("No formattable range returned.");
168+
return this.emptyPromise;
169+
}
170+
171+
return this.sendDocumentFormatRequest(
172+
document,
173+
toRange(scriptRegion),
174+
options,
175+
token);
176+
},
177+
(err) => {
178+
this.logger.writeVerbose(`Error while requesting script region for formatting: ${err}`)
179+
});
180+
}
181+
182+
private sendDocumentFormatRequest(
183+
document: TextDocument,
184+
range: Range,
185+
options: FormattingOptions,
186+
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
187+
149188
let editor: TextEditor = this.getEditor(document);
150189
if (editor === undefined) {
151190
return this.emptyPromise;
@@ -157,7 +196,6 @@ class PSDocumentFormattingEditProvider implements
157196
return this.emptyPromise;
158197
}
159198

160-
161199
// somehow range object gets serialized to an array of Position objects,
162200
// so we need to use the object literal syntax to initialize it.
163201
let rangeParam = null;
@@ -180,31 +218,25 @@ class PSDocumentFormattingEditProvider implements
180218
options: this.getEditorSettings()
181219
};
182220

221+
let formattingStartTime = new Date().valueOf();
222+
function getFormattingDuration() {
223+
return ((new Date().valueOf()) - formattingStartTime) / 1000;
224+
}
225+
183226
let textEdits = this.languageClient.sendRequest(
184227
DocumentRangeFormattingRequest.type,
185228
requestParams);
186229
this.lockDocument(document, textEdits);
187230
PSDocumentFormattingEditProvider.showStatusBar(document, textEdits);
188-
return textEdits;
189-
}
190-
191-
provideOnTypeFormattingEdits(
192-
document: TextDocument,
193-
position: Position,
194-
ch: string,
195-
options: FormattingOptions,
196-
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
197-
return this.getScriptRegion(document, position, ch).then(scriptRegion => {
198-
if (scriptRegion === null) {
199-
return this.emptyPromise;
200-
}
201231

202-
return this.provideDocumentRangeFormattingEdits(
203-
document,
204-
toRange(scriptRegion),
205-
options,
206-
token);
207-
});
232+
return textEdits.then(
233+
(edits) => {
234+
this.logger.writeVerbose(`Document formatting finished in ${getFormattingDuration()}s`);
235+
return edits;
236+
},
237+
(err) => {
238+
this.logger.writeVerbose(`Document formatting failed in ${getFormattingDuration()}: ${err}`);
239+
});
208240
}
209241

210242
setLanguageClient(languageClient: LanguageClient): void {
@@ -284,8 +316,8 @@ export class DocumentFormatterFeature implements IFeature {
284316
private languageClient: LanguageClient;
285317
private documentFormattingEditProvider: PSDocumentFormattingEditProvider;
286318

287-
constructor() {
288-
this.documentFormattingEditProvider = new PSDocumentFormattingEditProvider();
319+
constructor(private logger: Logger) {
320+
this.documentFormattingEditProvider = new PSDocumentFormattingEditProvider(logger);
289321
this.formattingEditProvider = vscode.languages.registerDocumentFormattingEditProvider(
290322
"powershell",
291323
this.documentFormattingEditProvider);

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function activate(context: vscode.ExtensionContext): void {
117117
new SelectPSSARulesFeature(),
118118
new CodeActionsFeature(),
119119
new NewFileOrProjectFeature(),
120-
new DocumentFormatterFeature(),
120+
new DocumentFormatterFeature(logger),
121121
new RemoteFilesFeature(),
122122
new DebugSessionFeature(sessionManager),
123123
new PickPSHostProcessFeature(),

0 commit comments

Comments
 (0)