Skip to content

Commit

Permalink
Refactor Preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jun 9, 2024
1 parent 92401f2 commit 8503131
Show file tree
Hide file tree
Showing 29 changed files with 810 additions and 549 deletions.
27 changes: 17 additions & 10 deletions lua/fittencode/actions/identify_programming_language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ local Log = require('fittencode.log')

local M = {}

local DEFER = 1000
local DEFER = 2000

-- milliseconds
local IPL_DEBOUNCE_TIME = 500
local IPL_DEBOUNCE_TIME = 1000

---@type uv_timer_t
---@type uv_timer_t?
local ipl_timer = nil

local function _identify_current_buffer()
Expand Down Expand Up @@ -46,19 +46,26 @@ local function _identify_current_buffer()
API.identify_programming_language({
headless = true,
content = content,
preprocess_format = {
condense_blank_line = {
convert_whitespace_to_blank = true,
},
trim_trailing_whitespace = true,
filter = {
count = 1,
exclude_markdown_code_blocks_marker = true,
remove_blank_lines = true,
}
},
on_success = function(suggestions)
if not suggestions or #suggestions == 0 then
return
end
local lang = suggestions[1]
if #lang == 0 then
if #lang > 10 then
return
end
lang = lang:lower()
lang = lang:gsub('^%s*(.-)%s*$', '%1')
if #lang == 0 then
return
end
lang = lang:gsub('c%+%+', 'cpp')
lang = lang:match('^(%w+)')
api.nvim_set_option_value('filetype', lang, {
Expand All @@ -70,7 +77,7 @@ local function _identify_current_buffer()
end

local function _ipl_wrap()
Base.debounce(ipl_timer, function()
ipl_timer = Base.debounce(ipl_timer, function()
_identify_current_buffer()
end, IPL_DEBOUNCE_TIME)
end
Expand All @@ -79,7 +86,7 @@ local function register_identify_current_buffer()
api.nvim_create_autocmd({ 'TextChangedI', 'BufReadPost' }, {
group = Base.augroup('Actions', 'IdentifyProgrammingLanguage'),
pattern = '*',
callback = function(params)
callback = function()
if not API.ready_for_generate() then
vim.defer_fn(function()
_ipl_wrap()
Expand Down
21 changes: 11 additions & 10 deletions lua/fittencode/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,18 @@ function M.get_cursor(window)
end

-- Debounce a function call.
-- * If a timer is already running, reset it.
-- * If a timer is not running, start a new timer with the given `wait` time.
-- * When the timer expires, call the `callback` function.
-- * If an error occurs, call `on_error` with the error message.
---@param timer uv_timer_t|nil
---@param timer? uv_timer_t
---@param callback function
---@param wait integer
---@param on_error function|nil
---@param on_error? function
function M.debounce(timer, callback, wait, on_error)
if type(wait) ~= 'number' or wait < 0 then
return
elseif wait == 0 then
callback()
return
end
local function destroy_timer()
local _destroy_timer = function()
if timer then
if timer:has_ref() then
timer:stop()
Expand All @@ -99,7 +95,7 @@ function M.debounce(timer, callback, wait, on_error)
timer = nil
end
end
if not timer then
local _create_timer = function()
timer = uv.new_timer()
if timer == nil then
if on_error then
Expand All @@ -111,13 +107,18 @@ function M.debounce(timer, callback, wait, on_error)
wait,
0,
vim.schedule_wrap(function()
destroy_timer()
_destroy_timer()
callback()
end)
)
end
if not timer then
_create_timer()
else
timer:again()
_destroy_timer()
_create_timer()
end
return timer
end

local function sysname()
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function M.setup()
table.remove(actions, 1)
return cmd(unpack(actions))
end
Log.debug('Invalid command fargs: {}', line.fargs)
-- Log.debug('Invalid command fargs: {}', line.fargs)
end, {
complete = function(_, line)
local args = vim.split(vim.trim(line), '%s+')
Expand Down
Loading

0 comments on commit 8503131

Please sign in to comment.