3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { RoslynLanguageServer } from '../server/roslynLanguageServer' ;
7
6
import * as vscode from 'vscode' ;
7
+ import { RoslynLanguageServer } from '../server/roslynLanguageServer' ;
8
8
import {
9
9
ColorInformation ,
10
10
ColorPresentationParams ,
11
11
ColorPresentationRequest ,
12
+ CompletionList ,
13
+ CompletionParams ,
14
+ CompletionRequest ,
12
15
DocumentColorParams ,
13
16
DocumentColorRequest ,
17
+ DocumentHighlight ,
18
+ DocumentHighlightKind ,
19
+ DocumentHighlightParams ,
20
+ DocumentHighlightRequest ,
21
+ FoldingRange ,
22
+ FoldingRangeParams ,
23
+ FoldingRangeRequest ,
24
+ Hover ,
25
+ HoverParams ,
26
+ HoverRequest ,
14
27
LogMessageParams ,
28
+ MarkupKind ,
15
29
NotificationType ,
16
30
RequestType ,
17
31
} from 'vscode-languageclient' ;
@@ -24,6 +38,9 @@ import { DocumentColorHandler } from '../../razor/src/documentColor/documentColo
24
38
import { razorOptions } from '../../shared/options' ;
25
39
import { ColorPresentationHandler } from '../../razor/src/colorPresentation/colorPresentationHandler' ;
26
40
import { ColorPresentation } from 'vscode-html-languageservice' ;
41
+ import { convertRangeToSerializable } from '../../razor/src/rpc/serializableRange' ;
42
+ import { FoldingRangeHandler } from '../../razor/src/folding/foldingRangeHandler' ;
43
+ import { CompletionHandler } from '../../razor/src/completion/completionHandler' ;
27
44
28
45
export function registerRazorEndpoints (
29
46
context : vscode . ExtensionContext ,
@@ -65,6 +82,59 @@ export function registerRazorEndpoints(
65
82
}
66
83
) ;
67
84
85
+ registerRequestHandler < FoldingRangeParams , FoldingRange [ ] > ( FoldingRangeRequest . method , async ( params ) => {
86
+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
87
+ const document = await documentManager . getDocument ( uri ) ;
88
+
89
+ const results = await vscode . commands . executeCommand < vscode . FoldingRange [ ] > (
90
+ 'vscode.executeFoldingRangeProvider' ,
91
+ document . uri
92
+ ) ;
93
+
94
+ return FoldingRangeHandler . convertFoldingRanges ( results , razorLogger ) ;
95
+ } ) ;
96
+
97
+ registerRequestHandler < HoverParams , Hover | undefined > ( HoverRequest . method , async ( params ) => {
98
+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
99
+ const document = await documentManager . getDocument ( uri ) ;
100
+
101
+ const results = await vscode . commands . executeCommand < vscode . Hover [ ] > (
102
+ 'vscode.executeHoverProvider' ,
103
+ document . uri ,
104
+ params . position
105
+ ) ;
106
+ const applicableHover = results . filter ( ( item ) => item . range ) [ 0 ] ;
107
+
108
+ return rewriteHover ( applicableHover ) ;
109
+ } ) ;
110
+
111
+ registerRequestHandler < DocumentHighlightParams , DocumentHighlight [ ] > (
112
+ DocumentHighlightRequest . method ,
113
+ async ( params ) => {
114
+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
115
+ const document = await documentManager . getDocument ( uri ) ;
116
+
117
+ const results = await vscode . commands . executeCommand < vscode . DocumentHighlight [ ] > (
118
+ 'vscode.executeDocumentHighlights' ,
119
+ document . uri ,
120
+ params . position
121
+ ) ;
122
+
123
+ return rewriteHighlight ( results ) ;
124
+ }
125
+ ) ;
126
+
127
+ registerRequestHandler < CompletionParams , CompletionList > ( CompletionRequest . method , async ( params ) => {
128
+ const uri = UriConverter . deserialize ( params . textDocument . uri ) ;
129
+ const document = await documentManager . getDocument ( uri ) ;
130
+
131
+ return CompletionHandler . provideVscodeCompletions (
132
+ document . uri ,
133
+ params . position ,
134
+ params . context ?. triggerCharacter
135
+ ) ;
136
+ } ) ;
137
+
68
138
// Helper method that registers a request handler, and logs errors to the Razor logger.
69
139
function registerRequestHandler < Params , Result > ( method : string , invocation : ( params : Params ) => Promise < Result > ) {
70
140
const requestType = new RequestType < Params , Result , Error > ( method ) ;
@@ -78,3 +148,47 @@ export function registerRazorEndpoints(
78
148
} ) ;
79
149
}
80
150
}
151
+
152
+ function rewriteHover ( hover : vscode . Hover ) : Hover | undefined {
153
+ if ( ! hover ) {
154
+ return undefined ;
155
+ }
156
+
157
+ const markdownString = new vscode . MarkdownString ( ) ;
158
+ for ( const content of hover . contents ) {
159
+ if ( ( content as { language : string ; value : string } ) . language ) {
160
+ const contentObject = content as { language : string ; value : string } ;
161
+ markdownString . appendCodeblock ( contentObject . value , contentObject . language ) ;
162
+ } else {
163
+ const contentValue = ( content as vscode . MarkdownString ) . value ;
164
+ markdownString . appendMarkdown ( contentValue ) ;
165
+ }
166
+ }
167
+
168
+ return {
169
+ contents : { kind : MarkupKind . Markdown , value : markdownString . value } ,
170
+ range : hover . range ? convertRangeToSerializable ( hover . range ) : undefined ,
171
+ } ;
172
+ }
173
+
174
+ function rewriteHighlight ( highlights : vscode . DocumentHighlight [ ] ) : DocumentHighlight [ ] {
175
+ return highlights . map ( ( highlight ) => {
176
+ return {
177
+ range : convertRangeToSerializable ( highlight . range ) ,
178
+ kind : convertHighlightKind ( highlight . kind ) ,
179
+ } ;
180
+ } ) ;
181
+ }
182
+
183
+ function convertHighlightKind ( kind : vscode . DocumentHighlightKind | undefined ) : DocumentHighlightKind | undefined {
184
+ switch ( kind ) {
185
+ case vscode . DocumentHighlightKind . Text :
186
+ return DocumentHighlightKind . Text ;
187
+ case vscode . DocumentHighlightKind . Read :
188
+ return DocumentHighlightKind . Read ;
189
+ case vscode . DocumentHighlightKind . Write :
190
+ return DocumentHighlightKind . Write ;
191
+ default :
192
+ return undefined ;
193
+ }
194
+ }
0 commit comments