-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ex.lsp.null_ls): add a new component to show actual null-ls sources
. .
- Loading branch information
1 parent
8f1ed1f
commit 49e3982
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
local log = require('plenary.log').new({ plugin = 'ex.lsp.null-ls' }) | ||
|
||
-- we should be ready to three possible cases: | ||
-- * when null-ls is not loaded we should load it only on demand; | ||
-- * when null-ls is not installed we should mock it to avoid errors; | ||
-- * when it is installed and loaded we should it use it. | ||
local null_ls = setmetatable({}, { | ||
__index = function(self, key) | ||
-- attempt to lazy load null-ls plugin | ||
if rawget(self, 'is_installed') == nil then | ||
local is_installed, null_ls = pcall(require, 'null-ls') | ||
rawset(self, 'is_installed', is_installed) | ||
rawset(self, 'null_ls', null_ls) | ||
if is_installed then | ||
log.debug('null-ls is installed') | ||
else | ||
log.warn('null-ls is not installed.') | ||
end | ||
end | ||
-- return original plugin if it's installed | ||
if rawget(self, 'is_installed') then | ||
return rawget(self, 'null_ls')[key] | ||
end | ||
-- return mock: | ||
if key == 'get_source' then | ||
return {} | ||
elseif key == 'is_registered' then | ||
return false | ||
else | ||
return nil | ||
end | ||
end, | ||
}) | ||
|
||
local NullLS = require('lualine.ex.component'):extend({ | ||
icon = '', | ||
query = function() | ||
return { filetype = vim.bo.filetype } | ||
end, | ||
component_name = 'ex_lsp_null_ls', | ||
source_names_separator = ',', | ||
is_enabled = function(component) | ||
return null_ls.is_registered(component:get_query()) | ||
end, | ||
}) | ||
|
||
function NullLS:get_query() | ||
if type(self.options.query) == 'function' then | ||
return self.options.query() | ||
else | ||
return self.options.query | ||
end | ||
end | ||
|
||
-- get sources by query, and concatenate their unique names with {source_names_separator} | ||
function NullLS:update_status() | ||
local sources = null_ls.get_source(self:get_query()) | ||
log.fmt_debug( | ||
'For query %s was found sources: %s', | ||
vim.inspect(self.options.query), | ||
vim.inspect(sources) | ||
) | ||
local names_set = {} | ||
local names = {} | ||
for _, source in pairs(sources) do | ||
-- merge similar sources and escape special symbols | ||
if not names_set[source.name] then | ||
names_set[source.name] = true | ||
local escaped_name = string.gsub(source.name, '%%', '%%%%') | ||
table.insert(names, escaped_name) | ||
end | ||
end | ||
return table.concat(names, self.options.source_names_separator) | ||
end | ||
|
||
return NullLS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
local null_ls = require('null-ls') | ||
local l = require('tests.ex.lualine') | ||
local t = require('tests.ex.busted') --:ignore_all_tests() | ||
|
||
local eq = assert.are.equal | ||
|
||
local component_name = 'ex.lsp.null_ls' | ||
describe(component_name, function() | ||
null_ls.setup({ | ||
sources = { | ||
null_ls.builtins.completion.spell, | ||
null_ls.builtins.formatting.stylua, | ||
null_ls.builtins.hover.dictionary, | ||
null_ls.builtins.diagnostics.clang_check, | ||
}, | ||
}) | ||
describe('draw method', function() | ||
it('should show the name of any source only once', function() | ||
l.test_matched_component(component_name, function(ctbl) | ||
local expected = { | ||
clang_check = true, | ||
spell = true, | ||
dictionary = true, | ||
stylua = true, | ||
} | ||
for name in string.gmatch(ctbl.value, '%w+') do | ||
assert(expected[name], 'Unexpected name ' .. name) | ||
expected[name] = nil | ||
end | ||
end) | ||
end) | ||
it('by default should return only sources for the current filetypes or for all', function() | ||
vim.bo.filetype = 'lua' | ||
l.test_matched_component(component_name, opts, function(ctbl) | ||
for name in string.gmatch(ctbl.value, '%w+') do | ||
assert(name ~= 'clang_check', 'Unexpected name ' .. name) | ||
end | ||
end) | ||
end) | ||
it('should show names only of sources sutisfied to the query', function() | ||
local opts = { query = { method = null_ls.methods.HOVER } } | ||
l.test_matched_component(component_name, opts, function(ctbl) | ||
eq('dictionary', ctbl.value) | ||
end) | ||
end) | ||
end) | ||
end) |