-- Per-server overrides
vim.lsp.config("sourcekit", {
    filetypes = { "swift", "objective-c", "objective-c++" },
})

vim.lsp.config("svelte", {
    settings = {
        svelte = {
            plugin = {
                svelte = {
                    format = {
                        config = { tabWidth = 4, useTabs = false },
                    },
                },
            },
        },
    },
})

vim.lsp.config("tinymist", {
    cmd = { "tinymist" },
    filetypes = { "typst" },
    settings = { formatterMode = "typstyle" },
})

vim.lsp.enable({
    "lua_ls", "ts_ls", "svelte", "tailwindcss", "cssls", "bashls",
    "rust_analyzer", "wgsl-analyzer", "clangd", "taplo", "sourcekit",
    "tinymist", "jsonls", "hls", "pylsp", "vala_ls",
})

-- JDTLS (managed manually because it needs per-project workspace)
vim.api.nvim_create_autocmd("FileType", {
    pattern = "java",
    callback = function()
        local jdtls = require("jdtls")
        local data = vim.fn.stdpath("data")
        local mason_jdtls = vim.fs.joinpath(data, "mason", "packages", "jdtls")
        local workspace = vim.fs.joinpath(data, "jdtls-workspace",
            vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t"))
        local sys = vim.uv.os_uname().sysname
        local cfg_dir = sys == "Windows_NT" and "config_win"
            or sys == "Linux" and "config_linux"
            or "config_mac"

        local launcher = vim.fn.glob(vim.fs.joinpath(
            mason_jdtls, "plugins", "org.eclipse.equinox.launcher_*.jar"))

        jdtls.start_or_attach({
            cmd = {
                "java",
                "-Declipse.application=org.eclipse.jdt.ls.core.id1",
                "-Dosgi.bundles.defaultStartLevel=4",
                "-Declipse.product=org.eclipse.jdt.ls.core.product",
                "-Dlog.protocol=true",
                "-Dlog.level=ALL",
                "-Xmx1g",
                "--add-modules=ALL-SYSTEM",
                "--add-opens", "java.base/java.util=ALL-UNNAMED",
                "--add-opens", "java.base/java.lang=ALL-UNNAMED",
                "-jar", launcher,
                "-configuration", vim.fs.joinpath(mason_jdtls, cfg_dir),
                "-data", workspace,
            },
            root_dir = vim.fs.root(0, { ".git", "mvnw", "gradlew" }),
            settings = { java = {} },
            init_options = {
                bundles = {
                    vim.fn.glob(vim.fs.joinpath(data, "mason", "share",
                        "java-debug-adapter", "com.microsoft.java.debug.plugin.jar")),
                },
            },
        })
    end,
})
