feat: formatting with conform

main
jaredgoldman 2024-04-08 10:46:05 -04:00
parent 9095ced549
commit 1a36e16bf6
8 changed files with 289 additions and 247 deletions

View File

@ -1,5 +1,4 @@
require("plugins") require("plugins")
require("options")
require("colorscheme") require("colorscheme")
require("lsp") require("lsp")
require("config.telescope") require("config.telescope")
@ -13,5 +12,7 @@ require("config.fterm")
require("config.harpoon") require("config.harpoon")
require("config.autopairs") require("config.autopairs")
require("config.autotags") require("config.autotags")
require("config.conform")
-- require("config.dashboard") -- require("config.dashboard")
require("options")
require("keymaps") require("keymaps")

View File

@ -6,6 +6,7 @@
"cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" }, "cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" },
"cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" }, "cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" },
"cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" },
"conform.nvim": { "branch": "master", "commit": "9d5ba06d6ee7418c674f498634617416d15b6239" },
"copilot.vim": { "branch": "release", "commit": "7097b09e52621a97d11f254e04de5e5a0f26e5f5" }, "copilot.vim": { "branch": "release", "commit": "7097b09e52621a97d11f254e04de5e5a0f26e5f5" },
"dashboard-nvim": { "branch": "master", "commit": "681300934baf36f6184ca41f0b26aed22056d4ee" }, "dashboard-nvim": { "branch": "master", "commit": "681300934baf36f6184ca41f0b26aed22056d4ee" },
"harpoon": { "branch": "harpoon2", "commit": "da326d0438ac68dee9b6b62a734be940a8bd8405" }, "harpoon": { "branch": "harpoon2", "commit": "da326d0438ac68dee9b6b62a734be940a8bd8405" },

View File

@ -0,0 +1,29 @@
require("conform").setup({
formatters_by_ft = {
lua = { "stylua" },
python = { "isort", "black" },
javascript = { { "prettierd", "prettier" } },
json = { { "prettierd", "prettier" } },
},
})
local M = {}
M.format = function(args)
local range = nil
if args and args.count ~= -1 then
-- Assuming args.line1 and args.line2 are provided and valid
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
range = {
start = { args.line1, 0 },
["end"] = { args.line2, end_line:len() },
}
end
require("conform").format({ async = true, lsp_fallback = true, range = range })
end
vim.api.nvim_create_user_command("Format", function(args)
M.format(args)
end, { range = true })
return M

View File

@ -20,6 +20,5 @@ require('mason-lspconfig').setup({
'tsserver', 'tsserver',
'jsonls', 'jsonls',
'lua_ls', 'lua_ls',
'prettier'
}, },
}) })

View File

