From 282c398c0e66cc06088cb53e825ce07d1441584a Mon Sep 17 00:00:00 2001 From: asoderlind Date: Wed, 9 Aug 2023 17:01:48 +0900 Subject: [PATCH] updates --- init.lua | 23 ++++++++++----- lua/core/autocmds.lua | 1 - lua/plugins/configs/lspinstaller.lua | 2 ++ lua/plugins/configs/null.lua | 42 ++++++++++++++++++++++++---- lua/vscode/init.lua | 10 +++++++ lua/vscode/mappings.lua | 6 ++++ 6 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 lua/vscode/init.lua create mode 100644 lua/vscode/mappings.lua diff --git a/init.lua b/init.lua index 3881708..d178df6 100644 --- a/init.lua +++ b/init.lua @@ -1,11 +1,20 @@ local init_modules = { - "core", - "plugins" + "core", + "plugins", } -for _, module in ipairs(init_modules) do - local ok, err = pcall(require, module) - if not ok then - error("Error loading " .. module .. "\n\n" .. err) - end +if vim.g.vscode then + for _, module in ipairs({ "vscode" }) do + local ok, err = pcall(require, module) + if not ok then + error("Error loading " .. module .. "\n\n" .. err) + end + end +else + for _, module in ipairs(init_modules) do + local ok, err = pcall(require, module) + if not ok then + error("Error loading " .. module .. "\n\n" .. err) + end + end end diff --git a/lua/core/autocmds.lua b/lua/core/autocmds.lua index 1e581f4..8890add 100644 --- a/lua/core/autocmds.lua +++ b/lua/core/autocmds.lua @@ -1,3 +1,2 @@ vim.cmd([[colorscheme gruvbox]]) vim.cmd([[hi Normal guibg=NONE ctermbg=NONE]]) -vim.cmd([[autocmd BufWritePre lua vim.lsp.buf.formatting_sync()]]) diff --git a/lua/plugins/configs/lspinstaller.lua b/lua/plugins/configs/lspinstaller.lua index 6157301..073735a 100644 --- a/lua/plugins/configs/lspinstaller.lua +++ b/lua/plugins/configs/lspinstaller.lua @@ -1,5 +1,6 @@ local _, lsp_installer = pcall(require, "nvim-lsp-installer") + local function on_attach(client, bufnr) -- Avoid conflicts with null_ls client.server_capabilities.document_formatting = false @@ -38,6 +39,7 @@ local function on_attach(client, bufnr) buf_set_keymap("n", "q", "lua vim.lsp.diagnostic.set_loclist()", opts) buf_set_keymap("n", "f", "lua vim.lsp.buf.format()", opts) buf_set_keymap("v", "ca", "lua vim.lsp.buf.range_code_action()", opts) + end -- Register a handler that will be called for all installed servers. diff --git a/lua/plugins/configs/null.lua b/lua/plugins/configs/null.lua index 3576dc3..ac00419 100644 --- a/lua/plugins/configs/null.lua +++ b/lua/plugins/configs/null.lua @@ -1,11 +1,43 @@ local _, null_ls = pcall(require, "null-ls") +local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) + +-- Set LSP auto format on buffer save +local 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() + -- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead + vim.lsp.buf.formatting_sync() + end, + }) + end +end + local sources = { --null_ls.builtins.formatting.prettierd, - null_ls.builtins.formatting.stylua, - null_ls.builtins.formatting.eslint_d, - null_ls.builtins.formatting.autopep8, - null_ls.builtins.formatting.tidy, + null_ls.builtins.formatting.stylua.with({ + filetypes = { "lua" }, + }), + + null_ls.builtins.formatting.autopep8.with({ + filetypes = { "python" }, + }), + + null_ls.builtins.formatting.prettierd.with({ + filetypes = { "javascript", "typescript", "json", "markdown", "graphql" }, + }), + + null_ls.builtins.formatting.tidy.with({ + filetypes = { "html" }, + }), + + null_ls.builtins.formatting.djlint.with({ + filetypes = { "django", "htmldjango", "jinja.html" }, + }), } -null_ls.setup({ sources = sources }) +null_ls.setup({ sources = sources, on_attach = on_attach }) diff --git a/lua/vscode/init.lua b/lua/vscode/init.lua new file mode 100644 index 0000000..e8ec278 --- /dev/null +++ b/lua/vscode/init.lua @@ -0,0 +1,10 @@ +local core_modules = { + "vscode.mappings", +} + +for _, module in ipairs(core_modules) do + local ok, err = pcall(require, module) + if not ok then + error("Error loading " .. module .. "\n\n" .. err) + end +end diff --git a/lua/vscode/mappings.lua b/lua/vscode/mappings.lua new file mode 100644 index 0000000..55b0326 --- /dev/null +++ b/lua/vscode/mappings.lua @@ -0,0 +1,6 @@ +vim.g.mapleader = "," +local map = vim.api.nvim_set_keymap + +-- custom escape +map("i", "jk", "", { noremap = true, silent = true }) +map("i", "kj", "", { noremap = true, silent = true })