refactor(utils): make prepend_line_numbers() more efficient and idiomatic

Between Utils.prepend_line_number() and its only caller, there is a lot
of conversions from list to string to list and back to string. Simplify
all of this by making it accept and return a list of strings, which
avoids unnecessary splitting and joining.

The implementation was updated to use vim.iter():map() and
string.format() for better performance and to align with the idiomatic
functional style used elsewhere in the project.

The name of the function has also been tweaked and is now
"prepend_line_numbers()" to better reflect that it operates on a list of
strings.

The caller has been updated to match the new function signature.
This commit is contained in:
Dmitry Torokhov
2025-08-06 15:31:26 -07:00
committed by yetone
parent 76fe3f615a
commit 638d237b75
2 changed files with 10 additions and 10 deletions

View File

@@ -166,8 +166,10 @@ function Suggestion:suggest()
local bufnr = api.nvim_get_current_buf()
local filetype = api.nvim_get_option_value("filetype", { buf = bufnr })
local code_content =
Utils.prepend_line_number(table.concat(api.nvim_buf_get_lines(bufnr, 0, -1, false), "\n") .. "\n\n")
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
table.insert(lines, "")
table.insert(lines, "")
local code_content = table.concat(Utils.prepend_line_numbers(lines), "\n")
local full_response = ""