-- bash        npm install -g bash-language-server
-- dockerfile  npm install -g dockerfile-language-server-nodejs
-- python      npm install -g pyright
-- js, ts      npm install -g typescript typescript-language-server
-- vue         npm install -g typescript vls

local map = require "settings.utils".map

require "lsp.cmp"
require "lsp.ts"
require "lsp.html"
require "lsp.css"
require "lsp.emmet"
require "lsp.json"
require "lsp.lua"
require "lsp.elixir"
require "lsp.bash"
require "lsp.efm"
require "lsp.vue"
require "lsp.diagnostics"
-- require "lsp.saga"
-- require "lsp.angular"
-- require "lsp.js"
-- require "lsp.dap"
-- require "lsp.nlua"
local opts = {noremap = true, silent = true}
-- LSP
map("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
map("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
map("n", "gt", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
--  map("n", "<space>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
map("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
map("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
-- Handle formatting in a smarter way
-- If the buffer has been edited before formatting has completed, do not try to
-- apply the changes
vim.lsp.handlers["textDocument/formatting"] = function(err, _, result, _, bufnr)
  if err ~= nil or result == nil then
    return
  end
  -- If the buffer hasn't been modified before the formatting has finished,
  -- update the buffer
  if not vim.api.nvim_buf_get_option(bufnr, "modified") then
    local view = vim.fn.winsaveview()
    vim.lsp.util.apply_text_edits(result, bufnr)
    vim.fn.winrestview(view)
    if bufnr == vim.api.nvim_get_current_buf() then
      vim.api.nvim_command("noautocmd :update")
    -- Trigger post-formatting autocommand which can be used to refresh gitgutter
    -- vim.api.nvim_command("silent doautocmd <nomodeline> User FormatterPost")
    end
  end
end

-- additional capabilities for autocompletion with nvim-cmp
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities)
capabilities.textDocument.completion.completionItem.snippetSupport = true
capabilities.textDocument.completion.completionItem.preselectSupport = true
capabilities.textDocument.completion.completionItem.insertReplaceSupport = true
capabilities.textDocument.completion.completionItem.labelDetailsSupport = true
capabilities.textDocument.completion.completionItem.deprecatedSupport = true
capabilities.textDocument.completion.completionItem.commitCharactersSupport = true
capabilities.textDocument.completion.completionItem.tagSupport = {valueSet = {1}}
capabilities.textDocument.completion.completionItem.resolveSupport = {
  properties = {
    "documentation",
    "detail",
    "additionalTextEdits"
  }
}

-- function _G.formatDocument()
-- check if LSP is attached
-- if (#vim.lsp.buf_get_clients()) < 1 then
-- return
-- end

-- local ft = vim.bo.filetype
-- if ft == "javascript" or ft == "typescript" or ft == "javascriptreact" or ft == "typescriptreact" then
-- local params = {
-- command = "_typescript.organizeImports",
-- arguments = {vim.api.nvim_buf_get_name(0)},
-- title = ""
-- }
-- vim.lsp.buf_request_sync(vim.api.nvim_get_current_buf(), "workspace/executeCommand", params, 1000)
-- end
-- vim.lsp.buf.formatting_sync(nil, 1000)
-- end

-- vim.cmd("command! Format :lua formatDocument()")