@ -1,11 +1,6 @@
local fterm = require('FTerm')
local keymap = vim.api.nvim_set_keymap local keymap = vim.api.nvim_set_keymap
local opts = { noremap = true, silent = true } local opts = { noremap = true, silent = true }
local api = vim.api
-- Custom commands
api.nvim_create_user_command('FTermToggle', fterm.toggle, { bang = true })
api.nvim_create_user_command('FTermClose', fterm.close, { bang = true })
local mappings = { local mappings = {
n = { n = {
-- highlighting -- highlighting
@ -36,7 +31,9 @@ local mappings = {
["<leader>h"] = "<cmd>FTermToggle<CR>", ["<leader>h"] = "<cmd>FTermToggle<CR>",
-- Harpoon -- Harpoon
["<leader>a"] = "<cmd>HarpoonAdd<CR>", ["<leader>a"] = "<cmd>HarpoonAdd<CR>",
["<C-e>"] = "<cmd>HarpoonUI<CR>" ["<C-e>"] = "<cmd>HarpoonUI<CR>",
-- Formating
["<leader>fm"] = "<cmd>Format<CR>",
}, },
x = { x = {
["<A-j>"] = ":move '>+1<CR>gv-gv", ["<A-j>"] = ":move '>+1<CR>gv-gv",
@ -47,8 +44,8 @@ local mappings = {
["<A-l>"] = "<esc>", ["<A-l>"] = "<esc>",
}, },
t = { t = {
["A-l"] = "<cmd>FTermClose<CR>" ["A-l"] = "<cmd>FTermClose<CR>",
} },
} }
for mode, mode_mappings in pairs(mappings) do for mode, mode_mappings in pairs(mappings) do

View File

@ -1,34 +1,34 @@
local lspconfig = require('lspconfig') local lspconfig = require("lspconfig")
local mappings = { local mappings = {
['<leader>k'] = vim.diagnostic.open_float, ["<leader>k"] = vim.diagnostic.open_float,
['[d'] = vim.diagnostic.goto_prev, ["[d"] = vim.diagnostic.goto_prev,
[']d'] = vim.diagnostic.goto_next, ["]d"] = vim.diagnostic.goto_next,
['<leader>q'] = vim.diagnostic.setloclist, ["<leader>q"] = vim.diagnostic.setloclist,
['gD'] = vim.lsp.buf.declaration, ["gD"] = vim.lsp.buf.declaration,
['gd'] = vim.lsp.buf.definition, ["gd"] = vim.lsp.buf.definition,
['K'] = vim.lsp.buf.hover, ["K"] = vim.lsp.buf.hover,
['gi'] = vim.lsp.buf.implementation, ["gi"] = vim.lsp.buf.implementation,
['<leader>wa'] = vim.lsp.buf.add_workspace_folder, ["<leader>wa"] = vim.lsp.buf.add_workspace_folder,
['<leader>wr'] = vim.lsp.buf.remove_workspace_folder, ["<leader>wr"] = vim.lsp.buf.remove_workspace_folder,
['<leader>wl'] = function() ["<leader>wl"] = function()
print(vim.inspect(vim.lsp.buf.list_workleader_folders())) print(vim.inspect(vim.lsp.buf.list_workleader_folders()))
end, end,
['<leader>D'] = vim.lsp.buf.type_definition, ["<leader>D"] = vim.lsp.buf.type_definition,
['<leader>rn'] = vim.lsp.buf.rename, ["<leader>rn"] = vim.lsp.buf.rename,
['<leader>ca'] = vim.lsp.buf.code_action, ["<leader>ca"] = vim.lsp.buf.code_action,
['gr'] = vim.lsp.buf.references, ["gr"] = vim.lsp.buf.references,
['<leader>fm'] = function() ["<leader>fm"] = function()
vim.lsp.buf.format({ async = true }) vim.lsp.buf.format({ async = true })
end, end,
} }
local on_attach = function(client, bufnr) local on_attach = function(client, bufnr)
-- Enable completion triggered by <c-x><c-o> -- Enable completion triggered by <c-x><c-o>
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
local bufopts = { noremap = true, silent = true, buffer = bufnr } local bufopts = { noremap = true, silent = true, buffer = bufnr }
for key, mapping in pairs(mappings) do for key, mapping in pairs(mappings) do
vim.keymap.set('n', key, mapping, bufopts) vim.keymap.set("n", key, mapping, bufopts)
end end
end end
@ -38,6 +38,7 @@ local servers = {
"tailwindcss", "tailwindcss",
"lua_ls", "lua_ls",
} }
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem = { capabilities.textDocument.completion.completionItem = {
@ -59,8 +60,8 @@ capabilities.textDocument.completion.completionItem = {
} }
for _, lsp in ipairs(servers) do for _, lsp in ipairs(servers) do
lspconfig[lsp].setup { lspconfig[lsp].setup({
on_attach = on_attach, on_attach = on_attach,
capabilities = capabilities, capabilities = capabilities,
} })
end end

View File

@ -1,6 +1,7 @@
local opt = vim.opt local opt = vim.opt
local api = vim.api local api = vim.api
local global = vim.g local global = vim.g
local formatter = require("config.conform")
-- Hint: use `:h <option>` to figure out the meaning if needed -- Hint: use `:h <option>` to figure out the meaning if needed
opt.clipboard = "unnamedplus" -- use system clipboard opt.clipboard = "unnamedplus" -- use system clipboard
@ -18,9 +19,9 @@ opt.cursorline = true
opt.backspace = "indent,eol,start" opt.backspace = "indent,eol,start"
-- Tab -- Tab
opt.tabstop = 4 -- number of visual spaces per TAB opt.tabstop = 2 -- number of visual spaces per TAB
opt.softtabstop = 4 -- number of spacesin tab when editing opt.softtabstop = 2 -- number of spacesin tab when editing
opt.shiftwidth = 4 -- insert 4 spaces on a tab opt.shiftwidth = 2 -- insert 4 spaces on a tab
opt.expandtab = true -- tabs are spaces, mainly because of python opt.expandtab = true -- tabs are spaces, mainly because of python
-- UI config -- UI config
@ -49,3 +50,10 @@ api.nvim_create_autocmd({ "BufWritePre" }, {
command = [[if &filetype !~# 'lsp' | %s/\s\+$//e | endif]], command = [[if &filetype !~# 'lsp' | %s/\s\+$//e | endif]],
}) })
-- Formatting
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*",
callback = function()
formatter.format(nil)
end,
})

