local lsp = require("lspconfig")
local fmt = require("null-ls")
local cmp = require("cmp")

require("nvim-autopairs").setup({})
require("lsp_signature").setup({ hint_enable = false })

local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
fmt.setup({
	sources = {
		fmt.builtins.formatting.clang_format,
		require("none-ls.formatting.rustfmt"),
		fmt.builtins.formatting.stylua,
		fmt.builtins.formatting.gofmt,
	},
	on_attach = function(client, bufnr)
		if client.supports_method("textDocument/formatting") then
			vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
			vim.api.nvim_create_autocmd("BufWritePre", {
				group = augroup,
				buffer = bufnr,
				callback = function()
					vim.lsp.buf.format({ async = false })
				end,
			})
		end
	end,
})

cmp.setup({
	snippet = {
		expand = function(args)
			vim.fn["vsnip#anonymous"](args.body)
		end,
	},
	mapping = cmp.mapping.preset.insert({
		["<C-b>"] = cmp.mapping.scroll_docs(-4),
		["<C-f>"] = cmp.mapping.scroll_docs(4),
		["<C-Space>"] = cmp.mapping.complete(),
		["<C-e>"] = cmp.mapping.abort(),
		["<CR>"] = cmp.mapping.confirm({ select = true }),
	}),
	window = { completion = cmp.config.window.bordered(), documentation = cmp.config.window.bordered() },
	sources = cmp.config.sources({
		{ name = "nvim_lsp" },
		{ name = "vsnip" },
		{ name = "spell" },
	}, {
		{ name = "buffer" },
		{ name = "nvim_lua" },
	}),
})

local capabilities = require("cmp_nvim_lsp").default_capabilities()

lsp.html.setup({
	cmd = { "vscode-html-language-server", "--stdio" },
	capabilities = capabilities,
	init_options = {
		configurationSection = { "html", "css", "javascript" },
		embeddedLanguages = { css = true, javascript = true },
		provideFormatter = true,
	},
	settings = {
		html = {
			format = {
				wrapLineLength = 120,
				wrapAttributes = "auto",
				contentUnformatted = "pre,code,textarea",
			},
			hover = { documentation = true, references = true },
		},
	},
})

lsp.cssls.setup({
	capabilities = capabilities,
	settings = { css = { validate = true }, scss = { validate = true }, less = { validate = true } },
})

lsp.ts_ls.setup({
	capabilities = capabilities,
	on_attach = function(client)
		client.server_capabilities.documentFormattingProvider = false
	end,
})

lsp.rust_analyzer.setup({ capabilities = capabilities })

local clangd_capabilities = vim.deepcopy(capabilities)
clangd_capabilities["offsetEncoding"] = "utf-8"
lsp.clangd.setup({
	cmd = { "clangd", "--background-index", "--function-arg-placeholders=0", "-j=12", "--clang-tidy" },
	capabilities = clangd_capabilities,
	init_options = { documentFormatting = true },
})

lsp.pyright.setup({ capabilities = capabilities })
lsp.gopls.setup({ capabilities = capabilities })
lsp.asm_lsp.setup({ capabilities = capabilities })
lsp.zls.setup({ capabilities = capabilities })
lsp.jdtls.setup({ capabilities = capabilities })
lsp.tinymist.setup({ capabilities = capabilities })

lsp.lua_ls.setup({
	on_init = function(client)
		local path = client.workspace_folders[1].name
		if not vim.loop.fs_stat(path .. "/.luarc.json") and not vim.loop.fs_stat(path .. "/.luarc.jsonc") then
			client.config.settings = vim.tbl_deep_extend("force", client.config.settings, {
				Lua = {
					runtime = { version = "LuaJIT" },
					workspace = { checkThirdParty = false, library = { vim.env.VIMRUNTIME } },
				},
			})
			client.notify("workspace/didChangeConfiguration", { settings = client.config.settings })
		end
		return true
	end,
	capabilities = capabilities,
})
