glua_ls 1.0.27

Language server for Garry's Mod Lua (GLua).
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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use glua_code_analysis::{
    DeferredVfsDrop, DiagnosticCode, Emmyrc, FileId, fetch_schema_urls, read_file_with_encoding,
    uri_to_file_path,
};
use glua_parser::{LineIndex, LuaParseError, LuaParseErrorKind, LuaParser, LuaSyntaxTree};
use lsp_types::{
    Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams, DidCloseTextDocumentParams,
    DidOpenTextDocumentParams, DidSaveTextDocumentParams, NumberOrString, PublishDiagnosticsParams,
};
use rowan::{NodeCache, TextRange};
use std::sync::Arc;
use std::time::Duration;

use crate::context::{ServerContextSnapshot, WorkspaceDiagnosticLevel};

struct PreparsedDocument {
    tree: LuaSyntaxTree,
    line_index: LineIndex,
    syntax_diagnostics: Vec<Diagnostic>,
}

fn spawn_deferred_drop(deferred_drop: DeferredVfsDrop) {
    tokio::task::spawn_blocking(move || drop(deferred_drop));
}

async fn should_drop_stale_version(
    context: &ServerContextSnapshot,
    uri: &lsp_types::Uri,
    version: i32,
) -> bool {
    context.has_newer_seen_document_version(uri, version).await
}

async fn apply_document_update_without_queuing(
    context: &ServerContextSnapshot,
    uri: &lsp_types::Uri,
    text: String,
    version: i32,
    mut preparsed: Option<PreparsedDocument>,
    trigger_reindex: bool,
) -> Option<FileId> {
    let mut pending_text = Some(text);
    let mut retries = 0u32;

    loop {
        if should_drop_stale_version(context, uri, version).await {
            return None;
        }

        if let Ok(mut analysis) = context.analysis().try_write() {
            let text = pending_text
                .take()
                .expect("document text should still be available");
            let (file_id, deferred_drop) = if let Some(preparsed) = preparsed.take() {
                if trigger_reindex {
                    (
                        analysis.update_file_preparsed(
                            uri.clone(),
                            Some(text),
                            preparsed.tree,
                            preparsed.line_index,
                            Some(version),
                            true,
                        ),
                        None,
                    )
                } else {
                    let (file_id, deferred_drop) = analysis.update_file_preparsed_deferred(
                        uri.clone(),
                        Some(text),
                        preparsed.tree,
                        preparsed.line_index,
                        Some(version),
                    )?;
                    (Some(file_id), Some(deferred_drop))
                }
            } else if trigger_reindex {
                (analysis.update_file_by_uri(uri, Some(text)), None)
            } else {
                (analysis.update_file_text_only(uri, text), None)
            };
            if file_id.is_some() {
                context
                    .file_diagnostic()
                    .invalidate_shared_diagnostic_data();
            }
            drop(analysis);

            if let Some(deferred_drop) = deferred_drop {
                spawn_deferred_drop(deferred_drop);
            }

            return file_id;
        }

        retries += 1;
        if retries <= 20 {
            tokio::task::yield_now().await;
        } else {
            tokio::time::sleep(Duration::from_millis(2)).await;
        }
    }
}

async fn check_schema_update(context: &ServerContextSnapshot) {
    let urls = {
        let read_analysis = context.analysis().read().await;
        if !read_analysis.check_schema_update() {
            return;
        }

        read_analysis.get_schemas_to_fetch()
    };

    if urls.is_empty() {
        return;
    }

    let url_contents = fetch_schema_urls(urls).await;

    let mut write_analysis = context.analysis().write().await;
    write_analysis.apply_fetched_schemas(url_contents);
    context
        .file_diagnostic()
        .invalidate_shared_diagnostic_data();
}

