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
use std::sync::Mutex;

use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer};

use crate::capabilities::{
    code_actions, completions, definition, diagnostics, folding, hover, inlay_hints, references,
    rename, selection_range, semantic_tokens, signature_help, symbols,
};
use crate::document::DocumentManager;
use crate::parser::SourceParser;

/// The LSP backend that handles all language server requests
pub struct Backend {
    client: Client,
    document_manager: DocumentManager,
    parser: Mutex<SourceParser>,
}

impl Backend {
    pub fn new(client: Client) -> Self {
        Self {
            client,
            document_manager: DocumentManager::new(),
            parser: Mutex::new(SourceParser::default()),
        }
    }

    /// Publish diagnostics for a document
    async fn publish_diagnostics(&self, uri: Url) {
        let diags = if let Some(doc) = self.document_manager.get(&uri) {
            if let Some(ref tree) = doc.tree {
                diagnostics::get_syntax_diagnostics(tree, &doc.content)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        self.client.publish_diagnostics(uri, diags, None).await;
    }
}

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
    async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
        Ok(InitializeResult {
            capabilities: ServerCapabilities {
                text_document_sync: Some(TextDocumentSyncCapability::Kind(
                    TextDocumentSyncKind::INCREMENTAL,
                )),
                document_symbol_provider: Some(OneOf::Left(true)),
                semantic_tokens_provider: Some(
                    SemanticTokensServerCapabilities::SemanticTokensOptions(
                        SemanticTokensOptions {
                            legend: semantic_tokens::get_legend(),
                            full: Some(SemanticTokensFullOptions::Bool(true)),
                            range: Some(false),
                            work_done_progress_options: WorkDoneProgressOptions::default(),
                        },
                    ),
                ),
                hover_provider: Some(HoverProviderCapability::Simple(true)),
                folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)),
                selection_range_provider: Some(SelectionRangeProviderCapability::Simple(true)),
                definition_provider: Some(OneOf::Left(true)),
                references_provider: Some(OneOf::Left(true)),
                rename_provider: Some(OneOf::Right(RenameOptions {
                    prepare_provider: Some(true),
                    work_done_progress_options: WorkDoneProgressOptions::default(),
                })),
                completion_provider: Some(CompletionOptions {
                    trigger_characters: Some(vec![
                        ".".to_string(),
                        "<".to_string(),
                        "/".to_string(),
                    ]),
                    resolve_provider: Some(true),
                    work_done_progress_options: WorkDoneProgressOptions::default(),
                    all_commit_characters: None,
                    completion_item: None,
                }),
                signature_help_provider: Some(SignatureHelpOptions {
                    trigger_characters: Some(vec!["(".to_string(), ",".to_string()]),
                    retrigger_characters: Some(vec![",".to_string()]),
                    work_done_progress_options: WorkDoneProgressOptions::default(),
                }),
                inlay_hint_provider: Some(OneOf::Left(true)),
                code_action_provider: Some(CodeActionProviderCapability::Options(
                    CodeActionOptions {
                        code_action_kinds: Some(vec![
                            CodeActionKind::QUICKFIX,
                            CodeActionKind::REFACTOR,
                            CodeActionKind::REFACTOR_EXTRACT,
                            CodeActionKind::REFACTOR_REWRITE,
                            CodeActionKind::SOURCE,
                            CodeActionKind::SOURCE_ORGANIZE_IMPORTS,
                        ]),
                        work_done_progress_options: WorkDoneProgressOptions::default(),
                        resolve_provider: Some(false),
                    },
                )),
                ..Default::default()
            },
            server_info: Some(ServerInfo {
                name: "typescript-language-server-rust".to_string(),
                version: Some(env!("CARGO_PKG_VERSION").to_string()),
            }),
        })
    }

    async fn initialized(&self, _: InitializedParams) {
        self.client
            .log_message(
                MessageType::INFO,
                "TypeScript/JavaScript Language Server initialized!",
            )
            .await;
    }

    async fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    async fn did_open(&self, params: DidOpenTextDocumentParams) {
        let uri = params.text_document.uri;
        let content = params.text_document.text;
        let version = params.text_document.version;

        {
            let mut parser = self.parser.lock().unwrap();
            self.document_manager
                .open(uri.clone(), content, version, &mut parser);
        }

        // Publish diagnostics for the newly opened document
        self.publish_diagnostics(uri.clone()).await;

        self.client
            .log_message(MessageType::INFO, format!("Opened document: {}", uri))
            .await;
    }

    async fn did_change(&self, params: DidChangeTextDocumentParams) {
        let uri = params.text_document.uri;
        let version = params.text_document.version;
        let changes = params.content_changes;

        {
            let mut parser = self.parser.lock().unwrap();
            self.document_manager
                .change(&uri, changes, version, &mut parser);
        }

        // Update diagnostics after changes
        self.publish_diagnostics(uri).await;
    }

    async fn did_close(&self, params: DidCloseTextDocumentParams) {
        let uri = params.text_document.uri;
        self.document_manager.close(&uri);

        // Clear diagnostics for closed document
        self.client
            .publish_diagnostics(uri.clone(), Vec::new(), None)
            .await;

        self.client
            .log_message(MessageType::INFO, format!("Closed document: {}", uri))
            .await;
    }

    async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
        let uri = &params.text_document_position_params.text_document.uri;
        let position = params.text_document_position_params.position;

        let result = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                hover::get_hover(tree, &doc.content, position)
            } else {
                None
            }
        } else {
            None
        };

        Ok(result)
    }

    async fn folding_range(&self, params: FoldingRangeParams) -> Result<Option<Vec<FoldingRange>>> {
        let uri = &params.text_document.uri;

        let mut ranges = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                folding::get_folding_ranges(tree, &doc.content)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        // Merge consecutive imports
        folding::merge_import_ranges(&mut ranges);

        Ok(Some(ranges))
    }

    async fn selection_range(
        &self,
        params: SelectionRangeParams,
    ) -> Result<Option<Vec<SelectionRange>>> {
        let uri = &params.text_document.uri;
        let positions = &params.positions;

        let ranges = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                selection_range::get_selection_ranges(tree, positions)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        Ok(Some(ranges))
    }

    async fn goto_definition(
        &self,
        params: GotoDefinitionParams,
    ) -> Result<Option<GotoDefinitionResponse>> {
        let uri = &params.text_document_position_params.text_document.uri;
        let position = params.text_document_position_params.position;

        let result = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref symbol_table) = doc.symbol_table {
                definition::get_definition(symbol_table, &doc.content, position, uri)
            } else {
                None
            }
        } else {
            None
        };

        Ok(result)
    }

    async fn references(&self, params: ReferenceParams) -> Result<Option<Vec<Location>>> {
        let uri = &params.text_document_position.text_document.uri;
        let position = params.text_document_position.position;
        let include_declaration = params.context.include_declaration;

        let result = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref symbol_table) = doc.symbol_table {
                let refs = references::get_references(
                    symbol_table,
                    &doc.content,
                    position,
                    uri,
                    include_declaration,
                );
                if refs.is_empty() { None } else { Some(refs) }
            } else {
                None
            }
        } else {
            None
        };

        Ok(result)
    }

    async fn prepare_rename(
        &self,
        params: TextDocumentPositionParams,
    ) -> Result<Option<PrepareRenameResponse>> {
        let uri = &params.text_document.uri;
        let position = params.position;

        let result = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref symbol_table) = doc.symbol_table {
                rename::prepare_rename(symbol_table, &doc.content, position)
                    .map(PrepareRenameResponse::Range)
            } else {
                None
            }
        } else {
            None
        };

        Ok(result)
    }

    async fn rename(&self, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
        let uri = &params.text_document_position.text_document.uri;
        let position = params.text_document_position.position;
        let new_name = &params.new_name;

        let result = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref symbol_table) = doc.symbol_table {
                rename::rename_symbol(symbol_table, &doc.content, position, new_name, uri)
            } else {
                None
            }
        } else {
            None
        };

        Ok(result)
    }

    async fn document_symbol(
        &self,
        params: DocumentSymbolParams,
    ) -> Result<Option<DocumentSymbolResponse>> {
        let uri = &params.text_document.uri;

        let symbols = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                symbols::get_document_symbols(tree, &doc.content)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        Ok(Some(DocumentSymbolResponse::Nested(symbols)))
    }

    async fn semantic_tokens_full(
        &self,
        params: SemanticTokensParams,
    ) -> Result<Option<SemanticTokensResult>> {
        let uri = &params.text_document.uri;

        let tokens = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                semantic_tokens::get_semantic_tokens(tree, &doc.content)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        Ok(Some(SemanticTokensResult::Tokens(SemanticTokens {
            result_id: None,
            data: tokens,
        })))
    }

    async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> {
        let uri = &params.text_document_position.text_document.uri;

        let items = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                if let Some(ref symbol_table) = doc.symbol_table {
                    completions::get_completions(tree, &doc.content, symbol_table, &params)
                } else {
                    Vec::new()
                }
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        Ok(Some(CompletionResponse::Array(items)))
    }

    async fn completion_resolve(&self, item: CompletionItem) -> Result<CompletionItem> {
        // For now, just return the item as-is
        // In a full implementation, we would fetch additional documentation here
        Ok(item)
    }

    async fn signature_help(&self, params: SignatureHelpParams) -> Result<Option<SignatureHelp>> {
        let uri = &params.text_document_position_params.text_document.uri;
        let position = params.text_document_position_params.position;

        let result = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                if let Some(ref symbol_table) = doc.symbol_table {
                    signature_help::get_signature_help(tree, &doc.content, symbol_table, position)
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            None
        };

        Ok(result)
    }

    async fn inlay_hint(&self, params: InlayHintParams) -> Result<Option<Vec<InlayHint>>> {
        let uri = &params.text_document.uri;
        let range = params.range;

        let hints = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref tree) = doc.tree {
                if let Some(ref symbol_table) = doc.symbol_table {
                    inlay_hints::get_inlay_hints(tree, &doc.content, symbol_table, range)
                } else {
                    Vec::new()
                }
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        Ok(Some(hints))
    }

    async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> {
        let uri = &params.text_document.uri;
        let range = params.range;
        let diagnostics = &params.context.diagnostics;

        let actions = if let Some(doc) = self.document_manager.get(uri) {
            if let Some(ref symbol_table) = doc.symbol_table {
                code_actions::get_code_actions(uri, range, diagnostics, symbol_table, &doc.content)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        Ok(Some(actions))
    }
}