Skip to content

Commit f3e2c0f

Browse files
committed
Fire telemetry if buffers get out of sync, and recover from same
1 parent c118328 commit f3e2c0f

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed

src/razor/src/csharp/csharpProjectedDocument.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export class CSharpProjectedDocument implements IProjectedDocument {
2929
return this.projectedDocumentVersion;
3030
}
3131

32+
public get length(): number {
33+
return this.content.length;
34+
}
35+
36+
public clear() {
37+
this.setContent('');
38+
}
39+
3240
public update(edits: ServerTextChange[], hostDocumentVersion: number) {
3341
this.removeProvisionalDot();
3442

src/razor/src/document/razorDocumentManager.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HtmlProjectedDocument } from '../html/htmlProjectedDocument';
99
import { RazorLanguage } from '../razorLanguage';
1010
import { RazorLanguageServerClient } from '../razorLanguageServerClient';
1111
import { RazorLogger } from '../razorLogger';
12+
import { TelemetryReporter } from '../telemetryReporter';
1213
import { UpdateBufferRequest } from '../rpc/updateBufferRequest';
1314
import { getUriPath } from '../uriPaths';
1415
import { IRazorDocument } from './IRazorDocument';
@@ -28,7 +29,11 @@ export class RazorDocumentManager implements IRazorDocumentManager {
2829

2930
public razorDocumentGenerationInitialized = false;
3031

31-
constructor(private readonly serverClient: RazorLanguageServerClient, private readonly logger: RazorLogger) {}
32+
constructor(
33+
private readonly serverClient: RazorLanguageServerClient,
34+
private readonly logger: RazorLogger,
35+
private readonly telemetryReporter: TelemetryReporter
36+
) {}
3237

3338
public get onChange() {
3439
return this.onChangeEmitter.event;
@@ -227,6 +232,7 @@ export class RazorDocumentManager implements IRazorDocumentManager {
227232
const projectedDocument = document.csharpDocument;
228233

229234
if (
235+
updateBufferRequest.previousWasEmpty ||
230236
!projectedDocument.hostDocumentSyncVersion ||
231237
projectedDocument.hostDocumentSyncVersion <= updateBufferRequest.hostDocumentVersion
232238
) {
@@ -237,6 +243,15 @@ export class RazorDocumentManager implements IRazorDocumentManager {
237243
await vscode.workspace.openTextDocument(document.csharpDocument.uri);
238244

239245
const csharpProjectedDocument = projectedDocument as CSharpProjectedDocument;
246+
247+
// If the language server is telling us that the previous document was empty, then we should clear
248+
// ours out. Hopefully outs would have been empty too, but there are cases where things get out of
249+
// sync
250+
if (updateBufferRequest.previousWasEmpty && projectedDocument.length !== 0) {
251+
this.telemetryReporter.reportBuffersOutOfSync();
252+
csharpProjectedDocument.clear();
253+
}
254+
240255
csharpProjectedDocument.update(updateBufferRequest.changes, updateBufferRequest.hostDocumentVersion);
241256

242257
this.notifyDocumentChange(document, RazorDocumentChangeKind.csharpChanged);
@@ -260,6 +275,7 @@ export class RazorDocumentManager implements IRazorDocumentManager {
260275
const projectedDocument = document.htmlDocument;
261276

262277
if (
278+
updateBufferRequest.previousWasEmpty ||
263279
!projectedDocument.hostDocumentSyncVersion ||
264280
projectedDocument.hostDocumentSyncVersion <= updateBufferRequest.hostDocumentVersion
265281
) {
@@ -270,6 +286,15 @@ export class RazorDocumentManager implements IRazorDocumentManager {
270286
await vscode.workspace.openTextDocument(document.htmlDocument.uri);
271287

272288
const htmlProjectedDocument = projectedDocument as HtmlProjectedDocument;
289+
290+
// If the language server is telling us that the previous document was empty, then we should clear
291+
// ours out. Hopefully outs would have been empty too, but there are cases where things get out of
292+
// sync
293+
if (updateBufferRequest.previousWasEmpty && projectedDocument.length !== 0) {
294+
this.telemetryReporter.reportBuffersOutOfSync();
295+
htmlProjectedDocument.clear();
296+
}
297+
273298
htmlProjectedDocument.update(updateBufferRequest.changes, updateBufferRequest.hostDocumentVersion);
274299

275300
this.notifyDocumentChange(document, RazorDocumentChangeKind.htmlChanged);

src/razor/src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export async function activate(
117117

118118
const languageServiceClient = new RazorLanguageServiceClient(languageServerClient);
119119

120-
const documentManager = new RazorDocumentManager(languageServerClient, logger);
120+
const documentManager = new RazorDocumentManager(languageServerClient, logger, razorTelemetryReporter);
121121
const documentSynchronizer = new RazorDocumentSynchronizer(documentManager, logger);
122122
reportTelemetryForDocuments(documentManager, razorTelemetryReporter);
123123
const languageConfiguration = new RazorLanguageConfiguration();

src/razor/src/html/htmlProjectedDocument.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export class HtmlProjectedDocument implements IProjectedDocument {
2626
return this.projectedDocumentVersion;
2727
}
2828

29+
public get length(): number {
30+
return this.content.length;
31+
}
32+
33+
public clear() {
34+
this.setContent('');
35+
}
36+
2937
public update(edits: ServerTextChange[], hostDocumentVersion: number) {
3038
this.hostDocumentVersion = hostDocumentVersion;
3139

src/razor/src/projection/IProjectedDocument.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export interface IProjectedDocument {
1010
readonly uri: vscode.Uri;
1111
readonly hostDocumentSyncVersion: number | null;
1212
readonly projectedDocumentSyncVersion: number;
13+
readonly length: number;
1314
getContent(): string;
1415
}

src/razor/src/rpc/updateBufferRequest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class UpdateBufferRequest {
99
constructor(
1010
public readonly hostDocumentVersion: number,
1111
public readonly hostDocumentFilePath: string,
12-
public readonly changes: ServerTextChange[]
12+
public readonly changes: ServerTextChange[],
13+
public readonly previousWasEmpty: boolean
1314
) {}
1415
}

src/razor/src/telemetryReporter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class TelemetryReporter {
1010
private readonly razorExtensionActivated = createTelemetryEvent('VSCode.Razor.RazorExtensionActivated');
1111
private readonly debugLanguageServerEvent = createTelemetryEvent('VSCode.Razor.DebugLanguageServer');
1212
private readonly workspaceContainsRazorEvent = createTelemetryEvent('VSCode.Razor.WorkspaceContainsRazor');
13+
private readonly buffersOutOfSyncEvent = createTelemetryEvent('VSCode.Razor.BuffersOutOfSync');
1314
private reportedWorkspaceContainsRazor = false;
1415

1516
constructor(private readonly eventStream: HostEventStream) {
@@ -24,6 +25,10 @@ export class TelemetryReporter {
2425
this.eventStream.post(traceLevelEvent);
2526
}
2627

28+
public reportBuffersOutOfSync() {
29+
this.eventStream.post(this.buffersOutOfSyncEvent);
30+
}
31+
2732
public reportErrorOnServerStart(error: unknown) {
2833
let realError;
2934
if (error instanceof Error) {

0 commit comments

Comments
 (0)