Skip to content
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

Add a TextDocumentContentProvider for source-generated files #7581

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
- Debug from .csproj and .sln [#5876](https://github.com/dotnet/vscode-csharp/issues/5876)

# Latest
* Update Roslyn to 4.13.0-1.24503.11 (PR: [#7618](https://github.com/dotnet/vscode-csharp/pull/7618))
* LSP hover responses escape backticks within inline code (PR: [#75364](https://github.com/dotnet/roslyn/pull/75364))
* Localize build host message output (PR: [#74910](https://github.com/dotnet/roslyn/pull/74910))
* Log and report NFW when we fail to apply project system update (PR: [#75362](https://github.com/dotnet/roslyn/pull/75362))
* Reduce allocations and UI thread CPU costs in WithDoNotCreateCreationPolicy (PR: [#75358](https://github.com/dotnet/roslyn/pull/75358))
* Enable support for an LSP client to open source generated files (PR: [#75180](https://github.com/dotnet/roslyn/pull/75180))
* Improve error reporting when reading bad metadata during EnC (PR: [#75304](https://github.com/dotnet/roslyn/pull/75304))
* Suppress recoverable errors from razor LSP (PR: [#7624](https://github.com/dotnet/vscode-csharp/pull/7624))
* NOTE: this can be re-enabled by setting `razor.languageServer.suppressLspErrorToasts = false`
* Update Roslyn to 4.13.0-1.24501.3 (PR: [#7618](https://github.com/dotnet/vscode-csharp/pull/7618))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
},
"defaults": {
"roslyn": "4.13.0-1.24501.3",
"roslyn": "4.13.0-1.24503.11",
"omniSharp": "1.39.11",
"razor": "9.0.0-preview.24480.1",
"razorOmnisharp": "7.0.0-preview.23363.1",
Expand Down
3 changes: 3 additions & 0 deletions src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
showErrorMessage,
showInformationMessage,
} from '../shared/observers/utils/showMessage';
import { registerSourceGeneratedFilesContentProvider } from './sourceGeneratedFilesContentProvider';

let _channel: vscode.OutputChannel;
let _traceChannel: vscode.OutputChannel;
Expand Down Expand Up @@ -1068,6 +1069,8 @@ export async function activateRoslynLanguageServer(

registerRestoreCommands(context, languageServer, dotnetChannel);

registerSourceGeneratedFilesContentProvider(context, languageServer);

context.subscriptions.push(registerLanguageServerOptionChanges(optionObservable));

return languageServer;
Expand Down
14 changes: 14 additions & 0 deletions src/lsptoolshost/roslynProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ export interface CopilotRelatedDocumentsReport {
_vs_file_paths?: string[];
}

export interface SourceGeneratorGetRequestParams {
textDocument: lsp.TextDocumentIdentifier;
}

export interface SourceGeneratedDocumentText {
text: string;
}

export namespace WorkspaceDebugConfigurationRequest {
export const method = 'workspace/debugConfiguration';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
Expand Down Expand Up @@ -351,3 +359,9 @@ export namespace CopilotRelatedDocumentsRequest {
void
>(method);
}

export namespace SourceGeneratorGetTextRequest {
export const method = 'sourceGeneratedDocument/_roslyn_getText';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
export const type = new lsp.RequestType<SourceGeneratorGetRequestParams, SourceGeneratedDocumentText, void>(method);
}
31 changes: 31 additions & 0 deletions src/lsptoolshost/sourceGeneratedFilesContentProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*---------------------------------------------------------------------------------------------
* 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 * as RoslynProtocol from './roslynProtocol';
import { RoslynLanguageServer } from './roslynLanguageServer';
import { UriConverter } from './uriConverter';
import * as lsp from 'vscode-languageserver-protocol';

export function registerSourceGeneratedFilesContentProvider(
context: vscode.ExtensionContext,
languageServer: RoslynLanguageServer
) {
context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
'roslyn-source-generated',
new (class implements vscode.TextDocumentContentProvider {
async provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): Promise<string> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we definitely need to add refresh support in. There is some interesting behavior where VSCode doesn't actually always call didClose on the document even if the only active view closes (it calls sometimes much later). So the content gets reused even if the view is closed and another go to def request is made.

I could implement a command to refresh, or refresh on textview changing, but IMHO we should just implement server side refresh instead.

const result = await languageServer.sendRequest(
RoslynProtocol.SourceGeneratorGetTextRequest.type,
{ textDocument: lsp.TextDocumentIdentifier.create(UriConverter.serialize(uri)) },
token
);
return result.text;
}
})()
)
);
}
Loading