Skip to content

Commit 0d03881

Browse files
author
Jose Alvarez
committed
refactor!: remove lspconfig integration
1 parent 532ac9e commit 0d03881

25 files changed

+565
-541
lines changed

.nvim.settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"lua-dev": { "library": { "plugins": ["plenary.nvim", "nvim-lspconfig"] } }
2+
"lua-dev": { "library": { "plugins": ["plenary.nvim"] } }
33
}

lua/null-ls/client.lua

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
local c = require("null-ls.config")
2+
local methods = require("null-ls.methods")
3+
local sources = require("null-ls.sources")
4+
local u = require("null-ls.utils")
5+
6+
local api = vim.api
7+
local client
8+
9+
local should_attach = function(bufnr)
10+
local all_sources = sources.get_all()
11+
-- don't attach if no sources have been registered
12+
if vim.tbl_isempty(all_sources) then
13+
return false
14+
end
15+
16+
-- be paranoid and try to make sure that the buffer represents an actual file
17+
if api.nvim_buf_get_option(bufnr, "buftype") ~= "" or api.nvim_buf_get_name(bufnr) == "" then
18+
return false
19+
end
20+
21+
local ft = api.nvim_buf_get_option(bufnr, "filetype")
22+
for _, source in ipairs(all_sources) do
23+
if sources.is_available(source, ft) then
24+
return true
25+
end
26+
end
27+
28+
return false
29+
end
30+
31+
local on_init = function(new_client)
32+
new_client.supports_method = function(method)
33+
local internal_method = methods.map[method]
34+
if internal_method then
35+
return require("null-ls.generators").can_run(vim.bo.filetype, internal_method)
36+
end
37+
38+
return methods.lsp[method] ~= nil
39+
end
40+
41+
client = new_client
42+
end
43+
44+
local on_exit = function()
45+
client = nil
46+
end
47+
48+
local start_client = function(fname)
49+
return vim.lsp.start_client({
50+
name = "null-ls",
51+
root_dir = u.root_pattern(".null-ls-root", "Makefile", ".git")(fname) or vim.loop.cwd(),
52+
on_init = on_init,
53+
on_exit = on_exit,
54+
cmd = c.get().cmd,
55+
flags = { debounce_text_changes = c.get().debounce },
56+
on_attach = c.get().on_attach,
57+
})
58+
end
59+
60+
local M = {}
61+
62+
M.start_client = start_client
63+
64+
M.try_add = function(bufnr)
65+
bufnr = bufnr or api.nvim_get_current_buf()
66+
if not should_attach(bufnr) then
67+
return
68+
end
69+
70+
local id = client and client.id or start_client(api.nvim_buf_get_name(bufnr))
71+
vim.lsp.buf_attach_client(bufnr, id)
72+
end
73+
74+
M.get_client = function()
75+
return client
76+
end
77+
78+
M.notify_client = function(method, params)
79+
if not client then
80+
return
81+
end
82+
83+
client.notify(method, params)
84+
end
85+
86+
M.resolve_handler = function(method)
87+
return client and client.handlers[method] or vim.lsp.handlers[method]
88+
end
89+
90+
M._reset = function()
91+
client = nil
92+
end
93+
94+
return M

lua/null-ls/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local validate = vim.validate
22

