From 126bcd93dd214d74a5c63212d1b71f8323a7103d Mon Sep 17 00:00:00 2001 From: Vladimir Popov Date: Sat, 8 Jun 2024 14:11:04 +0400 Subject: [PATCH] refactor(ex.lsp.null_ls)!: Rename ex.lsp.null_ls to ex.lsp.none_ls . . --- Makefile | 4 +- README.md | 6 +- lua/lualine/components/ex/lsp/all.lua | 7 +- lua/lualine/components/ex/lsp/none_ls.lua | 82 +++++++++++++++++++ lua/lualine/components/ex/lsp/null_ls.lua | 77 ++--------------- .../{null_ls_spec.lua => none_ls_spec.lua} | 16 ++-- tests/ex/lualine.lua | 12 ++- 7 files changed, 113 insertions(+), 91 deletions(-) create mode 100644 lua/lualine/components/ex/lsp/none_ls.lua rename tests/components/{null_ls_spec.lua => none_ls_spec.lua} (80%) diff --git a/Makefile b/Makefile index 6be481a..9ee1cce 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/README.md b/README.md index 6da3fdf..9909c2a 100644 --- a/README.md +++ b/README.md @@ -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) ## 📥 Installation @@ -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 @@ -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 diff --git a/lua/lualine/components/ex/lsp/all.lua b/lua/lualine/components/ex/lsp/all.lua index 64a97c4..d0c5b20 100644 --- a/lua/lualine/components/ex/lsp/all.lua +++ b/lua/lualine/components/ex/lsp/all.lua @@ -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() diff --git a/lua/lualine/components/ex/lsp/none_ls.lua b/lua/lualine/components/ex/lsp/none_ls.lua new file mode 100644 index 0000000..392b489 --- /dev/null +++ b/lua/lualine/components/ex/lsp/none_ls.lua @@ -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 diff --git a/lua/lualine/components/ex/lsp/null_ls.lua b/lua/lualine/components/ex/lsp/null_ls.lua index ffba369..aacb992 100644 --- a/lua/lualine/components/ex/lsp/null_ls.lua +++ b/lua/lualine/components/ex/lsp/null_ls.lua @@ -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 diff --git a/tests/components/null_ls_spec.lua b/tests/components/none_ls_spec.lua similarity index 80% rename from tests/components/null_ls_spec.lua rename to tests/components/none_ls_spec.lua index 4111a1e..3ffe3bb 100644 --- a/tests/components/null_ls_spec.lua +++ b/tests/components/none_ls_spec.lua @@ -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() @@ -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) diff --git a/tests/ex/lualine.lua b/tests/ex/lualine.lua index 363e5cd..8c78cff 100644 --- a/tests/ex/lualine.lua +++ b/tests/ex/lualine.lua @@ -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#'),