typescript-language-server 0.1.0

A high-performance TypeScript and JavaScript language server implemented in Rust
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Symbol table for storing symbols and scopes
//! Some methods are reserved for future type checking features

#![allow(dead_code)]

use std::collections::HashMap;

use tower_lsp::lsp_types::{Position, Range};

use super::{Scope, ScopeKind, Symbol, SymbolFlags, SymbolId};

/// Stores all symbols and scopes for a document
#[derive(Debug)]
pub struct SymbolTable {
    /// All symbols indexed by their id
    symbols: HashMap<SymbolId, Symbol>,
    /// All scopes indexed by their id
    scopes: HashMap<u32, Scope>,
    /// The root scope id
    root_scope_id: u32,
    /// Counter for generating symbol ids
    next_symbol_id: u32,
    /// Counter for generating scope ids
    next_scope_id: u32,
}

impl SymbolTable {
    pub fn new() -> Self {
        let root_scope = Scope::new(
            0,
            ScopeKind::Global,
            None,
            Range {
                start: Position::new(0, 0),
                end: Position::new(u32::MAX, 0),
            },
        );

        let mut scopes = HashMap::new();
        scopes.insert(0, root_scope);

        Self {
            symbols: HashMap::new(),
            scopes,
            root_scope_id: 0,
            next_symbol_id: 0,
            next_scope_id: 1,
        }
    }

    /// Create a new symbol and add it to the table
    pub fn create_symbol(
        &mut self,
        name: String,
        flags: SymbolFlags,
        declaration_range: Range,
        name_range: Range,
        scope_id: u32,
    ) -> SymbolId {
        let id = SymbolId::new(self.next_symbol_id);
        self.next_symbol_id += 1;

        let symbol = Symbol::new(
            id,
            name.clone(),
            flags,
            declaration_range,
            name_range,
            scope_id,
        );

        // Add to scope
        if let Some(scope) = self.scopes.get_mut(&scope_id) {
            if flags.intersects(
                SymbolFlags::INTERFACE | SymbolFlags::TYPE_ALIAS | SymbolFlags::TYPE_PARAMETER,
            ) {
                scope.add_type_symbol(name, id);
            } else {
                scope.add_symbol(name, id);
            }
        }

        self.symbols.insert(id, symbol);
        id
    }

    /// Create a new scope
    pub fn create_scope(&mut self, kind: ScopeKind, parent_id: u32, range: Range) -> u32 {
        let id = self.next_scope_id;
        self.next_scope_id += 1;

        let scope = Scope::new(id, kind, Some(parent_id), range);
        self.scopes.insert(id, scope);

        // Add as child to parent
        if let Some(parent) = self.scopes.get_mut(&parent_id) {
            parent.children.push(id);
        }

        id
    }

    /// Get the root scope id
    pub fn root_scope_id(&self) -> u32 {
        self.root_scope_id
    }

    /// Get a symbol by id
    pub fn get_symbol(&self, id: SymbolId) -> Option<&Symbol> {
        self.symbols.get(&id)
    }

    /// Get a mutable symbol by id
    #[allow(dead_code)] // Reserved for future type checking features
    pub fn get_symbol_mut(&mut self, id: SymbolId) -> Option<&mut Symbol> {
        self.symbols.get_mut(&id)
    }

    /// Get a scope by id
    pub fn get_scope(&self, id: u32) -> Option<&Scope> {
        self.scopes.get(&id)
    }

    /// Get a mutable scope by id
    #[allow(dead_code)] // Reserved for future scope manipulation
    pub fn get_scope_mut(&mut self, id: u32) -> Option<&mut Scope> {
        self.scopes.get_mut(&id)
    }

    /// Look up a symbol by name, searching from the given scope upward
    pub fn lookup(&self, name: &str, scope_id: u32) -> Option<SymbolId> {
        let mut current_scope_id = Some(scope_id);

        while let Some(id) = current_scope_id {
            if let Some(scope) = self.scopes.get(&id) {
                if let Some(symbol_id) = scope.lookup_local(name) {
                    return Some(symbol_id);
                }
                current_scope_id = scope.parent;
            } else {
                break;
            }
        }

        None
    }

    /// Look up a type symbol by name, searching from the given scope upward
    pub fn lookup_type(&self, name: &str, scope_id: u32) -> Option<SymbolId> {
        let mut current_scope_id = Some(scope_id);

        while let Some(id) = current_scope_id {
            if let Some(scope) = self.scopes.get(&id) {
                if let Some(symbol_id) = scope.lookup_type_local(name) {
                    return Some(symbol_id);
                }
                current_scope_id = scope.parent;
            } else {
                break;
            }
        }

        None
    }

    /// Find the innermost scope containing a position
    pub fn scope_at_position(&self, pos: Position) -> u32 {
        self.find_scope_at_position(self.root_scope_id, pos)
    }

