achitek-ls 0.2.0

achitekfile language server
Documentation
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
mod completion;
mod definition;
mod hover;
mod navigation;
mod reference;
mod rename;
mod shared;

pub use completion::{Completion, CompletionKind};
pub use definition::DefinitionTarget;
pub use hover::Hover;
pub use reference::ReferenceTarget;
pub use rename::PrepareRenameTarget;

use achitekfile::{ParseError, TextPosition, TextRange};
use tree_sitter::{Node, Tree};

#[derive(Debug)]
pub struct EditorBuffer {
    syntax: SourceTree,
    prompt_declarations: Vec<navigation::PromptDeclaration>,
    symbols: Vec<Symbol>,
}

impl EditorBuffer {
    /// Returns the parsed syntax tree for the document.
    pub fn syntax(&self) -> &SourceTree {
        &self.syntax
    }

    /// Returns document symbols derived from the parsed source.
    pub fn symbols(&self) -> &[Symbol] {
        &self.symbols
    }

    /// Returns hover information for a position in the document.
    pub fn hover(&self, position: TextPosition) -> Option<Hover> {
        hover::hover_for_position(&self.syntax, position)
    }

    /// Returns completion items for a position in the document.
    pub fn completions(&self, position: TextPosition) -> Vec<Completion> {
        completion::completions_for_position(&self.syntax, &self.symbols, position)
    }

    /// Returns the definition target for a position in the document.
    pub fn definition(&self, position: TextPosition) -> Option<DefinitionTarget> {
        navigation::definition_for_position(&self.syntax, &self.prompt_declarations, position)
    }

    /// Returns rename preparation details for a position in the document.
    pub fn prepare_rename(&self, position: TextPosition) -> Option<PrepareRenameTarget> {
        navigation::prepare_rename_for_position(&self.syntax, &self.prompt_declarations, position)
    }

    /// Returns all reference targets related to the symbol under the cursor.
    pub fn references(
        &self,
        position: TextPosition,
        include_declaration: bool,
    ) -> Vec<ReferenceTarget> {
        navigation::references_for_position(
            &self.syntax,
            &self.prompt_declarations,
            position,
            include_declaration,
        )
    }

    /// Returns the prompt name associated with the symbol under the cursor.
    pub fn prompt_name(&self, position: TextPosition) -> Option<String> {
        navigation::prompt_name_at_position(&self.syntax, position, &self.prompt_declarations)
    }
}

/// Parsed source plus the Tree-sitter tree used by editor features.
#[derive(Debug)]
pub struct SourceTree {
    source: String,
    tree: Tree,
}

impl SourceTree {
    /// Returns the original source used to build this syntax tree.
    pub fn source(&self) -> &str {
        &self.source
    }

    /// Returns the raw Tree-sitter tree.
    pub fn tree(&self) -> &Tree {
        &self.tree
    }

    /// Returns the root CST node.
    pub fn root_node(&self) -> Node<'_> {
        self.tree.root_node()
    }

    /// Returns the source range occupied by a given node.
    pub fn range_for(&self, node: Node<'_>) -> TextRange {
        TextRange {
            start: TextPosition {
                line: node.start_position().row,
                byte: node.start_position().column,
            },
            end: TextPosition {
                line: node.end_position().row,
                byte: node.end_position().column,
            },
        }
    }

    /// Returns the source text covered by a given node.
    pub fn text_for<'a>(&'a self, node: Node<'_>) -> &'a str {
        &self.source[node.byte_range()]
    }
}

/// A document symbol derived from Achitek source.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Symbol {
    name: String,
    detail: Option<String>,
    kind: SymbolKind,
    range: TextRange,
    selection_range: TextRange,
    children: Vec<Symbol>,
}

impl Symbol {
    /// Returns the symbol display name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns optional symbol detail text.
    pub fn detail(&self) -> Option<&str> {
        self.detail.as_deref()
    }

    /// Returns the symbol kind.
    pub fn kind(&self) -> SymbolKind {
        self.kind
    }

