Skip to content

Formatting on selection not possible in Lua #878

@rieje

Description

@rieje

With the suggestion from the wiki, there's no effect when triggering format on selection in a Lua file with lua_ls. In normal mode, format applies to the full file as expected.

Formatting on selection works fine a buffer with with bashls running.

Minimal reproducible config:

init.lua:

vim.pack.add({ "https://github.com/neovim/nvim-lspconfig", "https://github.com/stevearc/conform.nvim" })
require("conform").setup({
  formatters_by_ft = {
    lua = { "stylua" },
    sh = { "shfmt" },
  },
  default_format_opts = {
    lsp_format = "fallback",
  },
})

vim.keymap.set("", "<space>f", function()
  require("conform").format({}, function(err)
    if not err then
      local mode = vim.api.nvim_get_mode().mode
      if vim.startswith(string.lower(mode), "v") then
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
      end
    end
  end)
end, { desc = "Format code" })

vim.lsp.enable({ "bashls", "lua_ls" })

P.S. Is async required for the mapping? If not, I think it should be omitted and prefer defaults.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions