
vim.list_extend(lvim.lsp.automatic_configuration.skipped_servers, { "rust_analyzer" })

local mason_path = vim.fn.glob(vim.fn.stdpath "data" .. "/mason/")
local codelldb_adapter = {
  type = "server",
  port = "${port}",
  executable = {
    command = mason_path .. "bin/codelldb",
    args = { "--port", "${port}" },
  },
}

pcall(function()
  require("rust-tools").setup {
    tools = {
      -- executor = require("rust-tools/executors").termopen, -- can be quickfix or termopen
      executor = require("rust-tools/executors").toggleterm, -- can be quickfix or termopen
      reload_workspace_from_cargo_toml = true,
      runnables = {
        use_telescope = true,
      },
      inlay_hints = {
        auto = true,
        only_current_line = false,
        show_parameter_hints = true,
        parameter_hints_prefix = "<-",
        other_hints_prefix = "=>",
        max_len_align = false,
        max_len_align_padding = 1,
        right_align = false,
        right_align_padding = 7,
        highlight = "Comment",
      },
      hover_actions = {
        border = "rounded",
      },
      on_initialized = function()
        vim.api.nvim_create_autocmd({ "BufWritePost", "BufEnter", "CursorHold", "InsertLeave" }, {
          pattern = { "*.rs" },
          callback = function()
            local _, _ = pcall(vim.lsp.codelens.refresh)
          end,
        })
      end,
    },
    dap = {
      adapter = codelldb_adapter,
    },
    server = {
      on_attach = function(client, bufnr)
        require("lvim.lsp").common_on_attach(client, bufnr)
        local rt = require "rust-tools"
        vim.keymap.set("n", "K", rt.hover_actions.hover_actions, { buffer = bufnr })
      end,

      capabilities = require("lvim.lsp").common_capabilities(),
      settings = {
        ["rust-analyzer"] = {
          lens = {
            enable = true,
          },
          checkOnSave = {
            enable = true,
            command = "clippy",
          },
        },
      },
    },
  }
end)

-- CHANGED --
lvim.builtin.dap.on_config_done = function(dap)
  dap.adapters.codelldb = codelldb_adapter
  dap.configurations.rust = {
    {
      name = "Launch file",
      type = "codelldb",
      request = "launch",
      program = function()
        return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
      end,
      cwd = "${workspaceFolder}",
      stopOnEntry = false,
    },
  }
end



-- local status_ok, hints = pcall(require, "lsp-inlayhints")
-- if not status_ok then
--   return
-- end

-- local group = vim.api.nvim_create_augroup("LspAttach_inlayhints", {})
-- vim.api.nvim_create_autocmd("LspAttach", {
--   group = "LspAttach_inlayhints",
--   callback = function(args)
--     if not (args.data and args.data.client_id) then
--       return
--     end

--     local client = vim.lsp.get_client_by_id(args.data.client_id)
--     require("lsp-inlayhints").on_attach(client, args.buf)
--   end,
-- })

-- hints.setup {
--   inlay_hints = {
--     parameter_hints = {
--       show = true,
--       prefix = "<- ",
--       separator = ", ",
--     },
--     type_hints = {
--       -- type and other hints
--       show = true,
--       prefix = "",
--       separator = ", ",
--       remove_colon_end = false,
--       remove_colon_start = false,
--     },
--     -- separator between types and parameter hints. Note that type hints are
--     -- shown before parameter
--     labels_separator = "  ",
--     -- whether to align to the length of the longest line in the file
--     max_len_align = false,
--     -- padding from the left if max_len_align is true
--     max_len_align_padding = 1,
--     -- whether to align to the extreme right or not
--     right_align = false,
--     -- padding from the right if right_align is true
--     right_align_padding = 7,
--     -- highlight group
--     highlight = "Comment",
--   },
--   debug_mode = false,
-- }
