-
Notifications
You must be signed in to change notification settings - Fork 724
Support Html requests in cohosting #8210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { getUriPath } from '../../razor/src/uriPaths'; | ||
|
||
export class HtmlDocument { | ||
public readonly path: string; | ||
private content = ''; | ||
|
||
public constructor(public readonly uri: vscode.Uri) { | ||
this.path = getUriPath(uri); | ||
} | ||
|
||
public getContent() { | ||
return this.content; | ||
} | ||
|
||
public setContent(content: string) { | ||
this.content = content; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { HtmlDocumentManager } from './htmlDocumentManager'; | ||
import { RazorLogger } from '../../razor/src/razorLogger'; | ||
import { getUriPath } from '../../razor/src/uriPaths'; | ||
|
||
export class HtmlDocumentContentProvider implements vscode.TextDocumentContentProvider { | ||
public static readonly scheme = 'razor-html'; | ||
|
||
private readonly onDidChangeEmitter: vscode.EventEmitter<vscode.Uri> = new vscode.EventEmitter<vscode.Uri>(); | ||
|
||
constructor(private readonly documentManager: HtmlDocumentManager, private readonly logger: RazorLogger) {} | ||
|
||
public get onDidChange() { | ||
return this.onDidChangeEmitter.event; | ||
} | ||
|
||
public fireDidChange(uri: vscode.Uri) { | ||
this.onDidChangeEmitter.fire(uri); | ||
} | ||
|
||
public provideTextDocumentContent(uri: vscode.Uri) { | ||
const document = this.findDocument(uri); | ||
if (!document) { | ||
// Document was removed from the document manager, meaning there's no more content for this | ||
// file. Report an empty document. | ||
this.logger.logVerbose( | ||
`Could not find document '${getUriPath( | ||
uri | ||
)}' when updating the HTML buffer. This typically happens when a document is removed.` | ||
); | ||
return ''; | ||
} | ||
|
||
return document.getContent(); | ||
} | ||
|
||
private findDocument(uri: vscode.Uri) { | ||
const projectedPath = getUriPath(uri); | ||
|
||
return this.documentManager.documents.find( | ||
(document) => document.path.localeCompare(projectedPath, undefined, { sensitivity: 'base' }) === 0 | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { RazorLogger } from '../../razor/src/razorLogger'; | ||
import { PlatformInformation } from '../../shared/platform'; | ||
import { getUriPath } from '../../razor/src/uriPaths'; | ||
import { virtualHtmlSuffix } from '../../razor/src/razorConventions'; | ||
import { HtmlDocumentContentProvider } from './htmlDocumentContentProvider'; | ||
import { HtmlDocument } from './htmlDocument'; | ||
|
||
export class HtmlDocumentManager { | ||
private readonly htmlDocuments: { [hostDocumentPath: string]: HtmlDocument } = {}; | ||
private readonly contentProvider: HtmlDocumentContentProvider; | ||
|
||
constructor(private readonly platformInfo: PlatformInformation, private readonly logger: RazorLogger) { | ||
this.contentProvider = new HtmlDocumentContentProvider(this, this.logger); | ||
} | ||
|
||
public get documents() { | ||
return Object.values(this.htmlDocuments); | ||
} | ||
|
||
public register() { | ||
const didCloseRegistration = vscode.workspace.onDidCloseTextDocument(async (document) => { | ||
// We log when a virtual document is closed just in case it helps track down future bugs | ||
if (document.uri.scheme === HtmlDocumentContentProvider.scheme) { | ||
this.logger.logVerbose(`Virtual document '${document.uri}' timed out.`); | ||
return; | ||
} | ||
|
||
// When a Razor document is closed, only then can we be sure its okay to remove the virtual document. | ||
if (document.languageId === 'aspnetcorerazor') { | ||
this.logger.logVerbose(`Document '${document.uri}' was closed.`); | ||
|
||
await this.closeDocument(document.uri); | ||
|
||
// TODO: Send a notification back to the server so it can cancel any pending sync requests and clear its cache. | ||
} | ||
}); | ||
|
||
const providerRegistration = vscode.workspace.registerTextDocumentContentProvider( | ||
HtmlDocumentContentProvider.scheme, | ||
this.contentProvider | ||
); | ||
|
||
return vscode.Disposable.from(didCloseRegistration, providerRegistration); | ||
} | ||
|
||
public async updateDocumentText(uri: vscode.Uri, text: string) { | ||
const document = await this.getDocument(uri); | ||
|
||
this.logger.logVerbose(`New content for '${uri}', updating '${document.path}'.`); | ||
|
||
document.setContent(text); | ||
|
||
this.contentProvider.fireDidChange(document.uri); | ||
} | ||
|
||
private async closeDocument(uri: vscode.Uri) { | ||
const document = await this.findDocument(uri); | ||
|
||
if (document) { | ||
this.logger.logVerbose(`Removing '${document.uri}' from the document manager.`); | ||
|
||
delete this.htmlDocuments[document.path]; | ||
} | ||
} | ||
|
||
public async getDocument(uri: vscode.Uri): Promise<HtmlDocument> { | ||
let document = this.findDocument(uri); | ||
|
||
// This might happen in the case that a file is opened outside the workspace | ||
if (!document) { | ||
this.logger.logMessage( | ||
`File '${uri}' didn't exist in the Razor document list. This is likely because it's from outside the workspace.` | ||
); | ||
document = this.addDocument(uri); | ||
} | ||
|
||
await vscode.workspace.openTextDocument(document.uri); | ||
|
||
return document!; | ||
} | ||
|
||
private addDocument(uri: vscode.Uri): HtmlDocument { | ||
let document = this.findDocument(uri); | ||
if (document) { | ||
this.logger.logMessage(`Skipping document creation for '${document.path}' because it already exists.`); | ||
return document; | ||
} | ||
|
||
document = this.createDocument(uri); | ||
this.htmlDocuments[document.path] = document; | ||
|
||
return document; | ||
} | ||
|
||
private findDocument(uri: vscode.Uri): HtmlDocument | undefined { | ||
let path = getUriPath(uri); | ||
|
||
// We might be passed a Razor document Uri, but we store and manage Html projected documents. | ||
if (uri.scheme !== HtmlDocumentContentProvider.scheme) { | ||
path = `${path}${virtualHtmlSuffix}`; | ||
} | ||
|
||
if (this.platformInfo.isLinux()) { | ||
return this.htmlDocuments[path]; | ||
} | ||
|
||
return Object.values(this.htmlDocuments).find( | ||
(document) => document.path.localeCompare(path, undefined, { sensitivity: 'base' }) === 0 | ||
); | ||
} | ||
|
||
private createDocument(uri: vscode.Uri) { | ||
uri = uri.with({ | ||
scheme: HtmlDocumentContentProvider.scheme, | ||
path: `${uri.path}${virtualHtmlSuffix}`, | ||
}); | ||
const projectedDocument = new HtmlDocument(uri); | ||
|
||
return projectedDocument; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { TextDocumentIdentifier } from 'vscode-languageserver-protocol'; | ||
|
||
export class HtmlUpdateParameters { | ||
constructor(public readonly textDocument: TextDocumentIdentifier, public readonly text: string) {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to remove registration of dynamicFile endpoints if cohosting is on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think so. I think we never call them (though tbh I've put almost no effort into initialization yet)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be another pr for sure