66import * as vscode from 'vscode' ;
77import { RoslynLanguageServer } from '../server/roslynLanguageServer' ;
88import {
9- ColorInformation ,
10- ColorPresentationParams ,
119 ColorPresentationRequest ,
12- CompletionList ,
13- CompletionParams ,
1410 CompletionRequest ,
15- DocumentColorParams ,
11+ DefinitionRequest ,
1612 DocumentColorRequest ,
13+ DocumentFormattingRequest ,
1714 DocumentHighlight ,
1815 DocumentHighlightKind ,
19- DocumentHighlightParams ,
2016 DocumentHighlightRequest ,
21- FoldingRange ,
22- FoldingRangeParams ,
17+ DocumentOnTypeFormattingRequest ,
2318 FoldingRangeRequest ,
2419 Hover ,
25- HoverParams ,
2620 HoverRequest ,
21+ ImplementationRequest ,
22+ Location ,
2723 LogMessageParams ,
2824 MarkupKind ,
25+ MarkupContent ,
2926 NotificationType ,
27+ ReferencesRequest ,
3028 RequestType ,
29+ SignatureHelp ,
30+ SignatureHelpRequest ,
3131} from 'vscode-languageclient' ;
3232import { RazorLogger } from '../../razor/src/razorLogger' ;
3333import { HtmlUpdateParameters } from './htmlUpdateParameters' ;
@@ -37,7 +37,6 @@ import { HtmlDocumentManager } from './htmlDocumentManager';
3737import { DocumentColorHandler } from '../../razor/src/documentColor/documentColorHandler' ;
3838import { razorOptions } from '../../shared/options' ;
3939import { ColorPresentationHandler } from '../../razor/src/colorPresentation/colorPresentationHandler' ;
40- import { ColorPresentation } from 'vscode-html-languageservice' ;
4140import { convertRangeToSerializable } from '../../razor/src/rpc/serializableRange' ;
4241import { FoldingRangeHandler } from '../../razor/src/folding/foldingRangeHandler' ;
4342import { CompletionHandler } from '../../razor/src/completion/completionHandler' ;
@@ -49,6 +48,7 @@ import { RazorMapSpansResponse } from '../../razor/src/mapping/razorMapSpansResp
4948import { MappingHandler } from '../../razor/src/mapping/mappingHandler' ;
5049import { RazorMapTextChangesParams } from '../../razor/src/mapping/razorMapTextChangesParams' ;
5150import { RazorMapTextChangesResponse } from '../../razor/src/mapping/razorMapTextChangesResponse' ;
51+ import { FormattingHandler } from '../../razor/src/formatting/formattingHandler' ;
5252
5353export function registerRazorEndpoints (
5454 context : vscode . ExtensionContext ,
@@ -76,29 +76,26 @@ export function registerRazorEndpoints(
7676 const documentManager = new HtmlDocumentManager ( platformInfo , razorLogger ) ;
7777 context . subscriptions . push ( documentManager . register ( ) ) ;
7878
79- registerRequestHandler < HtmlUpdateParameters , void > ( 'razor/updateHtml' , async ( params ) => {
79+ registerMethodHandler < HtmlUpdateParameters , void > ( 'razor/updateHtml' , async ( params ) => {
8080 const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
8181 await documentManager . updateDocumentText ( uri , params . text ) ;
8282 } ) ;
8383
84- registerRequestHandler < DocumentColorParams , ColorInformation [ ] > ( DocumentColorRequest . method , async ( params ) => {
84+ registerRequestHandler ( DocumentColorRequest . type , async ( params ) => {
8585 const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
8686 const document = await documentManager . getDocument ( uri ) ;
8787
8888 return await DocumentColorHandler . doDocumentColorRequest ( document . uri ) ;
8989 } ) ;
9090
91- registerRequestHandler < ColorPresentationParams , ColorPresentation [ ] > (
92- ColorPresentationRequest . method ,
93- async ( params ) => {
94- const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
95- const document = await documentManager . getDocument ( uri ) ;
91+ registerRequestHandler ( ColorPresentationRequest . type , async ( params ) => {
92+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
93+ const document = await documentManager . getDocument ( uri ) ;
9694
97- return await ColorPresentationHandler . doColorPresentationRequest ( document . uri , params ) ;
98- }
99- ) ;
95+ return await ColorPresentationHandler . doColorPresentationRequest ( document . uri , params ) ;
96+ } ) ;
10097
101- registerRequestHandler < FoldingRangeParams , FoldingRange [ ] > ( FoldingRangeRequest . method , async ( params ) => {
98+ registerRequestHandler ( FoldingRangeRequest . type , async ( params ) => {
10299 const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
103100 const document = await documentManager . getDocument ( uri ) ;
104101
@@ -110,7 +107,7 @@ export function registerRazorEndpoints(
110107 return FoldingRangeHandler . convertFoldingRanges ( results , razorLogger ) ;
111108 } ) ;
112109
113- registerRequestHandler < HoverParams , Hover | undefined > ( HoverRequest . method , async ( params ) => {
110+ registerRequestHandler ( HoverRequest . type , async ( params ) => {
114111 const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
115112 const document = await documentManager . getDocument ( uri ) ;
116113
@@ -124,23 +121,20 @@ export function registerRazorEndpoints(
124121 return rewriteHover ( applicableHover ) ;
125122 } ) ;
126123
127- registerRequestHandler < DocumentHighlightParams , DocumentHighlight [ ] > (
128- DocumentHighlightRequest . method ,
129- async ( params ) => {
130- const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
131- const document = await documentManager . getDocument ( uri ) ;
124+ registerRequestHandler ( DocumentHighlightRequest . type , async ( params ) => {
125+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
126+ const document = await documentManager . getDocument ( uri ) ;
132127
133- const results = await vscode . commands . executeCommand < vscode . DocumentHighlight [ ] > (
134- 'vscode.executeDocumentHighlights' ,
135- document . uri ,
136- params . position
137- ) ;
128+ const results = await vscode . commands . executeCommand < vscode . DocumentHighlight [ ] > (
129+ 'vscode.executeDocumentHighlights' ,
130+ document . uri ,
131+ params . position
132+ ) ;
138133
139- return rewriteHighlight ( results ) ;
140- }
141- ) ;
134+ return rewriteHighlight ( results ) ;
135+ } ) ;
142136
143- registerRequestHandler < CompletionParams , CompletionList > ( CompletionRequest . method , async ( params ) => {
137+ registerRequestHandler ( CompletionRequest . type , async ( params ) => {
144138 const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
145139 const document = await documentManager . getDocument ( uri ) ;
146140
@@ -150,24 +144,108 @@ export function registerRazorEndpoints(
150144 params . context ?. triggerCharacter
151145 ) ;
152146 } ) ;
147+
148+ registerRequestHandler ( ReferencesRequest . type , async ( params ) => {
149+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
150+ const document = await documentManager . getDocument ( uri ) ;
151+
152+ const results = await vscode . commands . executeCommand < vscode . Location [ ] > (
153+ 'vscode.executeReferenceProvider' ,
154+ document . uri ,
155+ params . position
156+ ) ;
157+
158+ return rewriteLocations ( results ) ;
159+ } ) ;
160+
161+ registerRequestHandler ( ImplementationRequest . type , async ( params ) => {
162+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
163+ const document = await documentManager . getDocument ( uri ) ;
164+
165+ const results = await vscode . commands . executeCommand < vscode . Location [ ] > (
166+ 'vscode.executeImplementationProvider' ,
167+ document . uri ,
168+ params . position
169+ ) ;
170+
171+ return rewriteLocations ( results ) ;
172+ } ) ;
173+
174+ registerRequestHandler ( DefinitionRequest . type , async ( params ) => {
175+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
176+ const document = await documentManager . getDocument ( uri ) ;
177+
178+ const results = await vscode . commands . executeCommand < vscode . Location [ ] > (
179+ 'vscode.executeDefinitionProvider' ,
180+ document . uri ,
181+ params . position
182+ ) ;
183+
184+ return rewriteLocations ( results ) ;
185+ } ) ;
186+
187+ registerRequestHandler ( SignatureHelpRequest . type , async ( params ) => {
188+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
189+ const document = await documentManager . getDocument ( uri ) ;
190+
191+ const results = await vscode . commands . executeCommand < vscode . SignatureHelp > (
192+ 'vscode.executeSignatureHelpProvider' ,
193+ document . uri ,
194+ params . position
195+ ) ;
196+
197+ if ( ! results ) {
198+ return undefined ;
199+ }
200+
201+ return rewriteSignatureHelp ( results ) ;
202+ } ) ;
203+
204+ registerRequestHandler ( DocumentFormattingRequest . type , async ( params ) => {
205+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
206+ const document = await documentManager . getDocument ( uri ) ;
207+
208+ const content = document . getContent ( ) ;
209+ const options = < vscode . FormattingOptions > params . options ;
210+
211+ const response = await FormattingHandler . getHtmlFormattingResult ( document . uri , content , options ) ;
212+ return response ?. edits ;
213+ } ) ;
214+
215+ registerRequestHandler ( DocumentOnTypeFormattingRequest . type , async ( params ) => {
216+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
217+ const document = await documentManager . getDocument ( uri ) ;
218+
219+ const content = document . getContent ( ) ;
220+ const options = < vscode . FormattingOptions > params . options ;
221+
222+ const response = await FormattingHandler . getHtmlOnTypeFormattingResult (
223+ document . uri ,
224+ content ,
225+ params . position ,
226+ params . ch ,
227+ options
228+ ) ;
229+ return response ?. edits ;
230+ } ) ;
153231 }
154232
155233 function registerNonCohostingEndpoints ( ) {
156- registerRequestHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
234+ registerMethodHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
157235 'razor/provideDynamicFileInfo' ,
158236 async ( params ) =>
159237 vscode . commands . executeCommand ( DynamicFileInfoHandler . provideDynamicFileInfoCommand , params )
160238 ) ;
161239
162- registerRequestHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
240+ registerMethodHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
163241 'razor/removeDynamicFileInfo' ,
164242 async ( params ) =>
165243 vscode . commands . executeCommand ( DynamicFileInfoHandler . provideDynamicFileInfoCommand , params )
166244 ) ;
167- registerRequestHandler < RazorMapSpansParams , RazorMapSpansResponse > ( 'razor/mapSpans' , async ( params ) => {
245+ registerMethodHandler < RazorMapSpansParams , RazorMapSpansResponse > ( 'razor/mapSpans' , async ( params ) => {
168246 return await vscode . commands . executeCommand < RazorMapSpansResponse > ( MappingHandler . MapSpansCommand , params ) ;
169247 } ) ;
170- registerRequestHandler < RazorMapTextChangesParams , RazorMapTextChangesResponse > (
248+ registerMethodHandler < RazorMapTextChangesParams , RazorMapTextChangesResponse > (
171249 'razor/mapTextChanges' ,
172250 async ( params ) => {
173251 return await vscode . commands . executeCommand < RazorMapTextChangesResponse > (
@@ -179,7 +257,14 @@ export function registerRazorEndpoints(
179257 }
180258
181259 // Helper method that registers a request handler, and logs errors to the Razor logger.
182- function registerRequestHandler < Params , Result > ( method : string , invocation : ( params : Params ) => Promise < Result > ) {
260+ function registerRequestHandler < Params , Result , Error > (
261+ type : RequestType < Params , Result , Error > ,
262+ invocation : ( params : Params ) => Promise < Result >
263+ ) {
264+ return registerMethodHandler < Params , Result > ( type . method , invocation ) ;
265+ }
266+
267+ function registerMethodHandler < Params , Result > ( method : string , invocation : ( params : Params ) => Promise < Result > ) {
183268 const requestType = new RequestType < Params , Result , Error > ( method ) ;
184269 roslynLanguageServer . registerOnRequest ( requestType , async ( params ) => {
185270 try {
@@ -235,3 +320,47 @@ function convertHighlightKind(kind: vscode.DocumentHighlightKind | undefined): D
235320 return undefined ;
236321 }
237322}
323+
324+ function rewriteLocations ( locations : vscode . Location [ ] ) : Location [ ] {
325+ return locations . map ( ( location ) => {
326+ return {
327+ uri : UriConverter . serialize ( location . uri ) ,
328+ range : convertRangeToSerializable ( location . range ) ,
329+ } ;
330+ } ) ;
331+ }
332+
333+ function rewriteSignatureHelp ( signatureHelp : vscode . SignatureHelp ) : SignatureHelp {
334+ return {
335+ activeParameter : signatureHelp . activeParameter ?? undefined ,
336+ activeSignature : signatureHelp . activeSignature ?? undefined ,
337+ signatures : signatureHelp . signatures . map ( ( signature ) => {
338+ return {
339+ label : signature . label ,
340+ documentation : rewriteMarkdownString ( signature . documentation ) ,
341+ parameters : signature . parameters . map ( ( parameter ) => {
342+ return {
343+ label : parameter . label ,
344+ documentation : rewriteMarkdownString ( parameter . documentation ) ,
345+ } ;
346+ } ) ,
347+ } ;
348+ } ) ,
349+ } ;
350+ }
351+
352+ function rewriteMarkdownString ( documentation : string | vscode . MarkdownString | undefined ) : MarkupContent | undefined {
353+ if ( ! documentation ) {
354+ return undefined ;
355+ }
356+
357+ if ( ( documentation as vscode . MarkdownString ) . value ) {
358+ const markdownString = documentation as vscode . MarkdownString ;
359+ return {
360+ kind : MarkupKind . Markdown ,
361+ value : markdownString . value ,
362+ } ;
363+ }
364+
365+ return { kind : MarkupKind . PlainText , value : < string > documentation } ;
366+ }
0 commit comments