Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

feat: add NullLsToggle command #1188

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lua/null-ls/info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ M.show_window = function()
return info_lines
end

local create_inactive_sources_info = function(ft)
local info_lines = {
"Inactive source(s)",
}

for _, source in ipairs(sources.get({ ft = ft })) do
if source._disabled then
info_lines = vim.list_extend(info_lines, create_source_info(source))
table.insert(highlights, { "Title", "name:.*\\zs" .. source.name .. "\\ze" })
end
end
table.insert(highlights, { "Type", info_lines[1] })
return info_lines
end

local create_supported_methods_info = function(ft)
local supported_methods = sources.get_supported(ft)

Expand Down Expand Up @@ -140,16 +155,19 @@ M.show_window = function()
local methods_info = create_supported_methods_info(filetype)
local sources_info = nil
local logger_info = nil
local inactive_sources_info = nil
if is_attached then
sources_info = create_active_sources_info(filetype)
logger_info = create_logging_info()
end
inactive_sources_info = create_inactive_sources_info(filetype)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both of these should also be under the is_attached condition, right? At the moment the create_logging_info() call is causing an error for me when the client isn't attached.

logger_info = create_logging_info()

-- stylua: ignore
for _, section in pairs({
header,
logger_info,
sources_info,
inactive_sources_info,
methods_info,
}) do
if section then
Expand Down
14 changes: 14 additions & 0 deletions lua/null-ls/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,24 @@ M.setup = function(user_config)
vim.api.nvim_create_user_command("NullLsInfo", function()
require("null-ls.info").show_window()
end, {})

vim.api.nvim_create_user_command("NullLsLog", function()
vim.cmd(string.format("edit %s", require("null-ls.logger"):get_path()))
end, {})

vim.api.nvim_create_user_command("NullLsToggle", function(opts)
M.toggle(opts.args)
end, {
nargs = "?",
complete = function()
local list = {}
for _, source in ipairs(sources.get(vim.bo.filetype)) do
list[#list + 1] = source.name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a possibility of duplicate sources here, e.g. if the user has eslint registered for both formatting and diagnostics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will depend on how source.toggle() should behave, and since it's only eslint, I think it's probably fine to leave it like this?
otherwise, we'd need something similar to how LspStop handles nargs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just pass eslint to source.toggle() it'll toggle all sources matching the given query, which in this case means it'll toggle all sources named eslint. I think this would be the least surprising behavior for users, and there's at least a few other sources that share names, so I think we should deduplicate them.

end
return list
end,
})

local augroup = vim.api.nvim_create_augroup("NullLs", {})
vim.api.nvim_create_autocmd("FileType", {
group = augroup,
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, ft = query.name, query.method, query.id, query.ft

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 = ft == nil and true or matches_filetype(source, ft)
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