From 26ffb9dfa35f1222a79d461db0b016de6bd5846f Mon Sep 17 00:00:00 2001 From: w0rp Date: Sat, 22 Mar 2025 01:10:11 +0000 Subject: [PATCH] Implement ale.queue for calling ale#Queue in Lua --- doc/ale.txt | 10 ++++++---- lua/ale/init.lua | 15 ++++++++++++++ test/lua/ale_queue_spec.lua | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 test/lua/ale_queue_spec.lua diff --git a/doc/ale.txt b/doc/ale.txt index 4831da79..e2989c39 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -4506,19 +4506,21 @@ ale#Pad(str) *ale#Pad()* parts of a command from variables. -ale#Queue(delay, [linting_flag, buffer_number]) *ale#Queue()* +ale.queue(delay, [linting_flag, buffer]) *ale.queue()* +ale#Queue(delay, [linting_flag, buffer]) *ale#Queue()* Run linters for the current buffer, based on the filetype of the buffer, with a given `delay`. A `delay` of `0` will run the linters immediately. The linters will always be run in the background. Calling this function - again from the same buffer + several times will reset an internal timer so ALE doesn't check buffers too + often. An optional `linting_flag` argument can be given. If `linting_flag` is `'lint_file'`, then linters where the `lint_file` option evaluates to `1` will be run. Otherwise, those linters will not be run. - An optional `buffer_number` argument can be given for specifying the buffer - to check. The active buffer (`bufnr('')`) will be checked by default. + An optional `buffer` argument can be given for specifying the buffer to + check. The active buffer (`bufnr('')`) will be checked by default. *ale-cool-down* If an exception is thrown when queuing/running ALE linters, ALE will enter diff --git a/lua/ale/init.lua b/lua/ale/init.lua index e8631186..a436e78d 100644 --- a/lua/ale/init.lua +++ b/lua/ale/init.lua @@ -55,6 +55,21 @@ ale.setup = setmetatable({ end, }) +---Run ALE linters on a buffer after a delay. +--- +---If a delay in milliseconds multiple times, the internal timer used by ALE +---will be reset, so ALE doesn't lint too often. +--- +---If the `linting_flag` is not 'lint_file' then linters that require files to +---be saved will no be run. +---@param delay number Milliseconds to wait for. A delay of 0 lints immediately. +---@param linting_flag string|nil If set to 'lint_file', run all linters. +---@param buffer number|nil The buffer to check. Defaults to the current buffer. +---@return nil +ale.queue = function(delay, linting_flag, buffer) + vim.fn["ale#Queue"](delay, linting_flag, buffer) +end + ---Check if ALE supports a given feature. --- ---The ALE version can be checked with ale.has("ale-1.0.0"), etc. diff --git a/test/lua/ale_queue_spec.lua b/test/lua/ale_queue_spec.lua new file mode 100644 index 00000000..d9d5191f --- /dev/null +++ b/test/lua/ale_queue_spec.lua @@ -0,0 +1,40 @@ +local eq = assert.are.same +local ale = require("ale") + +describe("ale.queue", function() + local queue_calls + + setup(function() + _G.vim = { + fn = { + ["ale#Queue"] = function(...) + table.insert(queue_calls, {...}) + end, + }, + } + end) + + teardown(function() + _G.vim = nil + end) + + before_each(function() + queue_calls = {} + end) + + it("should call ale#Queue with the right arguments", function() + ale.queue(0) + ale.queue(0, "") + ale.queue(123, "lint_file") + ale.queue(0, "", 42) + ale.queue(123, "lint_file", 42) + + eq({ + {0, nil, nil}, + {0, "", nil}, + {123, "lint_file", nil}, + {0, "", 42}, + {123, "lint_file", 42}, + }, queue_calls) + end) +end)