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" },
@ -28,4 +29,4 @@
"telescope.nvim": { "branch": "master", "commit": "6312868392331c9c0f22725041f1ec2bef57c751" },
"themery.nvim": { "branch": "main", "commit": "1005a58801276d29c4b1e11244cf7631250f9143" },
"vim-maximizer": { "branch": "master", "commit": "2e54952fe91e140a2e69f35f22131219fcd9c5f1" }
}
}

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,71 +1,68 @@
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
["<leader>nh"] = ":nohl<CR>",
-- move between buffers
["<c-h>"] = "<C-w>h",
["<c-j>"] = "<C-w>j",
["<c-k>"] = "<C-w>k",
["<c-l>"] = "<C-w>l",
-- open and close buggers
["<leader>sv"] = "<C-w>s",
["<leader>sh"] = "<C-w>v",
["<leader>se"] = "<C-w>=",
["<leader>sx"] = ":close<CR>",
-- nvim-tree
["<leader>e"] = ":NvimTreeToggle<CR>",
["<leader>cf"] = ":NvimTreeCollapse<CR>",
-- LSP
["<leader>ls"] = ":lspstop<CR>",
["<leader>lo"] = ":lspstart<CR>",
-- Telescope
["<leader>ff"] = "<cmd>Telescope find_files<CR>",
["<leader>fw"] = "<cmd>Telescope live_grep<CR>",
["<leader>fc"] = "<cmd>Telescope grep_string<CR>",
["<leader>th"] = "<cmd>Themery<CR>",
["<leader>sm"] = ":MaximizerToggle<CR>",
-- FTerm
["<leader>h"] = "<cmd>FTermToggle<CR>",
-- Harpoon
["<leader>a"] = "<cmd>HarpoonAdd<CR>",
["<C-e>"] = "<cmd>HarpoonUI<CR>"
},
x = {
["<A-j>"] = ":move '>+1<CR>gv-gv",
["<A-k>"] = ":move '<-2<CR>gv-gv",
},
i = {
["<C-Space>"] = { 'copilot#Accept("<CR>")', expr = true },
["<A-l>"] = "<esc>",
},
t = {
["A-l"] = "<cmd>FTermClose<CR>"
}
n = {
-- highlighting
["<leader>nh"] = ":nohl<CR>",
-- move between buffers
["<c-h>"] = "<C-w>h",
["<c-j>"] = "<C-w>j",
["<c-k>"] = "<C-w>k",
["<c-l>"] = "<C-w>l",
-- open and close buggers
["<leader>sv"] = "<C-w>s",
["<leader>sh"] = "<C-w>v",
["<leader>se"] = "<C-w>=",
["<leader>sx"] = ":close<CR>",
-- nvim-tree
["<leader>e"] = ":NvimTreeToggle<CR>",
["<leader>cf"] = ":NvimTreeCollapse<CR>",
-- LSP
["<leader>ls"] = ":lspstop<CR>",
["<leader>lo"] = ":lspstart<CR>",
-- Telescope
["<leader>ff"] = "<cmd>Telescope find_files<CR>",
["<leader>fw"] = "<cmd>Telescope live_grep<CR>",
["<leader>fc"] = "<cmd>Telescope grep_string<CR>",
["<leader>th"] = "<cmd>Themery<CR>",
["<leader>sm"] = ":MaximizerToggle<CR>",
-- FTerm
["<leader>h"] = "<cmd>FTermToggle<CR>",
-- Harpoon
["<leader>a"] = "<cmd>HarpoonAdd<CR>",
["<C-e>"] = "<cmd>HarpoonUI<CR>",
-- Formating
["<leader>fm"] = "<cmd>Format<CR>",
},
x = {
["<A-j>"] = ":move '>+1<CR>gv-gv",
["<A-k>"] = ":move '<-2<CR>gv-gv",
},
i = {
["<C-Space>"] = { 'copilot#Accept("<CR>")', expr = true },
["<A-l>"] = "<esc>",
},
t = {
["A-l"] = "<cmd>FTermClose<CR>",
},
}
for mode, mode_mappings in pairs(mappings) do
for key, mapping in pairs(mode_mappings) do
if type(mapping) == "function" then
-- For direct Lua function calls; ensure your function returns a string command
keymap(mode, key, "<cmd>lua " .. mapping() .. "<CR>", opts)
elseif type(mapping) == "table" and mapping.expr then
-- For expression mappings, like for copilot
local expr_opts = vim.tbl_extend("force", opts, { expr = true })
keymap(mode, key, mapping[1], expr_opts)
elseif type(mapping) == "table" then
-- For mappings that have their options specified directly
keymap(mode, key, mapping[1], mapping[2] or opts)
else
-- For simple string command mappings
keymap(mode, key, mapping, opts)
end
for key, mapping in pairs(mode_mappings) do
if type(mapping) == "function" then
-- For direct Lua function calls; ensure your function returns a string command
keymap(mode, key, "<cmd>lua " .. mapping() .. "<CR>", opts)
elseif type(mapping) == "table" and mapping.expr then
-- For expression mappings, like for copilot
local expr_opts = vim.tbl_extend("force", opts, { expr = true })
keymap(mode, key, mapping[1], expr_opts)
elseif type(mapping) == "table" then
-- For mappings that have their options specified directly
keymap(mode, key, mapping[1], mapping[2] or opts)
else
-- For simple string command mappings
keymap(mode, key, mapping, opts)
end
end
end

