Skip to content

Commit 4fa5001

Browse files
committed
chore: lint + annotations
Signed-off-by: blob42 <[email protected]>
1 parent 3db7e28 commit 4fa5001

File tree

9 files changed

+113
-64
lines changed

9 files changed

+113
-64
lines changed

lua/codegpt.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function M.get_status(...)
1616
return Commands.get_status(...)
1717
end
1818

19+
---@param opts vim.api.keyset.create_user_command.command_args
1920
function M.run_cmd(opts)
2021
if opts.name and opts.name:match("^V") then
2122
Config.popup_override = "vertical"
@@ -30,7 +31,7 @@ function M.run_cmd(opts)
3031
Config.persistent_override = false
3132
end
3233

33-
local text_selection, bounds = Utils.get_selected_lines(opts)
34+
local text_selection, range = Utils.get_selected_lines(opts)
3435
local command_args = table.concat(opts.fargs, " ")
3536

3637
local command = opts.fargs[1]
@@ -61,7 +62,7 @@ function M.run_cmd(opts)
6162
return
6263
end
6364

64-
Commands.run_cmd(command, command_args, text_selection, bounds)
65+
Commands.run_cmd(command, command_args, text_selection, range)
6566
end
6667

6768
M.setup = Config.setup

lua/codegpt/api.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local function start_spinner_timer()
1919
)
2020
end
2121

22+
---@return string
2223
function M.get_status(...)
2324
local spinners = Config.opts.ui.spinners or { "", "", "", "", "", "" }
2425
local spinner_speed = Config.opts.ui.spinner_speed or 80

lua/codegpt/commands.lua

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
---@module 'plenary.curl'
2+
13
local Utils = require("codegpt.utils")
24
local Ui = require("codegpt.ui")
35
local Providers = require("codegpt.providers")
@@ -7,45 +9,55 @@ local models = require("codegpt.models")
79

810
local M = {}
911

10-
local text_popup_stream = function(job, stream, bufnr, start_row, start_col, end_row, end_col)
12+
---@param job Job
13+
---@param stream string
14+
---@param bufnr integer
15+
---@param range Range4
16+
local text_popup_stream = function(job, stream, bufnr, range)
1117
local popup_filetype = Config.opts.ui.text_popup_filetype
12-
Ui.popup_stream(job, stream, popup_filetype, bufnr, start_row, start_col, end_row, end_col)
18+
Ui.popup_stream(job, stream, popup_filetype, bufnr, range)
1319
end
1420

15-
local function replacement(lines, bufnr, start_row, start_col, end_row, end_col)
21+
---@param job Job
22+
---@param lines string[]
23+
---@param bufnr integer
24+
---@param range Range4
25+
local function replacement(job, lines, bufnr, range)
26+
local start_row, _, end_row, _ = unpack(range)
1627
lines = Utils.strip_reasoning(lines, "<think>", "</think>")
1728
lines = Utils.trim_to_code_block(lines)
1829
lines = Utils.remove_trailing_whitespace(lines)
1930
Utils.fix_indentation(bufnr, start_row, end_row, lines)
2031
-- if the buffer is not valid, open a popup. This can happen when the user closes the previous popup window before the request is finished.
2132
if vim.api.nvim_buf_is_valid(bufnr) ~= true then
22-
Ui.popup(job, lines, Utils.get_filetype(), bufnr, start_row, start_col, end_row, end_col)
33+
Ui.popup(job, lines, Utils.get_filetype(), bufnr, range)
2334
else
2435
return lines
2536
end
2637
end
2738

