Skip to content

Commit

Permalink
Add a TextDocumentContentProvider for source-generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmalinowski committed Jul 8, 2023
1 parent bb3d420 commit f4180b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ export async function activateRoslynLanguageServer(
// Register any needed debugger components that need to communicate with the language server.
registerDebugger(context, _languageServer, platformInfo, optionProvider, _channel);

registerSourceGeneratedFilesContentProvider(context, _languageServer);

const options = optionProvider.GetLatestOptions();
let source = new vscode.CancellationTokenSource();
vscode.workspace.onDidChangeTextDocument(async (e) => {
Expand Down Expand Up @@ -734,6 +736,27 @@ function registerRazorCommands(context: vscode.ExtensionContext, languageServer:
);
}

function registerSourceGeneratedFilesContentProvider(
context: vscode.ExtensionContext,
languageServer: RoslynLanguageServer
) {
context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
'source-generated',
new (class implements vscode.TextDocumentContentProvider {
async provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): Promise<string> {
const result = await languageServer.sendRequest(
RoslynProtocol.SourceGeneratorGetTextRequest.type,
{ textDocument: { uri: UriConverter.serialize(uri) } },
token
);
return result.text;
}
})()
)
);
}

async function applyAutoInsertEdit(e: vscode.TextDocumentChangeEvent, token: vscode.CancellationToken) {
const change = e.contentChanges[0];

Expand Down
14 changes: 14 additions & 0 deletions src/lsptoolshost/roslynProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export interface RegisterSolutionSnapshotResponseItem {
id: integer;
}

export interface SourceGeneratorGetRequestParams {
textDocument: TextDocumentIdentifier;
}

export interface SourceGeneratedDocumentText {
text: string;
}

export namespace WorkspaceDebugConfigurationRequest {
export const method = 'workspace/debugConfiguration';
export const messageDirection: MessageDirection = MessageDirection.clientToServer;
Expand All @@ -100,3 +108,9 @@ export namespace ProjectInitializationCompleteNotification {
export const messageDirection: MessageDirection = MessageDirection.serverToClient;
export const type = new NotificationType(method);
}

export namespace SourceGeneratorGetTextRequest {
export const method = 'sourceGenerator/getText';
export const messageDirection: MessageDirection = MessageDirection.clientToServer;
export const type = new RequestType<SourceGeneratorGetRequestParams, SourceGeneratedDocumentText, void>(method);
}

0 comments on commit f4180b8

Please sign in to comment.