View File

@ -1,66 +1,67 @@
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()
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()
vim.lsp.buf.format({ async = true })
end,
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()
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()
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')
local bufopts = { noremap = true, silent = true, buffer = bufnr }
for key, mapping in pairs(mappings) do
vim.keymap.set('n', key, mapping, bufopts)
end
-- Enable completion triggered by <c-x><c-o>
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)
end
end
local servers = {
"tsserver",
"intelephense",
"tailwindcss",
"lua_ls",
"tsserver",
"intelephense",
"tailwindcss",
"lua_ls",
}
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem = {
documentationFormat = { "markdown", "plaintext" },
snippetSupport = true,
preselectSupport = true,
insertReplaceSupport = true,
labelDetailsSupport = true,
deprecatedSupport = true,
commitCharactersSupport = true,
tagSupport = { valueSet = { 1 } },
resolveSupport = {
properties = {
"documentation",
"detail",
"additionalTextEdits",
},
},
documentationFormat = { "markdown", "plaintext" },
snippetSupport = true,
preselectSupport = true,
insertReplaceSupport = true,
labelDetailsSupport = true,
deprecatedSupport = true,
commitCharactersSupport = true,
tagSupport = { valueSet = { 1 } },
resolveSupport = {
properties = {
"documentation",
"detail",
"additionalTextEdits",
},
},
}
for _, lsp in ipairs(servers) do
lspconfig[lsp].setup {
on_attach = on_attach,
capabilities = capabilities,
}
lspconfig[lsp].setup({
on_attach = on_attach,
capabilities = capabilities,
})
end

View File

@ -1,11 +1,12 @@
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
opt.completeopt = { "menu", "menuone", "noselect" }
opt.mouse = "a" -- allow the mouse to be used in Nvim
opt.mouse = "a" -- allow the mouse to be used in Nvim
-- General
opt.wrap = false
@ -18,24 +19,24 @@ 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
opt.number = true -- show absolute number
opt.number = true -- show absolute number
-- opt.relativenumber = true -- add numbers to each line on the left side
opt.cursorline = true -- highlight cursor line underneath the cursor horizontally
opt.splitbelow = true -- open new vertical split bottom
opt.splitright = true -- open new horizontal splits right
opt.cursorline = true -- highlight cursor line underneath the cursor horizontally
opt.splitbelow = true -- open new vertical split bottom
opt.splitright = true -- open new horizontal splits right
opt.termguicolors = true -- enabl 24-bit RGB color in the TUI
-- Searching
opt.incsearch = true -- search as characters are entered
opt.hlsearch = false -- do not highlight matches
opt.incsearch = true -- search as characters are entered
opt.hlsearch = false -- do not highlight matches
opt.ignorecase = true -- ignore case in searches by default
opt.smartcase = true -- but make it case sensitive if an uppercase is entered
opt.smartcase = true -- but make it case sensitive if an uppercase is entered
-- History
opt.undofile = true
@ -45,7 +46,14 @@ opt.undodir = os.getenv("HOME") .. "/.local/share/nvim/undo"
api.nvim_set_option("list", true)
api.nvim_set_option("listchars", "eol:$,nbsp:_,tab:>-,trail:~,extends:>,precedes:<")
api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = { "*" },
command = [[if &filetype !~# 'lsp' | %s/\s\+$//e | endif]],
pattern = { "*" },
command = [[if &filetype !~# 'lsp' | %s/\s\+$//e | endif]],
})
-- Formatting
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*",
callback = function()
formatter.format(nil)
end,
})

