gdscript-ide 0.1.0

The public AnalysisHost/Analysis API for gdscript-analyzer — engine-neutral, protocol-neutral (POD + byte offsets).
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
//! The Tier-0 IDE features: parse diagnostics, document symbols, folding ranges, and
//! by-name completion. Each is a `(db, file) -> value` function over the **memoized**
//! [`parse`](gdscript_db::parse) query, so repeated queries in a revision share one parse and a
//! body edit reuses the cached tree.

use gdscript_base::{
    CompletionItem, CompletionKind, Diagnostic, DiagnosticSource, DocumentSymbol, FoldKind,
    FoldRange, LineIndex, Severity, SymbolKind, TextRange,
};
use gdscript_db::{Db, FileText, parse};
use gdscript_syntax::ast::{self, AstNode, Decl};
use gdscript_syntax::{GdNode, SyntaxKind, tokenize};

/// Convert a `text-size` byte range to the serde POD `TextRange`.
fn to_base_range(r: text_size::TextRange) -> TextRange {
    TextRange::new(u32::from(r.start()), u32::from(r.end()))
}

/// The byte range of a declaration's `Name` child, for the selection range.
fn name_range(node: &GdNode) -> Option<text_size::TextRange> {
    node.children()
        .find(|c| c.kind() == SyntaxKind::Name)
        .map(|n| n.text_range())
}

// ---- diagnostics ----------------------------------------------------------------

/// Parse-error diagnostics (lexer + parser recovery + indentation issues).
#[must_use]
pub fn diagnostics(db: &dyn Db, file: FileText) -> Vec<Diagnostic> {
    parse(db, file)
        .errors()
        .iter()
        .map(|e| Diagnostic {
            range: to_base_range(e.range),
            severity: Severity::Error,
            code: "GDSCRIPT_SYNTAX".to_owned(),
            message: e.message.clone(),
            source: DiagnosticSource::Syntax,
            fixes: Vec::new(),
        })
        .collect()
}

// ---- document symbols -----------------------------------------------------------

/// The document outline.
#[must_use]
pub fn document_symbols(db: &dyn Db, file: FileText) -> Vec<DocumentSymbol> {
    let parsed = parse(db, file);
    match ast::SourceFile::cast(parsed.syntax_node()) {
        Some(file) => decls_to_symbols(file.decls()),
        None => Vec::new(),
    }
}

fn decls_to_symbols(decls: impl Iterator<Item = Decl>) -> Vec<DocumentSymbol> {
    decls.filter_map(|d| decl_to_symbol(&d)).collect()
}

fn decl_to_symbol(decl: &Decl) -> Option<DocumentSymbol> {
    let name = decl.name()?;
    if name.is_empty() {
        return None;
    }
    let range = to_base_range(decl.syntax().text_range());
    let selection_range = name_range(decl.syntax()).map_or(range, to_base_range);
    let children = match decl {
        Decl::Class(c) => c
            .body()
            .map(|b| decls_to_symbols(b.decls()))
            .unwrap_or_default(),
        Decl::Enum(e) => e
            .variants()
            .filter_map(|v| {
                let vname = v.text()?;
                if vname.is_empty() {
                    return None;
                }
                let vrange = to_base_range(v.syntax().text_range());
                Some(DocumentSymbol {
                    name: vname,
                    detail: None,
                    kind: SymbolKind::EnumMember,
                    range: vrange,
                    selection_range: vrange,
                    children: Vec::new(),
                })
            })
            .collect(),
        _ => Vec::new(),
    };
    Some(DocumentSymbol {
        name,
        detail: None,
        kind: symbol_kind(decl),
        range,
        selection_range,
        children,
    })
}

fn symbol_kind(decl: &Decl) -> SymbolKind {
    match decl {
        Decl::ClassName(_) | Decl::Class(_) => SymbolKind::Class,
        Decl::Func(_) => SymbolKind::Function,
        Decl::Var(_) => SymbolKind::Variable,
        Decl::Const(_) => SymbolKind::Constant,
        Decl::Enum(_) => SymbolKind::Enum,
        Decl::Signal(_) => SymbolKind::Signal,
    }
}

// ---- folding ranges -------------------------------------------------------------

/// Foldable ranges: indented blocks, multi-line brackets, and `#region` pairs.
#[must_use]
pub fn folding_ranges(db: &dyn Db, file: FileText) -> Vec<FoldRange> {
    let parsed = parse(db, file);
    let text = file.text(db);
    let index = LineIndex::new(text);
    let mut out = Vec::new();

    for node in ast::descendants(&parsed.syntax_node()) {
        let kind = match node.kind() {
            SyntaxKind::Block | SyntaxKind::ClassBody | SyntaxKind::MatchStmt => FoldKind::Block,
            SyntaxKind::ArrayLit
            | SyntaxKind::DictLit
            | SyntaxKind::ArgList
            | SyntaxKind::ParamList
            | SyntaxKind::EnumDecl => FoldKind::Brackets,
            _ => continue,
        };
        let range = node.text_range();
        if spans_multiple_lines(&index, range) {
            out.push(FoldRange {
                range: to_base_range(range),
                kind,
            });
        }
    }

    out.extend(region_folds(text));
    out
}

