I’m trying to use null-ls
with multiple sources for formatting and diagnostics in Neovim. Everything seems to be working fine when I run node file.js/file.ts
from the terminal, but when I open the file in Neovim, I always get some issue (not specified in the question).
I’ve configured null-ls
and lspconfig
for several programming languages (JavaScript, TypeScript, Python, C++, Go, Shell scripts) as shown below, but the problem persists.
Configuration Files:
null-ls.lua
:
return {
{
'jose-elias-alvarez/null-ls.nvim',
event = "BufRead",
dependencies = { 'nvim-lua/plenary.nvim' },
config = function()
local null_ls = require('null-ls')
null_ls.setup({
sources = {
-- JavaScript/TypeScript
null_ls.builtins.formatting.prettier.with({ command = "/opt/homebrew/bin/prettier" }), -- Specify Prettier path
null_ls.builtins.formatting.eslint_d,
null_ls.builtins.diagnostics.eslint_d,
null_ls.builtins.code_actions.eslint_d,
-- Python
null_ls.builtins.formatting.black,
null_ls.builtins.formatting.isort,
null_ls.builtins.diagnostics.flake8,
null_ls.builtins.diagnostics.mypy,
-- C/C++
null_ls.builtins.formatting.clang_format,
null_ls.builtins.diagnostics.cppcheck,
-- Go
null_ls.builtins.formatting.gofmt,
null_ls.builtins.formatting.goimports,
null_ls.builtins.diagnostics.golangci_lint,
-- Shell scripts
null_ls.builtins.formatting.shfmt,
null_ls.builtins.diagnostics.shellcheck
},
on_attach = function(client, bufnr)
if client.name == "null-ls" then
vim.api.nvim_buf_set_option(bufnr, "formatexpr", "v:lua.vim.lsp.formatexpr()")
end
end,
settings = {
["null-ls"] = {
setup = {
formatting = {
prettier = {
tab_width = 2,
single_quote = true,
trailing_comma = "es5"
}
}
}
}
}
})
-- Keymap for formatting with null-ls
vim.keymap.set('n', '<leader>gf', function() vim.lsp.buf.format() end, { noremap = true, silent = true })
end
}
}
lsp-config.lua
:
return {
{
"williamboman/mason.nvim",
config = function()
require("mason").setup({
ui = {
border = "rounded",
icons = {
package_installed = "✓",
package_pending = "➜",
package_uninstalled = "✗"
}
}
})
end
},
{
"williamboman/mason-lspconfig.nvim",
config = function()
require("mason-lspconfig").setup({
ensure_installed = {
"clangd",
"lua_ls",
"gopls",
"pyright",
"ts_ls" -- Use ts_ls instead of tsserver
},
automatic_installation = true
})
end
},
{
"neovim/nvim-lspconfig",
config = function()
local lspconfig = require("lspconfig")
-- Global LSP settings
vim.diagnostic.config({
virtual_text = true,
signs = true,
update_in_insert = false,
underline = true,
severity_sort = true,
float = {
border = 'rounded',
source = 'always'
}
})
-- Grammarly specific setup with error handling
local grammarly_ok, _ = pcall(function()
lspconfig.grammarly.setup({
cmd = { "grammarly-languageserver", "--stdio" },
filetypes = {
"markdown",
"text",
"tex",
"latex"
},
root_dir = function()
return vim.loop.cwd()
end,
init_options = {
clientId = "client_BaDkMgx4X19X9UxxYRCXZo"
},
settings = {
grammarly = {
autoActivate = true,
suggestions = {
writing_style = "default",
dialect = "american"
}
}
}
})
end)
if not grammarly_ok then
vim.notify("Failed to setup Grammarly LSP", vim.log.levels.ERROR)
end
-- Setup other LSP servers
lspconfig.lua_ls.setup({})
lspconfig.clangd.setup({})
lspconfig.gopls.setup({})
lspconfig.pyright.setup({})
lspconfig.ts_ls.setup({})
-- Key mappings
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, { desc = "Code Action" })
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { desc = "Go to Definition" })
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { desc = "Hover Information" })
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, { desc = "Go to Implementation" })
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, { desc = "Rename" })
end
}
}
I have made sure that all required binaries (Prettier, ESLint, etc.) are properly installed. I’ve also checked both null-ls
and lspconfig
configurations, but the issue still persists.