Skip to content

Commit 43b1ac0

Browse files
authored
refactor(utils): split file operation to utils.files (CopilotC-Nvim#1367)
Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent f4ff91e commit 43b1ac0

File tree

8 files changed

+374
-389
lines changed

8 files changed

+374
-389
lines changed

lua/CopilotChat/client.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ local notify = require('CopilotChat.notify')
5959
local tiktoken = require('CopilotChat.tiktoken')
6060
local utils = require('CopilotChat.utils')
6161
local class = require('CopilotChat.utils.class')
62+
local files = require('CopilotChat.utils.files')
6263
local orderedmap = require('CopilotChat.utils.orderedmap')
6364
local stringbuffer = require('CopilotChat.utils.stringbuffer')
6465

@@ -97,7 +98,7 @@ local function generate_resource_block(content, mimetype, name, path, start_line
9798
end
9899

99100
local updated_content = table.concat(lines, '\n')
100-
local filetype = utils.mimetype_to_filetype(mimetype or 'text')
101+
local filetype = files.mimetype_to_filetype(mimetype or 'text')
101102
if not start_line then
102103
start_line = 1
103104
end

lua/CopilotChat/config/functions.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local resources = require('CopilotChat.resources')
22
local utils = require('CopilotChat.utils')
3+
local files = require('CopilotChat.utils.files')
34

45
---@class CopilotChat.config.functions.Function
56
---@field description string?
@@ -23,7 +24,7 @@ return {
2324
type = 'string',
2425
description = 'Path to file to include in chat context.',
2526
enum = function(source)
26-
return utils.glob(source.cwd(), {
27+
return files.glob(source.cwd(), {
2728
max_count = 0,
2829
})
2930
end,
@@ -67,15 +68,15 @@ return {
6768
},
6869

6970
resolve = function(input, source)
70-
local files = utils.glob(source.cwd(), {
71+
local out = files.glob(source.cwd(), {
7172
pattern = input.pattern,
7273
})
7374

7475
return {
7576
{
7677
uri = 'files://glob/' .. input.pattern,
7778
mimetype = 'text/plain',
78-
data = table.concat(files, '\n'),
79+
data = table.concat(out, '\n'),
7980
},
8081
}
8182
end,
@@ -98,15 +99,15 @@ return {
9899
},
99100

100101
resolve = function(input, source)
101-
local files = utils.grep(source.cwd(), {
102+
local out = files.grep(source.cwd(), {
102103
pattern = input.pattern,
103104
})
104105

105106
return {
106107
{
107108
uri = 'files://grep/' .. input.pattern,
108109
mimetype = 'text/plain',
109-
data = table.concat(files, '\n'),
110+
data = table.concat(out, '\n'),
110111
},
111112
}
112113
end,
@@ -230,7 +231,7 @@ return {
230231
{
231232
uri = 'neovim://selection',
232233
name = selection.filename,
233-
mimetype = utils.mimetype_to_filetype(selection.filetype),
234+
mimetype = files.mimetype_to_filetype(selection.filetype),
234235
data = selection.content,
235236
annotations = {
236237
start_line = selection.start_line,

lua/CopilotChat/config/mappings.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local client = require('CopilotChat.client')
44
local constants = require('CopilotChat.constants')
55
local select = require('CopilotChat.select')
66
local utils = require('CopilotChat.utils')
7+
local files = require('CopilotChat.utils.files')
78

89
---@class CopilotChat.config.mappings.Diff
910
---@field change string
@@ -45,16 +46,16 @@ local function get_diff(bufnr, block)
4546

4647
-- If we have header info, use it as source of truth
4748
if header.start_line and header.end_line then
48-
filename = utils.uri_to_filename(header.filename)
49-
filetype = header.filetype or utils.filetype(filename)
49+
filename = files.uri_to_filename(header.filename)
50+
filetype = header.filetype or files.filetype(filename)
5051
start_line = header.start_line
5152
end_line = header.end_line
5253

5354
-- Try to find matching buffer and window
5455
bufnr = nil
5556
for _, win in ipairs(vim.api.nvim_list_wins()) do
5657
local win_buf = vim.api.nvim_win_get_buf(win)
57-
if utils.filename_same(vim.api.nvim_buf_get_name(win_buf), header.filename) then
58+
if files.filename_same(vim.api.nvim_buf_get_name(win_buf), header.filename) then
5859
bufnr = win_buf
5960
break
6061
end
@@ -99,7 +100,7 @@ local function prepare_diff_buffer(diff, source)
99100
if not diff_bufnr then
100101
-- Try to find matching buffer first
101102
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
102-
if utils.filename_same(vim.api.nvim_buf_get_name(buf), diff.filename) then
103+
if files.filename_same(vim.api.nvim_buf_get_name(buf), diff.filename) then
103104
diff_bufnr = buf
104105
break
105106
end
@@ -534,7 +535,7 @@ return {
534535
end
535536

536537
table.insert(lines, header)
537-
table.insert(lines, '```' .. utils.mimetype_to_filetype(resource.mimetype))
538+
table.insert(lines, '```' .. files.mimetype_to_filetype(resource.mimetype))
538539
for _, line in ipairs(preview) do
539540
table.insert(lines, line)
540541
end

lua/CopilotChat/config/providers.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
local plenary_utils = require('plenary.async.util')
12
local constants = require('CopilotChat.constants')
23
local notify = require('CopilotChat.notify')
34
local utils = require('CopilotChat.utils')
4-
local plenary_utils = require('plenary.async.util')
5+
local files = require('CopilotChat.utils.files')
56

67
local EDITOR_VERSION = 'Neovim/' .. vim.version().major .. '.' .. vim.version().minor .. '.' .. vim.version().patch
78

@@ -14,7 +15,7 @@ local function load_tokens()
1415

1516
local config_path = vim.fs.normalize(vim.fn.stdpath('data') .. '/copilot_chat')
1617
local cache_file = config_path .. '/tokens.json'
17-
local file = utils.read_file(cache_file)
18+
local file = files.read_file(cache_file)
1819
if file then
1920
token_cache = vim.json.decode(file)
2021
else
@@ -42,7 +43,7 @@ local function set_token(tag, token, save)
4243
local tokens = load_tokens()
4344
tokens[tag] = token
4445
local config_path = vim.fs.normalize(vim.fn.stdpath('data') .. '/copilot_chat')
45-
utils.write_file(config_path .. '/tokens.json', vim.json.encode(tokens))
46+
files.write_file(config_path .. '/tokens.json', vim.json.encode(tokens))
4647
return token
4748
end
4849

@@ -141,7 +142,7 @@ local function get_github_copilot_token(tag)
141142
}
142143

143144
for _, file_path in ipairs(file_paths) do
144-
local file_data = utils.read_file(file_path)
145+
local file_data = files.read_file(file_path)
145146
if file_data then
146147
local parsed_data = utils.json_decode(file_data)
147148
if parsed_data then

lua/CopilotChat/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local notify = require('CopilotChat.notify')
77
local select = require('CopilotChat.select')
88
local utils = require('CopilotChat.utils')
99
local orderedmap = require('CopilotChat.utils.orderedmap')
10+
local files = require('CopilotChat.utils.files')
1011

1112
local WORD = '([^%s:]+)'
1213
local WORD_NO_INPUT = '([^%s]+)'
@@ -435,7 +436,7 @@ function M.resolve_functions(prompt, config)
435436
table.insert(state.sticky, content_out)
436437
end
437438
else
438-
content_out = string.format(BLOCK_OUTPUT_FORMAT, utils.mimetype_to_filetype(content.mimetype), content.data)
439+
content_out = string.format(BLOCK_OUTPUT_FORMAT, files.mimetype_to_filetype(content.mimetype), content.data)
439440
end
440441

441442
if not utils.empty(result) then

lua/CopilotChat/resources.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local async = require('plenary.async')
22
local utils = require('CopilotChat.utils')
3+
local files = require('CopilotChat.utils.files')
34
local file_cache = {}
45
local url_cache = {}
56

@@ -9,18 +10,19 @@ local M = {}
910
---@param filename string
1011
---@return string?, string?
1112
function M.get_file(filename)
12-
local filetype = utils.filetype(filename)
13+
local filetype = files.filetype(filename)
1314
if not filetype then
1415
return nil
1516
end
16-
local modified = utils.file_mtime(filename)
17-
if not modified then
17+
local err, stat = async.uv.fs_stat(filename)
18+
if err or not stat then
1819
return nil
1920
end
21+
local modified = stat.mtime.sec
2022

2123
local data = file_cache[filename]
2224
if not data or data._modified < modified then
23-
local content = utils.read_file(filename)
25+
local content = files.read_file(filename)
2426
if not content or content == '' then
2527
return nil
2628
end
@@ -31,7 +33,7 @@ function M.get_file(filename)
3133
file_cache[filename] = data
3234
end
3335

34-
return data.content, utils.filetype_to_mimetype(filetype)
36+
return data.content, files.filetype_to_mimetype(filetype)
3537
end
3638

3739
--- Get data for a buffer
@@ -47,7 +49,7 @@ function M.get_buffer(bufnr)
4749
return nil
4850
end
4951

50-
return table.concat(content, '\n'), utils.filetype_to_mimetype(vim.bo[bufnr].filetype)
52+
return table.concat(content, '\n'), files.filetype_to_mimetype(vim.bo[bufnr].filetype)
5153
end
5254

5355
--- Get the content of an URL
@@ -58,7 +60,7 @@ function M.get_url(url)
5860
return nil
5961
end
6062

61-
local ft = utils.filetype(url)
63+
local ft = files.filetype(url)
6264
local content = url_cache[url]
6365
if not content then
6466
local ok, out = async.util.apcall(utils.system, { 'lynx', '-dump', url })
@@ -96,7 +98,7 @@ function M.get_url(url)
9698
url_cache[url] = content
9799
end
98100

99-
return content, utils.filetype_to_mimetype(ft)
101+
return content, files.filetype_to_mimetype(ft)
100102
end
101103

102104
return M

0 commit comments

Comments
 (0)