    fn find_scope_at_position(&self, scope_id: u32, pos: Position) -> u32 {
        if let Some(scope) = self.scopes.get(&scope_id) {
            // Check children first (they're more specific)
            for &child_id in &scope.children {
                if let Some(child) = self.scopes.get(&child_id) {
                    if child.contains_position(pos) {
                        return self.find_scope_at_position(child_id, pos);
                    }
                }
            }

            // No child contains the position, return this scope
            if scope.contains_position(pos) {
                return scope_id;
            }
        }

        self.root_scope_id
    }

    /// Find symbol at a specific position
    pub fn symbol_at_position(&self, pos: Position) -> Option<SymbolId> {
        for symbol in self.symbols.values() {
            if symbol.name_range.start <= pos && pos <= symbol.name_range.end {
                return Some(symbol.id);
            }
        }
        None
    }

    /// Get all symbols in the table
    pub fn all_symbols(&self) -> impl Iterator<Item = &Symbol> {
        self.symbols.values()
    }

    /// Get all scopes in the table
    pub fn all_scopes(&self) -> impl Iterator<Item = &Scope> {
        self.scopes.values()
    }

    /// Add a reference to a symbol
    pub fn add_reference(&mut self, symbol_id: SymbolId, range: Range) {
        if let Some(symbol) = self.symbols.get_mut(&symbol_id) {
            symbol.add_reference(range);
        }
    }

    /// Find the definition of a symbol at a given position
    #[allow(dead_code)] // Reserved for future go-to-definition enhancements
    pub fn find_definition(&self, pos: Position) -> Option<&Symbol> {
        // First check if we're on a symbol's name
        if let Some(symbol_id) = self.symbol_at_position(pos) {
            return self.get_symbol(symbol_id);
        }

        // Otherwise, we might be on a reference - need to look up the name
        // This requires knowing what identifier we're on, which needs the source text
        None
    }

    /// Find all references to a symbol
    pub fn find_references(&self, symbol_id: SymbolId) -> Vec<Range> {
        if let Some(symbol) = self.get_symbol(symbol_id) {
            let mut refs = vec![symbol.name_range];
            refs.extend(symbol.references.iter().cloned());
            refs
        } else {
            Vec::new()
        }
    }
}

impl Default for SymbolTable {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_symbol_table_new() {
        let table = SymbolTable::new();
        assert_eq!(table.root_scope_id(), 0);

        let root = table.get_scope(0).unwrap();
        assert_eq!(root.kind, ScopeKind::Global);
        assert!(root.parent.is_none());
    }

    #[test]
    fn test_symbol_table_default() {
        let table = SymbolTable::default();
        assert_eq!(table.root_scope_id(), 0);
    }