fn spans_multiple_lines(index: &LineIndex, range: text_size::TextRange) -> bool {
    index.line_col(u32::from(range.start())).line != index.line_col(u32::from(range.end())).line
}

/// Pair `#region` … `#endregion` comment tokens (nesting-aware).
fn region_folds(text: &str) -> Vec<FoldRange> {
    let mut stack = Vec::new();
    let mut out = Vec::new();
    for tok in tokenize(text) {
        match tok.kind {
            SyntaxKind::RegionComment => stack.push(u32::from(tok.range.start())),
            SyntaxKind::EndRegionComment => {
                if let Some(start) = stack.pop() {
                    out.push(FoldRange {
                        range: TextRange::new(start, u32::from(tok.range.end())),
                        kind: FoldKind::Region,
                    });
                }
            }
            _ => {}
        }
    }
    out
}

// ---- completions ----------------------------------------------------------------

/// By-name completions. Detects an `@` prefix (annotations) vs. ordinary context
/// (keywords + document-local symbol names). No member completion in Tier 0.
#[must_use]
pub fn completions(db: &dyn Db, file: FileText, offset: u32) -> Vec<CompletionItem> {
    let text = file.text(db);
    let bytes = text.as_bytes();
    let cursor = (offset as usize).min(bytes.len());
    // Walk back over the identifier being typed.
    let mut word_start = cursor;
    while word_start > 0 && is_ident_byte(bytes[word_start - 1]) {
        word_start -= 1;
    }
    // Annotation context: the identifier is immediately preceded by `@`.
    if word_start > 0 && bytes[word_start - 1] == b'@' {
        return ANNOTATIONS
            .iter()
            .map(|a| CompletionItem {
                label: (*a).to_owned(),
                kind: CompletionKind::Annotation,
                insert_text: None,
                detail: None,
            })
            .collect();
    }

    let mut items: Vec<CompletionItem> = KEYWORDS
        .iter()
        .map(|k| CompletionItem {
            label: (*k).to_owned(),
            kind: CompletionKind::Keyword,
            insert_text: None,
            detail: None,
        })
        .collect();
    items.extend(local_symbols(db, file));
    items
}

fn is_ident_byte(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_'
}

/// Collect document-local symbol names (declarations, params, enum variants), deduped.
/// Not scope-aware in Tier 0 — every name in the file is offered.
fn local_symbols(db: &dyn Db, file: FileText) -> Vec<CompletionItem> {
    let parsed = parse(db, file);
    let mut seen = std::collections::HashSet::new();
    let mut out = Vec::new();
    for node in ast::descendants(&parsed.syntax_node()) {
        let (name, kind) = if let Some(decl) = Decl::cast(node.clone()) {
            let Some(name) = decl.name() else { continue };
            (name, decl_completion_kind(&decl))
        } else if node.kind() == SyntaxKind::Param {
            match ast::Param::cast(node.clone())
                .and_then(|p| p.name())
                .and_then(|n| n.text())
            {
                Some(name) => (name, CompletionKind::Variable),
                None => continue,
            }
        } else if node.kind() == SyntaxKind::EnumVariant {
            match ast::EnumVariant::cast(node.clone()).and_then(|v| v.text()) {
                Some(name) => (name, CompletionKind::Constant),
                None => continue,
            }
        } else {
            continue;
        };
        if !name.is_empty() && seen.insert(name.clone()) {
            out.push(CompletionItem {
                label: name,
                kind,
                insert_text: None,
                detail: None,
            });
        }
    }
    out
}

fn decl_completion_kind(decl: &Decl) -> CompletionKind {
    match decl {
        Decl::ClassName(_) | Decl::Class(_) => CompletionKind::Class,
        Decl::Func(_) => CompletionKind::Function,
        Decl::Var(_) => CompletionKind::Variable,
        Decl::Const(_) => CompletionKind::Constant,
        Decl::Enum(_) => CompletionKind::Enum,
        Decl::Signal(_) => CompletionKind::Signal,
    }
}

/// The GDScript keyword + literal-keyword table (by-name completion).
const KEYWORDS: &[&str] = &[
    "if",
    "elif",
    "else",
    "for",
    "while",
    "match",
    "when",
    "break",
    "continue",
    "pass",
    "return",
    "var",
    "const",
    "enum",
    "func",
    "static",
    "signal",
    "class",
    "class_name",
    "extends",
    "is",
    "in",
    "as",
    "self",
    "super",
    "void",
    "await",
    "preload",
    "assert",
    "breakpoint",
    "not",
    "and",
    "or",
    "true",
    "false",
    "null",
];

