Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ex.lsp.null_ls to ex.lsp.none_ls #32

Merged
merged 3 commits into from
Jun 8, 2024
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ jobs:
uses: JohnnyMorganz/stylua-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: 0.19.0
version: 0.20.0
args: --check .

test:
name: Run tests
runs-on: ubuntu-latest
strategy:
matrix:
neovim_version: ['v0.8.0', 'nightly']
neovim_version: ['v0.8.0', 'v0.10.0', 'nightly']

steps:
- uses: actions/checkout@v3
Expand Down
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
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
10 changes: 7 additions & 3 deletions lua/lualine/ex/component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ local log = require('plenary.log').new({ plugin = 'ex.component' })
---@field default_options table
---@field options ExComponentOptions

local Ex = require('lualine.component'):extend()
local LC = require('lualine.component')
-- extends all methods from the `lualine.component`:
local Ex = LC:extend()

-- override the `extend` method to be able to receive the default options:
function Ex:extend(default_options)
local cls = self.super.extend(self)
cls.default_options = ex.merge(vim.deepcopy(default_options or {}), {
local cls = LC.extend(self)
local parent_default_options = ex.merge(vim.deepcopy(self.default_options or {}), {
disabled_color = { fg = 'grey' },
disabled_icon_color = { fg = 'grey' },
draw_empty = true,
})
cls.default_options = vim.tbl_extend('force', parent_default_options, default_options or {})
return cls
end

Expand Down
2 changes: 2 additions & 0 deletions lua/lualine/ex/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ end

---@type fun(dest: table, source: table): table
--- Puts all absent key-value pairs from the {source} to the {dest}.
---@param dest table a table to which data should be added.
---@param source table a table from which a data should be copied.
---@return table dest with added pairs.
M.merge = function(dest, source, already_visited)
vim.validate({ dest = { dest, 'table' }, source = { source, 'table' } })
Expand Down
53 changes: 53 additions & 0 deletions tests/component_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,52 @@ local same = assert.are.same
local l = require('tests.ex.lualine')
local t = require('tests.ex.busted') --:ignore_all_tests()

describe('ex.component', function()
it('should have the same methods as lualine.component', function()
local Test = require('lualine.ex.component'):extend()
-- check only few:
assert(type(Test.update_status) == 'function')
assert(type(Test.draw) == 'function')
end)
it('child should have the same methods as ex.component', function()
local Test = require('lualine.ex.component'):extend()
-- check only few:
assert(type(Test.pre_init) == 'function')
assert(type(Test.post_init) == 'function')
end)
it('child`s default options should include parent`s', function()
-- given:
local Test = require('lualine.ex.component'):extend({ parent_opt = true })
-- when:
local Child = Test:extend({ child_opt = true })
-- then:
local clue = vim.inspect(Child.default_options)
assert(Child.default_options.parent_opt, clue)
assert(Child.default_options.child_opt, clue)
end)
it('child`s default options should override parent`s', function()
-- given:
local Test = require('lualine.ex.component'):extend({ opt = false })
-- when:
local Child = Test:extend({ opt = true })
-- then:
local clue = vim.inspect(Child.default_options)
assert(Child.default_options.opt, clue)
end)
it('instance of the child should have the same methods as a parent', function()
-- given:
local Test = require('lualine.ex.component'):extend()
function Test:test()
return true
end

local Child = Test:extend()
-- when:
local child = Child:new(u.opts({ icon = { align = 'right' } }))
-- then:
assert(type(child.test) == 'function')
end)
end)
describe('A child of the ex.component', function()
it('should have the passed default options as a property', function()
-- given:
Expand Down Expand Up @@ -46,6 +92,7 @@ describe('A child of the ex.component', function()
function Ex:post_init()
passed_opts = self.options
end

-- when:
Ex(init_opts)
-- then:
Expand All @@ -67,6 +114,7 @@ describe('A child of the ex.component', function()
function Child:update_status()
return ''
end

local cmp = Child(u.opts())

-- when:
Expand All @@ -88,6 +136,7 @@ describe('A child of the ex.component', function()
function Child:update_status()
return 'some_text'
end

local cmp = Child(u.opts())

-- when:
Expand All @@ -103,9 +152,11 @@ describe('A child of the ex.component', function()
function Child:update_status()
return 'some_text'
end

function Child:is_enabled()
return false
end

local cmp = Child(u.opts())

-- when:
Expand All @@ -130,9 +181,11 @@ describe('A child of the ex.component', function()
function Child:update_status()
return 'some_text'
end

function Child:is_enabled()
return is_enabled
end

local cmp = Child(u.opts())

-- when:
Expand Down
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
Loading