    #[test]
    fn test_create_symbol() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };

        let id = table.create_symbol("test".to_string(), SymbolFlags::VARIABLE, range, range, 0);

        let symbol = table.get_symbol(id).unwrap();
        assert_eq!(symbol.name, "test");
        assert_eq!(symbol.flags, SymbolFlags::VARIABLE);
        assert_eq!(symbol.scope_id, 0);
    }

    #[test]
    fn test_create_type_symbol() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };

        let id = table.create_symbol("User".to_string(), SymbolFlags::INTERFACE, range, range, 0);

        // Type symbols should be in type_symbols
        let scope = table.get_scope(0).unwrap();
        assert!(scope.type_symbols.contains_key("User"));
        assert_eq!(scope.type_symbols.get("User").copied(), Some(id));
    }

    #[test]
    fn test_create_scope() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(5, 0),
            end: Position::new(10, 0),
        };

        let scope_id = table.create_scope(ScopeKind::Function, 0, range);

        let scope = table.get_scope(scope_id).unwrap();
        assert_eq!(scope.kind, ScopeKind::Function);
        assert_eq!(scope.parent, Some(0));

        // Should be added as child to parent
        let parent = table.get_scope(0).unwrap();
        assert!(parent.children.contains(&scope_id));
    }

    #[test]
    fn test_lookup_in_scope() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };

        let id = table.create_symbol("x".to_string(), SymbolFlags::VARIABLE, range, range, 0);

        assert_eq!(table.lookup("x", 0), Some(id));
        assert_eq!(table.lookup("y", 0), None);
    }

    #[test]
    fn test_lookup_in_parent_scope() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(10, 0),
        };

        // Create symbol in root scope
        let id = table.create_symbol("x".to_string(), SymbolFlags::VARIABLE, range, range, 0);

        // Create child scope
        let child_scope_id = table.create_scope(ScopeKind::Function, 0, range);

        // Should find symbol from child scope
        assert_eq!(table.lookup("x", child_scope_id), Some(id));
    }

    #[test]
    fn test_lookup_shadowing() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(10, 0),
        };

        // Create symbol in root scope
        let outer_id = table.create_symbol("x".to_string(), SymbolFlags::VARIABLE, range, range, 0);

        // Create child scope
        let child_scope_id = table.create_scope(ScopeKind::Block, 0, range);

        // Create shadowing symbol in child scope
        let inner_id = table.create_symbol(
            "x".to_string(),
            SymbolFlags::VARIABLE,
            range,
            range,
            child_scope_id,
        );

        // Should find inner symbol from child scope
        assert_eq!(table.lookup("x", child_scope_id), Some(inner_id));
        // Should find outer symbol from root scope
        assert_eq!(table.lookup("x", 0), Some(outer_id));
    }

    #[test]
    fn test_lookup_type() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };

        let id = table.create_symbol("User".to_string(), SymbolFlags::INTERFACE, range, range, 0);

        assert_eq!(table.lookup_type("User", 0), Some(id));
        assert_eq!(table.lookup_type("Unknown", 0), None);
    }

    #[test]
    fn test_scope_at_position() {
        let mut table = SymbolTable::new();

        let child_range = Range {
            start: Position::new(5, 0),
            end: Position::new(10, 0),
        };

        let child_scope_id = table.create_scope(ScopeKind::Function, 0, child_range);

        // Position inside child scope
        let scope = table.scope_at_position(Position::new(7, 5));
        assert_eq!(scope, child_scope_id);

        // Position outside child scope
        let scope = table.scope_at_position(Position::new(2, 0));
        assert_eq!(scope, 0);
    }

    #[test]
    fn test_symbol_at_position() {
        let mut table = SymbolTable::new();

        let decl_range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 15),
        };
        let name_range = Range {
            start: Position::new(0, 6),
            end: Position::new(0, 7),
        };

        let id = table.create_symbol(
            "x".to_string(),
            SymbolFlags::VARIABLE,
            decl_range,
            name_range,
            0,
        );

        // Position on name
        assert_eq!(table.symbol_at_position(Position::new(0, 6)), Some(id));

        // Position outside name
        assert_eq!(table.symbol_at_position(Position::new(0, 10)), None);
    }

    #[test]
    fn test_add_reference() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };

        let id = table.create_symbol("x".to_string(), SymbolFlags::VARIABLE, range, range, 0);

        let ref_range = Range {
            start: Position::new(5, 0),
            end: Position::new(5, 1),
        };

        table.add_reference(id, ref_range);

        let symbol = table.get_symbol(id).unwrap();
        assert_eq!(symbol.references.len(), 1);
        assert_eq!(symbol.references[0], ref_range);
    }

    #[test]
    fn test_find_references() {
        let mut table = SymbolTable::new();

        let decl_range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };
        let name_range = Range {
            start: Position::new(0, 6),
            end: Position::new(0, 7),
        };

        let id = table.create_symbol(
            "x".to_string(),
            SymbolFlags::VARIABLE,
            decl_range,
            name_range,
            0,
        );

        let ref1 = Range {
            start: Position::new(1, 0),
            end: Position::new(1, 1),
        };
        let ref2 = Range {
            start: Position::new(2, 5),
            end: Position::new(2, 6),
        };

        table.add_reference(id, ref1);
        table.add_reference(id, ref2);

        let refs = table.find_references(id);
        assert_eq!(refs.len(), 3); // name_range + 2 references
        assert!(refs.contains(&name_range));
        assert!(refs.contains(&ref1));
        assert!(refs.contains(&ref2));
    }

    #[test]
    fn test_all_symbols() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };

        table.create_symbol("a".to_string(), SymbolFlags::VARIABLE, range, range, 0);
        table.create_symbol("b".to_string(), SymbolFlags::FUNCTION, range, range, 0);
        table.create_symbol("c".to_string(), SymbolFlags::CLASS, range, range, 0);

        let symbols: Vec<_> = table.all_symbols().collect();
        assert_eq!(symbols.len(), 3);

        let names: Vec<&str> = symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(names.contains(&"a"));
        assert!(names.contains(&"b"));
        assert!(names.contains(&"c"));
    }

    #[test]
    fn test_all_scopes() {
        let mut table = SymbolTable::new();

        let range = Range {
            start: Position::new(0, 0),
            end: Position::new(10, 0),
        };

        table.create_scope(ScopeKind::Function, 0, range);
        table.create_scope(ScopeKind::Block, 0, range);

        let scopes: Vec<_> = table.all_scopes().collect();
        assert_eq!(scopes.len(), 3); // root + 2 created
    }

    #[test]
    fn test_nested_scopes() {
        let mut table = SymbolTable::new();

        let outer_range = Range {
            start: Position::new(0, 0),
            end: Position::new(20, 0),
        };
        let inner_range = Range {
            start: Position::new(5, 0),
            end: Position::new(15, 0),
        };

        let outer_id = table.create_scope(ScopeKind::Function, 0, outer_range);
        let inner_id = table.create_scope(ScopeKind::Block, outer_id, inner_range);

        // Position in inner scope
        let scope = table.scope_at_position(Position::new(10, 0));
        assert_eq!(scope, inner_id);

        // Position in outer scope but not inner
        let scope = table.scope_at_position(Position::new(17, 0));
        assert_eq!(scope, outer_id);
    }
}