/// The 36 GDScript annotations (names without the leading `@`), Godot 4.5
/// (`plans/research/04-gdscript-semantics-and-features.md` §1.10).
const ANNOTATIONS: &[&str] = &[
    "abstract",
    "export",
    "export_category",
    "export_color_no_alpha",
    "export_custom",
    "export_dir",
    "export_enum",
    "export_exp_easing",
    "export_file",
    "export_file_path",
    "export_flags",
    "export_flags_2d_navigation",
    "export_flags_2d_physics",
    "export_flags_2d_render",
    "export_flags_3d_navigation",
    "export_flags_3d_physics",
    "export_flags_3d_render",
    "export_flags_avoidance",
    "export_global_dir",
    "export_global_file",
    "export_group",
    "export_multiline",
    "export_node_path",
    "export_placeholder",
    "export_range",
    "export_storage",
    "export_subgroup",
    "export_tool_button",
    "icon",
    "onready",
    "rpc",
    "static_unload",
    "tool",
    "warning_ignore",
    "warning_ignore_restore",
    "warning_ignore_start",
];

#[cfg(test)]
mod tests {
    use super::*;
    use gdscript_base::FileId;
    use gdscript_db::RootDatabase;
    use salsa::Durability;

    fn db_ft(src: &str) -> (RootDatabase, FileText) {
        let mut db = RootDatabase::default();
        db.set_file_text(FileId(0), src, Durability::LOW);
        let ft = db.file_text(FileId(0)).unwrap();
        (db, ft)
    }

    #[test]
    fn diagnostics_report_parse_errors() {
        let (db, ft) = db_ft("func f(:\n\tpass\n");
        let diags = diagnostics(&db, ft);
        assert!(!diags.is_empty());
        assert_eq!(diags[0].code, "GDSCRIPT_SYNTAX");
        assert_eq!(diags[0].severity, Severity::Error);
        // Valid code → no diagnostics.
        let (db2, ft2) = db_ft("func f():\n\tpass\n");
        assert!(diagnostics(&db2, ft2).is_empty());
    }

    #[test]
    fn document_symbols_nest_class_and_enum() {
        let src =
            "class_name Foo\nenum E { A, B }\nclass Inner:\n\tvar y = 1\n\tfunc m():\n\t\tpass\n";
        let (db, ft) = db_ft(src);
        let syms = document_symbols(&db, ft);
        let names: Vec<_> = syms.iter().map(|s| s.name.as_str()).collect();
        assert_eq!(names, vec!["Foo", "E", "Inner"]);

        let en = syms.iter().find(|s| s.name == "E").unwrap();
        assert_eq!(en.kind, SymbolKind::Enum);
        let variants: Vec<_> = en.children.iter().map(|c| c.name.as_str()).collect();
        assert_eq!(variants, vec!["A", "B"]);

        let inner = syms.iter().find(|s| s.name == "Inner").unwrap();
        assert_eq!(inner.kind, SymbolKind::Class);
        let members: Vec<_> = inner.children.iter().map(|c| c.name.as_str()).collect();
        assert_eq!(members, vec!["y", "m"]);
    }

    #[test]
    fn folding_includes_blocks_and_regions() {
        let src = "#region Setup\nfunc f():\n\tvar a = 1\n\tvar b = 2\n#endregion\n";
        let (db, ft) = db_ft(src);
        let folds = folding_ranges(&db, ft);
        assert!(folds.iter().any(|f| f.kind == FoldKind::Region));
        assert!(folds.iter().any(|f| f.kind == FoldKind::Block));
    }

    #[test]
    fn completion_after_at_offers_annotations() {
        let src = "@e\n";
        let (db, ft) = db_ft(src);
        let items = completions(&db, ft, 2); // cursor right after "@e"
        assert!(items.iter().all(|i| i.kind == CompletionKind::Annotation));
        assert!(items.iter().any(|i| i.label == "export"));
    }

    #[test]
    fn completion_offers_keywords_and_locals() {
        let src = "var health = 100\nfunc take_damage(amount):\n\tpass\n";
        let (db, ft) = db_ft(src);
        let items = completions(&db, ft, u32::try_from(src.len()).unwrap());
        assert!(
            items
                .iter()
                .any(|i| i.label == "func" && i.kind == CompletionKind::Keyword)
        );
        assert!(items.iter().any(|i| i.label == "health"));
        assert!(
            items
                .iter()
                .any(|i| i.label == "take_damage" && i.kind == CompletionKind::Function)
        );
        assert!(items.iter().any(|i| i.label == "amount"));
    }
}