From 3902f3e598b821032db73aecc9f6e7e00521a567 Mon Sep 17 00:00:00 2001 From: ItaloBorrelli Date: Thu, 22 May 2025 22:30:01 -0700 Subject: [PATCH] Update lint and lsp configs for ts --- .../lua/plugins/language_support/conform.lua | 40 ++++++++++++++-- .../plugins/language_support/nvim-lint.lua | 48 +++++++++++++++++++ .config/nvim/lua/plugins/lsp/mason.lua | 9 +++- .../nvim/lua/plugins/lsp/nvim-lspconfig.lua | 21 ++++++++ 4 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 .config/nvim/lua/plugins/language_support/nvim-lint.lua diff --git a/.config/nvim/lua/plugins/language_support/conform.lua b/.config/nvim/lua/plugins/language_support/conform.lua index 1f1e36f..99617bd 100644 --- a/.config/nvim/lua/plugins/language_support/conform.lua +++ b/.config/nvim/lua/plugins/language_support/conform.lua @@ -3,14 +3,48 @@ return { config = function() local conform = require("conform") + local function file_exists(name) + local f = io.open(name, "r") + if f ~= nil then + io.close(f) + return true + else + return false + end + end + local function decide_js_formatter() + local cwd = vim.fn.getcwd() + if file_exists(cwd .. "/biome.json") then + return "biome" + else + return "prettier" + end + end + local function decide_json_formatter() + local cwd = vim.fn.getcwd() + if file_exists(cwd .. "/biome.json") then + return "biome" + elseif file_exists(cwd .. "/.prettierrc") then + return "prettier" + else + return "jq" + end + end + conform.setup({ formatters_by_ft = { + javascript = { decide_js_formatter() }, + javascriptreact = { decide_js_formatter() }, + json = { decide_json_formatter() }, lua = { "stylua" }, + markdown = { "mdformat" }, nginx = { "nginxfmt" }, - python = { "black", "flake8" }, + python = { "isort", "black" }, + sh = { "shfmt" }, + typescript = { decide_js_formatter() }, + typescriptreact = { decide_js_formatter() }, yaml = { "yamlfix" }, - -- Use the "*" filetype to run formatters on all filetypes. - -- ["*"] = { "codespell" }, + ["*"] = { "codespell" }, }, default_format_opts = { diff --git a/.config/nvim/lua/plugins/language_support/nvim-lint.lua b/.config/nvim/lua/plugins/language_support/nvim-lint.lua new file mode 100644 index 0000000..b1467d6 --- /dev/null +++ b/.config/nvim/lua/plugins/language_support/nvim-lint.lua @@ -0,0 +1,48 @@ +return { + "mfussenegger/nvim-lint", + config = function() + local function file_exists(name) + local f = io.open(name, "r") + if f ~= nil then + io.close(f) + return true + else + return false + end + end + local function decide_linter() + local cwd = vim.fn.getcwd() + if file_exists(cwd .. "/biome.json") then + return "biome" + else + return "eslint_d" + end + end + + local lint = require("lint") + + lint.linters_by_ft = { + javascript = { "eslint_d" }, + javascriptreact = { "eslint_d" }, + markdown = { "markdownlint" }, + python = { "flake8" }, + sh = { "shellcheck" }, + typescript = { "eslint_d" }, + typescriptreact = { "eslint_d" }, + yaml = { "yamllint" }, + } + + 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, + }) + + vim.keymap.set("n", "l", function() + lint.try_lint() + end, { desc = "Trigger linting for current file" }) + end, +} diff --git a/.config/nvim/lua/plugins/lsp/mason.lua b/.config/nvim/lua/plugins/lsp/mason.lua index 80003c3..d2eaaaa 100644 --- a/.config/nvim/lua/plugins/lsp/mason.lua +++ b/.config/nvim/lua/plugins/lsp/mason.lua @@ -18,9 +18,11 @@ return { require("mason-lspconfig").setup({ ensure_installed = { + "bashls", "cssls", "dockerls", "emmet_ls", + "eslint", "html", "jdtls", "jsonls", @@ -43,10 +45,15 @@ return { "eslint_d", "firefox-debug-adapter", "flake8", + "isort", "java-debug-adapter", "jq", + "markdownlint", + "mdformat", "nginx-config-formatter", - "prettierd", + "prettier", + "shellcheck", + "shfmt", "stylua", "yamlfix", "yamllint", diff --git a/.config/nvim/lua/plugins/lsp/nvim-lspconfig.lua b/.config/nvim/lua/plugins/lsp/nvim-lspconfig.lua index 2a09ef2..48ad38d 100644 --- a/.config/nvim/lua/plugins/lsp/nvim-lspconfig.lua +++ b/.config/nvim/lua/plugins/lsp/nvim-lspconfig.lua @@ -134,6 +134,17 @@ return { root_markers = { "tsconfig.json", "jsconfig.json", "package.json" }, }) + vim.lsp.config("jsonls", { + on_attach = function(client) + -- Disable formatting capability for tsserver + client.server_capabilities.documentFormattingProvider = false + client.server_capabilities.documentRangeFormattingProvider = false + end, + filetypes = { + "json", + }, + }) + vim.lsp.config("pylsp", { on_attach = function(client) client.server_capabilities.documentFormattingProvider = false @@ -144,5 +155,15 @@ return { }, root_markers = { "requirements.txt" }, }) + + vim.lsp.config("bashls", { + on_attach = function(client) + client.server_capabilities.documentFormattingProvider = false + client.server_capabilities.documentRangeFormattingProvider = false + end, + filetypes = { + "sh", + }, + }) end, }