1
1
import { DecorationOptions , DecorationRangeBehavior , Disposable , OutputChannel , Range , TextDocument , TextDocumentChangeEvent , TextEditor , TextEditorDecorationType , ThemeColor , window , workspace } from "vscode" ;
2
2
import { LanguageClient } from "vscode-languageclient/node" ;
3
3
4
- type Implicit = { position : number , text : string } ;
5
- type ImplicitSet = { before : Implicit [ ] , after : Implicit [ ] } ;
4
+ type InlayHint = { position : number , text : string } ;
5
+ type InlayHintSet = { before : InlayHint [ ] , after : InlayHint [ ] } ;
6
6
7
- class Implicits implements Disposable {
7
+ class InlayHints implements Disposable {
8
8
// The client used to communicate with the language server. In the case of
9
- // this class it's used to send a syntaxTree/implicits request.
9
+ // this class it's used to send a textDocument/inlayHints request.
10
10
private readonly languageClient : LanguageClient ;
11
11
12
12
// The output channel used for logging for this class. It's given from the
13
13
// main file so that it uses the same as the rest of the extension.
14
14
private readonly outputChannel : OutputChannel ;
15
15
16
16
private readonly decorationType : TextEditorDecorationType ;
17
- private readonly implicitsCache : WeakMap < TextDocument , ImplicitSet > ;
17
+ private readonly inlayHintsCache : WeakMap < TextDocument , InlayHintSet > ;
18
18
private readonly debouncedHandleTextDocumentChange : Debounced < TextEditor > ;
19
19
20
20
private readonly disposables : Disposable [ ] ;
@@ -23,31 +23,31 @@ class Implicits implements Disposable {
23
23
this . languageClient = languageClient ;
24
24
this . outputChannel = outputChannel ;
25
25
26
- const color = new ThemeColor ( "syntaxTree.implicits " ) ;
26
+ const color = new ThemeColor ( "syntaxTree.inlayHints " ) ;
27
27
this . decorationType = window . createTextEditorDecorationType ( {
28
28
before : { color, fontStyle : "normal" } ,
29
29
after : { color, fontStyle : "normal" } ,
30
30
rangeBehavior : DecorationRangeBehavior . ClosedClosed
31
31
} ) ;
32
32
33
- this . implicitsCache = new WeakMap ( ) ;
34
- this . setImplicitsForEditors ( window . visibleTextEditors ) ;
33
+ this . inlayHintsCache = new WeakMap ( ) ;
34
+ this . setInlayHintsForEditors ( window . visibleTextEditors ) ;
35
35
36
36
// Here we're going to debounce the handleTextDocumentChange callback so
37
37
// that we're not reacting too quickly to user inputs and making it flash
38
38
// alll around the editor.
39
39
this . debouncedHandleTextDocumentChange = debounce ( 300 , ( editor : TextEditor ) => {
40
40
this . outputChannel . appendLine ( "Handling text document changes (debounced)" ) ;
41
- this . implicitsCache . delete ( editor . document ) ;
42
- this . setImplicitsForEditor ( editor ) ;
41
+ this . inlayHintsCache . delete ( editor . document ) ;
42
+ this . setInlayHintsForEditor ( editor ) ;
43
43
} ) ;
44
44
45
45
// Track all of the various callbacks and objects that implement Disposable
46
- // so that when we need to dispose of the entire Implicits instance we can
46
+ // so that when we need to dispose of the entire InlayHints instance we can
47
47
// iterate through and dispose all of them.
48
48
this . disposables = [
49
49
this . decorationType ,
50
- window . onDidChangeVisibleTextEditors ( this . setImplicitsForEditors , this ) ,
50
+ window . onDidChangeVisibleTextEditors ( this . setInlayHintsForEditors , this ) ,
51
51
workspace . onDidChangeTextDocument ( this . handleTextDocumentChange , this ) ,
52
52
this . debouncedHandleTextDocumentChange
53
53
] ;
@@ -63,20 +63,20 @@ class Implicits implements Disposable {
63
63
if ( editor !== undefined && event . document === editor . document ) {
64
64
this . debouncedHandleTextDocumentChange ( editor ) ;
65
65
} else {
66
- this . implicitsCache . delete ( event . document ) ;
66
+ this . inlayHintsCache . delete ( event . document ) ;
67
67
}
68
68
}
69
69
70
- // Asynchronously get the implicits for a given text document, optionally
70
+ // Asynchronously get the inlay hints for a given text document, optionally
71
71
// using a cache if it has already been populated.
72
- async getImplicitsForTextDocument ( document : TextDocument ) : Promise < ImplicitSet | undefined > {
72
+ async getInlayHintsForTextDocument ( document : TextDocument ) : Promise < InlayHintSet | undefined > {
73
73
if ( document . languageId !== "ruby" ) {
74
74
// This editor may have previously been a Ruby file, but that has now
75
- // changed. So we should delete the implicits that may be there.
76
- if ( this . implicitsCache . has ( document ) ) {
77
- this . implicitsCache . delete ( document ) ;
75
+ // changed. So we should delete the inlay hints that may be there.
76
+ if ( this . inlayHintsCache . has ( document ) ) {
77
+ this . inlayHintsCache . delete ( document ) ;
78
78
79
- // Return an empty set of implicits so that it gets properly cleared
79
+ // Return an empty set of inlay hints so that it gets properly cleared
80
80
// from the document.
81
81
return { before : [ ] , after : [ ] } ;
82
82
}
@@ -86,56 +86,56 @@ class Implicits implements Disposable {
86
86
return undefined ;
87
87
}
88
88
89
- // Check the cache first to see if we have already computed the implicits
89
+ // Check the cache first to see if we have already computed the inlay hints
90
90
// for this document. Return them if we have them.
91
- let implicits = this . implicitsCache . get ( document ) ;
92
- if ( implicits ) {
93
- this . outputChannel . appendLine ( "Loading implicits from cache" ) ;
94
- return implicits ;
91
+ let inlayHints = this . inlayHintsCache . get ( document ) ;
92
+ if ( inlayHints ) {
93
+ this . outputChannel . appendLine ( "Loading inlay hints from cache" ) ;
94
+ return inlayHints ;
95
95
}
96
96
97
- // Otherwise, asynchronously request the implicits from the language server,
98
- // cache the response, and return it.
99
- this . outputChannel . appendLine ( "Requesting implicits " ) ;
100
- implicits = await this . languageClient . sendRequest < ImplicitSet > ( "syntaxTree/implicits " , {
97
+ // Otherwise, asynchronously request the inlay hints from the language
98
+ // server, cache the response, and return it.
99
+ this . outputChannel . appendLine ( "Requesting inlay hints " ) ;
100
+ inlayHints = await this . languageClient . sendRequest < InlayHintSet > ( "textDocument/inlayHints " , {
101
101
textDocument : { uri : document . uri . toString ( ) }
102
102
} ) ;
103
103
104
104
// In case of a syntax error, this is not going to return anything. In that
105
105
// case, we don't want to set the cache to anything, but we also don't want
106
- // to clear the previous implicits either. So we're just going to return
106
+ // to clear the previous inlay hints either. So we're just going to return
107
107
// undefined
108
- if ( ! implicits ) {
108
+ if ( ! inlayHints ) {
109
109
return undefined ;
110
110
}
111
111
112
- this . implicitsCache . set ( document , implicits ) ;
113
- return implicits ;
112
+ this . inlayHintsCache . set ( document , inlayHints ) ;
113
+ return inlayHints ;
114
114
}
115
115
116
- async setImplicitsForEditor ( editor : TextEditor ) {
117
- const implicits = await this . getImplicitsForTextDocument ( editor . document ) ;
118
- if ( ! implicits ) {
116
+ async setInlayHintsForEditor ( editor : TextEditor ) {
117
+ const inlayHints = await this . getInlayHintsForTextDocument ( editor . document ) ;
118
+ if ( ! inlayHints ) {
119
119
return ;
120
120
}
121
121
122
122
const decorations : DecorationOptions [ ] = [
123
- ...implicits . before . map ( ( { position, text : contentText } ) => ( {
123
+ ...inlayHints . before . map ( ( { position, text : contentText } ) => ( {
124
124
range : new Range ( editor . document . positionAt ( position ) , editor . document . positionAt ( position ) ) ,
125
125
renderOptions : { before : { contentText } }
126
126
} ) ) ,
127
- ...implicits . after . map ( ( { position, text : contentText } ) => ( {
127
+ ...inlayHints . after . map ( ( { position, text : contentText } ) => ( {
128
128
range : new Range ( editor . document . positionAt ( position ) , editor . document . positionAt ( position ) ) ,
129
129
renderOptions : { after : { contentText } }
130
130
} ) )
131
131
] ;
132
132
133
- this . outputChannel . appendLine ( "Settings implicits " ) ;
133
+ this . outputChannel . appendLine ( "Settings inlay hints " ) ;
134
134
editor . setDecorations ( this . decorationType , decorations ) ;
135
135
}
136
136
137
- setImplicitsForEditors ( editors : readonly TextEditor [ ] ) {
138
- editors . forEach ( ( editor ) => this . setImplicitsForEditor ( editor ) ) ;
137
+ setInlayHintsForEditors ( editors : readonly TextEditor [ ] ) {
138
+ editors . forEach ( ( editor ) => this . setInlayHintsForEditor ( editor ) ) ;
139
139
}
140
140
}
141
141
@@ -187,4 +187,4 @@ function debounce(delay: number, callback: (argument: T) => vo
187
187
return debounced ;
188
188
}
189
189
190
- export default Implicits ;
190
+ export default InlayHints ;
0 commit comments