async fn preparse_document(text: String, emmyrc: Arc<Emmyrc>) -> Option<PreparsedDocument> {
    let emmyrc_for_parse = emmyrc.clone();
    let parsed = tokio::task::spawn_blocking(move || {
        let mut node_cache = NodeCache::default();
        let line_index = LineIndex::parse(&text);
        let parse_config = emmyrc_for_parse.get_parse_config(&mut node_cache);
        let tree = LuaParser::parse(&text, parse_config);
        let parse_errors = tree.get_errors().to_vec();
        (tree, line_index, parse_errors, text)
    })
    .await;

    let (tree, line_index, parse_errors, source_text) = match parsed {
        Ok(parsed) => parsed,
        Err(err) => {
            log::error!("failed to preparse text document: {}", err);
            return None;
        }
    };

    let syntax_diagnostics =
        build_syntax_diagnostics(&parse_errors, &line_index, &source_text, emmyrc.as_ref());
    Some(PreparsedDocument {
        tree,
        line_index,
        syntax_diagnostics,
    })
}

fn build_syntax_diagnostics(
    parse_errors: &[LuaParseError],
    line_index: &LineIndex,
    source_text: &str,
    emmyrc: &Emmyrc,
) -> Vec<Diagnostic> {
    parse_errors
        .iter()
        .map(|error| {
            let code = match error.kind {
                LuaParseErrorKind::SyntaxError => DiagnosticCode::SyntaxError,
                LuaParseErrorKind::DocError => DiagnosticCode::DocSyntaxError,
            };

            let severity = emmyrc
                .diagnostics
                .severity
                .get(&code)
                .copied()
                .map(Into::into)
                .unwrap_or(DiagnosticSeverity::ERROR);

            Diagnostic {
                message: error.message.clone(),
                range: parse_error_range_to_lsp_range(error.range, line_index, source_text),
                severity: Some(severity),
                code: Some(NumberOrString::String(code.get_name().to_string())),
                source: Some("GLuaLS".into()),
                ..Default::default()
            }
        })
        .collect()
}

fn parse_error_range_to_lsp_range(
    range: TextRange,
    line_index: &LineIndex,
    source_text: &str,
) -> lsp_types::Range {
    let (start_line, start_character) = line_index
        .get_line_col(range.start(), source_text)
        .unwrap_or((0, 0));
    let (end_line, end_character) = line_index
        .get_line_col(range.end(), source_text)
        .unwrap_or((start_line, start_character));

    lsp_types::Range {
        start: lsp_types::Position {
            line: start_line as u32,
            character: start_character as u32,
        },
        end: lsp_types::Position {
            line: end_line as u32,
            character: end_character as u32,
        },
    }
}

pub async fn on_did_open_text_document(
    context: ServerContextSnapshot,
    params: DidOpenTextDocumentParams,
) -> Option<()> {
    let uri = params.text_document.uri;
    let text = params.text_document.text;
    let version = params.text_document.version;
    let supports_pull = context.lsp_features().supports_pull_diagnostic();

    // Check if file should be filtered before acquiring locks
    // Follow lock order: workspace_manager (read) -> analysis (write)
    let should_process = {
        let analysis = context.analysis().read().await;
        let old_file_id = analysis.get_file_id(&uri);
        if old_file_id.is_some() {
            true
        } else {
            drop(analysis);
            let workspace_manager = context.workspace_manager().read().await;
            workspace_manager.is_workspace_file(&uri)
        }
    };

    if !should_process {
        context.mark_document_closed(&uri).await;
        return None;
    }

    if should_drop_stale_version(&context, &uri, version).await {
        return Some(());
    }

    let emmyrc = {
        let analysis = context.analysis().read().await;
        analysis.get_emmyrc()
    };
    let interval = emmyrc.diagnostics.diagnostic_interval.unwrap_or(500);
    let preparsed = preparse_document(text.clone(), emmyrc).await;
    if should_drop_stale_version(&context, &uri, version).await {
        return Some(());
    }

    let diagnostics = preparsed
        .as_ref()
        .map_or_else(Vec::new, |parsed| parsed.syntax_diagnostics.clone());

    let file_id =
        apply_document_update_without_queuing(&context, &uri, text, version, preparsed, true).await;
    if file_id.is_some() {
        context.note_document_applied_version(&uri, version).await;
        if context.lsp_features().supports_semantic_tokens_refresh() {
            context.client().refresh_semantic_tokens();
        }
    }

    if !supports_pull && file_id.is_some() {
        context
            .client()
            .publish_diagnostics(PublishDiagnosticsParams {
                uri: uri.clone(),
                diagnostics,
                version: Some(version),
            });
    }

    // Schedule diagnostic task without holding any locks
    if !supports_pull {
        if let Some(file_id) = file_id {
            context
                .file_diagnostic()
                .add_diagnostic_task(file_id, interval, Some(context.debounced_analysis_arc()))
                .await;
        }
    }

    Some(())
}

