Skip to content

Commit 7b11eae

Browse files
committed
Wati for Html buffer updates before making requests
1 parent 0e111a2 commit 7b11eae

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Fix(ish) formatting of RenderFragments (C# templates) (PR: [#12397](https://github.com/dotnet/razor/pull/12397))
3030
* Drop Html edits that would split a C# literal across multiple lines (PR: [#12396](https://github.com/dotnet/razor/pull/12396))
3131
* Fix completion resolve for provisional completion (PR: [#12403](https://github.com/dotnet/razor/pull/12403))
32+
* Wait for Html buffer updates before making requests (PR: [#8748](https://github.com/dotnet/vscode-csharp/pull/8748))
3233

3334
# 2.96.x
3435
* Update Debugger to v2.95.0 (PR: [#8710](https://github.com/dotnet/vscode-csharp/pull/8710))

src/lsptoolshost/razor/htmlDocument.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class HtmlDocument {
1010
public readonly path: string;
1111
private content = '';
1212
private checksum = '';
13+
private previousVersion = -1;
1314

1415
public constructor(public readonly uri: vscode.Uri, checksum: string) {
1516
this.path = getUriPath(uri);
@@ -24,8 +25,21 @@ export class HtmlDocument {
2425
return this.checksum;
2526
}
2627

27-
public setContent(checksum: string, content: string) {
28+
public async setContent(checksum: string, content: string) {
29+
var document = await vscode.workspace.openTextDocument(this.uri);
30+
// Capture the version _before_ the change, so we can know for sure if it's been seen
31+
this.previousVersion = document.version;
2832
this.checksum = checksum;
2933
this.content = content;
3034
}
35+
36+
public async waitForBufferUpdate() {
37+
var document = await vscode.workspace.openTextDocument(this.uri);
38+
39+
// Wait for VS Code to process any previous content change. We don't care about finding
40+
// a specific version, just that it's moved on from the previous one.
41+
while (document.version === this.previousVersion) {
42+
await new Promise((resolve) => setTimeout(resolve, 10));
43+
}
44+
}
3145
}

src/lsptoolshost/razor/htmlDocumentManager.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ export class HtmlDocumentManager {
7979

8080
this.logger.logTrace(`New content for '${uri}', updating '${document.path}', checksum '${checksum}'.`);
8181

82-
await vscode.workspace.openTextDocument(document.uri);
83-
84-
document.setContent(checksum, text);
82+
await document.setContent(checksum, text);
8583

8684
this.contentProvider.fireDidChange(document.uri);
8785
}

src/lsptoolshost/razor/razorEndpoints.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ export function registerRazorEndpoints(
252252
return undefined;
253253
}
254254

255+
// We know that we've got the right document, and we've been told about the right content by the server,
256+
// but all we can be sure of at this point is that we've fired the change event for it. The event firing
257+
// is async, and the didChange notification that it would generate is a notification, so doesn't necessarily
258+
// block. Before we actually make a call to the Html server, we should at least make sure that the document
259+
// version is not still the same as before we updated the content.
260+
await document.waitForBufferUpdate();
261+
255262
return invocation(document, params.request);
256263
});
257264
}

0 commit comments

Comments
 (0)