Skip to content

Commit

Permalink
feat: add filetype filter to get_source
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Mar 23, 2023
1 parent 13dd1fc commit 430d37d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
1 change: 1 addition & 0 deletions doc/SOURCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ structure:
```lua
local query = {
name = "my-source", -- string
filetype = "lua", -- filetype
method = require("null-ls").methods.FORMATTING, -- null-ls method
id = 1, -- number
}
Expand Down
45 changes: 23 additions & 22 deletions lua/null-ls/sources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,35 @@ local registered = {

local M = {}

local matches_filetype = function(source, filetype)
if filetype:find("%.") then
local filetypes = vim.split(filetype, ".", { plain = true })
table.insert(filetypes, filetype)

local is_match = source.filetypes["_all"]
for _, ft in ipairs(filetypes) do
if source.filetypes[ft] == false then
return false
end

is_match = is_match or source.filetypes[ft]
end

return is_match
end

return source.filetypes[filetype] or source.filetypes["_all"] and source.filetypes[filetype] == nil
end

local matches_query = function(source, query)
query = type(query) == "string" and { name = vim.pesc(query) } or query
local name, method, id = query.name, query.method, query.id
local name, method, id, filetype = query.name, query.method, query.id, query.filetype

local name_matches = name == nil and true or source.name:find(name)
local method_matches = method == nil and true or source.methods[method]
local id_matches = id == nil and true or source.id == id
return name_matches and method_matches and id_matches
local filetype_matches = filetype == nil and true or matches_filetype(source, filetype)
return name_matches and method_matches and id_matches and filetype_matches
end

local for_each_matching = function(query, cb)
Expand Down Expand Up @@ -61,26 +82,6 @@ local register_source = function(source)
registered.names[source.name] = true
end

local matches_filetype = function(source, filetype)
if filetype:find("%.") then
local filetypes = vim.split(filetype, ".", { plain = true })
table.insert(filetypes, filetype)

local is_match = source.filetypes["_all"]
for _, ft in ipairs(filetypes) do
if source.filetypes[ft] == false then
return false
end

is_match = is_match or source.filetypes[ft]
end

return is_match
end

return source.filetypes[filetype] or source.filetypes["_all"] and source.filetypes[filetype] == nil
end

local matches_method = function(source, method)
if source.methods[method] then
return true
Expand Down
27 changes: 27 additions & 0 deletions test/spec/sources_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,19 @@ describe("sources", function()
name = "first-mock-source",
methods = { [methods.internal.FORMATTING] = true },
id = 1,
filetypes = { ["lua"] = true },
}
local second_source = {
name = "second-mock-source",
methods = { [methods.internal.DIAGNOSTICS] = true },
id = 2,
filetypes = { ["_all"] = true },
}
local third_source = {
name = "third-mock-source",
methods = { [methods.internal.FORMATTING] = true },
id = 3,
filetypes = { ["_all"] = true, ["lua"] = false },
}
sources._set({ first_source, second_source, third_source })
end)
Expand Down Expand Up @@ -228,6 +231,12 @@ describe("sources", function()

assert.truthy(#matching == 1)
end)

it("should get source matching filetype query", function()
local matching = sources.get({ filetype = "lua" })

assert.truthy(#matching == 2)
end)
end)

describe("complex query", function()
Expand All @@ -249,12 +258,30 @@ describe("sources", function()
assert.truthy(#matching == 1)
end)

it("should get sources matching name and filetype query", function()
local matching = sources.get({ name = "mock", filetype = "lua" })

assert.truthy(#matching == 2)
end)

it("should get source matching method and id query", function()
local matching = sources.get({ method = methods.internal.FORMATTING, id = 1 })

assert.truthy(#matching == 1)
end)

it("should get sources matching method and filetype query", function()
local matching = sources.get({ method = methods.internal.FORMATTING, filetype = "lua" })

assert.truthy(#matching == 1)
end)

it("should get sources matching id and filetype query", function()
local matching = sources.get({ id = 1, filetype = "lua" })

assert.truthy(#matching == 1)
end)

it("should not get any sources when query does not match", function()
local matching = sources.get({ method = methods.internal.FORMATTING, id = 2 })

Expand Down

0 comments on commit 430d37d

Please sign in to comment.