Implement ale.get_filename_mappings in Lua

This commit is contained in:
w0rp
2025-03-22 00:35:53 +00:00
parent 1aae83497d
commit a1c57918ef
3 changed files with 99 additions and 0 deletions

View File

@@ -4477,6 +4477,7 @@ ale#Escape(str) *ale#Escape()*
In all other cases, ALE will call |shellescape|.
ale.get_filename_mappings(buffer, name) *ale.get_filename_mappings()*
ale#GetFilenameMappings(buffer, name) *ale#GetFilenameMappings()*
Given a `buffer` and the `name` of either a linter for fixer, return a

View File

@@ -114,4 +114,25 @@ ale.env = function(variable_name, value)
return variable_name .. "=" .. ale.escape(value) .. " "
end
---Get an array of arrays for mapping paths to and from filesystems for an ALE
---linter, as configured in the `filename_mappings` setting.
---
---The result can be used to instruct ALE how to map between filesystems.
---@param buffer number The buffer number.
---@param name string The linter name.
---@return table mappings An array of arrays for mapping filenames.
ale.get_filename_mappings = function(buffer, name)
local linter_mappings = ale.var(buffer, "filename_mappings")
if linter_mappings[1] ~= nil then
return linter_mappings
end
if linter_mappings[name] == nil then
name = "*"
end
return linter_mappings[name] or {}
end
return ale

View File

@@ -0,0 +1,77 @@
local eq = assert.are.same
local ale = require("ale")
describe("ale.get_filename_mappings", function()
local buffer_map
setup(function()
_G.vim = {
api = {
nvim_buf_get_var = function(buffer, key)
local buffer_table = buffer_map[buffer] or {}
local value = buffer_table[key]
if value == nil then
error(key .. " is missing")
end
return value
end,
},
g = {
},
}
end)
teardown(function()
_G.vim = nil
end)
before_each(function()
buffer_map = {[42] = {}}
_G.vim.g = {}
end)
it("should return the correct mappings for given linters/fixers", function()
vim.g.ale_filename_mappings = {
a = {{"foo", "bar"}},
b = {{"baz", "foo"}},
}
eq({{"foo", "bar"}}, ale.get_filename_mappings(42, "a"))
eq({{"baz", "foo"}}, ale.get_filename_mappings(42, "b"))
eq({}, ale.get_filename_mappings(42, "c"))
buffer_map[42].ale_filename_mappings = {b = {{"abc", "xyz"}}}
eq({}, ale.get_filename_mappings(42, "a"))
eq({{"abc", "xyz"}}, ale.get_filename_mappings(42, "b"))
eq({}, ale.get_filename_mappings(42, "c"))
end)
it("should return arrays set for use with all tools", function()
vim.g.ale_filename_mappings = {{"foo", "bar"}}
eq({{"foo", "bar"}}, ale.get_filename_mappings(42, "a"))
eq({{"foo", "bar"}}, ale.get_filename_mappings(42, ""))
eq({{"foo", "bar"}}, ale.get_filename_mappings(42, nil))
buffer_map[42].ale_filename_mappings = {{"abc", "xyz"}}
eq({{"abc", "xyz"}}, ale.get_filename_mappings(42, "a"))
eq({{"abc", "xyz"}}, ale.get_filename_mappings(42, ""))
eq({{"abc", "xyz"}}, ale.get_filename_mappings(42, nil))
end)
it("should let you use * as a fallback", function()
vim.g.ale_filename_mappings = {
a = {{"foo", "bar"}},
["*"] = {{"abc", "xyz"}},
}
eq({{"foo", "bar"}}, ale.get_filename_mappings(42, "a"))
eq({{"abc", "xyz"}}, ale.get_filename_mappings(42, "b"))
eq({{"abc", "xyz"}}, ale.get_filename_mappings(42, ""))
eq({{"abc", "xyz"}}, ale.get_filename_mappings(42, nil))
end)
end)