remove prettier format

This commit is contained in:
Ivan Dimitrov 2023-09-16 02:43:00 +03:00
parent 003177e093
commit ea45a78b26

View File

@ -13,13 +13,13 @@ vim.g.maplocalleader = " "
vim.keymap.set({ "n", "v" }, "<Space>", "<Nop>", { silent = true }) -- nop leader vim.keymap.set({ "n", "v" }, "<Space>", "<Nop>", { silent = true }) -- nop leader
local nmap = function(keys, cmd) local nmap = function(keys, cmd)
vim.keymap.set("n", keys, cmd, { noremap = true, silent = true }) vim.keymap.set("n", keys, cmd, { noremap = true, silent = true })
end end
local vmap = function(keys, cmd) local vmap = function(keys, cmd)
vim.keymap.set("v", keys, cmd, { noremap = true, silent = true }) vim.keymap.set("v", keys, cmd, { noremap = true, silent = true })
end end
local tmap = function(keys, cmd) local tmap = function(keys, cmd)
vim.keymap.set("t", keys, cmd, { noremap = true, silent = true }) vim.keymap.set("t", keys, cmd, { noremap = true, silent = true })
end end
nmap("<leader>/", require("Comment.api").toggle.linewise.current) nmap("<leader>/", require("Comment.api").toggle.linewise.current)
@ -44,115 +44,75 @@ nmap("<leader>e", vim.diagnostic.open_float)
-- START LSP -- START LSP
local highlight_filetypes = { "*.ts", "*.tsx", "*.js", "*.jsx", "*.html", "*.lua", "*.hs" } local highlight_filetypes = { "*.ts", "*.tsx", "*.js", "*.jsx", "*.html", "*.lua", "*.hs" }
local prettier_filetypes = { "%.ts$", "%.tsx$", "%.js$", "%.jsx$", "%.html$", "%.json$", "%.css$" }
local function matches_filetypes(file_path, filetypes)
for _, pattern in ipairs(filetypes) do
if file_path:match(pattern) then
return true
end
end
return false
end
local function str_to_table(str)
local t = {}
for line in string.gmatch(str, "([^\n]+)") do
table.insert(t, line)
end
return t
end
local function prettier_format(file_path, content)
local prettier_command = string.format("prettier --stdin-filepath '%s' <<< '%s'", file_path, content)
local output = io.popen(prettier_command, "r")
local result = output:read("*a")
output:close()
return result
end
local cmp = require("cmp") local cmp = require("cmp")
local lspconfig = require("lspconfig") local lspconfig = require("lspconfig")
local servers = { local servers = {
tsserver = { tsserver = {
settings = { settings = {
completions = { completions = {
completeFunctionCalls = true, completeFunctionCalls = true,
}, },
}, },
}, },
pylsp = {}, pylsp = {},
lua_ls = {}, lua_ls = {},
rnix = {}, rnix = {},
gopls = {}, gopls = {},
tailwindcss = {}, tailwindcss = {},
prismals = {}, prismals = {},
hls = {}, hls = {},
bashls = {}, bashls = {},
} }
local cmp_capabilities = require("cmp_nvim_lsp").default_capabilities() local cmp_capabilities = require("cmp_nvim_lsp").default_capabilities()
local on_attach = function(client, bufnr) local on_attach = function(client, bufnr)
local format_buffer = function() nmap("<leader>ca", vim.lsp.buf.code_action)
local file_path = vim.api.nvim_buf_get_name(bufnr) nmap("<leader>lr", vim.lsp.buf.rename)
if (matches_filetypes(file_path, prettier_filetypes)) then nmap("gd", vim.lsp.buf.definition)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) nmap("<leader>lf", function()
local content = table.concat(lines, "\n") vim.lsp.buf.format()
local result = prettier_format(file_path, content) end)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, str_to_table(result)) nmap("K", vim.lsp.buf.hover)
else nmap("gr", require("telescope.builtin").lsp_references)
vim.lsp.buf.format() vim.api.nvim_create_autocmd("CursorHold", {
end callback = function()
end vim.lsp.buf.document_highlight()
end,
nmap("<leader>ca", vim.lsp.buf.code_action) pattern = highlight_filetypes
nmap("<leader>lr", vim.lsp.buf.rename) })
nmap("gd", vim.lsp.buf.definition) vim.api.nvim_create_autocmd("CursorMoved", {
nmap("<leader>lf", function() callback = function()
format_buffer() vim.lsp.buf.clear_references()
end) end,
nmap("K", vim.lsp.buf.hover) pattern = highlight_filetypes
nmap("gr", require("telescope.builtin").lsp_references) })
vim.api.nvim_create_autocmd("CursorHold", {
callback = function()
vim.lsp.buf.document_highlight()
end,
pattern = highlight_filetypes
})
vim.api.nvim_create_autocmd("CursorMoved", {
callback = function()
vim.lsp.buf.clear_references()
end,
pattern = highlight_filetypes
})
end end
cmp.setup({ cmp.setup({
snippet = { snippet = {
expand = function(args) expand = function(args)
require("luasnip").lsp_expand(args.body) require("luasnip").lsp_expand(args.body)
end, end,
}, },
mapping = cmp.mapping.preset.insert({ mapping = cmp.mapping.preset.insert({
["<C-Space>"] = cmp.mapping.complete(), ["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({ select = true }), ["<CR>"] = cmp.mapping.confirm({ select = true }),
}), }),
sources = cmp.config.sources({ { name = "nvim_lsp" }, { name = "luasnip" } }, { { name = "buffer" } }), sources = cmp.config.sources({ { name = "nvim_lsp" }, { name = "luasnip" } }, { { name = "buffer" } }),
}) })
for server, cfg in pairs(servers) do for server, cfg in pairs(servers) do
lspconfig[server].setup({ lspconfig[server].setup({
settings = cfg.settings, settings = cfg.settings,
capabilities = cmp_capabilities, capabilities = cmp_capabilities,
on_attach = on_attach, on_attach = on_attach,
}) })
end end
-- END LSP -- END LSP
require 'nvim-treesitter.configs'.setup { require 'nvim-treesitter.configs'.setup {
autotag = { autotag = {
enable = true, enable = true,
} }
} }
require("Comment").setup() require("Comment").setup()