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

formatter/format.lua: index out of bounds #305

Closed
tjex opened this issue Dec 5, 2023 · 0 comments · Fixed by #333
Closed

formatter/format.lua: index out of bounds #305

tjex opened this issue Dec 5, 2023 · 0 comments · Fixed by #333

Comments

@tjex
Copy link
Contributor

tjex commented Dec 5, 2023

When calling vim.lsp.buf.format() in ["*"] = {} (for any file type), an error is thrown about an out of bound index in get_lines.

Shell command and output (optional):

E5108: Error executing lua ...ack/packer/start/formatter.nvim/lua/formatter/format.lua:61: Index out of bounds
stack traceback:
        [C]: in function 'get_lines'
        ...ack/packer/start/formatter.nvim/lua/formatter/format.lua:61: in function 'start_task'
        ...ack/packer/start/formatter.nvim/lua/formatter/format.lua:49: in function 'format'
        [string ":lua"]:1: in main chunk

Which configuration?
Type (custom or builtin): custom
Filetype: * / all
Formatter: vim.lsp.buf.format()

Configuration(s) (post all the variants you tried):

require("formatter").setup({
	logging = true,
	log_level = vim.log.levels.WARN,
	filetype = {
		-- formatters are executed in order within their own tables
		lua = {
			require("formatter.filetypes.lua").stylua,
		},
		bash = {
			require("formatter.filetypes.sh").shfmt,
		},
		javascript = {
			require("formatter.filetypes.javascript").eslint_d,
		},
		css = {
			require("formatter.filetypes.css").prettierd,
		},
		scss = {
			require("formatter.filetypes.css").prettierd,
		},
		html = {
			require("formatter.filetypes.html").prettierd,
		},
		json = {
			require("formatter.filetypes.json").prettierd,
		},

		-- for any filetype
		["*"] = {
			require("formatter.filetypes.any").remove_trailing_whitespace,
			-- formatting logic: See if formatter.nvim can do it, then check if lsp can do it,
			-- then notify if neither can do it. The keybind is then universally ":Format", as this
			-- logic is run when formatter.nvim is run.
			function()
				local formatters = require("formatter.util").get_available_formatters_for_ft(vim.bo.filetype)
				if #formatters > 0 then
					print("formatted with formatter.nvim")
					return
				end
				local lsp_clients = vim.lsp.buf_get_clients()
				-- check if there are any LSP formatters
				for _, client in pairs(lsp_clients) do
					if client.server_capabilities.document_formatting then
						vim.lsp.buf.format()
						print("formatted with lsp")
						return
					end
				end
				print("no formatters found from formatter.nvim OR lsp")
			end,
		},
	},
})

I've also tried by only calling format(), same error:

-- ... unchanged code

		["*"] = {
			function()
                                 vim.lsp.buf.format()
			end,
		},

Expected behavior

That when running vim.lsp.buf.format() via formatter.nvim that the text is formatted without error.

+ Note: I ran formatter.nvim when editing a go file (i.e. not a filetype I'm targeting).

I am able to call vim.lsp.buf.format() manually in the file and format without error.
If I simply undo the formatting and try with formatter.nvim (via :Format or :FormatWrite) I get the above error.

Actual behaviour
The error is output, and when cleared with a keypress (confirming the error), the text formats correctly.

Additional context

This has been commented on before here: #260

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