    /// Returns the full source range occupied by the symbol.
    pub fn range(&self) -> TextRange {
        self.range
    }

    /// Returns the preferred selection range for the symbol.
    pub fn selection_range(&self) -> TextRange {
        self.selection_range
    }

    /// Returns nested child symbols.
    pub fn children(&self) -> &[Symbol] {
        &self.children
    }
}

/// Symbol kinds understood by editor features.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolKind {
    /// The top-level blueprint block.
    Blueprint,
    /// A prompt block.
    Prompt,
    /// A validate block nested inside a prompt.
    Validate,
}

/// Builds editor features for a single Achitek source document.
pub fn build(source: &str) -> Result<EditorBuffer, ParseError> {
    from_source(source)
}

/// Builds editor features for a single Achitek source document.
pub fn from_source(source: &str) -> Result<EditorBuffer, ParseError> {
    let tree = achitekfile::parse(source)?;
    let analysis =
        achitekfile::analyze(source).expect("analysis should not fail after parsing succeeds");
    let syntax = SourceTree {
        source: source.to_owned(),
        tree,
    };
    let prompt_declarations = navigation::collect_prompt_declarations(&syntax, &analysis);
    let symbols = collect_symbols(&syntax, &analysis);

    Ok(EditorBuffer {
        syntax,
        prompt_declarations,
        symbols,
    })
}

fn collect_symbols(syntax: &SourceTree, analysis: &achitekfile::Analysis<'_>) -> Vec<Symbol> {
    let mut symbols = Vec::new();

    if let Some(range) = analysis.file().blueprint().range {
        symbols.push(Symbol {
            name: "blueprint".to_owned(),
            detail: None,
            kind: SymbolKind::Blueprint,
            range,
            selection_range: range,
            children: Vec::new(),
        });
    }

    for prompt in analysis.file().prompts() {
        symbols.push(prompt_symbol(syntax, prompt));
    }

    symbols
}

fn prompt_symbol(
    syntax: &SourceTree,
    prompt: &achitekfile::model::Spanned<achitekfile::model::Prompt>,
) -> Symbol {
    let prompt_block = shared::prompt_block_for_range(syntax, prompt.range);
    let selection_range = prompt_block
        .and_then(|node| node.child_by_field_name("name"))
        .map(|node| syntax.range_for(node))
        .unwrap_or(prompt.range);
    let children = prompt_block
        .map(|node| collect_prompt_children(syntax, node))
        .unwrap_or_default();

    Symbol {
        name: prompt.value.name.clone(),
        detail: Some("prompt".to_owned()),
        kind: SymbolKind::Prompt,
        range: prompt.range,
        selection_range,
        children,
    }
}

fn collect_prompt_children(syntax: &SourceTree, prompt_node: Node<'_>) -> Vec<Symbol> {
    let mut children = Vec::new();

    for index in 0..prompt_node.child_count() {
        let Some(child) =
            prompt_node.child(u32::try_from(index).expect("child index should fit into u32"))
        else {
            continue;
        };

        if child.kind() == "validate_block" {
            let range = syntax.range_for(child);
            children.push(Symbol {
                name: "validate".to_owned(),
                detail: None,
                kind: SymbolKind::Validate,
                range,
                selection_range: range,
                children: Vec::new(),
            });
        }
    }

    children
}

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

    #[test]
    fn filters_prompt_attribute_completions_by_type_and_existing_attributes() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "name" {
              type = string

            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let completions = analysis.completions(TextPosition { line: 7, byte: 2 });

        assert!(!completions.iter().any(|item| item.label() == "type"));
        assert!(!completions.iter().any(|item| item.label() == "choices"));
        assert!(completions.iter().any(|item| item.label() == "default"));
    }

    #[test]
    fn filters_validate_completions_by_prompt_type() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "kind" {
              type = multiselect
              choices = ["a", "b"]

              validate {

              }
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let completions = analysis.completions(TextPosition { line: 10, byte: 4 });

        assert!(
            completions
                .iter()
                .any(|item| item.label() == "min_selections")
        );
        assert!(!completions.iter().any(|item| item.label() == "min_length"));
    }

    #[test]
    fn collects_document_symbols() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = string
              help = "Project name"

              validate {
                min_length = 2
              }
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");

        assert_eq!(analysis.symbols().len(), 2);
        assert_eq!(analysis.symbols()[0].name(), "blueprint");
        assert_eq!(analysis.symbols()[0].kind(), SymbolKind::Blueprint);
        assert_eq!(analysis.symbols()[1].name(), "project_name");
        assert_eq!(analysis.symbols()[1].kind(), SymbolKind::Prompt);
        assert_eq!(analysis.symbols()[1].children().len(), 1);
        assert_eq!(analysis.symbols()[1].children()[0].name(), "validate");
        assert_eq!(
            analysis.symbols()[1].children()[0].kind(),
            SymbolKind::Validate
        );
    }

    #[test]
    fn returns_hover_for_prompt_type() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = string
              help = "Project name"
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let hover = analysis
            .hover(TextPosition { line: 6, byte: 9 })
            .expect("hover should exist for prompt type");

        assert!(hover.contents().contains("`string`"));
        assert!(hover.contents().contains("single-line text prompt"));
    }

    #[test]
    fn returns_prompt_type_completions() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = 
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let completions = analysis.completions(TextPosition { line: 6, byte: 9 });

        assert!(completions.iter().any(|item| item.label() == "string"));
        assert!(completions.iter().any(|item| item.label() == "paragraph"));
    }

    #[test]
    fn returns_depends_on_completions() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = string
              help = "Project name"
            }

            prompt "kind" {
              type = string
              help = "Kind"
              depends_on = 
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let completions = analysis.completions(TextPosition { line: 13, byte: 15 });

        assert!(completions.iter().any(|item| item.label() == "all"));
        assert!(
            completions
                .iter()
                .any(|item| item.label() == "project_name")
        );
    }

    #[test]
    fn resolves_definition_for_prompt_reference() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = string
              help = "Project name"
            }

            prompt "kind" {
              type = string
              help = "Kind"
              depends_on = project_name
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let definition = analysis
            .definition(TextPosition { line: 13, byte: 16 })
            .expect("definition should exist for prompt reference");

        assert_eq!(definition.selection_range().start.line, 5);
        assert_eq!(definition.selection_range().start.byte, 7);
    }

    #[test]
    fn finds_references_for_prompt_definition() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = string
              help = "Project name"
            }

            prompt "kind" {
              type = string
              help = "Kind"
              depends_on = project_name
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let references = analysis.references(TextPosition { line: 5, byte: 9 }, true);

        assert_eq!(references.len(), 2);
        assert_eq!(references[0].range().start.line, 5);
        assert_eq!(references[1].range().start.line, 13);
    }

    #[test]
    fn prepares_rename_for_prompt_definition() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = string
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let target = analysis
            .prepare_rename(TextPosition { line: 5, byte: 10 })
            .expect("prepare rename should exist for prompt definition");

        assert_eq!(target.placeholder(), "project_name");
        assert_eq!(target.range().start.line, 5);
        assert_eq!(target.range().start.byte, 7);
    }

    #[test]
    fn prepares_rename_for_prompt_reference() {
        let source = indoc! {r#"
            blueprint {
              version = "1.0.0"
              name = "minimal"
            }

            prompt "project_name" {
              type = string
            }

            prompt "kind" {
              type = string
              depends_on = project_name
            }
        "#};

        let analysis = from_source(source).expect("valid source should build");
        let target = analysis
            .prepare_rename(TextPosition { line: 11, byte: 16 })
            .expect("prepare rename should exist for prompt reference");

        assert_eq!(target.placeholder(), "project_name");
        assert_eq!(target.range().start.line, 11);
        assert_eq!(target.range().start.byte, 15);
    }
}