Run :TSLspOrganizeSync as formatter? #109
-
Hi! I'm using prettier with null-ls, so if I want to organize then format I need to do something like:
Is there a recommended way to register Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The safest way to do this is to set up a command that does both, something like this (untested but it should make sense): _G.formatting = function()
local bufnr = vim.api.nvim_get_current_buf()
local tsserver_is_attached = false
for _, client in ipairs(vim.lsp.buf_get_clients(bufnr)) do
if client.name == "tsserver" then
tsserver_is_attached = true
break
end
end
if tsserver_is_attached then
require("nvim-lsp-ts-utils").organize_imports_sync(bufnr)
end
vim.lsp.buf.formatting_seq_sync()
end If you really want to set this up as a null-ls formatter, it's possible, but it's pretty hacky and I can't guarantee that it'll work in all cases (it seems to work with a local organize_imports = {
method = null_ls.methods.FORMATTING,
name = "organize_imports",
filetypes = { "typescript", "typescriptreact" },
generator = {
async = true,
fn = function(params, done)
require("nvim-lsp-ts-utils").organize_imports(params.bufnr, done)
end,
},
} If you plan on trying out the custom source, make sure to update, since a1ba6c9 fixes an issue that would otherwise pop up. |
Beta Was this translation helpful? Give feedback.
The safest way to do this is to set up a command that does both, something like this (untested but it should make sense):
If you really want to set this up as a null-ls formatter, it's possible, but it's pretty hacky and I can't guarantee that it'll…