Add prettier formatting to tsserver-attached buffers.

main
Joshua Potter 2023-12-03 12:54:43 -07:00
parent 8947e1d2b9
commit 1055847812
3 changed files with 16 additions and 6 deletions

View File

@ -1,12 +1,21 @@
{ pkgs, ... }:
{
home.packages = with pkgs; [
nodePackages.prettier
nodePackages.typescript-language-server
];
programs.neovim = {
nvim-lspconfig = ''
require('init.lsp').setup(require('lspconfig').tsserver) {}
require('init.lsp').setup(require('lspconfig').tsserver) {
on_attach = function(client, bufnr)
require('init.lsp').on_attach(client, bufnr)
-- Override the default formatter in typescript-language-server.
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gq', "<CMD>PrettierAsync<CR>", {
silent = true,
})
end,
}
'';
};
}

View File

@ -40,7 +40,7 @@ let
"mfussenegger/nvim-dap";
config = ''
require('dap').defaults.fallback.terminal_win_cmd = 'below 10split new'
${config.programs.neovim.nvim-dap};
${config.programs.neovim.nvim-dap}
'';
};
@ -123,6 +123,7 @@ in
pkgs.vimPlugins.cmp_luasnip
pkgs.vimPlugins.luasnip
pkgs.vimPlugins.nvim-web-devicons
pkgs.vimPlugins.vim-prettier
];
viAlias = true;
vimAlias = true;

View File

@ -1,6 +1,6 @@
local M = {}
local function on_attach(client, bufnr)
function M.on_attach(client, bufnr)
local function set_nnoremap(key, func)
vim.keymap.set('n', key, func, { buffer = bufnr })
end
@ -12,14 +12,14 @@ local function on_attach(client, bufnr)
set_nnoremap('gr', vim.lsp.buf.rename)
end
local capabilities = require('cmp_nvim_lsp').default_capabilities()
M.capabilities = require('cmp_nvim_lsp').default_capabilities()
function M.setup(client)
-- Return a nested function so that we can continue invoking `setup` in the
-- familiar way.
return function(opts)
opts.on_attach = opts.on_attach or on_attach
opts.cabailities = opts.cabailities or capabilities
opts.on_attach = opts.on_attach or M.on_attach
opts.cabailities = opts.cabailities or M.capabilities
client.setup(opts)
end
end