Skip to content

Commit

Permalink
Merge pull request #6494 from davidwengier/BuffersOutOfSync
Browse files Browse the repository at this point in the history
Fire telemetry if Razor buffers get out of sync, and recover from same
  • Loading branch information
davidwengier authored Oct 5, 2023
2 parents b49adf5 + 369e566 commit 1564600
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/razor/src/csharp/csharpProjectedDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export class CSharpProjectedDocument implements IProjectedDocument {
return this.projectedDocumentVersion;
}

public get length(): number {
return this.content.length;
}

public clear() {
this.setContent('');
}

public update(edits: ServerTextChange[], hostDocumentVersion: number) {
this.removeProvisionalDot();

Expand Down
27 changes: 26 additions & 1 deletion src/razor/src/document/razorDocumentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HtmlProjectedDocument } from '../html/htmlProjectedDocument';
import { RazorLanguage } from '../razorLanguage';
import { RazorLanguageServerClient } from '../razorLanguageServerClient';
import { RazorLogger } from '../razorLogger';
import { TelemetryReporter } from '../telemetryReporter';
import { UpdateBufferRequest } from '../rpc/updateBufferRequest';
import { getUriPath } from '../uriPaths';
import { IRazorDocument } from './IRazorDocument';
Expand All @@ -28,7 +29,11 @@ export class RazorDocumentManager implements IRazorDocumentManager {

public razorDocumentGenerationInitialized = false;

constructor(private readonly serverClient: RazorLanguageServerClient, private readonly logger: RazorLogger) {}
constructor(
private readonly serverClient: RazorLanguageServerClient,
private readonly logger: RazorLogger,
private readonly telemetryReporter: TelemetryReporter
) {}

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

if (
updateBufferRequest.previousWasEmpty ||
!projectedDocument.hostDocumentSyncVersion ||
projectedDocument.hostDocumentSyncVersion <= updateBufferRequest.hostDocumentVersion
) {
Expand All @@ -237,6 +243,15 @@ export class RazorDocumentManager implements IRazorDocumentManager {
await vscode.workspace.openTextDocument(document.csharpDocument.uri);

const csharpProjectedDocument = projectedDocument as CSharpProjectedDocument;

// If the language server is telling us that the previous document was empty, then we should clear
// ours out. Hopefully ours would have been empty too, but there are cases where things get out of
// sync
if (updateBufferRequest.previousWasEmpty && projectedDocument.length !== 0) {
this.telemetryReporter.reportBuffersOutOfSync();
csharpProjectedDocument.clear();
}

csharpProjectedDocument.update(updateBufferRequest.changes, updateBufferRequest.hostDocumentVersion);

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

if (
updateBufferRequest.previousWasEmpty ||
!projectedDocument.hostDocumentSyncVersion ||
projectedDocument.hostDocumentSyncVersion <= updateBufferRequest.hostDocumentVersion
) {
Expand All @@ -270,6 +286,15 @@ export class RazorDocumentManager implements IRazorDocumentManager {
await vscode.workspace.openTextDocument(document.htmlDocument.uri);

const htmlProjectedDocument = projectedDocument as HtmlProjectedDocument;

// If the language server is telling us that the previous document was empty, then we should clear
// ours out. Hopefully ours would have been empty too, but there are cases where things get out of
// sync
if (updateBufferRequest.previousWasEmpty && projectedDocument.length !== 0) {
this.telemetryReporter.reportBuffersOutOfSync();
htmlProjectedDocument.clear();
}

htmlProjectedDocument.update(updateBufferRequest.changes, updateBufferRequest.hostDocumentVersion);

this.notifyDocumentChange(document, RazorDocumentChangeKind.htmlChanged);
Expand Down
2 changes: 1 addition & 1 deletion src/razor/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export async function activate(

const languageServiceClient = new RazorLanguageServiceClient(languageServerClient);

const documentManager = new RazorDocumentManager(languageServerClient, logger);
const documentManager = new RazorDocumentManager(languageServerClient, logger, razorTelemetryReporter);
const documentSynchronizer = new RazorDocumentSynchronizer(documentManager, logger);
reportTelemetryForDocuments(documentManager, razorTelemetryReporter);
const languageConfiguration = new RazorLanguageConfiguration();
Expand Down
8 changes: 8 additions & 0 deletions src/razor/src/html/htmlProjectedDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export class HtmlProjectedDocument implements IProjectedDocument {
return this.projectedDocumentVersion;
}

public get length(): number {
return this.content.length;
}

public clear() {
this.setContent('');
}

public update(edits: ServerTextChange[], hostDocumentVersion: number) {
this.hostDocumentVersion = hostDocumentVersion;

Expand Down
1 change: 1 addition & 0 deletions src/razor/src/projection/IProjectedDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export interface IProjectedDocument {
readonly uri: vscode.Uri;
readonly hostDocumentSyncVersion: number | null;
readonly projectedDocumentSyncVersion: number;
readonly length: number;
getContent(): string;
}
3 changes: 2 additions & 1 deletion src/razor/src/rpc/updateBufferRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class UpdateBufferRequest {
constructor(
public readonly hostDocumentVersion: number,
public readonly hostDocumentFilePath: string,
public readonly changes: ServerTextChange[]
public readonly changes: ServerTextChange[],
public readonly previousWasEmpty: boolean
) {}
}
5 changes: 5 additions & 0 deletions src/razor/src/telemetryReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class TelemetryReporter {
private readonly razorExtensionActivated = createTelemetryEvent('VSCode.Razor.RazorExtensionActivated');
private readonly debugLanguageServerEvent = createTelemetryEvent('VSCode.Razor.DebugLanguageServer');
private readonly workspaceContainsRazorEvent = createTelemetryEvent('VSCode.Razor.WorkspaceContainsRazor');
private readonly buffersOutOfSyncEvent = createTelemetryEvent('VSCode.Razor.BuffersOutOfSync');
private reportedWorkspaceContainsRazor = false;

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

public reportBuffersOutOfSync() {
this.eventStream.post(this.buffersOutOfSyncEvent);
}

public reportErrorOnServerStart(error: unknown) {
let realError;
if (error instanceof Error) {
Expand Down

0 comments on commit 1564600

Please sign in to comment.