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("options")
require("colorscheme")
require("lsp")
require("config.telescope")
@ -13,5 +12,7 @@ require("config.fterm")
require("config.harpoon")
require("config.autopairs")
require("config.autotags")
require("config.conform")
-- require("config.dashboard")
require("options")
require("keymaps")

View File

@ -6,6 +6,7 @@
"cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" },
"cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" },
"cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" },
"conform.nvim": { "branch": "master", "commit": "9d5ba06d6ee7418c674f498634617416d15b6239" },
"copilot.vim": { "branch": "release", "commit": "7097b09e52621a97d11f254e04de5e5a0f26e5f5" },
"dashboard-nvim": { "branch": "master", "commit": "681300934baf36f6184ca41f0b26aed22056d4ee" },
"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',
'jsonls',
'lua_ls',
'prettier'
},
})

View File

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

View File

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

View File

@ -1,6 +1,7 @@
local opt = vim.opt
local api = vim.api
local global = vim.g
local formatter = require("config.conform")
-- Hint: use `:h <option>` to figure out the meaning if needed
opt.clipboard = "unnamedplus" -- use system clipboard
@ -18,9 +19,9 @@ opt.cursorline = true
opt.backspace = "indent,eol,start"
-- Tab
opt.tabstop = 4 -- number of visual spaces per TAB
opt.softtabstop = 4 -- number of spacesin tab when editing
opt.shiftwidth = 4 -- insert 4 spaces on a tab
opt.tabstop = 2 -- number of visual spaces per TAB
opt.softtabstop = 2 -- number of spacesin tab when editing
opt.shiftwidth = 2 -- insert 4 spaces on a tab
opt.expandtab = true -- tabs are spaces, mainly because of python
-- UI config
@ -49,3 +50,10 @@ api.nvim_create_autocmd({ "BufWritePre" }, {
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()
local stats = require("lazy").stats()
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,
},
}
@ -110,31 +112,35 @@ require("lazy").setup({
"nvim-tree/nvim-web-devicons",
},
{
'nvim-telescope/telescope.nvim',
tag = '0.1.6',
dependencies = { 'nvim-lua/plenary.nvim' }
"nvim-telescope/telescope.nvim",
tag = "0.1.6",
dependencies = { "nvim-lua/plenary.nvim" },
},
'zaldih/themery.nvim',
'terrortylor/nvim-comment',
'szw/vim-maximizer',
"zaldih/themery.nvim",
"terrortylor/nvim-comment",
"szw/vim-maximizer",
{
'nvim-lualine/lualine.nvim',
dependencies = { 'nvim-tree/nvim-web-devicons' }
"nvim-lualine/lualine.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
},
'numToStr/FTerm.nvim',
'github/copilot.vim',
"numToStr/FTerm.nvim",
"github/copilot.vim",
"nvim-lua/plenary.nvim",
{
"ThePrimeagen/harpoon",
branch = "harpoon2",
requires = { { "nvim-lua/plenary.nvim" } }
requires = { { "nvim-lua/plenary.nvim" } },
},
{
'windwp/nvim-autopairs',
"windwp/nvim-autopairs",
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'
})