View File

@ -53,7 +53,9 @@ require("lazy").setup({
footer = function() footer = function()
local stats = require("lazy").stats() local stats = require("lazy").stats()
local ms = (math.floor(stats.startuptime * 100 + 0.5) / 100) local ms = (math.floor(stats.startuptime * 100 + 0.5) / 100)
return { "⚡ Neovim loaded " .. stats.loaded .. "/" .. stats.count .. " plugins in " .. ms .. "ms" } return {
"⚡ Neovim loaded " .. stats.loaded .. "/" .. stats.count .. " plugins in " .. ms .. "ms",
}
end, end,
}, },
} }
@ -110,31 +112,35 @@ require("lazy").setup({
"nvim-tree/nvim-web-devicons", "nvim-tree/nvim-web-devicons",
}, },
{ {
'nvim-telescope/telescope.nvim', "nvim-telescope/telescope.nvim",
tag = '0.1.6', tag = "0.1.6",
dependencies = { 'nvim-lua/plenary.nvim' } dependencies = { "nvim-lua/plenary.nvim" },
}, },
'zaldih/themery.nvim', "zaldih/themery.nvim",
'terrortylor/nvim-comment', "terrortylor/nvim-comment",
'szw/vim-maximizer', "szw/vim-maximizer",
{ {
'nvim-lualine/lualine.nvim', "nvim-lualine/lualine.nvim",
dependencies = { 'nvim-tree/nvim-web-devicons' } dependencies = { "nvim-tree/nvim-web-devicons" },
}, },
'numToStr/FTerm.nvim', "numToStr/FTerm.nvim",
'github/copilot.vim', "github/copilot.vim",
"nvim-lua/plenary.nvim", "nvim-lua/plenary.nvim",
{ {
"ThePrimeagen/harpoon", "ThePrimeagen/harpoon",
branch = "harpoon2", branch = "harpoon2",
requires = { { "nvim-lua/plenary.nvim" } } requires = { { "nvim-lua/plenary.nvim" } },
}, },
{ {
'windwp/nvim-autopairs', "windwp/nvim-autopairs",
event = "InsertEnter", event = "InsertEnter",
config = true config = true,
},
"windwp/nvim-ts-autotag",
"nvim-treesitter/nvim-treesitter",
"mfussenegger/nvim-dap",
{
"stevearc/conform.nvim",
opts = {},
}, },
'windwp/nvim-ts-autotag',
'nvim-treesitter/nvim-treesitter',
'mfussenegger/nvim-dap'
}) })