Skip to content

Commit

Permalink
refactor(ex.lsp.null_ls)!: Rename ex.lsp.null_ls to ex.lsp.none_ls
Browse files Browse the repository at this point in the history
.

.
  • Loading branch information
vladimir-popov committed Jun 8, 2024
1 parent eaa09f0 commit 126bcd9
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 91 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LUALINE=$(START)/nvim-lspconfig

# Specific for components:
LSPCONFIG=$(START)/lualine.nvim
NULL_LS=$(START)/null-ls.nvim
NONE_LS=$(START)/none-ls.nvim
# ====================

define HELP
Expand Down Expand Up @@ -74,7 +74,7 @@ install:
@[ -d $(DEVICONS) ] || git clone --depth 1 https://github.com/nvim-tree/nvim-web-devicons $(DEVICONS)
@[ -d $(LSPCONFIG) ] || git clone --depth 1 https://github.com/neovim/nvim-lspconfig $(LSPCONFIG)
@[ -d $(LUALINE) ] || git clone --depth 1 https://github.com/nvim-lualine/lualine.nvim $(LUALINE)
@[ -d $(NULL_LS) ] || git clone --depth 1 https://github.com/nvimtools/none-ls.nvim $(NULL_LS)
@[ -d $(NONE_LS) ] || git clone --depth 1 https://github.com/nvimtools/none-ls.nvim $(NONE_LS)
ifdef plugin
@[ -d $(START)/$(notdir $(plugin)) ] || git clone --depth 1 https://github.com/$(plugin) $(START)/$(notdir $(plugin))
endif
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ for `lualine.nvim` with additional components.
- [ex.git.branch](#exgitbranch)
- [ex.lsp.single](#exlspsingle)
- [ex.lsp.all](#exlspall)
- [ex.lsp.null_ls](#exlspnull_ls)
- [ex.lsp.none_ls](#exlspnone_ls)
- [🛠️ Tools](#tools)

## 📥 <a name="installation">Installation</a>
Expand Down Expand Up @@ -500,7 +500,7 @@ on_click = function(clicks, button, modified)
end
```

### ex.lsp.null_ls
### ex.lsp.none_ls

This component shows names of the
[null-ls](https://github.com/nvimtools/none-ls.nvim) sources according to the specified
Expand All @@ -512,7 +512,7 @@ duplicated names are merged.
sections = {
lualine_a = {
{
'ex.lsp.null_ls',
'ex.lsp.none_ls',

-- The table or function that returns the table with the source query.
-- By default it shows only actual sorces. To show all registered sources
Expand Down
7 changes: 4 additions & 3 deletions lua/lualine/components/ex/lsp/all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ end
---@class AllLspComponent: ExComponent
---@field options AllLspOptions
---@field components table
local AllLsp =
require('lualine.ex.component'):extend(vim.tbl_extend('force', SingleLsp.default_options, {
local AllLsp = require('lualine.ex.component'):extend(
vim.tbl_extend('force', SingleLsp.default_options, {
is_enabled = function(component)
return not ex.is_empty(component:__clients())
end,
notify_enabled = true,
notify_hl = 'Comment',
}))
})
)

---@protected
function AllLsp:pre_init()
Expand Down
82 changes: 82 additions & 0 deletions lua/lualine/components/ex/lsp/none_ls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
local log = require('plenary.log').new({ plugin = 'ex.lsp.none-ls' })

-- we should be ready to three possible cases:
-- * when none-ls is not loaded we should load it only on demand;
-- * when none-ls is not installed we should mock it to avoid errors;
-- * when it is installed and loaded we should use it.
local none_ls = setmetatable({}, {
__index = function(self, key)
-- attempt to lazy load none-ls plugin
if rawget(self, 'is_installed') == nil then
-- null-ls is old name of the none-ls plugin,
-- which is still used for back compatibility
local is_installed, none_ls = pcall(require, 'null-ls')
rawset(self, 'is_installed', is_installed)
rawset(self, 'none_ls', none_ls)
if is_installed then
log.debug('none-ls is installed')
else
log.warn('none-ls is not installed.')
end
end
-- return original plugin if it's installed
if rawget(self, 'is_installed') then
return rawget(self, 'none_ls')[key]
end
-- return mock:
if key == 'get_source' then
return function()
return {}
end
elseif key == 'is_registered' then
return function()
return false
end
else
return nil
end
end,
})

local NoneLS = require('lualine.ex.component'):extend({
icon = '',
query = function()
return { filetype = vim.bo.filetype }
end,
component_name = 'ex_lsp_none_ls',
source_names_separator = ',',
is_enabled = function(component)
return none_ls.is_registered(component:get_query())
end,
})

function NoneLS: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 NoneLS:update_status()
local sources = none_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 NoneLS
77 changes: 6 additions & 71 deletions lua/lualine/components/ex/lsp/null_ls.lua
Original file line number Diff line number Diff line change
@@ -1,80 +1,15 @@
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 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('none-ls is installed')
else
log.warn('none-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 function()
return {}
end
elseif key == 'is_registered' then
return function()
return false
end
else
return nil
end
end,
})

local NullLS = require('lualine.ex.component'):extend({
icon = '',
query = function()
return { filetype = vim.bo.filetype }
end,
-- This is a component mock to notify about not back compatibly renaming
-- of the component `ex.lsp.null_ls` to the `ex.lsp.none_ls`
local NullLS = require('lualine.components.ex.lsp.none_ls'):extend({
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)
function NullLS:post_init()
log.warn(
'The `ex.lsp.null_ls` component was renamed to `ex.lsp.none_ls`. Please, use the actual component.'
)
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
local null_ls = require('null-ls')
local none_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'
local component_name = 'ex.lsp.none_ls'
describe(component_name, function()
null_ls.setup({
none_ls.setup({
sources = {
null_ls.builtins.completion.spell,
null_ls.builtins.formatting.stylua,
null_ls.builtins.hover.dictionary,
null_ls.builtins.diagnostics.clang_check,
none_ls.builtins.completion.spell,
none_ls.builtins.formatting.stylua,
none_ls.builtins.hover.dictionary,
none_ls.builtins.diagnostics.clang_check,
},
})
describe('draw method', function()
Expand All @@ -38,7 +38,7 @@ describe(component_name, function()
end)
end)
it('should show names only of sources sutisfied to the query', function()
local opts = { query = { method = null_ls.methods.HOVER } }
local opts = { query = { method = none_ls.methods.HOVER } }
l.test_matched_component(component_name, opts, function(ctbl)
eq('dictionary', ctbl.value)
end)
Expand Down
12 changes: 8 additions & 4 deletions tests/ex/lualine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,20 @@ function M.match_rendered_component(rendered_component, opts)

-- Try to match the short pattern with icon color only
if is_right_icon then
t.value, t.icon_hl, t.icon =
string.match(rendered_component, p_value .. p_padding .. p_hl .. p_icon .. '$')
t.value, t.icon_hl, t.icon = string.match(
rendered_component,
p_value .. p_padding .. p_hl .. p_icon .. '$'
)
t.hl = default_hl
t.icon_color = {
fg = M.get_gui_color(t.icon_hl, 'fg#'),
bg = M.get_gui_color(t.icon_hl, 'bg#'),
}
else
t.icon_hl, t.icon, t.value =
string.match(rendered_component, p_hl .. p_icon .. p_padding .. p_value)
t.icon_hl, t.icon, t.value = string.match(
rendered_component,
p_hl .. p_icon .. p_padding .. p_value
)
t.icon_color = {
fg = M.get_gui_color(t.icon_hl, 'fg#'),
bg = M.get_gui_color(t.icon_hl, 'bg#'),
Expand Down

0 comments on commit 126bcd9

Please sign in to comment.