pub async fn on_did_save_text_document(
    context: ServerContextSnapshot,
    _: DidSaveTextDocumentParams,
) -> Option<()> {
    let emmyrc = context.analysis().read().await.get_emmyrc();
    if !emmyrc.workspace.enable_reindex {
        if context.lsp_features().supports_workspace_diagnostic() {
            context
                .file_diagnostic()
                .cancel_workspace_diagnostic()
                .await;

            {
                let workspace_manager = context.workspace_manager().write().await;
                workspace_manager.update_workspace_version(WorkspaceDiagnosticLevel::Slow, true);
            }

            check_schema_update(&context).await;
        }

        return Some(());
    }

    let mut duration = emmyrc.workspace.reindex_duration;
    // if duration is less than 1000ms, set it to 1000ms
    if duration < 1000 {
        duration = 1000;
    }
    {
        let workspace = context.workspace_manager().read().await;
        workspace
            .reindex_workspace(Duration::from_millis(duration))
            .await;
    }

    check_schema_update(&context).await;
    Some(())
}

pub async fn on_did_change_text_document(
    context: ServerContextSnapshot,
    params: DidChangeTextDocumentParams,
) -> Option<()> {
    let uri = params.text_document.uri;
    let text = params.content_changes.first()?.text.clone();
    let version = params.text_document.version;
    let supports_pull = context.lsp_features().supports_pull_diagnostic();

    // Single read-lock acquisition: get file_id + emmyrc + should_process
    let (existing_file_id, previous_text, emmyrc, should_process) = {
        let analysis = context.analysis().read().await;
        let file_id = analysis.get_file_id(&uri);
        let previous_text = file_id.and_then(|file_id| {
            analysis
                .compilation
                .get_db()
                .get_vfs()
                .get_file_content(&file_id)
                .cloned()
        });
        let emmyrc = analysis.get_emmyrc();
        if file_id.is_some() {
            (file_id, previous_text, emmyrc, true)
        } else {
            drop(analysis);
            let workspace_manager = context.workspace_manager().read().await;
            let should = workspace_manager.is_workspace_file(&uri);
            (file_id, previous_text, emmyrc, should)
        }
    };

    // Cancel outstanding diagnostics immediately for this file
    if let Some(file_id) = existing_file_id {
        context
            .file_diagnostic()
            .cancel_file_diagnostic(file_id)
            .await;
    }

    if !should_process {
        context.mark_document_closed(&uri).await;
        return None;
    }

    if should_drop_stale_version(&context, &uri, version).await {
        return Some(());
    }

    let interval = emmyrc.diagnostics.diagnostic_interval.unwrap_or(500);
    context
        .file_diagnostic()
        .note_recent_edit(
            &uri,
            previous_text.as_deref(),
            &text,
            Duration::from_millis(interval),
        )
        .await;
    let preparsed = preparse_document(text.clone(), emmyrc.clone()).await;
    let syntax_diagnostics = preparsed
        .as_ref()
        .map_or_else(Vec::new, |parsed| parsed.syntax_diagnostics.clone());
    if should_drop_stale_version(&context, &uri, version).await {
        return Some(());
    }

    let file_id =
        apply_document_update_without_queuing(&context, &uri, text, version, preparsed, false)
            .await;
    if file_id.is_some() {
        context.note_document_applied_version(&uri, version).await;
    }

    if should_drop_stale_version(&context, &uri, version).await {
        return Some(());
    }

    if !supports_pull && file_id.is_some() {
        if let Some(cached_diagnostics) = context
            .file_diagnostic()
            .cached_display_diagnostics(&uri)
            .await
        {
            context
                .client()
                .publish_diagnostics(PublishDiagnosticsParams {
                    uri: uri.clone(),
                    diagnostics: cached_diagnostics,
                    version: Some(version),
                });
        } else {
            let syntax_diagnostics = context
                .file_diagnostic()
                .filter_display_diagnostics(&uri, syntax_diagnostics)
                .await;
            context
                .client()
                .publish_diagnostics(PublishDiagnosticsParams {
                    uri: uri.clone(),
                    diagnostics: syntax_diagnostics,
                    version: Some(version),
                });
        }
    }

    // Schedule debounced reindex — rapid edits into a single reindex
    if let Some(file_id) = file_id {
        context.debounced_analysis().schedule(file_id).await;
    }

    // Handle reindex without holding locks
    if emmyrc.workspace.enable_reindex {
        let workspace = context.workspace_manager().read().await;
        workspace.extend_reindex_delay().await;
    }

    // Schedule diagnostic task
    if !supports_pull {
        if let Some(file_id) = file_id {
            context
                .file_diagnostic()
                .add_diagnostic_task(file_id, interval, Some(context.debounced_analysis_arc()))
                .await;
        }
    }

    Some(())
}