2839
M.CallbackTypes = {
2940
["text_popup_stream"] = text_popup_stream,
30-
["text_popup"] = function(job, lines, bufnr, start_row, start_col, end_row, end_col)
41+
["text_popup"] = function(job, lines, bufnr, range)
3142
local popup_filetype = Config.opts.ui.text_popup_filetype
32-
Ui.popup(job, lines, popup_filetype, bufnr, start_row, start_col, end_row, end_col)
43+
Ui.popup(job, lines, popup_filetype, bufnr, range)
3344
end,
34-
["code_popup"] = function(job, lines, bufnr, start_row, start_col, end_row, end_col)
45+
["code_popup"] = function(job, lines, bufnr, range)
46+
local start_row, _, end_row, _ = unpack(range)
3547
lines = Utils.trim_to_code_block(lines)
3648
Utils.fix_indentation(bufnr, start_row, end_row, lines)
37-
Ui.popup(job, lines, Utils.get_filetype(), bufnr, start_row, start_col, end_row, end_col)
49+
Ui.popup(job, lines, Utils.get_filetype(), bufnr, range)
3850
end,
39-
["replace_lines"] = function(job, lines, bufnr, start_row, start_col, end_row, end_col)
40-
lines = replacement(lines, bufnr, start_row, start_col, end_row, end_col)
41-
Utils.replace_lines(lines, bufnr, start_row, start_col, end_row, end_col)
51+
["replace_lines"] = function(job, lines, bufnr, range)
52+
lines = replacement(job, lines, bufnr, range)
53+
Utils.replace_lines(lines, bufnr, range)
4254
end,
43-
["insert_lines"] = function(job, lines, bufnr, start_row, start_col, end_row, end_col)
44-
lines = replacement(lines, bufnr, start_row, start_col, end_row, end_col)
55+
["insert_lines"] = function(job, lines, bufnr, range)
56+
lines = replacement(job, lines, bufnr, range)
4557
Utils.insert_lines(lines)
4658
end,
47-
["prepend_lines"] = function(job, lines, bufnr, start_row, start_col, end_row, end_col)
48-
lines = replacement(lines, bufnr, start_row, start_col, end_row, end_col)
59+
["prepend_lines"] = function(job, lines, bufnr, range)
60+
lines = replacement(job, lines, bufnr, range)
4961
Utils.prepend_lines(lines)
5062
end,
5163
["custom"] = nil,
@@ -94,8 +106,8 @@ end
94106
---@param command string
95107
---@param command_args string
96108
---@param text_selection string
97-
---@param bounds bounding_box
98-
function M.run_cmd(command, command_args, text_selection, bounds)
109+
---@param range Range4
110+
function M.run_cmd(command, command_args, text_selection, range)
99111
local provider = Providers.get_provider()
100112
local cmd_opts, is_stream = get_cmd_opts(command)
101113
if cmd_opts == nil then
@@ -110,11 +122,11 @@ function M.run_cmd(command, command_args, text_selection, bounds)
110122

111123
if is_stream then
112124
new_callback = function(stream, job)
113-
cmd_opts.callback(job, stream, bufnr, unpack(bounds))
125+
cmd_opts.callback(job, stream, bufnr, range)
114126
end
115127
else
116128
new_callback = function(lines, job) -- called from Provider.handle_response
117-
cmd_opts.callback(job, lines, bufnr, unpack(bounds))
129+
cmd_opts.callback(job, lines, bufnr, range)
118130
end
119131
end
120132

@@ -126,6 +138,7 @@ function M.run_cmd(command, command_args, text_selection, bounds)
126138
end
127139
end
128140

141+
---@return string
129142
function M.get_status(...)
130143
return Api.get_status(...)
131144
end

lua/codegpt/config.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local M = {}
1010
---@field max_tokens? number Custom max_tokens for this command
1111
---@field append_string? string String to append to prompt -- ex: /no_think
1212
---@field model? string Model to always use with this command
13+
---@field [string] any -- merged command parameters
1314

1415
---@type { [string]: codegpt.CommandOpts }
1516
local default_commands = {
@@ -133,7 +134,7 @@ M.persistent_override = nil
133134
---@field popup_options? table nui.nvim popup options
134135
---@field persistent? boolean Do not close popup window on mouse leave. Useful with vertical and horizontal layouts.
135136
---@field actions? table | {custom?: table} -- ui key mappings
136-
---@field text_popup_filetype? string Set the filetype of the text popup
137+
---@field text_popup_filetype string Set the filetype of the text popup
137138
---@field popup_type? "popup" | "vertical" | "horizontal" Set the type of ui to use for the popup
138139
---@field horizontal_popup_size? string Set the height of the horizontal popup
139140
---@field vertical_popup_size? string Set the width of the vertical popup

lua/codegpt/models.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ local Providers = require("codegpt.providers")
33
local M = {}
44

55
function M.get_model_by_name(name)
6-
---@type codegpt.Model
7-
86
local provider_name = vim.fn.tolower(Config.opts.connection.api_provider)
97
local provider_config = Config.opts.models[provider_name]
108

lua/codegpt/providers/ollama.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ local errors = require("codegpt.errors")
88

99
local M = {}
1010

11+
---@param command string
1112
---@param cmd_opts codegpt.CommandOpts
13+
---@param command_args string[]
1214
local function generate_messages(command, cmd_opts, command_args, text_selection)
1315
local system_message =
1416
Render.render(command, cmd_opts.system_message_template, command_args, text_selection, cmd_opts)

lua/codegpt/template_render.lua

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,50 @@ local Utils = require("codegpt.utils")
22

33
local Render = {}
44

5+
---TODO!: add lang->vim_filety pemappings for other common languages
56
local function get_language()
6-
local filetype = Utils.get_filetype()
7-
if filetype == "cpp" then
8-
return "C++"
9-
else
10-
return filetype
11-
end
7+
local filetype = Utils.get_filetype()
8+
if filetype == "cpp" then
9+
return "C++"
10+
else
11+
return filetype
12+
end
1213
end
1314

1415
local function safe_replace(template, key, value)
15-
if value == nil then
16-
return template:gsub(key, "")
17-
end
16+
if value == nil then
17+
return template:gsub(key, "")
18+
end
1819

19-
if type(value) == "table" then
20-
value = table.concat(value, "\n")
21-
end
20+
if type(value) == "table" then
21+
value = table.concat(value, "\n")
22+
end
2223

23-
if value then
24-
-- Replace '%' with '%%' to escape it in the template
25-
value = value:gsub("%%", "%%%%")
26-
end
24+
if value then
25+
-- Replace '%' with '%%' to escape it in the template
26+
value = value:gsub("%%", "%%%%")
27+
end
2728

28-
return template:gsub(key, value)
29+
return template:gsub(key, value)
2930
end
3031

32+
---@param cmd string
33+
---@param template string
34+
---@param command_args string[]
35+
---@param cmd_opts table
3136
function Render.render(cmd, template, command_args, text_selection, cmd_opts)
32-
local language = get_language()
33-
local language_instructions = ""
34-
if cmd_opts.language_instructions ~= nil then
35-
language_instructions = cmd_opts.language_instructions[language]
36-
end
37-
38-
template = safe_replace(template, "{{filetype}}", Utils.get_filetype())
39-
template = safe_replace(template, "{{text_selection}}", text_selection)
40-
template = safe_replace(template, "{{language}}", language)
41-
template = safe_replace(template, "{{command_args}}", command_args)
42-
template = safe_replace(template, "{{language_instructions}}", language_instructions)
43-
return template
37+
local language = get_language()
38+
local language_instructions = ""
39+
if cmd_opts.language_instructions ~= nil then
40+
language_instructions = cmd_opts.language_instructions[language]
41+
end
42+
43+
template = safe_replace(template, "{{filetype}}", Utils.get_filetype())
44+
template = safe_replace(template, "{{text_selection}}", text_selection)
45+
template = safe_replace(template, "{{language}}", language)
46+
template = safe_replace(template, "{{command_args}}", command_args)
47+
template = safe_replace(template, "{{language_instructions}}", language_instructions)
48+
return template
4449
end
4550

4651
return Render

lua/codegpt/ui.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
---@module 'plenary.job'
2+
13
local Popup = require("nui.popup")
24
local Split = require("nui.split")
35
local Config = require("codegpt.config")
@@ -77,7 +79,12 @@ local function create_window()
7779
return ui_elem
7880
end
7981

80-
function M.popup(job, lines, filetype, bufnr, start_row, start_col, end_row, end_col)
82+
---@param job Job
83+
---@param lines string[]
84+
---@param filetype string
85+
---@param range Range4
86+
function M.popup(job, lines, filetype, bufnr, range)
87+
local start_row, start_col, end_row, end_col = unpack(range)
8188
if job ~= nil and job.is_shutdown then
8289
return
8390
end

0 commit comments

Comments
 (0)