mirror of
https://github.com/dense-analysis/ale.git
synced 2025-12-06 12:44:23 +08:00
Implement ale.get_filename_mappings in Lua
This commit is contained in:
@@ -4477,6 +4477,7 @@ ale#Escape(str) *ale#Escape()*
|
|||||||
In all other cases, ALE will call |shellescape|.
|
In all other cases, ALE will call |shellescape|.
|
||||||
|
|
||||||
|
|
||||||
|
ale.get_filename_mappings(buffer, name) *ale.get_filename_mappings()*
|
||||||
ale#GetFilenameMappings(buffer, name) *ale#GetFilenameMappings()*
|
ale#GetFilenameMappings(buffer, name) *ale#GetFilenameMappings()*
|
||||||
|
|
||||||
Given a `buffer` and the `name` of either a linter for fixer, return a
|
Given a `buffer` and the `name` of either a linter for fixer, return a
|
||||||
|
|||||||
@@ -114,4 +114,25 @@ ale.env = function(variable_name, value)
|
|||||||
return variable_name .. "=" .. ale.escape(value) .. " "
|
return variable_name .. "=" .. ale.escape(value) .. " "
|
||||||
end
|
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
|
return ale
|
||||||
|
|||||||
77
test/lua/ale_get_filename_mappings_spec.lua
Normal file
77
test/lua/ale_get_filename_mappings_spec.lua
Normal 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)
|
||||||
Reference in New Issue
Block a user