From 6efd8a3873e435c09b5a4fbfc806c667e634df9f Mon Sep 17 00:00:00 2001 From: Simon Lasbrugnas Date: Mon, 28 Jul 2025 21:55:38 +0200 Subject: [PATCH] feat: modify LSPs to prefer conform for formatting Add eslint LSP feat: Add linter support with 'nvim-lint' Add linting to JavaScript / TypeScript and Go files --- lua/config/options.lua | 3 +++ lua/custom/lsp/config/biome.lua | 2 +- lua/custom/lsp/config/eslint.lua | 11 +++++++++++ lua/custom/lsp/config/html.lua | 2 +- lua/custom/lsp/config/jsonls.lua | 18 +++++++++++++++++- lua/custom/lsp/config/yamlls.lua | 12 +++++++++++- lua/custom/plugins/colorscheme.lua | 2 +- lua/custom/plugins/conform.lua | 26 ++++++++++++++++++-------- lua/custom/plugins/nvim-lint.lua | 27 +++++++++++++++++++++++++++ 9 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 lua/custom/lsp/config/eslint.lua create mode 100644 lua/custom/plugins/nvim-lint.lua diff --git a/lua/config/options.lua b/lua/config/options.lua index 42237c3..a32f47f 100644 --- a/lua/config/options.lua +++ b/lua/config/options.lua @@ -58,3 +58,6 @@ opt.listchars = "tab: ,extends:›,precedes:‹,nbsp:·,trail:·" -- System wide clipboard opt.clipboard = "unnamedplus" + +-- Use .editorconfig files +vim.g.editorconfig = true diff --git a/lua/custom/lsp/config/biome.lua b/lua/custom/lsp/config/biome.lua index c82d78d..602e46c 100644 --- a/lua/custom/lsp/config/biome.lua +++ b/lua/custom/lsp/config/biome.lua @@ -4,7 +4,7 @@ return { vim.api.nvim_create_autocmd("BufWritePre", { buffer = bufnr, callback = function() - vim.lsp.buf.format() + require("conform").format({ async = true, lsp_fallback = true }) end, }) end diff --git a/lua/custom/lsp/config/eslint.lua b/lua/custom/lsp/config/eslint.lua new file mode 100644 index 0000000..602e46c --- /dev/null +++ b/lua/custom/lsp/config/eslint.lua @@ -0,0 +1,11 @@ +return { + on_attach = function(_, bufnr) + -- Format document on save + vim.api.nvim_create_autocmd("BufWritePre", { + buffer = bufnr, + callback = function() + require("conform").format({ async = true, lsp_fallback = true }) + end, + }) + end +} diff --git a/lua/custom/lsp/config/html.lua b/lua/custom/lsp/config/html.lua index c82d78d..602e46c 100644 --- a/lua/custom/lsp/config/html.lua +++ b/lua/custom/lsp/config/html.lua @@ -4,7 +4,7 @@ return { vim.api.nvim_create_autocmd("BufWritePre", { buffer = bufnr, callback = function() - vim.lsp.buf.format() + require("conform").format({ async = true, lsp_fallback = true }) end, }) end diff --git a/lua/custom/lsp/config/jsonls.lua b/lua/custom/lsp/config/jsonls.lua index a564707..bb9d459 100644 --- a/lua/custom/lsp/config/jsonls.lua +++ b/lua/custom/lsp/config/jsonls.lua @@ -1 +1,17 @@ -return {} +return { + on_attach = function(_, bufnr) + -- Format document on save + vim.api.nvim_create_autocmd("BufWritePre", { + buffer = bufnr, + callback = function() + -- check that biome lsp is running + local biome = vim.lsp.get_clients({ buffer = bufnr, name = "biome" }) + if #biome == 0 then + vim.lsp.buf.format() + else + vim.lsp.buf.format({ filter = function(client) return client.name == "biome" end }) + end + end, + }) + end +} diff --git a/lua/custom/lsp/config/yamlls.lua b/lua/custom/lsp/config/yamlls.lua index a564707..602e46c 100644 --- a/lua/custom/lsp/config/yamlls.lua +++ b/lua/custom/lsp/config/yamlls.lua @@ -1 +1,11 @@ -return {} +return { + on_attach = function(_, bufnr) + -- Format document on save + vim.api.nvim_create_autocmd("BufWritePre", { + buffer = bufnr, + callback = function() + require("conform").format({ async = true, lsp_fallback = true }) + end, + }) + end +} diff --git a/lua/custom/plugins/colorscheme.lua b/lua/custom/plugins/colorscheme.lua index b9d638a..16ef198 100644 --- a/lua/custom/plugins/colorscheme.lua +++ b/lua/custom/plugins/colorscheme.lua @@ -33,7 +33,7 @@ return { }, }, init = function() - -- vim.cmd("colorscheme lackluster-night") + -- vim.cmd("colorscheme lackluster-mint") end }, { diff --git a/lua/custom/plugins/conform.lua b/lua/custom/plugins/conform.lua index 440b991..4b7a4b6 100644 --- a/lua/custom/plugins/conform.lua +++ b/lua/custom/plugins/conform.lua @@ -3,9 +3,9 @@ return { config = function() require("conform").setup({ formatters_by_ft = { + go = { "go" }, html = { "biome", "html-lsp" }, - json = { "biome" }, - yaml = { "yamlls" }, + yaml = { "prettier" }, javascript = { "biome" }, javascriptreact = { "biome" }, markdown = { "biome" }, @@ -13,15 +13,25 @@ return { typescriptreact = { "biome" }, ["*"] = { "trim_whitespace" }, }, - format_on_save = { - timeout_ms = 500, - lsp_fallback = true, - }, formatters = { biome = { condition = function() - return vim.uv.fs_realpath("biome.json") ~= nil end, - }, + return vim.uv.fs_realpath("biome.json") ~= nil + end, + command = "biome", + args = { + "check", + "--fix", + "--unsafe", + "$FILENAME", + }, + stdin = false, + } + }, + format_on_save = { + lsp_fallback = true, + async = false, + timeout_ms = 500, }, }) end, diff --git a/lua/custom/plugins/nvim-lint.lua b/lua/custom/plugins/nvim-lint.lua new file mode 100644 index 0000000..b094db8 --- /dev/null +++ b/lua/custom/plugins/nvim-lint.lua @@ -0,0 +1,27 @@ +return { + "mfussenegger/nvim-lint", + event = { + "BufReadPre", + "BufNewFile", + }, + config = function() + local lint = require("lint") + + lint.linters_by_ft = { + go = { "golangcilint" }, + javascript = { "biomejs", "eslint_d" }, + typescript = { "biomejs", "eslint_d" }, + javascriptreact = { "biomejs", "eslint_d" }, + typescriptreact = { "biomejs", "eslint_d" }, + } + + local lint_augroup = vim.api.nvim_create_augroup("lint", { clear = true }) + + vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "InsertLeave" }, { + group = lint_augroup, + callback = function() + lint.try_lint() + end, + }) + end, +}