6
6
import * as vscode from 'vscode' ;
7
7
import { RoslynLanguageServer } from '../server/roslynLanguageServer' ;
8
8
import {
9
- ColorInformation ,
10
- ColorPresentationParams ,
11
9
ColorPresentationRequest ,
12
- CompletionList ,
13
- CompletionParams ,
14
10
CompletionRequest ,
15
- DocumentColorParams ,
11
+ DefinitionRequest ,
16
12
DocumentColorRequest ,
13
+ DocumentFormattingRequest ,
17
14
DocumentHighlight ,
18
15
DocumentHighlightKind ,
19
- DocumentHighlightParams ,
20
16
DocumentHighlightRequest ,
21
- FoldingRange ,
22
- FoldingRangeParams ,
17
+ DocumentOnTypeFormattingRequest ,
23
18
FoldingRangeRequest ,
24
19
Hover ,
25
- HoverParams ,
26
20
HoverRequest ,
21
+ ImplementationRequest ,
22
+ Location ,
27
23
LogMessageParams ,
28
24
MarkupKind ,
25
+ MarkupContent ,
29
26
NotificationType ,
27
+ ReferencesRequest ,
30
28
RequestType ,
29
+ SignatureHelp ,
30
+ SignatureHelpRequest ,
31
31
} from 'vscode-languageclient' ;
32
32
import { RazorLogger } from '../../razor/src/razorLogger' ;
33
33
import { HtmlUpdateParameters } from './htmlUpdateParameters' ;
@@ -37,7 +37,6 @@ import { HtmlDocumentManager } from './htmlDocumentManager';
37
37
import { DocumentColorHandler } from '../../razor/src/documentColor/documentColorHandler' ;
38
38
import { razorOptions } from '../../shared/options' ;
39
39
import { ColorPresentationHandler } from '../../razor/src/colorPresentation/colorPresentationHandler' ;
40
- import { ColorPresentation } from 'vscode-html-languageservice' ;
41
40
import { convertRangeToSerializable } from '../../razor/src/rpc/serializableRange' ;
42
41
import { FoldingRangeHandler } from '../../razor/src/folding/foldingRangeHandler' ;
43
42
import { CompletionHandler } from '../../razor/src/completion/completionHandler' ;
@@ -49,6 +48,7 @@ import { RazorMapSpansResponse } from '../../razor/src/mapping/razorMapSpansResp
49
48
import { MappingHandler } from '../../razor/src/mapping/mappingHandler' ;
50
49
import { RazorMapTextChangesParams } from '../../razor/src/mapping/razorMapTextChangesParams' ;
51
50
import { RazorMapTextChangesResponse } from '../../razor/src/mapping/razorMapTextChangesResponse' ;
51
+ import { FormattingHandler } from '../../razor/src/formatting/formattingHandler' ;
52
52
53
53
export function registerRazorEndpoints (
54
54
context : vscode . ExtensionContext ,
@@ -76,29 +76,26 @@ export function registerRazorEndpoints(
76
76
const documentManager = new HtmlDocumentManager ( platformInfo , razorLogger ) ;
77
77
context . subscriptions . push ( documentManager . register ( ) ) ;
78
78
79
- registerRequestHandler < HtmlUpdateParameters , void > ( 'razor/updateHtml' , async ( params ) => {
79
+ registerMethodHandler < HtmlUpdateParameters , void > ( 'razor/updateHtml' , async ( params ) => {
80
80
const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
81
81
await documentManager . updateDocumentText ( uri , params . text ) ;
82
82
} ) ;
83
83
84
- registerRequestHandler < DocumentColorParams , ColorInformation [ ] > ( DocumentColorRequest . method , async ( params ) => {
84
+ registerRequestHandler ( DocumentColorRequest . type , async ( params ) => {
85
85
const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
86
86
const document = await documentManager . getDocument ( uri ) ;
87
87
88
88
return await DocumentColorHandler . doDocumentColorRequest ( document . uri ) ;
89
89
} ) ;
90
90
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 ) ;
96
94
97
- return await ColorPresentationHandler . doColorPresentationRequest ( document . uri , params ) ;
98
- }
99
- ) ;
95
+ return await ColorPresentationHandler . doColorPresentationRequest ( document . uri , params ) ;
96
+ } ) ;
100
97
101
- registerRequestHandler < FoldingRangeParams , FoldingRange [ ] > ( FoldingRangeRequest . method , async ( params ) => {
98
+ registerRequestHandler ( FoldingRangeRequest . type , async ( params ) => {
102
99
const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
103
100
const document = await documentManager . getDocument ( uri ) ;
104
101
@@ -110,7 +107,7 @@ export function registerRazorEndpoints(
110
107
return FoldingRangeHandler . convertFoldingRanges ( results , razorLogger ) ;
111
108
} ) ;
112
109
113
- registerRequestHandler < HoverParams , Hover | undefined > ( HoverRequest . method , async ( params ) => {
110
+ registerRequestHandler ( HoverRequest . type , async ( params ) => {
114
111
const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
115
112
const document = await documentManager . getDocument ( uri ) ;
116
113
@@ -124,23 +121,20 @@ export function registerRazorEndpoints(
124
121
return rewriteHover ( applicableHover ) ;
125
122
} ) ;
126
123
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 ) ;
132
127
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
+ ) ;
138
133
139
- return rewriteHighlight ( results ) ;
140
- }
141
- ) ;
134
+ return rewriteHighlight ( results ) ;
135
+ } ) ;
142
136
143
- registerRequestHandler < CompletionParams , CompletionList > ( CompletionRequest . method , async ( params ) => {
137
+ registerRequestHandler ( CompletionRequest . type , async ( params ) => {
144
138
const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
145
139
const document = await documentManager . getDocument ( uri ) ;
146
140
@@ -150,24 +144,108 @@ export function registerRazorEndpoints(
150
144
params . context ?. triggerCharacter
151
145
) ;
152
146
} ) ;
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
+ } ) ;
153
231
}
154
232
155
233
function registerNonCohostingEndpoints ( ) {
156
- registerRequestHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
234
+ registerMethodHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
157
235
'razor/provideDynamicFileInfo' ,
158
236
async ( params ) =>
159
237
vscode . commands . executeCommand ( DynamicFileInfoHandler . provideDynamicFileInfoCommand , params )
160
238
) ;
161
239
162
- registerRequestHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
240
+ registerMethodHandler < ProvideDynamicFileParams , ProvideDynamicFileResponse > (
163
241
'razor/removeDynamicFileInfo' ,
164
242
async ( params ) =>
165
243
vscode . commands . executeCommand ( DynamicFileInfoHandler . provideDynamicFileInfoCommand , params )
166
244
) ;
167
- registerRequestHandler < RazorMapSpansParams , RazorMapSpansResponse > ( 'razor/mapSpans' , async ( params ) => {
245
+ registerMethodHandler < RazorMapSpansParams , RazorMapSpansResponse > ( 'razor/mapSpans' , async ( params ) => {
168
246
return await vscode . commands . executeCommand < RazorMapSpansResponse > ( MappingHandler . MapSpansCommand , params ) ;
169
247
} ) ;
170
- registerRequestHandler < RazorMapTextChangesParams , RazorMapTextChangesResponse > (
248
+ registerMethodHandler < RazorMapTextChangesParams , RazorMapTextChangesResponse > (
171
249
'razor/mapTextChanges' ,
172
250
async ( params ) => {
173
251
return await vscode . commands . executeCommand < RazorMapTextChangesResponse > (
@@ -179,7 +257,14 @@ export function registerRazorEndpoints(
179
257
}
180
258
181
259
// 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 > ) {
183
268
const requestType = new RequestType < Params , Result , Error > ( method ) ;
184
269
roslynLanguageServer . registerOnRequest ( requestType , async ( params ) => {
185
270
try {
@@ -235,3 +320,47 @@ function convertHighlightKind(kind: vscode.DocumentHighlightKind | undefined): D
235
320
return undefined ;
236
321
}
237
322
}
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