33
local defaults = {
4+
cmd = { "nvim" },
45
diagnostics_format = "#{m}",
56
debounce = 250,
67
default_timeout = 5000,

lua/null-ls/formatting.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ M.apply_edits = function(edits, params)
5050
local bufnr = params.bufnr
5151
-- directly use lsp handler, since formatting_sync uses a custom handler that won't work if called twice
5252
-- formatting and rangeFormatting handlers are identical
53-
local handler = u.resolve_handler(params.lsp_method)
53+
local handler = require("null-ls.client").resolve_handler(params.lsp_method)
5454

5555
log:debug("received edits from generators")
5656
log:trace(edits)

lua/null-ls/handlers.lua

-22
This file was deleted.

lua/null-ls/helpers.lua

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
local u = require("null-ls.utils")
21
local c = require("null-ls.config")
3-
local s = require("null-ls.state")
42
local log = require("null-ls.logger")
3+
local s = require("null-ls.state")
4+
local u = require("null-ls.utils")
55

66
local api = vim.api
77
local validate = vim.validate
@@ -253,7 +253,7 @@ M.generator_factory = function(opts)
253253
return done()
254254
end
255255

256-
local client = u.get_client()
256+
local client = require("null-ls.client").get_client()
257257
local root = client and client.config.root_dir or vim.loop.cwd()
258258
params.root = root
259259

@@ -432,8 +432,6 @@ M.make_builtin = function(opts)
432432

433433
if prefer_local or only_local then
434434
builtin_copy._opts.dynamic_command = function(params)
435-
local lsputil = require("lspconfig.util")
436-
437435
local resolved = s.get_resolved_command(params.bufnr, params.command)
438436
-- a string means command was resolved on last run
439437
-- false means the command already failed to resolve, so don't bother checking again
@@ -443,15 +441,15 @@ M.make_builtin = function(opts)
443441

444442
local maybe_prefix = prefer_local or only_local
445443
local prefix = type(maybe_prefix) == "string" and maybe_prefix
446-
local executable_to_find = prefix and lsputil.path.join(prefix, params.command) or params.command
444+
local executable_to_find = prefix and u.path.join(prefix, params.command) or params.command
447445
log:debug("attempting to find local executable " .. executable_to_find)
448446

449-
local client = u.get_client()
447+
local client = require("null-ls.client").get_client()
450448
local root = client and client.root_dir or vim.fn.getcwd()
451449

452450
local found, resolved_cwd
453-
lsputil.path.traverse_parents(params.bufname, function(dir)
454-
found = lsputil.path.join(dir, executable_to_find)
451+
u.path.traverse_parents(params.bufname, function(dir)
452+
found = u.path.join(dir, executable_to_find)
455453
if u.is_executable(found) then
456454
resolved_cwd = dir
457455
return true

lua/null-ls/info.lua

+44-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
local methods = require("null-ls.methods")
2-
local u = require("null-ls.utils")
31
local c = require("null-ls.config")
42
local log = require("null-ls.logger")
3+
local methods = require("null-ls.methods")
54

65
local lsp = vim.lsp
76
local api = vim.api
87

8+
-- adapted from nvim-lspconfig's :LspInfo window
9+
local make_window = function(height_percentage, width_percentage)
10+
local row_start_percentage = (1 - height_percentage) / 2
11+
local col_start_percentage = (1 - width_percentage) / 2
12+
13+
local row = math.ceil(vim.o.lines * row_start_percentage)
14+
local col = math.ceil(vim.o.columns * col_start_percentage)
15+
local width = math.floor(vim.o.columns * width_percentage)
16+
local height = math.ceil(vim.o.lines * height_percentage)
17+
18+
local opts = {
19+
relative = "editor",
20+
row = row,
21+
col = col,
22+
width = width,
23+
height = height,
24+
style = "minimal",
25+
border = {
26+
{ " ", "NormalFloat" },
27+
{ " ", "NormalFloat" },
28+
{ " ", "NormalFloat" },
29+
{ " ", "NormalFloat" },
30+
{ " ", "NormalFloat" },
31+
{ " ", "NormalFloat" },
32+
{ " ", "NormalFloat" },
33+
{ " ", "NormalFloat" },
34+
},
35+
}
36+
37+
local bufnr = api.nvim_create_buf(false, true)
38+
local win_id = api.nvim_open_win(bufnr, true, opts)
39+
api.nvim_win_set_buf(win_id, bufnr)
40+
41+
vim.cmd("setlocal nocursorcolumn ts=2 sw=2")
42+
43+
return {
44+
bufnr = bufnr,
45+
win_id = win_id,
46+
}
47+
end
48+
949
local M = {}
1050

1151
M.get_active_sources = function(bufnr, ft)
@@ -23,9 +63,7 @@ M.get_active_sources = function(bufnr, ft)
2363
end
2464

2565
M.show_window = function()
26-
local windows = require("lspconfig.ui.windows")
27-
28-
local client = u.get_client()
66+
local client = require("null-ls.client").get_client()
2967
local bufnr = api.nvim_get_current_buf()
3068
if not client or not lsp.buf_is_attached(bufnr, client.id) then
3169
log:warn("failed to get info: buffer is not attached")
@@ -52,7 +90,7 @@ M.show_window = function()
5290
table.insert(lines, methods.readable[method] .. ": " .. table.concat(sources, ", "))
5391
end
5492

55-
local win_info = windows.percentage_range_window(0.8, 0.7)
93+
local win_info = make_window(0.8, 0.7)
5694
local win_bufnr, win_id = win_info.bufnr, win_info.win_id
5795

5896
api.nvim_buf_set_lines(win_bufnr, 0, -1, true, lines)

lua/null-ls/init.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ M.config = function(user_config)
2828
end
2929

3030
c.setup(user_config or {})
31-
require("null-ls.lspconfig").setup()
3231
require("null-ls.rpc").setup()
3332

3433
vim.cmd("command! NullLsInfo lua require('null-ls').null_ls_info()")
@@ -37,7 +36,7 @@ M.config = function(user_config)
3736
vim.cmd([[
3837
augroup NullLs
3938
autocmd!
40-
autocmd FileType * lua require("null-ls.lspconfig").try_add()
39+
autocmd FileType * lua require("null-ls.client").try_add()
4140
autocmd InsertLeave * unsilent lua require("null-ls.rpc").flush()
4241
augroup end
4342
]])

lua/null-ls/logger.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
local c = require("null-ls.config")
2+
local u = require("null-ls.utils")
3+
24
local log = {}
35

46
--- Adds a log entry using Plenary.log
@@ -31,7 +33,7 @@ end
3133
---Retrieves the path of the logfile
3234
---@return string path of the logfile
3335
function log:get_path()
34-
return string.format("%s/%s.log", vim.fn.stdpath("cache"), "null-ls")
36+
return u.path.join(vim.fn.stdpath("cache"), "null-ls.log")
3537
end
3638

3739
---Add a log entry at TRACE level

lua/null-ls/loop.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local log = require("null-ls.logger")
2+
local u = require("null-ls.utils")
23

34
local uv = vim.loop
45
local wrap = vim.schedule_wrap
@@ -151,13 +152,12 @@ M.timer = function(timeout, interval, should_start, callback)
151152
end
152153

153154
M.temp_file = function(content, extension)
154-
local lsputil = require("lspconfig.util")
155-
local tmp_dir = lsputil.path.sep == "\\" and vim.fn.getenv("TEMP") or "/tmp"
155+
local tmp_dir = u.path.is_windows and vim.fn.getenv("TEMP") or "/tmp"
156156

157157
local fd, tmp_path
158158
if uv.fs_mkstemp then
159159
-- prefer fs_mkstemp, since we can modify the directory
160-
fd, tmp_path = uv.fs_mkstemp(lsputil.path.join(tmp_dir, "null-ls_XXXXXX"))
160+
fd, tmp_path = uv.fs_mkstemp(u.path.join(tmp_dir, "null-ls_XXXXXX"))
161161
else
162162
-- fall back to os.tmpname, which is Unix-only
163163
tmp_path = os.tmpname()

0 commit comments

Comments
 (0)