From 77d4250057b5c5f5bd4e306c9bc4c1660c05eba3 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Wed, 22 Oct 2025 17:17:24 +0300 Subject: [PATCH] refactor: unify syntax langs --- lazy-lock.json | 1 + lsp/{json_ls.lua => jsonls.lua} | 0 lsp/{lua_ls.lua => luals.lua} | 0 lua/config/keymaps.lua | 4 +- lua/config/options.lua | 22 +++++ lua/plugins/syntax.lua | 152 +++++++++++++++++--------------- 6 files changed, 107 insertions(+), 72 deletions(-) rename lsp/{json_ls.lua => jsonls.lua} (100%) rename lsp/{lua_ls.lua => luals.lua} (100%) diff --git a/lazy-lock.json b/lazy-lock.json index 26afe77..a175a3b 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -2,6 +2,7 @@ "conform.nvim": { "branch": "master", "commit": "235dd79731c1dc51ec04abb4045cbc54727a172a" }, "invero.nvim": { "branch": "main", "commit": "6acdefa2f2fdf544b53b5786e748be0ae8e9fd23" }, "lazy.nvim": { "branch": "main", "commit": "1ea3c4085785f460fb0e46d2fe1ee895f5f9e7c1" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "2304ff65ecc8cb2afc2484de3e2ed9a407edf0b9" }, "mason.nvim": { "branch": "main", "commit": "ad7146aa61dcaeb54fa900144d768f040090bff0" }, "nvim-autopairs": { "branch": "master", "commit": "7a2c97cccd60abc559344042fefb1d5a85b3e33b" }, "nvim-lint": { "branch": "master", "commit": "9da1fb942dd0668d5182f9c8dee801b9c190e2bb" }, diff --git a/lsp/json_ls.lua b/lsp/jsonls.lua similarity index 100% rename from lsp/json_ls.lua rename to lsp/jsonls.lua diff --git a/lsp/lua_ls.lua b/lsp/luals.lua similarity index 100% rename from lsp/lua_ls.lua rename to lsp/luals.lua diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 90d5aac..1567dbe 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -50,9 +50,9 @@ end map('n', ']b', cmd('bnext')) map('n', '[b', cmd('bprevious')) map('n', 'bl', cmd('ls')) -map('n', 'bb', '') -- toggle between buffers -map('n', 'bd', cmd('bdelete')) +map('n', 'bd', cmd(':bp | bd #')) map('n', 'bo', cmd('%bd|e#')) -- close all except current +map('n', 'bb', '') -- toggle between buffers -- Terminal map('n', 'xx', cmd('TermDefault')) diff --git a/lua/config/options.lua b/lua/config/options.lua index ef64b18..d6ec47b 100644 --- a/lua/config/options.lua +++ b/lua/config/options.lua @@ -87,3 +87,25 @@ vim.opt.timeout = true vim.opt.ttimeout = true vim.opt.timeoutlen = 500 vim.opt.ttimeoutlen = 10 + +vim.keymap.set('n', 'd', vim.diagnostic.open_float, { noremap = true, silent = true }) +vim.diagnostic.config({ + update_in_insert = false, + virtual_text = { + prefix = '', -- remove annoying ▎ etc + format = function(diagnostic) + if diagnostic.source then + return string.format('[%s] %s', diagnostic.source, diagnostic.message) + end + return diagnostic.message + end, + }, + float = { + border = 'rounded', + source = true, -- show source in floating window too + }, + severity_sort = true, +}) + +vim.opt.foldmethod = 'expr' +vim.opt.foldexpr = 'nvim_treesitter#foldexpr()' diff --git a/lua/plugins/syntax.lua b/lua/plugins/syntax.lua index 7ed22c6..9416f54 100644 --- a/lua/plugins/syntax.lua +++ b/lua/plugins/syntax.lua @@ -1,31 +1,87 @@ -vim.keymap.set('n', 'd', vim.diagnostic.open_float, { noremap = true, silent = true }) +-- or { , , , , filetype= } +-- filetype defaults to . If different, specify it. +-- any positional argument can be skipped with nil +local languages = { + -- Docs / Config + 'vim', + 'vimdoc', + 'markdown', + 'markdown_inline', + 'yaml', + 'toml', -vim.diagnostic.config({ - update_in_insert = false, - virtual_text = { - prefix = '', -- remove annoying ▎ etc - format = function(diagnostic) - if diagnostic.source then - return string.format('[%s] %s', diagnostic.source, diagnostic.message) - end - return diagnostic.message - end, - }, - float = { - border = 'rounded', - source = true, -- show source in floating window too - }, - severity_sort = true, -}) + -- Data + 'gitcommit', + 'gitignore', + 'dockerfile', + 'diff', + { 'json', 'jsonls', nil, 'prettier' }, + { 'jsonc', 'jsonls', nil, 'prettier' }, -vim.lsp.enable({ 'lua_ls' }) -vim.lsp.enable({ 'json_ls' }) + -- Shell / Scripting + { 'bash', 'bashls', 'shellcheck', 'shfmt' }, + { 'lua', 'luals', 'luacheck', 'stylua' }, + { 'sql', 'sqlls', nil, 'sqlfluff' }, + + -- Programming + { 'c', 'clangd', nil, 'clang-format' }, + { 'cpp', 'clangd', nil, 'clang-format' }, + { 'go', 'gopls', 'golangci-lint', 'gofmt' }, + { 'rust', 'rust_analyzer', 'clippy', 'rustfmt' }, + { 'python', 'pyright', 'ruff', 'black' }, + + -- Web stack + { 'html', 'html', nil, 'prettier' }, + { 'css', 'cssls', 'stylelint', 'prettier' }, + { 'javascript', 'tsls', 'eslint', 'prettier' }, + { 'typescript', 'tsls', 'eslint', 'prettier' }, + { nil, 'tsls', 'eslint', 'prettier', filetype = 'javascriptreact' }, + { 'tsx', 'tsls', 'eslint', 'prettier', filetype = 'typescriptreact' }, +} + +local function normalize(lang) + if type(lang) == 'string' then + lang = { lang } + end + local t = { + treesitter = lang[1], + lsp = lang[2], + linter = lang[3], + formatter = lang[4], + filetype = lang.filetype or lang[1], + } + return t +end + +local normalized = vim.tbl_map(normalize, languages) +local general = { + ts_parsers = {}, + lsps = {}, + linters_by_ft = {}, + formatters_by_ft = {}, +} +for _, opts in ipairs(normalized) do + if opts.treesitter then + table.insert(general.ts_parsers, opts.treesitter) + end + + if opts.lsp then + table.insert(general.lsps, opts.lsp) + end + + if opts.linter then + general.linters_by_ft[opts.filetype] = { opts.linter } + end + + if opts.formatter then + general.formatters_by_ft[opts.filetype] = { opts.formatter } + end +end return { - { 'mason-org/mason.nvim', config = true }, { 'windwp/nvim-ts-autotag', config = true }, { 'windwp/nvim-autopairs', event = 'InsertEnter', config = true }, - + { 'mason-org/mason.nvim', config = true }, -- { 'saghen/blink.cmp', version = '1.*' }, { 'nvim-treesitter/nvim-treesitter', @@ -35,48 +91,14 @@ return { highlight = { enable = true }, incremental_selection = { enable = true }, textobjects = { enable = true }, - ensure_installed = { - -- Documentation - 'vim', - 'vimdoc', - 'markdown', - 'markdown_inline', - -- Data - 'gitcommit', - 'gitignore', - 'dockerfile', - 'diff', - 'json', - 'jsonc', - -- Scripting - 'bash', - 'lua', - 'sql', - -- Programming - 'c', - 'cpp', - 'go', - 'rust', - 'python', - -- Web - 'html', - 'css', - 'javascript', - 'typescript', - 'tsx', - }, + fold = { enable = true }, + ensure_installed = general.ts_parsers, }, }, { 'mfussenegger/nvim-lint', event = { 'BufReadPre', 'BufNewFile' }, - opts = { - linters_by_ft = { - lua = { 'luacheck' }, - python = { 'ruff' }, - javascript = { 'eslint_d' }, - }, - }, + opts = { linters_by_ft = general.linters_by_ft }, config = function(_, opts) local lint = require('lint') lint.linters_by_ft = opts.linters_by_ft @@ -100,17 +122,7 @@ return { -- Formatters per filetype default_format_opts = { stop_after_first = true }, - formatters_by_ft = { - lua = { 'stylua' }, - sh = { 'shfmt' }, - swift = { 'swift_format' }, - python = { 'isort', 'black' }, - json = { 'jq' }, - javascript = { 'prettierd', 'prettier' }, - javascriptreact = { 'prettierd', 'prettier' }, - typescript = { 'prettierd', 'prettier' }, - typescriptreact = { 'prettierd', 'prettier' }, - }, + formatters_by_ft = general.formatters_by_ft, }, config = function(_, opts)