Skip to main content

zoi_package/
init_lsp.rs

1use anyhow::{Result, anyhow};
2use std::fs;
3use std::path::{Path, PathBuf};
4
5const ZOI_LUA_DEFINITIONS: &str = include_str!("./builtin/lsp/zoi.lua");
6
7pub fn setup_lsp_workspace(path: &Path) -> Result<()> {
8    let lsp_dir = get_lsp_definitions_dir()?;
9    fs::create_dir_all(&lsp_dir)?;
10
11    let zoi_lua_path = lsp_dir.join("zoi.lua");
12    fs::write(&zoi_lua_path, ZOI_LUA_DEFINITIONS)?;
13
14    let luarc_path = path.join(".luarc.json");
15    if !luarc_path.exists() {
16        let luarc_content = generate_luarc_json(&lsp_dir)?;
17        fs::write(luarc_path, luarc_content)?;
18    }
19
20    Ok(())
21}
22
23pub fn get_lsp_definitions_dir() -> Result<PathBuf> {
24    let home_dir = zoi_core::utils::get_user_home()
25        .ok_or_else(|| anyhow!("Could not find home directory."))?;
26    Ok(home_dir.join(".zoi").join("lsp"))
27}
28
29fn generate_luarc_json(lsp_dir: &Path) -> Result<String> {
30    let lsp_dir_str = lsp_dir.to_string_lossy().replace('\\', "/");
31
32    let json = serde_json::json!({
33        "$schema": "https://raw.githubusercontent.com/sumneko/lua-language-server/master/setting/schema.json",
34        "runtime": {
35            "version": "Luau"
36        },
37        "workspace": {
38            "library": [
39                lsp_dir_str
40            ],
41            "checkThirdParty": false
42        },
43        "diagnostics": {
44            "disable": [
45                "lowercase-global"
46            ],
47            "globals": [
48                "SYSTEM", "ZOI", "PKG", "BUILD_DIR", "STAGING_DIR", "BUILD_TYPE", "SUBPKG",
49                "metadata", "dependencies", "updates", "hooks", "service", "prepare", "package",
50                "verify", "test", "uninstall", "cmd", "zcp", "zlicense", "zdoc", "zman", "zsed", "zpatch", "zln", "zchmod", "zchown", "zmkdir",
51                "zrm", "IMPORT", "INCLUDE", "verifyHash", "verifySignature", "addPgpKey", "UTILS"
52            ]
53        }
54    });
55
56    Ok(serde_json::to_string_pretty(&json)?)
57}