Skip to content

Commit 7460399

Browse files
authored
refactor: abstract vim.api (olimorris#1960)
Co-authored-by: Oli Morris <[email protected]>
1 parent 92b4884 commit 7460399

File tree

8 files changed

+33
-22
lines changed

8 files changed

+33
-22
lines changed

lua/codecompanion/strategies/chat/tools/catalog/list_code_usages/code_extractor.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local Utils = require("codecompanion.strategies.chat.tools.catalog.list_code_usages.utils")
22
local log = require("codecompanion.utils.log")
33

4+
local api = vim.api
5+
46
---@class ListCodeUsages.CodeExtractor
57
local CodeExtractor = {}
68

@@ -231,7 +233,7 @@ function CodeExtractor.get_fallback_code_block(bufnr, row, col)
231233

232234
-- Find end of block (going downward)
233235
local end_row = row
234-
local total_lines = vim.api.nvim_buf_line_count(bufnr)
236+
local total_lines = api.nvim_buf_line_count(bufnr)
235237
for i = row + 1, math.min(row + CONSTANTS.MAX_BLOCK_SCAN_LINES, total_lines - 1) do
236238
local curr_lines = Utils.safe_get_lines(bufnr, i, i + 1)
237239
local curr_line = curr_lines[1]

lua/codecompanion/strategies/chat/tools/catalog/list_code_usages/init.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local ResultProcessor = require("codecompanion.strategies.chat.tools.catalog.lis
33
local SymbolFinder = require("codecompanion.strategies.chat.tools.catalog.list_code_usages.symbol_finder")
44
local Utils = require("codecompanion.strategies.chat.tools.catalog.list_code_usages.utils")
55

6+
local api = vim.api
67
local fmt = string.format
78

89
---@class CodeCompanion.Tool.ListCodeUsages: CodeCompanion.Tools.Tool
@@ -52,7 +53,7 @@ local function process_lsp_symbols_async(symbols, state, callback)
5253
if edit_success then
5354
Utils.async_set_cursor(line, col, function(cursor_success)
5455
if cursor_success then
55-
local current_bufnr = vim.api.nvim_get_current_buf()
56+
local current_bufnr = api.nvim_get_current_buf()
5657
local methods_to_process = {}
5758

5859
-- Collect all methods to process
@@ -121,7 +122,7 @@ local function process_grep_results_async(grep_result, state, callback)
121122
if edit_success then
122123
Utils.async_set_cursor(grep_result.line, grep_result.col, function(cursor_success)
123124
if cursor_success then
124-
local current_bufnr = vim.api.nvim_get_current_buf()
125+
local current_bufnr = api.nvim_get_current_buf()
125126
local methods_to_process = {}
126127

127128
-- Collect all methods to process
@@ -204,14 +205,14 @@ return {
204205
-- Save current state of view
205206
local context_winnr = self.chat.context.winnr
206207
local context_bufnr = self.chat.context.bufnr
207-
local chat_winnr = vim.api.nvim_get_current_win()
208+
local chat_winnr = api.nvim_get_current_win()
208209

209210
-- Get file extension from context buffer if available
210211
local file_extension = get_file_extension(context_bufnr)
211212

212213
-- Exit insert mode and switch focus to context window
213214
vim.cmd("stopinsert")
214-
vim.api.nvim_set_current_win(context_winnr)
215+
api.nvim_set_current_win(context_winnr)
215216

216217
-- Start async processing
217218
SymbolFinder.find_with_lsp_async(symbol_name, file_paths, function(all_lsp_symbols)
@@ -227,7 +228,7 @@ return {
227228

228229
-- Handle case where we have no results
229230
if total_results == 0 then
230-
vim.api.nvim_set_current_win(chat_winnr)
231+
api.nvim_set_current_win(chat_winnr)
231232
local filetype_msg = file_extension and (" in " .. file_extension .. " files") or ""
232233
output_handler(
233234
Utils.create_result(
@@ -241,8 +242,8 @@ return {
241242
end
242243

243244
-- Restore original state of view
244-
vim.api.nvim_set_current_buf(context_bufnr)
245-
vim.api.nvim_set_current_win(chat_winnr)
245+
api.nvim_set_current_buf(context_bufnr)
246+
api.nvim_set_current_win(chat_winnr)
246247

247248
-- Store state for output handler
248249
ListCodeUsagesTool.symbol_data = state.symbol_data

lua/codecompanion/strategies/chat/tools/catalog/list_code_usages/result_processor.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local CodeExtractor = require("codecompanion.strategies.chat.tools.catalog.list_
22
local Utils = require("codecompanion.strategies.chat.tools.catalog.list_code_usages.utils")
33
local log = require("codecompanion.utils.log")
44

5+
local api = vim.api
6+
57
---@class ListCodeUsages.ResultProcessor
68
local ResultProcessor = {}
79

@@ -199,7 +201,7 @@ function ResultProcessor.process_quickfix_references(qflist, symbol_data)
199201
local col = qfitem.col - 1 -- Convert to 0-indexed
200202

201203
-- Load buffer if needed
202-
if not vim.api.nvim_buf_is_loaded(target_bufnr) then
204+
if not api.nvim_buf_is_loaded(target_bufnr) then
203205
vim.fn.bufload(target_bufnr)
204206
end
205207

lua/codecompanion/strategies/chat/tools/catalog/list_code_usages/utils.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local api = vim.api
2+
13
---@class ListCodeUsages.Utils
24
local Utils = {}
35

@@ -52,7 +54,7 @@ end
5254
---@param bufnr number|nil The buffer number to validate
5355
---@return boolean True if the buffer is valid and exists
5456
function Utils.is_valid_buffer(bufnr)
55-
return bufnr and vim.api.nvim_buf_is_valid(bufnr)
57+
return bufnr and api.nvim_buf_is_valid(bufnr)
5658
end
5759

5860
---@param bufnr number The buffer number to get filetype from
@@ -62,7 +64,7 @@ function Utils.safe_get_filetype(bufnr)
6264
return ""
6365
end
6466

65-
local success, filetype = pcall(vim.api.nvim_get_option_value, "filetype", { buf = bufnr })
67+
local success, filetype = pcall(api.nvim_get_option_value, "filetype", { buf = bufnr })
6668
return success and filetype or ""
6769
end
6870

@@ -73,7 +75,7 @@ function Utils.safe_get_buffer_name(bufnr)
7375
return ""
7476
end
7577

76-
local success, name = pcall(vim.api.nvim_buf_get_name, bufnr)
78+
local success, name = pcall(api.nvim_buf_get_name, bufnr)
7779
return success and name or ""
7880
end
7981

@@ -87,7 +89,7 @@ function Utils.safe_get_lines(bufnr, start_row, end_row, strict_indexing)
8789
return {}
8890
end
8991

90-
local success, lines = pcall(vim.api.nvim_buf_get_lines, bufnr, start_row, end_row, strict_indexing or false)
92+
local success, lines = pcall(api.nvim_buf_get_lines, bufnr, start_row, end_row, strict_indexing or false)
9193
return success and lines or {}
9294
end
9395

@@ -105,7 +107,7 @@ end
105107
---@param callback function Callback function called with success boolean
106108
function Utils.async_set_cursor(line, col, callback)
107109
vim.schedule(function()
108-
local success = pcall(vim.api.nvim_win_set_cursor, 0, { line, col })
110+
local success = pcall(api.nvim_win_set_cursor, 0, { line, col })
109111
if success then
110112
pcall(vim.cmd, "normal! zz")
111113
end

lua/codecompanion/strategies/chat/tools/catalog/next_edit_suggestion.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local log = require("codecompanion.utils.log")
22

3+
local api = vim.api
4+
35
---@class CodeCompanion.Tool.NextEditSuggestion.Args
46
---@field filepath string
57
---@field line integer
@@ -78,19 +80,19 @@ When you suggest a change to the codebase, you may call this tool to jump to the
7880
---@type jump_action
7981
self.tool.opts.jump_action = function(path)
8082
vim.cmd(action_command .. " " .. path)
81-
return vim.api.nvim_get_current_win()
83+
return api.nvim_get_current_win()
8284
end
8385
end
8486
local winnr = self.tool.opts.jump_action(args.filepath)
8587
if args.line >= 0 and winnr then
86-
local ok = pcall(vim.api.nvim_win_set_cursor, winnr, { args.line + 1, 0 })
88+
local ok = pcall(api.nvim_win_set_cursor, winnr, { args.line + 1, 0 })
8789
if not ok then
88-
local bufnr = vim.api.nvim_win_get_buf(winnr)
90+
local bufnr = api.nvim_win_get_buf(winnr)
8991
return {
9092
status = "error",
9193
data = string.format(
9294
"The jump to the file was successful, but This file only has %d lines. Unable to jump to line %d",
93-
vim.api.nvim_buf_line_count(bufnr),
95+
api.nvim_buf_line_count(bufnr),
9496
args.line
9597
),
9698
}

lua/codecompanion/strategies/chat/ui/builder.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ local Reasoning = require("codecompanion.strategies.chat.ui.formatters.reasoning
44
local Standard = require("codecompanion.strategies.chat.ui.formatters.standard")
55
local Tools = require("codecompanion.strategies.chat.ui.formatters.tools")
66

7+
local api = vim.api
8+
79
---@class CodeCompanion.Chat.UI.Builder
810
---@field chat CodeCompanion.Chat
911
---@field state table
@@ -188,8 +190,8 @@ function Builder:_write_to_buffer(lines, opts, fold_info, state)
188190
last_column = 0
189191
end
190192

191-
local cursor_moved = vim.api.nvim_win_get_cursor(0)[1] == line_count
192-
vim.api.nvim_buf_set_text(self.chat.bufnr, last_line, last_column, last_line, last_column, lines)
193+
local cursor_moved = api.nvim_win_get_cursor(0)[1] == line_count
194+
api.nvim_buf_set_text(self.chat.bufnr, last_line, last_column, last_line, last_column, lines)
193195

194196
-- Handle folding
195197
if fold_info and opts.type == self.chat.MESSAGE_TYPES.TOOL_MESSAGE and #lines > 1 then

lua/codecompanion/strategies/chat/ui/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ function UI:add_line_break()
505505
local _, _, line_count = self:last()
506506

507507
self:unlock_buf()
508-
vim.api.nvim_buf_set_lines(self.chat_bufnr, line_count, line_count, false, { "" })
508+
api.nvim_buf_set_lines(self.chat_bufnr, line_count, line_count, false, { "" })
509509
self:lock_buf()
510510

511511
self:move_cursor(true)

lua/codecompanion/utils/treesitter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function M.goto_node(node, goto_end, avoid_set_jump)
8181

8282
-- Enter visual mode if we are in operator pending mode
8383
-- If we don't do this, it will miss the last character.
84-
local mode = vim.api.nvim_get_mode()
84+
local mode = api.nvim_get_mode()
8585

8686
if mode.mode == "no" then
8787
vim.cmd("normal! v")

0 commit comments

Comments
 (0)