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
mod build_hover;
mod color_swatch;
mod find_origin;
mod function;
mod hover_builder;
pub mod hover_expand;
pub mod hover_expand_request;
mod humanize_type_decl;
mod humanize_types;
mod keyword_hover;
mod net_hover;
mod realm_badge;

use super::RegisterCapabilities;
use crate::context::ServerContextSnapshot;
use crate::util::{find_ref_at, resolve_ref_single};
pub use build_hover::build_hover_content_for_completion;
use build_hover::{build_assignment_target_hover, build_semantic_info_hover};
pub use find_origin::{
    DeclOriginResult, find_all_same_named_members, find_member_origin_owner,
    find_member_origin_owners,
};
use glua_code_analysis::{
    EmmyLuaAnalysis, FileId, GmodRealm, LuaMemberKey, LuaSemanticDeclId, LuaType, LuaTypeDeclId,
    RenderLevel, WorkspaceId, humanize_type, resolve_gmod_hook_add_callback_doc_function,
};
use glua_parser::{
    LuaAstNode, LuaAstToken, LuaCallArgList, LuaCallExpr, LuaDocDescription, LuaLiteralExpr,
    LuaStringToken, LuaTokenKind, PathTrait,
};
use glua_parser_desc::parse_ref_target;
pub use hover_builder::HoverBuilder;
pub use humanize_types::infer_prefix_global_name;
use humanize_types::infer_property_owner_realm;
use keyword_hover::{hover_keyword, is_keyword};
use lsp_types::{
    ClientCapabilities, Hover, HoverContents, HoverParams, HoverProviderCapability, MarkupContent,
    Position, ServerCapabilities,
};
use rowan::{TextSize, TokenAtOffset};
use tokio_util::sync::CancellationToken;

pub async fn on_hover(
    context: ServerContextSnapshot,
    params: HoverParams,
    cancel_token: CancellationToken,
) -> Option<Hover> {
    if cancel_token.is_cancelled() {
        return None;
    }
    let uri = params.text_document_position_params.text_document.uri;
    let position = params.text_document_position_params.position;
    let analysis = context.read_analysis(&cancel_token).await?;
    if cancel_token.is_cancelled() {
        return None;
    }
    let file_id = analysis.get_file_id(&uri)?;
    hover(&analysis, file_id, position, None)
}

pub fn hover(
    analysis: &EmmyLuaAnalysis,
    file_id: FileId,
    position: Position,
    render_level: Option<RenderLevel>,
) -> Option<Hover> {
    let semantic_model = analysis.compilation.get_semantic_model(file_id)?;
    if !semantic_model.get_emmyrc().hover.enable {
        return None;
    }

    let root = semantic_model.get_root();
    let position_offset = {
        let document = semantic_model.get_document();
        document.get_offset(position.line as usize, position.character as usize)?
    };

    if position_offset > root.syntax().text_range().end() {
        return None;
    }

    let token = match root.syntax().token_at_offset(position_offset) {
        TokenAtOffset::Single(token) => token,
        TokenAtOffset::Between(left, right) => {
            if matches!(
                right.kind().into(),
                LuaTokenKind::TkDot
                    | LuaTokenKind::TkColon
                    | LuaTokenKind::TkLeftBracket
                    | LuaTokenKind::TkRightBracket
            ) {
                left
            } else {
                right
            }
        }
        TokenAtOffset::None => return None,
    };
    match token {
        function_kw if function_kw.kind() == LuaTokenKind::TkFunction.into() => {
            // For `function` keyword tokens, check if this is a hook.Add callback first.
            // If so, show hook-specific hover (signature + description) instead of generic
            // keyword docs.
            if let Some(hook_callback_hover) = hover_gmod_hook_callback_function(
                analysis,
                &semantic_model,
                file_id,
                position_offset,
                &function_kw,
            ) {
                return Some(hook_callback_hover);
            }
            let document = semantic_model.get_document();
            Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: lsp_types::MarkupKind::Markdown,
                    value: hover_keyword(function_kw.clone()),
                }),
                range: document.to_lsp_range(function_kw.text_range()),
            })
        }
        keywords if is_keyword(keywords.clone()) => {
            let document = semantic_model.get_document();
            Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: lsp_types::MarkupKind::Markdown,
                    value: hover_keyword(keywords.clone()),
                }),
                range: document.to_lsp_range(keywords.text_range()),
            })
        }
        detail if detail.kind() == LuaTokenKind::TkDocDetail.into() => {
            let parent = detail.parent()?;
            let description = LuaDocDescription::cast(parent)?;
            let document = semantic_model.get_document();

            let path = find_ref_at(
                semantic_model
                    .get_module()
                    .map(|m| m.workspace_id)
                    .unwrap_or(WorkspaceId::MAIN),
                semantic_model.get_emmyrc(),
                document.get_text(),
                description.clone(),
                position_offset,
            )?;

            let db = analysis.compilation.get_db();
            let semantic_info = resolve_ref_single(db, file_id, &path, &detail)?;

            build_semantic_info_hover(
                &analysis.compilation,
                &semantic_model,
                db,
                &document,
                detail,
                semantic_info,
                path.last()?.1,
                render_level,
            )
        }
        doc_see if doc_see.kind() == LuaTokenKind::TkDocSeeContent.into() => {
            let document = semantic_model.get_document();

            let path =
                parse_ref_target(document.get_text(), doc_see.text_range(), position_offset)?;

            let db = analysis.compilation.get_db();
            let semantic_info = resolve_ref_single(db, file_id, &path, &doc_see)?;

            build_semantic_info_hover(
                &analysis.compilation,
                &semantic_model,
                db,
                &document,
                doc_see,
                semantic_info,
                path.last()?.1,
                render_level,
            )
        }
        _ => {
            if let Some(net_hover) =
                net_hover::hover_gmod_net_message_string(analysis, &semantic_model, &token)
            {
                return Some(net_hover);
            }

            if let Some(hook_hover) = hover_gmod_hook_name_string(
                analysis,
                &semantic_model,
                file_id,
                position_offset,
                &token,
            ) {
                return Some(hook_hover);
            }

            let db = semantic_model.get_db();
            let document = semantic_model.get_document();
            let Some(semantic_info) = semantic_model.get_semantic_info(token.clone().into()) else {
                return build_assignment_target_hover(
                    &analysis.compilation,
                    &semantic_model,
                    db,
                    &document,
                    token,
                    render_level,
                );
            };
            let range = token.text_range();

            build_semantic_info_hover(
                &analysis.compilation,
                &semantic_model,
                db,
                &document,
                token.clone(),
                semantic_info,
                range,
                render_level,
            )
            .or_else(|| {
                build_assignment_target_hover(
                    &analysis.compilation,
                    &semantic_model,
                    db,
                    &document,
                    token,
                    render_level,
                )
            })
        }
    }
}

