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
use tower_lsp::lsp_types::{Hover, HoverContents, MarkupContent, MarkupKind, Position, Range};
use tree_sitter::Tree;

/// Get hover information for a position in the document
pub fn get_hover(tree: &Tree, source: &str, position: Position) -> Option<Hover> {
    let root = tree.root_node();

    // Find the node at the given position
    let point = tree_sitter::Point {
        row: position.line as usize,
        column: position.character as usize,
    };

    let node = root.descendant_for_point_range(point, point)?;

    // Get JSDoc comment if available
    let jsdoc = find_jsdoc_comment(&node, source);

    // Build hover content
    let mut content = String::new();

    // Show the node kind and text
    let node_text = node.utf8_text(source.as_bytes()).ok()?;
    let node_kind = node.kind();

    // Get parent context
    let parent_kind = node.parent().map(|p| p.kind()).unwrap_or("root");

    content.push_str(&format!("**{}**", get_display_kind(node_kind, parent_kind)));

    // Add the identifier name if it's short enough
    if node_text.len() <= 50 && !node_text.contains('\n') {
        content.push_str(&format!(": `{}`", node_text));
    }

    // Add JSDoc if found
    if let Some(doc) = jsdoc {
        content.push_str("\n\n---\n\n");
        content.push_str(&doc);
    }

    // Add syntax tree path for debugging
    content.push_str("\n\n---\n\n");
    content.push_str(&format!("*Node: {}{}*", parent_kind, node_kind));

    let range = Range {
        start: Position::new(
            node.start_position().row as u32,
            node.start_position().column as u32,
        ),
        end: Position::new(
            node.end_position().row as u32,
            node.end_position().column as u32,
        ),
    };

    Some(Hover {
        contents: HoverContents::Markup(MarkupContent {
            kind: MarkupKind::Markdown,
            value: content,
        }),
        range: Some(range),
    })
}

/// Find JSDoc comment associated with a node
fn find_jsdoc_comment(node: &tree_sitter::Node, source: &str) -> Option<String> {
    // Look for comment in previous siblings or parent's previous siblings
    let mut current = *node;

    // First, try to find the declaration this node belongs to
    while let Some(parent) = current.parent() {
        match parent.kind() {
            "function_declaration"
            | "class_declaration"
            | "method_definition"
            | "variable_declaration"
            | "lexical_declaration"
            | "interface_declaration"
            | "type_alias_declaration"
            | "enum_declaration" => {
                current = parent;
                break;
            }
            _ => current = parent,
        }
    }

    // Look for a comment before the declaration
    if let Some(prev) = current.prev_sibling() {
        if prev.kind() == "comment" {
            let text = prev.utf8_text(source.as_bytes()).ok()?;
            return parse_jsdoc(text);
        }
    }

    None
}

/// Parse JSDoc comment and extract documentation
fn parse_jsdoc(comment: &str) -> Option<String> {
    // Check if it's a JSDoc comment (starts with /**)
    if !comment.starts_with("/**") {
        return None;
    }

    let mut lines: Vec<&str> = comment.lines().collect();

    // Remove /** and */
    if let Some(first) = lines.first_mut() {
        *first = first.trim_start_matches("/**").trim();
    }
    if let Some(last) = lines.last_mut() {
        *last = last.trim_end_matches("*/").trim();
    }

    // Process each line
    let processed: Vec<String> = lines
        .iter()
        .map(|line| line.trim().trim_start_matches('*').trim_start().to_string())
        .filter(|line| !line.is_empty())
        .collect();

    if processed.is_empty() {
        return None;
    }

    // Format JSDoc tags
    let mut result = String::new();
    for line in processed {
        if line.starts_with('@') {
            // Format JSDoc tags
            let tag_line = format!("*{}*\n", line);
            result.push_str(&tag_line);
        } else {
            result.push_str(&line);
            result.push('\n');
        }
    }

    Some(result.trim().to_string())
}