pub async fn on_did_close_document(
    context: ServerContextSnapshot,
    params: DidCloseTextDocumentParams,
) -> Option<()> {
    let uri = &params.text_document.uri;
    context.file_diagnostic().clear_recent_edit(uri).await;
    let lsp_features = context.lsp_features();
    let (encoding, interval) = {
        let analysis = context.analysis().read().await;
        let emmyrc = analysis.get_emmyrc();
        (
            emmyrc.workspace.encoding.clone(),
            emmyrc.diagnostics.diagnostic_interval.unwrap_or(500),
        )
    };

    // Only remove from the index when the file no longer exists on disk
    // (e.g. it was deleted while open). Files that still exist on disk —
    // including library/annotation files opened via "Go to Definition" —
    // must stay in the index, but their in-memory contents need to revert
    // to the on-disk state once the editor buffer closes.
    if let Some(file_path) = uri_to_file_path(uri) {
        if file_path.exists() {
            if let Some(text) = read_file_with_encoding(&file_path, &encoding) {
                if !context.is_document_closed(uri).await {
                    return Some(());
                }

                let file_id = {
                    let mut analysis = context.analysis().write().await;
                    if !context.is_document_closed(uri).await {
                        return Some(());
                    }
                    let file_id = analysis.update_file_by_uri(uri, Some(text));
                    if file_id.is_some() {
                        context
                            .file_diagnostic()
                            .invalidate_shared_diagnostic_data();
                    }
                    file_id
                };

                if !lsp_features.supports_pull_diagnostic()
                    && let Some(file_id) = file_id
                {
                    if !context.is_document_closed(uri).await {
                        return Some(());
                    }
                    context
                        .file_diagnostic()
                        .add_diagnostic_task(
                            file_id,
                            interval,
                            Some(context.debounced_analysis_arc()),
                        )
                        .await;
                }
            }
        } else {
            if !context.is_document_closed(uri).await {
                return Some(());
            }
            let mut mut_analysis = context.analysis().write().await;
            if !context.is_document_closed(uri).await {
                return Some(());
            }
            mut_analysis.remove_file_by_uri(uri);
            context
                .file_diagnostic()
                .invalidate_shared_diagnostic_data();
            drop(mut_analysis);

            if !lsp_features.supports_pull_diagnostic() {
                context
                    .file_diagnostic()
                    .clear_push_file_diagnostics(uri.clone())
                    .await;
            }
        }
    }

    Some(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::ServerContext;
    use googletest::prelude::*;
    use lsp_server::Connection;
    use lsp_types::{
        ClientCapabilities, SemanticTokensWorkspaceClientCapabilities, TextDocumentItem, Uri,
        WorkspaceClientCapabilities,
    };
    use std::str::FromStr;
    use std::time::Duration;

    #[gtest]
    fn test_on_did_open_text_document_requests_semantic_tokens_refresh() -> Result<()> {
        let runtime = tokio::runtime::Runtime::new().expect("tokio runtime should build");
        let (proxy_connection, peer_connection) = Connection::memory();
        let capabilities = ClientCapabilities {
            workspace: Some(WorkspaceClientCapabilities {
                semantic_tokens: Some(SemanticTokensWorkspaceClientCapabilities {
                    refresh_support: Some(true),
                }),
                ..Default::default()
            }),
            ..Default::default()
        };

        runtime.block_on(async {
            let context = ServerContext::new(proxy_connection, capabilities);
            let snapshot = context.snapshot();
            let uri = Uri::from_str("file:///test.lua").unwrap();

            // Manually add the file to analysis so `should_process` becomes true without dealing with paths
            snapshot
                .analysis()
                .write()
                .await
                .update_file_by_uri(&uri, Some("local x = 1".to_string()));

            on_did_open_text_document(
                snapshot.clone(),
                DidOpenTextDocumentParams {
                    text_document: TextDocumentItem {
                        uri: uri.clone(),
                        language_id: "lua".to_string(),
                        version: 1,
                        text: "local x = 1".to_string(),
                    },
                },
            )
            .await;

            let mut found_refresh = false;
            for _ in 0..5 {
                if let Ok(message) = peer_connection
                    .receiver
                    .recv_timeout(Duration::from_secs(1))
                {
                    if let lsp_server::Message::Request(request) = message {
                        if request.method == "workspace/semanticTokens/refresh" {
                            found_refresh = true;
                            break;
                        }
                    }
                }
            }

            verify_that!(found_refresh, eq(true))?;

            Ok(())
        })
    }

    #[gtest]
    fn test_on_did_open_text_document_does_not_request_refresh_for_stale_version() -> Result<()> {
        let runtime = tokio::runtime::Runtime::new().expect("tokio runtime should build");
        let (proxy_connection, peer_connection) = Connection::memory();
        let capabilities = ClientCapabilities {
            workspace: Some(WorkspaceClientCapabilities {
                semantic_tokens: Some(SemanticTokensWorkspaceClientCapabilities {
                    refresh_support: Some(true),
                }),
                ..Default::default()
            }),
            ..Default::default()
        };

        runtime.block_on(async {
            let context = ServerContext::new(proxy_connection, capabilities);
            let snapshot = context.snapshot();
            let uri = Uri::from_str("file:///test.lua").unwrap();

            snapshot
                .analysis()
                .write()
                .await
                .update_file_by_uri(&uri, Some("local x = 1".to_string()));

            // Mark a newer version as seen so the version 1 is considered stale
            snapshot.note_document_seen_version(&uri, 2).await;

            on_did_open_text_document(
                snapshot.clone(),
                DidOpenTextDocumentParams {
                    text_document: TextDocumentItem {
                        uri: uri.clone(),
                        language_id: "lua".to_string(),
                        version: 1,
                        text: "local x = 1".to_string(),
                    },
                },
            )
            .await;

            let mut found_refresh = false;
            for _ in 0..5 {
                if let Ok(message) = peer_connection
                    .receiver
                    .recv_timeout(Duration::from_millis(50))
                {
                    if let lsp_server::Message::Request(request) = message {
                        if request.method == "workspace/semanticTokens/refresh" {
                            found_refresh = true;
                            break;
                        }
                    }
                }
            }

            verify_that!(found_refresh, eq(false))?;

            Ok(())
        })
    }
}