fix: get filetype (#1258)
This commit is contained in:
parent
3a7277386f
commit
f8636315a5
@ -368,9 +368,9 @@ end
|
|||||||
function FileSelector:get_selected_files_contents()
|
function FileSelector:get_selected_files_contents()
|
||||||
local contents = {}
|
local contents = {}
|
||||||
for _, file_path in ipairs(self.selected_filepaths) do
|
for _, file_path in ipairs(self.selected_filepaths) do
|
||||||
local lines, filetype, error = Utils.read_file_from_buf_or_disk(file_path)
|
local lines, error = Utils.read_file_from_buf_or_disk(file_path)
|
||||||
lines = lines or {}
|
lines = lines or {}
|
||||||
filetype = filetype or "unknown"
|
local filetype = Utils.get_filetype(file_path)
|
||||||
if error ~= nil then
|
if error ~= nil then
|
||||||
Utils.error("error reading file: " .. error)
|
Utils.error("error reading file: " .. error)
|
||||||
else
|
else
|
||||||
|
@ -29,21 +29,10 @@ end
|
|||||||
function RepoMap.setup() vim.defer_fn(RepoMap._init_repo_map_lib, 1000) end
|
function RepoMap.setup() vim.defer_fn(RepoMap._init_repo_map_lib, 1000) end
|
||||||
|
|
||||||
function RepoMap.get_ts_lang(filepath)
|
function RepoMap.get_ts_lang(filepath)
|
||||||
local filetype = RepoMap.get_filetype(filepath)
|
local filetype = Utils.get_filetype(filepath)
|
||||||
return filetype_map[filetype] or filetype
|
return filetype_map[filetype] or filetype
|
||||||
end
|
end
|
||||||
|
|
||||||
function RepoMap.get_filetype(filepath)
|
|
||||||
-- Some files are sometimes not detected correctly when buffer is not included
|
|
||||||
-- https://github.com/neovim/neovim/issues/27265
|
|
||||||
|
|
||||||
local buf = vim.api.nvim_create_buf(false, true)
|
|
||||||
local filetype = vim.filetype.match({ filename = filepath, buf = buf })
|
|
||||||
vim.api.nvim_buf_delete(buf, { force = true })
|
|
||||||
|
|
||||||
return filetype
|
|
||||||
end
|
|
||||||
|
|
||||||
function RepoMap._build_repo_map(project_root, file_ext)
|
function RepoMap._build_repo_map(project_root, file_ext)
|
||||||
local output = {}
|
local output = {}
|
||||||
local gitignore_path = project_root .. "/.gitignore"
|
local gitignore_path = project_root .. "/.gitignore"
|
||||||
@ -70,7 +59,7 @@ function RepoMap._build_repo_map(project_root, file_ext)
|
|||||||
if definitions == "" then return end
|
if definitions == "" then return end
|
||||||
table.insert(output, {
|
table.insert(output, {
|
||||||
path = Utils.relative_path(filepath),
|
path = Utils.relative_path(filepath),
|
||||||
lang = RepoMap.get_filetype(filepath),
|
lang = Utils.get_filetype(filepath),
|
||||||
defs = definitions,
|
defs = definitions,
|
||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
@ -142,7 +131,7 @@ function RepoMap._get_repo_map(file_ext)
|
|||||||
if not found then
|
if not found then
|
||||||
table.insert(repo_map, {
|
table.insert(repo_map, {
|
||||||
path = Utils.relative_path(abs_filepath),
|
path = Utils.relative_path(abs_filepath),
|
||||||
lang = RepoMap.get_filetype(abs_filepath),
|
lang = Utils.get_filetype(abs_filepath),
|
||||||
defs = definitions,
|
defs = definitions,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -337,8 +337,7 @@ local function transform_result_content(selected_files, result_content, prev_fil
|
|||||||
-- can happen if the llm tries to edit or create a file outside of it's context.
|
-- can happen if the llm tries to edit or create a file outside of it's context.
|
||||||
if not match_filetype then
|
if not match_filetype then
|
||||||
local snippet_file_path = current_filepath or prev_filepath
|
local snippet_file_path = current_filepath or prev_filepath
|
||||||
local snippet_file_type = vim.filetype.match({ filename = snippet_file_path }) or "unknown"
|
match_filetype = Utils.get_filetype(snippet_file_path)
|
||||||
match_filetype = snippet_file_type
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local search_start_tag_idx_in_transformed_lines = 0
|
local search_start_tag_idx_in_transformed_lines = 0
|
||||||
|
@ -890,9 +890,19 @@ function M.is_same_file(filepath_a, filepath_b) return M.uniform_path(filepath_a
|
|||||||
|
|
||||||
function M.trim_think_content(content) return content:gsub("^<think>.-</think>", "", 1) end
|
function M.trim_think_content(content) return content:gsub("^<think>.-</think>", "", 1) end
|
||||||
|
|
||||||
|
function M.get_filetype(filepath)
|
||||||
|
-- Some files are sometimes not detected correctly when buffer is not included
|
||||||
|
-- https://github.com/neovim/neovim/issues/27265
|
||||||
|
|
||||||
|
local buf = vim.api.nvim_create_buf(false, true)
|
||||||
|
local filetype = vim.filetype.match({ filename = filepath, buf = buf })
|
||||||
|
vim.api.nvim_buf_delete(buf, { force = true })
|
||||||
|
|
||||||
|
return filetype
|
||||||
|
end
|
||||||
|
|
||||||
---@param file_path string
|
---@param file_path string
|
||||||
---@return string[]|nil lines
|
---@return string[]|nil lines
|
||||||
---@return string|nil file_type
|
|
||||||
---@return string|nil error
|
---@return string|nil error
|
||||||
function M.read_file_from_buf_or_disk(file_path)
|
function M.read_file_from_buf_or_disk(file_path)
|
||||||
--- Lookup if the file is loaded in a buffer
|
--- Lookup if the file is loaded in a buffer
|
||||||
@ -900,8 +910,7 @@ function M.read_file_from_buf_or_disk(file_path)
|
|||||||
if bufnr ~= -1 and vim.api.nvim_buf_is_loaded(bufnr) then
|
if bufnr ~= -1 and vim.api.nvim_buf_is_loaded(bufnr) then
|
||||||
-- If buffer exists and is loaded, get buffer content
|
-- If buffer exists and is loaded, get buffer content
|
||||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||||
local file_type = vim.api.nvim_get_option_value("filetype", { buf = bufnr })
|
return lines, nil
|
||||||
return lines, file_type, nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Fallback: read file from disk
|
-- Fallback: read file from disk
|
||||||
@ -909,12 +918,10 @@ function M.read_file_from_buf_or_disk(file_path)
|
|||||||
if file then
|
if file then
|
||||||
local content = file:read("*all")
|
local content = file:read("*all")
|
||||||
file:close()
|
file:close()
|
||||||
-- Detect the file type using the specific file's content
|
return vim.split(content, "\n"), nil
|
||||||
local file_type = vim.filetype.match({ filename = file_path, contents = { content } }) or "unknown"
|
|
||||||
return vim.split(content, "\n"), file_type, nil
|
|
||||||
else
|
else
|
||||||
-- M.error("failed to open file: " .. file_path .. " with error: " .. open_err)
|
-- M.error("failed to open file: " .. file_path .. " with error: " .. open_err)
|
||||||
return {}, nil, open_err
|
return {}, open_err
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user