const HOOK_OWNER_TYPES: &[&str] = &["GM", "GAMEMODE", "SANDBOX", "PLUGIN"];

fn hover_gmod_hook_name_string(
    analysis: &EmmyLuaAnalysis,
    semantic_model: &glua_code_analysis::SemanticModel,
    file_id: FileId,
    position_offset: TextSize,
    token: &glua_parser::LuaSyntaxToken,
) -> Option<Hover> {
    if !semantic_model.get_emmyrc().gmod.enabled {
        return None;
    }

    let string_token = LuaStringToken::cast(token.clone())?;
    let literal_expr = string_token.get_parent::<LuaLiteralExpr>()?;
    let call_expr = literal_expr
        .get_parent::<LuaCallArgList>()?
        .get_parent::<LuaCallExpr>()?;
    if !is_hook_name_string_context(&call_expr, literal_expr) {
        return None;
    }

    let hook_name = string_token.get_value();
    let hook_name = hook_name.trim();
    if hook_name.is_empty() {
        return None;
    }

    let property_owner =
        resolve_hook_property_owner(semantic_model, file_id, position_offset, hook_name)?;
    let db = semantic_model.get_db();
    let document = semantic_model.get_document();
    let builder = build_hover_content_for_completion(
        &analysis.compilation,
        semantic_model,
        db,
        property_owner,
    )?;
    builder.build_hover_result(document.to_lsp_range(token.text_range()))
}

fn hover_gmod_hook_callback_function(
    analysis: &EmmyLuaAnalysis,
    semantic_model: &glua_code_analysis::SemanticModel,
    file_id: FileId,
    position_offset: TextSize,
    token: &glua_parser::LuaSyntaxToken,
) -> Option<Hover> {
    if !semantic_model.get_emmyrc().gmod.enabled {
        return None;
    }

    // This function is only called from the TkFunction dispatch arm, so the
    // token kind is already guaranteed. No redundant check needed here.

    let closure_expr = glua_parser::LuaClosureExpr::cast(token.parent()?)?;
    let call_arg_list = closure_expr.get_parent::<LuaCallArgList>()?;
    let call_expr = call_arg_list.get_parent::<LuaCallExpr>()?;

    let call_path = call_expr.get_access_path()?;
    if !matches_call_path(&call_path, "hook.Add") {
        return None;
    }

    // Use text range comparison instead of syntax node identity to robustly
    // locate the closure's position in the argument list across traversal paths.
    let closure_range = closure_expr.syntax().text_range();
    let param_idx = call_arg_list
        .get_args()
        .enumerate()
        .find(|(_, arg)| arg.syntax().text_range() == closure_range)
        .map(|(idx, _)| idx);

    if param_idx != Some(2) {
        return None;
    }

    let hook_name = glua_code_analysis::extract_hook_name(&call_expr)?;
    let property_owner =
        resolve_hook_property_owner(semantic_model, file_id, position_offset, &hook_name)?;
    let db = semantic_model.get_db();
    let document = semantic_model.get_document();

    // Build the base hover from the hook property owner (gives description, realm, param docs)
    let mut builder = build_hover_content_for_completion(
        &analysis.compilation,
        semantic_model,
        db,
        property_owner,
    )?;

    // Now override the primary type description with an anonymous callback signature,
    // e.g. `function(ply: Player, seat: Vehicle) -> boolean`
    // using the resolved callback doc function for this hook.
    // param_idx == Some(2) is guaranteed by the guard above.
    if let Some(callback_func) =
        resolve_gmod_hook_add_callback_doc_function(db, &call_expr, 2, None, file_id)
    {
        let params_str = callback_func
            .get_params()
            .iter()
            .map(|(name, ty)| {
                if let Some(ty) = ty {
                    format!("{}: {}", name, humanize_type(db, ty, RenderLevel::Simple))
                } else {
                    name.clone()
                }
            })
            .collect::<Vec<_>>()
            .join(", ");

        let ret = callback_func.get_ret();
        // Show the return type if it is documented. Nil here means the hook signature has
        // no @return annotation (filter_signature_type uses Nil as the default), so we
        // suppress it to avoid a misleading "-> nil" in the hover.
        let ret_str = if ret.is_nil() || ret.is_unknown() {
            String::new()
        } else {
            format!(" -> {}", humanize_type(db, ret, RenderLevel::Simple))
        };

        builder.set_type_description(format!("function({}){}", params_str, ret_str));
        // Clear overloads — an anonymous callback shouldn't show named overloads.
        builder.signature_overload = None;
    }

    builder.build_hover_result(document.to_lsp_range(token.text_range()))
}

