Implement ale.pad in Lua

This commit is contained in:
w0rp
2025-03-22 00:43:29 +00:00
parent a1c57918ef
commit 33a902f489
3 changed files with 30 additions and 1 deletions

View File

@@ -4498,7 +4498,8 @@ ale#Has(feature) *ale#Has()*
`ale#Has('ale-x.y.z')`, such as `ale#Has('ale-2.4.0')`.
ale#Pad(string) *ale#Pad()*
ale.pad(str) *ale.pad()*
ale#Pad(str) *ale#Pad()*
Given a string or any |empty()| value, return either the string prefixed
with a single space, or an empty string. This function can be used to build

View File

@@ -64,6 +64,21 @@ ale.has = function(feature)
return vim.fn["ale#Has"](feature) == 1
end
---Prefix a string with a single space if it is not empty.
---nil will be treated the same as an empty string.
---
---This function is a convenience for chaining options for commands together
---without adding redundant whitespace.
---@param str string|nil A value to pad with whitespace.
---@return string padded A value padded with whitespace.
ale.pad = function(str)
if str == nil or str == "" then
return ""
end
return " " .. str
end
---Get an ALE variable for a buffer (first) or globally (second)
---@param buffer number The buffer number to retreive the variable for.
---@param variable_name string The variable to retrieve.

13
test/lua/ale_pad_spec.lua Normal file
View File

@@ -0,0 +1,13 @@
local eq = assert.are.same
local ale = require("ale")
describe("ale.pad", function()
it("should pad non-empty strings", function()
eq(" foo", ale.pad("foo"))
end)
it("should return empty strings for nil or empty strings", function()
eq("", ale.pad(nil))
eq("", ale.pad(""))
end)
end)