-
Notifications
You must be signed in to change notification settings - Fork 680
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 Razor inlay hint and inlay hint resolve handlers #6857
Merged
+185
−0
Merged
Changes from all commits
Commits
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 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 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 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,80 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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 { InlayHint, InlayHintParams, RequestType, TextDocumentIdentifier } from 'vscode-languageclient'; | ||
import { RazorDocumentManager } from '../document/razorDocumentManager'; | ||
import { RazorLanguageServerClient } from '../razorLanguageServerClient'; | ||
import { RazorLogger } from '../razorLogger'; | ||
import { SerializableInlayHintParams } from './serializableInlayHintParams'; | ||
import { provideInlayHintsCommand } from '../../../lsptoolshost/razorCommands'; | ||
import { UriConverter } from '../../../lsptoolshost/uriConverter'; | ||
import { RazorDocumentSynchronizer } from '../document/razorDocumentSynchronizer'; | ||
|
||
export class InlayHintHandler { | ||
private static readonly provideInlayHint = 'razor/inlayHint'; | ||
private InlayHintRequestType: RequestType<SerializableInlayHintParams, InlayHint[], any> = new RequestType( | ||
InlayHintHandler.provideInlayHint | ||
); | ||
private emptyInlayHintResponse = new Array<InlayHint>(); | ||
|
||
constructor( | ||
private readonly serverClient: RazorLanguageServerClient, | ||
private readonly documentManager: RazorDocumentManager, | ||
private readonly documentSynchronizer: RazorDocumentSynchronizer, | ||
private readonly logger: RazorLogger | ||
) {} | ||
|
||
public async register() { | ||
await this.serverClient.onRequestWithParams<SerializableInlayHintParams, InlayHint[], any>( | ||
this.InlayHintRequestType, | ||
async (request, token) => this.provideInlayHints(request, token) | ||
); | ||
} | ||
|
||
private async provideInlayHints(inlayHintParams: SerializableInlayHintParams, token: vscode.CancellationToken) { | ||
try { | ||
const razorDocumentUri = vscode.Uri.parse(inlayHintParams.identifier.textDocumentIdentifier.uri, true); | ||
const razorDocument = await this.documentManager.getDocument(razorDocumentUri); | ||
if (razorDocument === undefined) { | ||
return this.emptyInlayHintResponse; | ||
} | ||
|
||
if (!this.documentManager.roslynActivated) { | ||
// Unlike most other handlers, inlay hints works by directly sending an LSP request to Roslyn, so if Roslyn isn't | ||
// activated we need to catch that here. | ||
return this.emptyInlayHintResponse; | ||
} | ||
|
||
const textDocument = await vscode.workspace.openTextDocument(razorDocumentUri); | ||
const synchronized = await this.documentSynchronizer.trySynchronizeProjectedDocument( | ||
textDocument, | ||
razorDocument.csharpDocument, | ||
inlayHintParams.identifier.version, | ||
token | ||
); | ||
if (!synchronized) { | ||
return this.emptyInlayHintResponse; | ||
} | ||
|
||
const virtualCSharpUri = UriConverter.serialize(razorDocument.csharpDocument.uri); | ||
|
||
const roslynInlayHintParams = <InlayHintParams>{ | ||
textDocument: TextDocumentIdentifier.create(virtualCSharpUri), | ||
range: inlayHintParams.projectedRange, | ||
}; | ||
const csharpInlayHints = await vscode.commands.executeCommand<InlayHint[]>( | ||
provideInlayHintsCommand, | ||
roslynInlayHintParams | ||
); | ||
|
||
return csharpInlayHints; | ||
} catch (error) { | ||
this.logger.logWarning(`${InlayHintHandler.provideInlayHint} failed with ${error}`); | ||
} | ||
|
||
return this.emptyInlayHintResponse; | ||
} | ||
} |
This file contains 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,52 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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 { InlayHint, RequestType } from 'vscode-languageclient'; | ||
import { RazorDocumentManager } from '../document/razorDocumentManager'; | ||
import { RazorLanguageServerClient } from '../razorLanguageServerClient'; | ||
import { RazorLogger } from '../razorLogger'; | ||
import { SerializableInlayHintResolveParams } from './serializableInlayHintResolveParams'; | ||
import { resolveInlayHintCommand } from '../../../lsptoolshost/razorCommands'; | ||
|
||
export class InlayHintResolveHandler { | ||
private static readonly resolveInlayHint = 'razor/inlayHintResolve'; | ||
private InlayHintResolveRequestType: RequestType<SerializableInlayHintResolveParams, InlayHint | null, any> = | ||
new RequestType(InlayHintResolveHandler.resolveInlayHint); | ||
|
||
constructor( | ||
private readonly serverClient: RazorLanguageServerClient, | ||
private readonly documentManager: RazorDocumentManager, | ||
private readonly logger: RazorLogger | ||
) {} | ||
|
||
public async register() { | ||
await this.serverClient.onRequestWithParams<SerializableInlayHintResolveParams, InlayHint | null, any>( | ||
this.InlayHintResolveRequestType, | ||
async (request, token) => this.resolveInlayHint(request, token) | ||
); | ||
} | ||
|
||
private async resolveInlayHint(InlayHintParams: SerializableInlayHintResolveParams, _: vscode.CancellationToken) { | ||
try { | ||
const razorDocumentUri = vscode.Uri.parse(InlayHintParams.identifier.textDocumentIdentifier.uri, true); | ||
const razorDocument = await this.documentManager.getDocument(razorDocumentUri); | ||
if (razorDocument === undefined) { | ||
return null; | ||
} | ||
|
||
const response = await vscode.commands.executeCommand<InlayHint>( | ||
resolveInlayHintCommand, | ||
InlayHintParams.inlayHint | ||
); | ||
|
||
return response; | ||
} catch (error) { | ||
this.logger.logWarning(`${InlayHintResolveHandler.resolveInlayHint} failed with ${error}`); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains 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,12 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Range } from 'vscode-languageserver-protocol'; | ||
import { SerializableTextDocumentIdentifierAndVersion } from '../simplify/serializableTextDocumentIdentifierAndVersion'; | ||
|
||
export interface SerializableInlayHintParams { | ||
identifier: SerializableTextDocumentIdentifierAndVersion; | ||
projectedRange: Range; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/razor/src/inlayHint/serializableInlayHintResolveParams.ts
This file contains 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,12 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { InlayHint } from 'vscode-languageserver-protocol'; | ||
import { SerializableTextDocumentIdentifierAndVersion } from '../simplify/serializableTextDocumentIdentifierAndVersion'; | ||
|
||
export interface SerializableInlayHintResolveParams { | ||
identifier: SerializableTextDocumentIdentifierAndVersion; | ||
inlayHint: InlayHint; | ||
} |
Oops, something went wrong.
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.
For my understanding - why do we not need to do this for the resolve request?