Skip to content

Commit

Permalink
feat: adds format on save
Browse files Browse the repository at this point in the history
  • Loading branch information
ixahmedxi committed Dec 5, 2022
1 parent 2100ba5 commit e9295be
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.diagnostics.globals": [
"vim",
"import",
"augroup"
]
}
8 changes: 8 additions & 0 deletions lua/.luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.diagnostics.globals": [
"vim",
"import",
"augroup"
]
}
18 changes: 17 additions & 1 deletion lua/config/lsp/null-ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,21 @@ import({ "mason-null-ls", "null-ls", "mason-null-ls.automatic_setup" }, function
})

-- will setup any installed and configured sources above
null_ls.setup()
null_ls.setup({
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()
if vim.bo.filetype == "typescriptreact" or vim.bo.filetype == "typescript" then
vim.cmd("TypescriptOrganizeImports!")
end
vim.lsp.buf.format({ bufnr = bufnr })
end,
})
end
end,
})
end)
17 changes: 11 additions & 6 deletions lua/config/lsp/on_attach.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local M = {}

M.on_attach = function(client, bufnr)
local opts = { noremap = true, silent = true }
local keymap = vim.api.nvim_buf_set_keymap

if client.name == "tsserver" then
client.server_capabilities.document_formatting = false
end
Expand All @@ -9,14 +12,16 @@ M.on_attach = function(client, bufnr)
client.server_capabilities.document_formatting = false
end

local opts = { noremap = true, silent = true }
local keymap = vim.api.nvim_buf_set_keymap
if client.name == "tsserver" then
keymap(bufnr, "n", "gd", "<cmd>TypescriptGoToSourceDefinition<CR>", opts)
else
keymap(bufnr, "n", "gd", "<cmd>Trouble lsp_definitions<CR>", opts)
end

keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
keymap(bufnr, "n", "gt", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
keymap(bufnr, "n", "gt", "<cmd>Trouble lsp_type_definitions<CR>", opts)
keymap(bufnr, "n", "gi", "<cmd>Trouble lsp_implementations<CR>", opts)
keymap(bufnr, "n", "gr", "<cmd>Trouble lsp_references<CR>", opts)
keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
keymap(bufnr, "n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
keymap(bufnr, "n", "<leader>rn", ":IncRename ", opts)
Expand Down

0 comments on commit e9295be

Please sign in to comment.