1
- import { CancellationToken , EventEmitter , InlayHint , InlayHintKind , InlayHintsProvider , Range , TextDocument } from "vscode" ;
1
+ import { CancellationToken , Command , EventEmitter , InlayHint , InlayHintKind , InlayHintLabelPart , InlayHintsProvider , MarkdownString , Range , TextDocument } from "vscode" ;
2
2
import * as ls from 'vscode-languageserver-protocol' ;
3
3
import { LanguageClient , RequestType } from "vscode-languageclient/node" ;
4
4
5
5
export class QuteInlayHintsProvider implements InlayHintsProvider {
6
6
7
- private onDidChange = new EventEmitter < void > ( ) ;
8
- public onDidChangeInlayHints = this . onDidChange . event ;
7
+ private onDidChange = new EventEmitter < void > ( ) ;
8
+ public onDidChangeInlayHints = this . onDidChange . event ;
9
9
10
- constructor ( private client : LanguageClient ) {
11
- this . client . onRequest ( InlayHintRefreshRequest . type , async ( ) => {
12
- this . onDidChange . fire ( ) ;
13
- } ) ;
14
- }
10
+ constructor ( private client : LanguageClient ) {
11
+ this . client . onRequest ( InlayHintRefreshRequest . type , async ( ) => {
12
+ this . onDidChange . fire ( ) ;
13
+ } ) ;
14
+ }
15
15
16
- public async provideInlayHints ( document : TextDocument , range : Range , token : CancellationToken ) : Promise < InlayHint [ ] > {
17
- const requestParams : InlayHintParams = {
18
- textDocument : this . client . code2ProtocolConverter . asTextDocumentIdentifier ( document ) ,
19
- range : this . client . code2ProtocolConverter . asRange ( range )
20
- } ;
21
- try {
22
- const values = await this . client . sendRequest ( InlayHintRequest . type , requestParams , token ) ;
23
- if ( token . isCancellationRequested ) {
24
- return [ ] ;
25
- }
26
- return asInlayHints ( values , this . client ) ;
27
- } catch ( error ) {
28
- return this . client . handleFailedRequest ( InlayHintRequest . type , token , error ) ;
29
- }
16
+ public async provideInlayHints ( document : TextDocument , range : Range , token : CancellationToken ) : Promise < InlayHint [ ] > {
17
+ const requestParams : InlayHintParams = {
18
+ textDocument : this . client . code2ProtocolConverter . asTextDocumentIdentifier ( document ) ,
19
+ range : this . client . code2ProtocolConverter . asRange ( range )
20
+ } ;
21
+ try {
22
+ const values = await this . client . sendRequest ( InlayHintRequest . type , requestParams , token ) ;
23
+ if ( token . isCancellationRequested ) {
24
+ return [ ] ;
25
+ }
26
+ return asInlayHints ( values , this . client ) ;
27
+ } catch ( error ) {
28
+ return this . client . handleFailedRequest ( InlayHintRequest . type , token , error ) ;
30
29
}
30
+ }
31
31
}
32
32
33
33
/**
34
34
* A parameter literal used in inlay hints requests.
35
35
*
36
36
* @since 3.17.0 - proposed state
37
37
*/
38
- export type InlayHintParams = /*WorkDoneProgressParams &*/ {
39
- /**
40
- * The text document.
41
- */
42
- textDocument : ls . TextDocumentIdentifier ;
43
-
44
- /**
45
- * The document range for which inlay hints should be computed.
46
- */
47
- range : ls . Range ;
38
+ export type InlayHintParams = /*WorkDoneProgressParams &*/ {
39
+ /**
40
+ * The text document.
41
+ */
42
+ textDocument : ls . TextDocumentIdentifier ;
43
+
44
+ /**
45
+ * The document range for which inlay hints should be computed.
46
+ */
47
+ range : ls . Range ;
48
48
} ;
49
49
50
50
/**
@@ -54,42 +54,89 @@ export class QuteInlayHintsProvider implements InlayHintsProvider {
54
54
*/
55
55
export type LSInlayHint = {
56
56
57
- /**
58
- * The position of this hint.
59
- */
60
- position : ls . Position ;
61
-
62
- /**
63
- * The label of this hint. A human readable string or an array of
64
- * InlayHintLabelPart label parts.
65
- *
66
- * *Note* that neither the string nor the label part can be empty.
67
- */
68
- label : string ; // label: string | InlayHintLabelPart[];
57
+ /**
58
+ * The position of this hint.
59
+ */
60
+ position : ls . Position ;
61
+
62
+ /**
63
+ * The label of this hint. A human readable string or an array of
64
+ * InlayHintLabelPart label parts.
65
+ *
66
+ * *Note* that neither the string nor the label part can be empty.
67
+ */
68
+ label : string | LSInlayHintLabelPart [ ] ;
69
+ } ;
70
+
71
+ export type LSInlayHintLabelPart = {
72
+
73
+ /**
74
+ * The value of this label part.
75
+ */
76
+ value : string ;
77
+
78
+ /**
79
+ * An optional command for this label part.
80
+ *
81
+ * Depending on the client capability `inlayHint.resolveSupport` clients
82
+ * might resolve this property late using the resolve request.
83
+ */
84
+ command ?: ls . Command ;
85
+
86
+ /**
87
+ * The tooltip text when you hover over this label part. Depending on
88
+ * the client capability `inlayHint.resolveSupport` clients might resolve
89
+ * this property late using the resolve request.
90
+ */
91
+ tooltip ?: string | ls . MarkupContent ;
92
+
69
93
} ;
70
94
71
95
namespace InlayHintRequest {
72
- export const type : RequestType < InlayHintParams , LSInlayHint [ ] , any > = new RequestType ( 'textDocument/inlayHint' ) ;
96
+ export const type : RequestType < InlayHintParams , LSInlayHint [ ] , any > = new RequestType ( 'textDocument/inlayHint' ) ;
73
97
}
74
98
75
99
/**
76
100
* @since 3.17.0 - proposed state
77
101
*/
78
102
namespace InlayHintRefreshRequest {
79
- export const type : RequestType < void , void , void > = new RequestType ( 'workspace/inlayHint/refresh' ) ;
103
+ export const type : RequestType < void , void , void > = new RequestType ( 'workspace/inlayHint/refresh' ) ;
80
104
}
81
105
82
- async function asInlayHints ( values : LSInlayHint [ ] | undefined | null , client : LanguageClient , ) : Promise < InlayHint [ ] | undefined > {
83
- if ( ! Array . isArray ( values ) ) {
84
- return undefined ;
85
- }
86
- return values . map ( lsHint => asInlayHint ( lsHint , client ) ) ;
106
+ async function asInlayHints ( values : LSInlayHint [ ] | undefined | null , client : LanguageClient , ) : Promise < InlayHint [ ] | undefined > {
107
+ if ( ! Array . isArray ( values ) ) {
108
+ return undefined ;
109
+ }
110
+ return values . map ( lsHint => asInlayHint ( lsHint , client ) ) ;
111
+ }
112
+
113
+ function asInlayHint ( item : LSInlayHint , client : LanguageClient ) : InlayHint {
114
+ const label = typeof item . label === 'string'
115
+ ? item . label
116
+ : item . label . map ( asInlayHintLabelPart ) ;
117
+ const result = new InlayHint ( client . protocol2CodeConverter . asPosition ( item . position ) , label ) ;
118
+ result . paddingRight = true ;
119
+ result . kind = InlayHintKind . Parameter ;
120
+ return result ;
121
+ }
122
+
123
+ function asInlayHintLabelPart ( item : LSInlayHintLabelPart ) : InlayHintLabelPart {
124
+ const result = new InlayHintLabelPart ( item . value ) ;
125
+ if ( item . command !== undefined ) { result . command = asCommand ( item . command ) ; }
126
+ if ( item . tooltip !== undefined ) { result . tooltip = asTooltip ( item . tooltip ) ; }
127
+ return result ;
128
+ }
129
+
130
+ function asCommand ( item : ls . Command ) : Command {
131
+ const result = { title : item . title , command : item . command } as Command ;
132
+ if ( item . arguments ) { result . arguments = item . arguments ; }
133
+ return result ;
87
134
}
88
135
89
- function asInlayHint ( value : LSInlayHint , client : LanguageClient ) : InlayHint {
90
- const label = value . label ;
91
- const result = new InlayHint ( client . protocol2CodeConverter . asPosition ( value . position ) , label ) ;
92
- result . paddingRight = true ;
93
- result . kind = InlayHintKind . Parameter ;
94
- return result ;
136
+ function asTooltip ( value : string | ls . MarkupContent ) : string | MarkdownString {
137
+ if ( typeof value === 'string' ) {
138
+ return value ;
139
+ }
140
+ const result = new MarkdownString ( value . value ) ;
141
+ return result ;
95
142
}
0 commit comments