Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Re-design document synchronization.
Browse files Browse the repository at this point in the history
- The purpose of this redesign is to make completion more reliable. We now have a great understanding of how VSCode's object model mutates and therefore can make better assumptiosn on how to synchronize it.
- In this changeset we primarily do 2 things to synchronize a document:
  1. Make sure our `IProjectedDocument` has been updated by our language server.
  2. Make sure VSCode's TextDocument reflects the appropriate content detailed by the `IProjectedDocument`
If these two assertions are true then every other extension sees updated C#/Html and can respond accordingly.
- To properly accomplish 2 I had to persist information into the `vscode.TextDocument` to identify when the content properly matches our DTO. We do this by adding a `// SomeNumber` to the end of projected text documents.
- Updated RazorDocumentFactory to generate virtual C# files without a `__` prefix to prevent URIs from being placed as "authoritative" sources labeled by `__`. This caused issues in VSCode when working with copmletion.
- Flowed cancellation tokens into synchronization APIs so that when VSCode cancels an action we can also cancel synchronization.

#210
  • Loading branch information
NTaylorMullen committed Nov 10, 2018
1 parent 55d6c21 commit f9323c9
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class CSharpProjectedDocument implements IProjectedDocument {
private preProvisionalContent: string | undefined;
private provisionalEditAt: number | undefined;
private hostDocumentVersion: number | null = null;
private projectedDocumentVersion = 0;

public constructor(public readonly uri: vscode.Uri) {
this.path = getUriPath(uri);
Expand All @@ -24,6 +25,10 @@ export class CSharpProjectedDocument implements IProjectedDocument {
return this.hostDocumentVersion;
}

public get projectedDocumentSyncVersion(): number {
return this.projectedDocumentVersion;
}

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

Expand Down Expand Up @@ -85,6 +90,7 @@ export class CSharpProjectedDocument implements IProjectedDocument {
}

private setContent(content: string) {
this.projectedDocumentVersion++;
this.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class CSharpProjectedDocumentContentProvider implements vscode.TextDocume
return;
}

const content = razorDocument.csharpDocument.getContent();
const content = `${razorDocument.csharpDocument.getContent()}
// ${razorDocument.csharpDocument.projectedDocumentSyncVersion}`;

return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class HtmlProjectedDocument implements IProjectedDocument {
public readonly path: string;
private content = '';
private hostDocumentVersion: number | null = null;
private projectedDocumentVersion = 0;

public constructor(public readonly uri: vscode.Uri) {
this.path = getUriPath(uri);
Expand All @@ -20,12 +21,17 @@ export class HtmlProjectedDocument implements IProjectedDocument {
return this.hostDocumentVersion;
}

public get projectedDocumentSyncVersion(): number {
return this.projectedDocumentVersion;
}

public getContent() {
return this.content;
}

public setContent(content: string, hostDocumentVersion: number | null) {
this.content = content;
this.projectedDocumentVersion++;
this.hostDocumentVersion = hostDocumentVersion;
this.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class HtmlProjectedDocumentContentProvider implements vscode.TextDocument
return;
}

const content = razorDocument.htmlDocument.getContent();
const content = `${razorDocument.htmlDocument.getContent()}
// ${razorDocument.htmlDocument.projectedDocumentSyncVersion}`;

return content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export interface IProjectedDocument {
readonly path: string;
readonly uri: vscode.Uri;
readonly hostDocumentSyncVersion: number | null;
readonly projectedDocumentSyncVersion: number;
getContent(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class RazorCompletionItemProvider
public async provideCompletionItems(
document: vscode.TextDocument, position: vscode.Position,
token: vscode.CancellationToken, context: vscode.CompletionContext) {
const projection = await this.getProjection(document, position);
const projection = await this.getProjection(document, position, token);

if (this.logger.verboseEnabled) {
this.logger.logVerbose(`Providing completions for document ${getUriPath(document.uri)} ` +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export function createDocument(uri: vscode.Uri) {
}

function createProjectedHtmlDocument(hostDocumentUri: vscode.Uri) {
// Index.cshtml => __Index.cshtml.html
const projectedPath = `__${hostDocumentUri.path}.html`;
// Index.cshtml => Index.cshtml__virtual.html
const projectedPath = `${hostDocumentUri.path}__virtual.html`;
const projectedUri = vscode.Uri.parse(`${HtmlProjectedDocumentContentProvider.scheme}://${projectedPath}`);
const projectedDocument = new HtmlProjectedDocument(projectedUri);

return projectedDocument;
}

function createProjectedCSharpDocument(hostDocumentUri: vscode.Uri) {
// Index.cshtml => __Index.cshtml__virtual.cs
const projectedPath = `__${hostDocumentUri.path}__virtual.cs`;
// Index.cshtml => Index.cshtml__virtual.cs
const projectedPath = `${hostDocumentUri.path}__virtual.cs`;
const projectedUri = vscode.Uri.parse(`${CSharpProjectedDocumentContentProvider.scheme}://${projectedPath}`);
const projectedDocument = new CSharpProjectedDocument(projectedUri);

Expand Down
Loading

0 comments on commit f9323c9

Please sign in to comment.