Skip to content

Instantly share code, notes, and snippets.

@majamin
Created January 16, 2023 06:13
Show Gist options
  • Save majamin/71c19bc463fdbdd55388a1c0e3398c73 to your computer and use it in GitHub Desktop.
Save majamin/71c19bc463fdbdd55388a1c0e3398c73 to your computer and use it in GitHub Desktop.
MRE of a Neovim config using Mason and Null-ls to format files using prettierd
local install_path = vim.fn.stdpath 'data' .. '/site/pack/packer/start/packer.nvim'
local is_bootstrap = false
if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
is_bootstrap = true
vim.fn.system { 'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path }
vim.cmd [[packadd packer.nvim]]
end
require('packer').startup(function(use)
use 'wbthomason/packer.nvim'
use {
'neovim/nvim-lspconfig',
requires = {
'williamboman/mason.nvim',
'williamboman/mason-lspconfig.nvim',
},
}
use {
'jose-elias-alvarez/null-ls.nvim',
requires = 'nvim-lua/plenary.nvim'
}
if is_bootstrap then
require('packer').sync()
end
end)
if is_bootstrap then
return
end
local on_attach = function(_, bufnr)
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
vim.lsp.buf.format({
bufnr = bufnr,
filter = function(client)
return client.name == "null-ls"
end
})
print("File formatted")
end, { desc = 'Format current buffer with LSP' })
end
local servers = {
tsserver = {},
}
require('mason').setup()
local mason_lspconfig = require 'mason-lspconfig'
mason_lspconfig.setup {
ensure_installed = vim.tbl_keys(servers),
}
mason_lspconfig.setup_handlers {
function(server_name)
require('lspconfig')[server_name].setup {
capabilities = capabilities,
on_attach = on_attach,
settings = servers[server_name],
}
end,
}
local null_ls = require("null-ls")
null_ls.setup({
sources = {
null_ls.builtins.formatting.prettierd,
},
})
vim.cmd([[colo slate]])
@Sc4ramouche
Copy link

Sc4ramouche commented Jan 24, 2023

Hi there! Thanks a lot for sharing this snippet, I followed it from a question in r/neovim.

This was the missing piece for me to understand how to properly setup prettierd, I'm so happy to see it working now! Huge thanks ๐Ÿ™Œ

@majamin
Copy link
Author

majamin commented Jan 24, 2023

@Sc4ramouche glad to hear it!

@austintheriot
Copy link

Same--thank you for sharing this. Was finally able to get things working (using lazy.nvim)

@daveyholler
Copy link

Heck yeah. Thanks for this. ๐ŸŽ‰

For the future internet travelers that may find themselves here. My issue was that I was using prettier instead of prettierd.

My steps to fix were pretty simple:

  1. Install prettierd
:MasonInstall prettierd
  1. Update my null-ls config
-- Replace
null_ls.builtins.formatting.prettier.with({

-- With
null_ls.builtins.formatting.prettierd.with({ 
  1. Restart Neovim
  2. Profit

@TriupLauro
Copy link

Is there a way to undo what this script has done ?

Now I have this message everytime I open nvim

[lspconfig] requires neovim 0.8 or later. Detected neovim version: 0.7.2

@Ifeanyi-Ani
Copy link

Is there a way to undo what this script has done ?

Now I have this message everytime I open nvim

[lspconfig] requires neovim 0.8 or later. Detected neovim version: 0.7.2

update your neovim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment