Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp formatting #400

Open
Astrantia opened this issue Feb 5, 2022 · 1 comment
Open

lsp formatting #400

Astrantia opened this issue Feb 5, 2022 · 1 comment

Comments

@Astrantia
Copy link

hello, a pretty basic feature request actually. is it possible to add an LSP formatter? some language servers implement formatting, and when I'm writing code using those lsp's, i always prefer formatting with the lsp.using the following command :lua vim.lsp.buf.formatting()

all languages are handled through that one command, so would it be possible to add an option for adding lsp formatting as the default?

@festeh
Copy link

festeh commented Jan 29, 2023

I needed to use LSP formatter for Lua and neoformat for Python and wrote a wrapper around neoformat. It checks global variable neoformat_enabled_* for the current buffer and calls neoformat if it exists. Of course, one can set up additional logic.

Here's my neoformat config

vim.g.neoformat_enabled_python = { 'yapf' }

local ext_to_lang = {
  py = "python",
  lua = "lua",
  rs = "rust",
}

local function format()
  local buf = vim.api.nvim_get_current_buf()
  local name = vim.api.nvim_buf_get_name(buf)
  local ext = name:match("%.(%w+)$")
  local lang = ext_to_lang[ext]
  if lang then
    local key = "neoformat_enabled_" .. lang
    if vim.g[key] then
      vim.cmd("Neoformat")
      return
    end
  end
  vim.lsp.buf.format()
end

vim.keymap.set('n', '<leader>m', format, { desc = "Format buf with Neoformat or ls" })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants