1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Symbol-oriented `ApiClient` methods.
//!
//! These helpers cover name resolution and symbol lookup entry points from the
//! `tsgo` API. Most methods mirror upstream endpoint names closely so it is easy
//! to correlate source code, wire traces, and TypeScript checker concepts.
use serde_json::json;
use super::{
ApiClient, DocumentIdentifier, DocumentPosition, NodeHandle, ProjectHandle, SnapshotHandle,
SymbolHandle, SymbolResponse, TypeResponse,
requests_core::{
ResolveNameRequest, ShorthandValueRequest, SymbolAtLocationRequest,
SymbolAtPositionRequest, TypeOfSymbolAtLocationRequest,
},
requests_symbols::{
NodeBatchRequest, PositionBatchRequest, SymbolBatchRequest, SymbolOnlyRequest,
},
};
use crate::Result;
impl ApiClient {
/// Returns the symbol visible at a specific UTF-16 position in a file.
///
/// Returns `Ok(None)` when the position does not resolve to a symbol.
pub async fn get_symbol_at_position(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
file: impl Into<DocumentIdentifier>,
position: u32,
) -> Result<Option<SymbolResponse>> {
self.call_optional(
"getSymbolAtPosition",
SymbolAtPositionRequest {
snapshot,
project,
file: file.into(),
position,
},
)
.await
}
/// Resolves symbols for multiple positions in a single file.
///
/// The output order matches the input `positions` order.
pub async fn get_symbols_at_positions(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
file: impl Into<DocumentIdentifier>,
positions: Vec<u32>,
) -> Result<Vec<Option<SymbolResponse>>> {
self.call(
"getSymbolsAtPositions",
PositionBatchRequest {
snapshot,
project,
file: file.into(),
positions,
},
)
.await
}
/// Returns the symbol associated with a specific syntax node.
///
/// Returns `Ok(None)` when the node has no symbol binding.
pub async fn get_symbol_at_location(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
location: NodeHandle,
) -> Result<Option<SymbolResponse>> {
self.call_optional(
"getSymbolAtLocation",
SymbolAtLocationRequest {
snapshot,
project,
location,
},
)
.await
}
/// Resolves symbols for multiple syntax nodes.
///
/// The output order matches the input `locations` order.
pub async fn get_symbols_at_locations(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
locations: Vec<NodeHandle>,
) -> Result<Vec<Option<SymbolResponse>>> {
self.call(
"getSymbolsAtLocations",
NodeBatchRequest {
snapshot,
project,
locations,
},
)
.await
}
/// Returns the apparent checker type of a symbol.
///
/// Returns `Ok(None)` when `tsgo` cannot associate a type with the symbol.
pub async fn get_type_of_symbol(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
symbol: SymbolHandle,
) -> Result<Option<TypeResponse>> {
self.call_optional(
"getTypeOfSymbol",
json!({ "snapshot": snapshot, "project": project, "symbol": symbol }),
)
.await
}
/// Returns the apparent checker types for multiple symbols.
///
/// The output order matches the input `symbols` order.
pub async fn get_types_of_symbols(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
symbols: Vec<SymbolHandle>,
) -> Result<Vec<Option<TypeResponse>>> {
self.call(
"getTypesOfSymbols",
SymbolBatchRequest {
snapshot,
project,
symbols,
},
)
.await
}
/// Returns the declared type of a symbol, if any.
///
/// This differs from [`Self::get_type_of_symbol`] when inference or
/// contextual typing changes the apparent type.
pub async fn get_declared_type_of_symbol(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
symbol: SymbolHandle,
) -> Result<Option<TypeResponse>> {
self.call_optional(
"getDeclaredTypeOfSymbol",
json!({ "snapshot": snapshot, "project": project, "symbol": symbol }),
)
.await
}
#[allow(clippy::too_many_arguments)]
/// Resolves a name through the checker using TypeScript's meaning flags.
///
/// Callers can provide either a node `location` or a `(file, position)`
/// pair, depending on which information they already have on hand.
pub async fn resolve_name(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
name: impl Into<String>,
meaning: u32,
location: Option<NodeHandle>,
file: Option<DocumentIdentifier>,
position: Option<u32>,
exclude_globals: Option<bool>,
) -> Result<Option<SymbolResponse>> {
self.call_optional(
"resolveName",
ResolveNameRequest {
snapshot,
project,
name: name.into(),
location,
file,
position,
meaning,
exclude_globals,
},
)
.await
}
/// Convenience wrapper around [`Self::resolve_name`] for file positions.
pub async fn resolve_name_at_position(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
name: impl Into<String>,
meaning: u32,
location: DocumentPosition,
exclude_globals: Option<bool>,
) -> Result<Option<SymbolResponse>> {
self.resolve_name(
snapshot,
project,
name,
meaning,
None,
Some(location.document),
Some(location.position),
exclude_globals,
)
.await
}
/// Returns the value symbol referenced by a shorthand assignment node.
///
/// For example, in `{ foo }`, this resolves the symbol bound to `foo`.
pub async fn get_shorthand_assignment_value_symbol(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
location: NodeHandle,
) -> Result<Option<SymbolResponse>> {
self.call_optional(
"getShorthandAssignmentValueSymbol",
ShorthandValueRequest {
snapshot,
project,
location,
},
)
.await
}
/// Returns the type of `symbol` as seen from a particular node location.
///
/// This is useful when the same symbol has different contextual views at
/// different use sites.
pub async fn get_type_of_symbol_at_location(
&self,
snapshot: SnapshotHandle,
project: ProjectHandle,
symbol: SymbolHandle,
location: NodeHandle,
) -> Result<Option<TypeResponse>> {
self.call_optional(
"getTypeOfSymbolAtLocation",
TypeOfSymbolAtLocationRequest {
snapshot,
project,
symbol,
location,
},
)
.await
}
/// Returns the parent symbol of `symbol`, if any.
pub async fn get_parent_of_symbol(
&self,
snapshot: SnapshotHandle,
symbol: SymbolHandle,
) -> Result<Option<SymbolResponse>> {
self.call_optional("getParentOfSymbol", SymbolOnlyRequest { snapshot, symbol })
.await
}
/// Returns member symbols directly attached to `symbol`.
///
/// Missing server data is normalized to an empty vector.
pub async fn get_members_of_symbol(
&self,
snapshot: SnapshotHandle,
symbol: SymbolHandle,
) -> Result<Vec<SymbolResponse>> {
self.call::<Option<Vec<SymbolResponse>>, _>(
"getMembersOfSymbol",
SymbolOnlyRequest { snapshot, symbol },
)
.await
.map(|items| items.unwrap_or_default())
}
/// Returns exported symbols directly attached to `symbol`.
///
/// Missing server data is normalized to an empty vector.
pub async fn get_exports_of_symbol(
&self,
snapshot: SnapshotHandle,
symbol: SymbolHandle,
) -> Result<Vec<SymbolResponse>> {
self.call::<Option<Vec<SymbolResponse>>, _>(
"getExportsOfSymbol",
SymbolOnlyRequest { snapshot, symbol },
)
.await
.map(|items| items.unwrap_or_default())
}
/// Returns the export-facing symbol associated with `symbol`.
///
/// Unlike many other helpers in this group, this endpoint is expected to
/// succeed with a concrete symbol response.
pub async fn get_export_symbol_of_symbol(
&self,
snapshot: SnapshotHandle,
symbol: SymbolHandle,
) -> Result<SymbolResponse> {
self.call(
"getExportSymbolOfSymbol",
SymbolOnlyRequest { snapshot, symbol },
)
.await
}
}