Skip to content

Commit 2fe9dfe

Browse files
committed
prompt improvement: get 100 buffer lines above and below instead of whole file
1 parent ec9872f commit 2fe9dfe

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎lua/99/prompt-settings.lua‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
---@param buffer number
1+
---@param range _99.Range
2+
---@param n number lines of context above and below the selection
23
---@return string
3-
local function get_file_contents(buffer)
4-
local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
4+
local function get_surrounding_context(range, n)
5+
local line_count = vim.api.nvim_buf_line_count(range.buffer)
6+
local start_row, _ = range.start:to_vim()
7+
local end_row, _ = range.end_:to_vim()
8+
9+
-- nvim_buf_get_lines safely clamps out-of-bounds indexes
10+
local from = start_row - n
11+
local to = end_row + 1 + n -- +1 because end_row is end-inclusive
12+
local lines = vim.api.nvim_buf_get_lines(range.buffer, from, to, false) -- nvim_buf_get_lines is end-exclusive
13+
514
return table.concat(lines, "\n")
615
end
716

@@ -148,13 +157,13 @@ consider the context of the selection and what you are suppose to be implementin
148157
<SELECTION_CONTENT>
149158
%s
150159
</SELECTION_CONTENT>
151-
<FILE_CONTAINING_SELECTION>
160+
<SURROUNDING_CONTEXT>
152161
%s
153-
</FILE_CONTAINING_SELECTION>
162+
</SURROUNDING_CONTEXT>
154163
]],
155164
range:to_string(),
156165
range:to_text(),
157-
get_file_contents(range.buffer)
166+
get_surrounding_context(range, 100)
158167
)
159168
end,
160169
read_tmp = function()

0 commit comments

Comments
 (0)