-
Notifications
You must be signed in to change notification settings - Fork 676
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
Enable Project Context status item for Razor files #7333
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -10,6 +10,9 @@ import { TextDocumentIdentifier } from 'vscode-languageserver-protocol'; | |||
import { UriConverter } from '../uriConverter'; | ||||
import { LanguageServerEvents } from '../languageServerEvents'; | ||||
import { ServerState } from '../serverStateChange'; | ||||
import { DynamicFileInfoHandler } from '../../razor/src/dynamicFile/dynamicFileInfoHandler'; | ||||
import { ProvideDynamicFileResponse } from '../../razor/src/dynamicFile/provideDynamicFileResponse'; | ||||
import { ProvideDynamicFileParams } from '../../razor/src/dynamicFile/provideDynamicFileParams'; | ||||
|
||||
export interface ProjectContextChangeEvent { | ||||
uri: vscode.Uri; | ||||
|
@@ -39,11 +42,22 @@ export class ProjectContextService { | |||
|
||||
public async refresh() { | ||||
const textEditor = vscode.window.activeTextEditor; | ||||
if (textEditor?.document?.languageId !== 'csharp') { | ||||
const languageId = textEditor?.document?.languageId; | ||||
if (languageId !== 'csharp' && languageId !== 'aspnetcorerazor') { | ||||
return; | ||||
} | ||||
|
||||
const uri = textEditor.document.uri; | ||||
let uri = textEditor!.document.uri; | ||||
|
||||
// If the active document is a Razor file, we need to map it back to a C# file. | ||||
if (languageId === 'aspnetcorerazor') { | ||||
const virtualUri = await this.getVirtualCSharpUri(uri); | ||||
if (!virtualUri) { | ||||
return; | ||||
} | ||||
|
||||
uri = virtualUri; | ||||
} | ||||
|
||||
// If we have an open request, cancel it. | ||||
this._source.cancel(); | ||||
|
@@ -58,6 +72,20 @@ export class ProjectContextService { | |||
this._contextChangeEmitter.fire({ uri, context }); | ||||
} | ||||
|
||||
private async getVirtualCSharpUri(uri: vscode.Uri): Promise<vscode.Uri | undefined> { | ||||
const response = await vscode.commands.executeCommand<ProvideDynamicFileResponse>( | ||||
DynamicFileInfoHandler.provideDynamicFileInfoCommand, | ||||
new ProvideDynamicFileParams([uri.fsPath]) | ||||
); | ||||
|
||||
const responseUri = response.generatedFiles[0]; | ||||
if (!responseUri) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need an explicit check for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The false-y check should cover null, undefined, and i think '' as well. |
||||
return undefined; | ||||
} | ||||
|
||||
return UriConverter.deserialize(responseUri); | ||||
} | ||||
|
||||
private async getProjectContexts( | ||||
uri: vscode.Uri, | ||||
token: vscode.CancellationToken | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,8 @@ export class RazorDocumentSynchronizer { | |
} | ||
|
||
private removeSynchronization(context: SynchronizationContext) { | ||
context.dispose(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidwengier Does this seem right? What I was seeing in CI and locally was that the document and text document were up to date. So the context was removed. The token then got cancelled and the callbacks were rejected. This created an unhandled exception. I figure if we are removing the context, then we don't care about cancellation and can remove the callbacks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I see why the token would get cancelled, but clearing the rejections like this doesn't seem like it would be an issue. |
||
|
||
const documentKey = getUriPath(context.projectedDocument.uri); | ||
const synchronizations = this.synchronizations[documentKey]; | ||
clearTimeout(context.timeoutId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Consider moving this inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since integration tests have been flaky for me, I will take your suggestions as a follow up and get this merged for snap. |
||
|
@@ -167,6 +169,11 @@ export class RazorDocumentSynchronizer { | |
reject(reason); | ||
} | ||
}, | ||
dispose: () => { | ||
while (rejectionsForCancel.length > 0) { | ||
rejectionsForCancel.pop(); | ||
} | ||
Comment on lines
+173
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think |
||
}, | ||
projectedDocumentSynchronized, | ||
onProjectedDocumentSynchronized, | ||
projectedTextDocumentSynchronized, | ||
|
@@ -271,4 +278,5 @@ interface SynchronizationContext { | |
readonly projectedTextDocumentSynchronized: () => void; | ||
readonly onProjectedTextDocumentSynchronized: Promise<void>; | ||
readonly cancel: (reason: string) => void; | ||
readonly dispose: () => void; | ||
} |
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.
@davidwengier I'm not sure if there will be any problem using this endpoint or if there's something else that should be used to find the dynamic file URI
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.
I don't think it would cause any issues. In theory it could trigger Razor activation, but if this only gets called when a Razor document is open in the editor, that either has happened, or is about to anyway.
I guess the only risk here is that the uri in this method is different from the one Roslyn sends us, but points to the same physical file. Not sure what would happen in that case
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.
I think it would get added to the document list with a wrong uri and either be fixed or show up in misc files