Skip to content

Commit 85a80e0

Browse files
author
Kapil Borle
committed
Prevent concurrent formatting requests for a document
A user can execute `Format Document` command on a document even if the same document is already being formatted. This sends another, redundant, formatting request to the server for the same file. Another annoying side effect is that a new formatting status for the same file gets created on the status bar.
1 parent 9b2a6c6 commit 85a80e0

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/features/DocumentFormatter.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ interface ScriptRegion {
5959

6060
interface MarkerCorrection {
6161
name: string;
62-
edits: ScriptRegion[]
62+
edits: ScriptRegion[];
6363
}
6464

6565
function editComparer(leftOperand: ScriptRegion, rightOperand: ScriptRegion): number {
@@ -81,6 +81,7 @@ function editComparer(leftOperand: ScriptRegion, rightOperand: ScriptRegion): nu
8181
}
8282

8383
class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider {
84+
private static filesBeingFormatted: Object = new Object;
8485
private languageClient: LanguageClient;
8586

8687
// The order in which the rules will be executed starting from the first element.
@@ -111,11 +112,43 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
111112
options: FormattingOptions,
112113
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
113114

115+
if (this.isDocumentLocked(document)) {
116+
return;
117+
}
118+
119+
this.lockDocument(document);
114120
let textEdits: Thenable<TextEdit[]> = this.executeRulesInOrder(document, range, options, 0);
121+
this.releaseDocument(document, textEdits);
115122
AnimatedStatusBar.showAnimatedStatusBarMessage("Formatting PowerShell document", textEdits);
116123
return textEdits;
117124
}
118125

126+
isDocumentLocked(document: TextDocument): boolean {
127+
if (PSDocumentFormattingEditProvider.filesBeingFormatted.hasOwnProperty(this.getDocumentKey(document))) {
128+
return true;
129+
}
130+
131+
return false;
132+
}
133+
134+
lockDocument(document: TextDocument): void {
135+
if (!this.isDocumentLocked(document)) {
136+
PSDocumentFormattingEditProvider.filesBeingFormatted[this.getDocumentKey(document)] = true;
137+
}
138+
}
139+
140+
releaseDocument(document: TextDocument, releaseWhenDone: Thenable<any>): void {
141+
if (this.isDocumentLocked(document)) {
142+
releaseWhenDone.then(() => {
143+
delete PSDocumentFormattingEditProvider.filesBeingFormatted[this.getDocumentKey(document)];
144+
});
145+
}
146+
}
147+
148+
getDocumentKey(document: TextDocument): string {
149+
return document.uri.toString();
150+
}
151+
119152
executeRulesInOrder(
120153
document: TextDocument,
121154
range: Range,

0 commit comments

Comments
 (0)