/// Get a user-friendly display name for a node kind
fn get_display_kind(kind: &str, parent_kind: &str) -> String {
    match kind {
        "identifier" => match parent_kind {
            "function_declaration" | "function" => "function".to_string(),
            "class_declaration" | "class" => "class".to_string(),
            "interface_declaration" => "interface".to_string(),
            "type_alias_declaration" => "type alias".to_string(),
            "enum_declaration" => "enum".to_string(),
            "variable_declarator" => "variable".to_string(),
            "formal_parameters" | "required_parameter" | "optional_parameter" => {
                "parameter".to_string()
            }
            "property_signature" | "public_field_definition" => "property".to_string(),
            "method_definition" => "method".to_string(),
            _ => "identifier".to_string(),
        },
        "type_identifier" => "type".to_string(),
        "property_identifier" => "property".to_string(),
        "string" | "template_string" => "string literal".to_string(),
        "number" => "number literal".to_string(),
        "true" | "false" => "boolean literal".to_string(),
        "null" => "null".to_string(),
        "undefined" => "undefined".to_string(),
        "this" => "this".to_string(),
        "super" => "super".to_string(),
        "arrow_function" => "arrow function".to_string(),
        "function_declaration" => "function declaration".to_string(),
        "class_declaration" => "class declaration".to_string(),
        "interface_declaration" => "interface declaration".to_string(),
        "type_alias_declaration" => "type alias".to_string(),
        "enum_declaration" => "enum declaration".to_string(),
        "import_statement" => "import statement".to_string(),
        "export_statement" => "export statement".to_string(),
        _ => kind.replace('_', " "),
    }
}

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

    fn parse_typescript(code: &str) -> Tree {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into())
            .unwrap();
        parser.parse(code, None).unwrap()
    }

    #[test]
    fn test_hover_on_variable() {
        let code = "const myVar = 42;";
        let tree = parse_typescript(code);

        // Hover on "myVar" (position 6)
        let hover = get_hover(&tree, code, Position::new(0, 8));
        assert!(hover.is_some());

        let hover = hover.unwrap();
        if let HoverContents::Markup(content) = hover.contents {
            assert!(content.value.contains("myVar"));
        }
    }

    #[test]
    fn test_hover_on_function() {
        let code = "function greet(name: string) { return name; }";
        let tree = parse_typescript(code);

        // Hover on "greet"
        let hover = get_hover(&tree, code, Position::new(0, 11));
        assert!(hover.is_some());
    }

    #[test]
    fn test_hover_on_number() {
        let code = "const x = 42;";
        let tree = parse_typescript(code);

        // Hover on "42"
        let hover = get_hover(&tree, code, Position::new(0, 10));
        assert!(hover.is_some());

        let hover = hover.unwrap();
        if let HoverContents::Markup(content) = hover.contents {
            assert!(content.value.contains("number"));
        }
    }

    #[test]
    fn test_hover_on_string() {
        let code = r#"const x = "hello";"#;
        let tree = parse_typescript(code);

        // Hover on "hello"
        let hover = get_hover(&tree, code, Position::new(0, 12));
        assert!(hover.is_some());

        let hover = hover.unwrap();
        if let HoverContents::Markup(content) = hover.contents {
            assert!(content.value.contains("string"));
        }
    }

    #[test]
    fn test_hover_returns_range() {
        let code = "const x = 42;";
        let tree = parse_typescript(code);

        let hover = get_hover(&tree, code, Position::new(0, 6)).unwrap();
        assert!(hover.range.is_some());

        let range = hover.range.unwrap();
        assert!(range.start.line <= range.end.line);
    }

    #[test]
    fn test_hover_with_jsdoc() {
        let code = r#"
/** This is a greeting function */
function greet() { }
"#;
        let tree = parse_typescript(code);

        // Hover on "greet"
        let hover = get_hover(&tree, code, Position::new(2, 11));
        assert!(hover.is_some());

        let hover = hover.unwrap();
        if let HoverContents::Markup(content) = hover.contents {
            assert!(content.value.contains("greeting"));
        }
    }

    #[test]
    fn test_get_display_kind_identifier() {
        assert_eq!(
            get_display_kind("identifier", "function_declaration"),
            "function"
        );
        assert_eq!(get_display_kind("identifier", "class_declaration"), "class");
        assert_eq!(
            get_display_kind("identifier", "interface_declaration"),
            "interface"
        );
        assert_eq!(
            get_display_kind("identifier", "variable_declarator"),
            "variable"
        );
        assert_eq!(
            get_display_kind("identifier", "method_definition"),
            "method"
        );
        assert_eq!(get_display_kind("identifier", "unknown"), "identifier");
    }

    #[test]
    fn test_get_display_kind_types() {
        assert_eq!(get_display_kind("type_identifier", "any"), "type");
        assert_eq!(get_display_kind("property_identifier", "any"), "property");
    }

    #[test]
    fn test_get_display_kind_literals() {
        assert_eq!(get_display_kind("string", "any"), "string literal");
        assert_eq!(get_display_kind("number", "any"), "number literal");
        assert_eq!(get_display_kind("true", "any"), "boolean literal");
        assert_eq!(get_display_kind("false", "any"), "boolean literal");
    }

    #[test]
    fn test_get_display_kind_keywords() {
        assert_eq!(get_display_kind("null", "any"), "null");
        assert_eq!(get_display_kind("undefined", "any"), "undefined");
        assert_eq!(get_display_kind("this", "any"), "this");
        assert_eq!(get_display_kind("super", "any"), "super");
    }

    #[test]
    fn test_get_display_kind_declarations() {
        assert_eq!(
            get_display_kind("function_declaration", "any"),
            "function declaration"
        );
        assert_eq!(
            get_display_kind("class_declaration", "any"),
            "class declaration"
        );
        assert_eq!(
            get_display_kind("interface_declaration", "any"),
            "interface declaration"
        );
    }

    #[test]
    fn test_get_display_kind_unknown() {
        assert_eq!(
            get_display_kind("some_unknown_node", "any"),
            "some unknown node"
        );
    }

    #[test]
    fn test_hover_empty_position() {
        let code = "const x = 42;";
        let tree = parse_typescript(code);

        // Even position 0,0 should return something
        let hover = get_hover(&tree, code, Position::new(0, 0));
        assert!(hover.is_some());
    }

    #[test]
    fn test_hover_on_class() {
        let code = "class MyClass { }";
        let tree = parse_typescript(code);

        // Hover on "MyClass"
        let hover = get_hover(&tree, code, Position::new(0, 8));
        assert!(hover.is_some());
    }

    #[test]
    fn test_hover_on_interface() {
        let code = "interface IUser { name: string; }";
        let tree = parse_typescript(code);

        // Hover on "IUser"
        let hover = get_hover(&tree, code, Position::new(0, 12));
        assert!(hover.is_some());
    }

    #[test]
    fn test_parse_jsdoc_simple() {
        let comment = "/** Simple comment */";
        let result = parse_jsdoc(comment);
        assert!(result.is_some());
        assert!(result.unwrap().contains("Simple comment"));
    }

    #[test]
    fn test_parse_jsdoc_multiline() {
        let comment = r#"/**
         * This is a description
         * @param name The name
         * @returns The greeting
         */"#;
        let result = parse_jsdoc(comment);
        assert!(result.is_some());
        let result = result.unwrap();
        assert!(result.contains("description"));
        assert!(result.contains("@param"));
    }

    #[test]
    fn test_parse_jsdoc_not_jsdoc() {
        let comment = "// Regular comment";
        let result = parse_jsdoc(comment);
        assert!(result.is_none());
    }

    #[test]
    fn test_parse_jsdoc_empty() {
        let comment = "/** */";
        let result = parse_jsdoc(comment);
        assert!(result.is_none());
    }
}