pub(crate) fn resolve_hook_property_owner(
    semantic_model: &glua_code_analysis::SemanticModel,
    file_id: FileId,
    position_offset: TextSize,
    hook_name: &str,
) -> Option<LuaSemanticDeclId> {
    let member_key = LuaMemberKey::Name(hook_name.into());
    let db = semantic_model.get_db();
    let call_realm = db
        .get_gmod_infer_index()
        .get_realm_at_offset(&file_id, position_offset);
    let mut fallback = None;

    // Build the full set of owner type names, matching the logic in iter_hook_owner_names()
    // in resolve_closure.rs so that user-configured hook_mappings.method_prefixes are included.
    let mut owner_names: Vec<String> = HOOK_OWNER_TYPES.iter().map(|s| s.to_string()).collect();
    for prefix in &db.get_emmyrc().gmod.hook_mappings.method_prefixes {
        let normalized = prefix.trim().trim_end_matches([':', '.']).to_string();
        if !normalized.is_empty()
            && !owner_names
                .iter()
                .any(|n| n.eq_ignore_ascii_case(&normalized))
        {
            owner_names.push(normalized);
        }
    }

    for owner_name in &owner_names {
        let owner_type = LuaType::Ref(LuaTypeDeclId::global(owner_name));
        let Some(member_infos) = semantic_model.get_member_info_with_key_at_offset(
            &owner_type,
            member_key.clone(),
            true,
            position_offset,
        ) else {
            continue;
        };

        for member_info in member_infos {
            let Some(property_owner) = member_info.property_owner_id else {
                continue;
            };
            if fallback.is_none() {
                fallback = Some(property_owner.clone());
            }

            let Some(property_realm) = infer_property_owner_realm(semantic_model, &property_owner)
            else {
                return Some(property_owner);
            };
            if is_realm_compatible(call_realm, property_realm) {
                return Some(property_owner);
            }
        }
    }

    fallback
}

fn is_hook_name_string_context(call_expr: &LuaCallExpr, literal_expr: LuaLiteralExpr) -> bool {
    let Some(call_path) = call_expr.get_access_path() else {
        return false;
    };
    if !matches_call_path(&call_path, "hook.Add")
        && !matches_call_path(&call_path, "hook.Run")
        && !matches_call_path(&call_path, "hook.Call")
    {
        return false;
    }

    let Some(args_list) = call_expr.get_args_list() else {
        return false;
    };
    let arg_idx = args_list
        .get_args()
        .position(|arg| arg.get_position() == literal_expr.get_position());
    arg_idx == Some(0)
}

fn matches_call_path(path: &str, target: &str) -> bool {
    // All call sites pass a fully-qualified target (e.g. "hook.Add").
    // get_access_path() also returns the full qualified path, so an exact equality check
    // is both necessary and sufficient. A suffix check would produce false positives for
    // paths like "mylib.hook.Add" when the target is "hook.Add".
    path == target
}

fn is_realm_compatible(call_realm: GmodRealm, item_realm: GmodRealm) -> bool {
    !matches!(
        (call_realm, item_realm),
        (GmodRealm::Client, GmodRealm::Server) | (GmodRealm::Server, GmodRealm::Client)
    )
}

pub struct HoverCapabilities;

impl RegisterCapabilities for HoverCapabilities {
    fn register_capabilities(server_capabilities: &mut ServerCapabilities, _: &ClientCapabilities) {
        server_capabilities.hover_provider = Some(HoverProviderCapability::Simple(true));
    }
}