View File

@ -1,24 +1,24 @@
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
})
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({
{
"nvimdev/dashboard-nvim",
event = "VimEnter",
opts = function()
local logo = [[
{
"nvimdev/dashboard-nvim",
event = "VimEnter",
opts = function()
local logo = [[
Z
Z
z
@ -27,114 +27,120 @@ require("lazy").setup({
]]
logo = string.rep("\n", 8) .. logo .. "\n\n"
logo = string.rep("\n", 8) .. logo .. "\n\n"
local opts = {
theme = "doom",
hide = {
-- this is taken care of by lualine
-- enabling this messes up the actual laststatus setting after loading a file
statusline = false,
},
config = {
header = vim.split(logo, "\n"),
-- stylua: ignore
center = {
-- { action = LazyVim.telescope("files"), desc = " Find File", icon = " ", key = "f" },
{ action = "ene | startinsert", desc = " New File", icon = "", key = "n" },
{ action = "Telescope oldfiles", desc = " Recent Files", icon = "", key = "r" },
{ action = "Telescope live_grep", desc = " Find Text", icon = "", key = "g" },
{ action = [[lua LazyVim.telescope.config_files()()]], desc = " Config", icon = "", key = "c" },
{ action = 'lua require("persistence").load()', desc = " Restore Session", icon = "", key = "s" },
{ action = "LazyExtras", desc = " Lazy Extras", icon = "", key = "x" },
{ action = "Lazy", desc = " Lazy", icon = "󰒲 ", key = "l" },
{ action = "qa", desc = " Quit", icon = "", key = "q" },
},
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" }
end,
},
local opts = {
theme = "doom",
hide = {
-- this is taken care of by lualine
-- enabling this messes up the actual laststatus setting after loading a file
statusline = false,
},
config = {
header = vim.split(logo, "\n"),
-- stylua: ignore
center = {
-- { action = LazyVim.telescope("files"), desc = " Find File", icon = " ", key = "f" },
{ action = "ene | startinsert", desc = " New File", icon = "", key = "n" },
{ action = "Telescope oldfiles", desc = " Recent Files", icon = "", key = "r" },
{ action = "Telescope live_grep", desc = " Find Text", icon = "", key = "g" },
{ action = [[lua LazyVim.telescope.config_files()()]], desc = " Config", icon = "", key = "c" },
{ action = 'lua require("persistence").load()', desc = " Restore Session", icon = "", key = "s" },
{ action = "LazyExtras", desc = " Lazy Extras", icon = "", key = "x" },
{ action = "Lazy", desc = " Lazy", icon = "󰒲 ", key = "l" },
{ action = "qa", desc = " Quit", icon = "", key = "q" },
},
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",
}
end,
},
}
for _, button in ipairs(opts.config.center) do
button.desc = button.desc .. string.rep(" ", 43 - #button.desc)
button.key_format = " %s"
end
for _, button in ipairs(opts.config.center) do
button.desc = button.desc .. string.rep(" ", 43 - #button.desc)
button.key_format = " %s"
end
-- close Lazy and re-open when the dashboard is ready
if vim.o.filetype == "lazy" then
vim.cmd.close()
vim.api.nvim_create_autocmd("User", {
pattern = "DashboardLoaded",
callback = function()
require("lazy").show()
end,
})
end
-- close Lazy and re-open when the dashboard is ready
if vim.o.filetype == "lazy" then
vim.cmd.close()
vim.api.nvim_create_autocmd("User", {
pattern = "DashboardLoaded",
callback = function()
require("lazy").show()
end,
})
end
return opts
end,
},
"tanvirtin/monokai.nvim",
{
"onsails/lspkind.nvim",
event = { "VimEnter" },
},
{ "catppuccin/nvim", name = "catppuccin", priority = 1000 },
-- Auto-completion engine
{
"hrsh7th/nvim-cmp",
dependencies = { "lspkind.nvim" },
config = function()
require("config.nvim-cmp")
end,
},
{ "hrsh7th/cmp-nvim-lsp", dependencies = { "nvim-cmp" } },
{ "hrsh7th/cmp-buffer", dependencies = { "nvim-cmp" } }, -- buffer auto-completion
{ "hrsh7th/cmp-path", dependencies = { "nvim-cmp" } }, -- path auto-completion
{ "hrsh7th/cmp-cmdline", dependencies = { "nvim-cmp" } }, -- cmdline auto-completion
-- Code snippet engine
{
"L3MON4D3/LuaSnip",
version = "v2.*",
},
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
"neovim/nvim-lspconfig",
"nvim-tree/nvim-tree.lua",
version = "*",
lazy = false,
dependencies = {
"nvim-tree/nvim-web-devicons",
},
{
'nvim-telescope/telescope.nvim',
tag = '0.1.6',
dependencies = { 'nvim-lua/plenary.nvim' }
},
'zaldih/themery.nvim',
'terrortylor/nvim-comment',
'szw/vim-maximizer',
{
'nvim-lualine/lualine.nvim',
dependencies = { 'nvim-tree/nvim-web-devicons' }
},
'numToStr/FTerm.nvim',
'github/copilot.vim',
"nvim-lua/plenary.nvim",
{
"ThePrimeagen/harpoon",
branch = "harpoon2",
requires = { { "nvim-lua/plenary.nvim" } }
},
{
'windwp/nvim-autopairs',
event = "InsertEnter",
config = true
},
'windwp/nvim-ts-autotag',
'nvim-treesitter/nvim-treesitter',
'mfussenegger/nvim-dap'
return opts
end,
},
"tanvirtin/monokai.nvim",
{
"onsails/lspkind.nvim",
event = { "VimEnter" },
},
{ "catppuccin/nvim", name = "catppuccin", priority = 1000 },
-- Auto-completion engine
{
"hrsh7th/nvim-cmp",
dependencies = { "lspkind.nvim" },
config = function()
require("config.nvim-cmp")
end,
},
{ "hrsh7th/cmp-nvim-lsp", dependencies = { "nvim-cmp" } },
{ "hrsh7th/cmp-buffer", dependencies = { "nvim-cmp" } }, -- buffer auto-completion
{ "hrsh7th/cmp-path", dependencies = { "nvim-cmp" } }, -- path auto-completion
{ "hrsh7th/cmp-cmdline", dependencies = { "nvim-cmp" } }, -- cmdline auto-completion
-- Code snippet engine
{
"L3MON4D3/LuaSnip",
version = "v2.*",
},
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
"neovim/nvim-lspconfig",
"nvim-tree/nvim-tree.lua",
version = "*",
lazy = false,
dependencies = {
"nvim-tree/nvim-web-devicons",
},
{
"nvim-telescope/telescope.nvim",
tag = "0.1.6",
dependencies = { "nvim-lua/plenary.nvim" },
},
"zaldih/themery.nvim",
"terrortylor/nvim-comment",
"szw/vim-maximizer",
{
"nvim-lualine/lualine.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
},
"numToStr/FTerm.nvim",
"github/copilot.vim",
"nvim-lua/plenary.nvim",
{
"ThePrimeagen/harpoon",
branch = "harpoon2",
requires = { { "nvim-lua/plenary.nvim" } },
},
{
"windwp/nvim-autopairs",
event = "InsertEnter",
config = true,
},
"windwp/nvim-ts-autotag",
"nvim-treesitter/nvim-treesitter",
"mfussenegger/nvim-dap",
{
"stevearc/conform.nvim",
opts = {},
},
})