local lspconfig = require("lspconfig")

vim.diagnostic.config({
  virtual_text = {
    prefix = '',
    spacing = 2,
  },
  signs = true,
  underline = true,
  update_in_insert = false,
  severity_sort = true,
})

lspconfig.rust_analyzer.setup({
  settings = {
    ["rust-analyzer"] = {
      cargo = {
        buildScripts = {
          enable = true
        }
      },
      procMacro = {
        enable = true
      },
      checkOnSave = true,
    }
  }
})

lspconfig.clangd.setup {
    cmd = { "clangd" },
    filetypes = { "c", "cpp", "objc", "objcpp"},
    root_dir = lspconfig.util.root_pattern("compile_commands.json", ".git", "Makefile"),
}

lspconfig.tinymist.setup {
    settings = {
        formatterMode = "typstyle",
        exportPdf = "onSave",
        semanticTokens = "enable"
    }
}

lspconfig.jdtls.setup {
  cmd = {'jdtls'},
  filetypes = { "java" }
}

lspconfig.kotlin_language_server.setup {
  cmd = {'/opt/kotlin-language-server/bin/kotlin-language-server'},
}

lspconfig.bashls.setup({
    filetypes = { "sh", "bash", "zsh", "yash" },
})

lspconfig.lua_ls.setup {
    settings = {
        Lua = {
            diagnostics = {
                globals = { "vim" }, -- Recognize `vim` as a global
            },
            workspace = {
                library = vim.api.nvim_get_runtime_file("", true), -- Make LSP aware of runtime files
                checkThirdParty = false,
            },
            telemetry = {
                enable = false,
            },
        },
    },
}

lspconfig.pylsp.setup {
    cmd = { "pylsp" },
    settings = {
        pylsp = {
            plugins = {
                pycodestyle = { enabled = false }, -- Use ruff instead
                mccabe = { enabled = false },
                pyflakes = { enabled = false },
                pylint = { enabled = false },
                mypy = { enabled = true },
                black = { enabled = true },
                ruff = { enabled = true },
                rope_autoimport = { enabled = true },
            },
        },
    },
}

lspconfig.csharp_ls.setup{
  cmd = { "csharp-ls" },
  filetypes = { "cs", "cshtml", "razor" },
  root_dir = function ()
        -- Manually treat any directory as the root for a single .cs file
        return vim.fn.getcwd()  -- Uses the current working directory
    end,
}
