Skip to content

Commit 003e4a4

Browse files
author
Jose Alvarez
committed
feat: add make_builtin helper and builtin.with() method
1 parent 895ed48 commit 003e4a4

File tree

5 files changed

+207
-128
lines changed

5 files changed

+207
-128
lines changed

lua/null-ls/builtins/diagnostics.lua

+68-70
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,88 @@
1+
local h = require("null-ls.helpers")
12
local methods = require("null-ls.methods")
2-
local helpers = require("null-ls.helpers")
33

44
local DIAGNOSTICS = methods.internal.DIAGNOSTICS
55

66
local M = {}
77

8-
M.write_good = {
8+
M.write_good = h.make_builtin({
99
method = DIAGNOSTICS,
1010
filetypes = {"markdown"},
11-
generator = helpers.generator_factory(
12-
{
13-
command = "write-good",
14-
args = {"--text=$TEXT", "--parse"},
15-
format = "line",
16-
check_exit_code = function(code)
17-
return code == 0 or code == 255
18-
end,
19-
on_output = function(line, params)
20-
local pos = vim.split(string.match(line, "%d+:%d+"), ":")
21-
local row = pos[1]
11+
generator_opts = {
12+
command = "write-good",
13+
args = {"--text=$TEXT", "--parse"},
14+
format = "line",
15+
check_exit_code = function(code) return code == 0 or code == 255 end,
16+
on_output = function(line, params)
17+
local pos = vim.split(string.match(line, "%d+:%d+"), ":")
18+
local row = pos[1]
2219

23-
local message = string.match(line, ":([^:]+)$")
20+
local message = string.match(line, ":([^:]+)$")
2421

25-
local col, end_col
26-
local issue = string.match(line, "%b\"\"")
27-
local issue_line = params.content[tonumber(row)]
28-
if issue and issue_line then
29-
local issue_start, issue_end =
30-
string.find(issue_line, string.match(issue, "([^\"]+)"))
31-
if issue_start and issue_end then
32-
col = tonumber(issue_start) - 1
33-
end_col = issue_end
34-
end
22+
local col, end_col
23+
local issue = string.match(line, "%b\"\"")
24+
local issue_line = params.content[tonumber(row)]
25+
if issue and issue_line then
26+
local issue_start, issue_end =
27+
string.find(issue_line, string.match(issue, "([^\"]+)"))
28+
if issue_start and issue_end then
29+
col = tonumber(issue_start) - 1
30+
end_col = issue_end
3531
end
36-
37-
return {
38-
row = row,
39-
col = col,
40-
end_col = end_col,
41-
message = message,
42-
severity = 1,
43-
source = "write-good"
44-
}
4532
end
46-
})
47-
}
4833

49-
M.markdownlint = {
34+
return {
35+
row = row,
36+
col = col,
37+
end_col = end_col,
38+
message = message,
39+
severity = 1,
40+
source = "write-good"
41+
}
42+
end
43+
},
44+
factory = h.generator_factory
45+
})
46+
47+
M.markdownlint = h.make_builtin({
5048
method = DIAGNOSTICS,
5149
filetypes = {"markdown"},
52-
generator = helpers.generator_factory(
53-
{
54-
command = "markdownlint",
55-
args = {"--stdin"},
56-
to_stdin = true,
57-
to_stderr = true,
58-
format = "line",
59-
check_exit_code = function(code) return code <= 1 end,
60-
on_output = function(line, params)
61-
local split = vim.split(line, " ")
62-
local rule = split[2]
63-
local _, rule_end = string.find(line, rule, nil, true)
64-
local message = string.sub(line, rule_end + 2)
50+
generator_opts = {
51+
command = "markdownlint",
52+
args = {"--stdin"},
53+
to_stdin = true,
54+
to_stderr = true,
55+
format = "line",
56+
check_exit_code = function(code) return code <= 1 end,
57+
on_output = function(line, params)
58+
local split = vim.split(line, " ")
59+
local rule = split[2]
60+
local _, rule_end = string.find(line, rule, nil, true)
61+
local message = string.sub(line, rule_end + 2)
6562

66-
local pos = vim.split(split[1], ":")
67-
local row = pos[2]
68-
local col = pos[3]
63+
local pos = vim.split(split[1], ":")
64+
local row = pos[2]
65+
local col = pos[3]
6966

70-
local end_col
71-
local issue = string.match(line, "%b[]")
72-
if issue then
73-
issue = string.sub(issue, 2, #issue - 1)
74-
local issue_line = params.content[tonumber(row)]
75-
_, end_col = string.find(issue_line, issue, nil, true)
76-
end
77-
78-
return {
79-
row = row,
80-
col = col and col - 1,
81-
end_col = end_col,
82-
message = message,
83-
severity = 1,
84-
source = "markdownlint"
85-
}
67+
local end_col
68+
local issue = string.match(line, "%b[]")
69+
if issue then
70+
issue = string.sub(issue, 2, #issue - 1)
71+
local issue_line = params.content[tonumber(row)]
72+
_, end_col = string.find(issue_line, issue, nil, true)
8673
end
87-
})
88-
}
74+
75+
return {
76+
row = row,
77+
col = col and col - 1,
78+
end_col = end_col,
79+
message = message,
80+
severity = 1,
81+
source = "markdownlint"
82+
}
83+
end
84+
},
85+
factory = h.generator_factory
86+
})
8987

9088
return M

lua/null-ls/builtins/formatting.lua

+34-36
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,59 @@
1+
local h = require("null-ls.helpers")
12
local methods = require("null-ls.methods")
2-
local helpers = require("null-ls.helpers")
33

44
local FORMATTING = methods.internal.FORMATTING
55

66
local M = {}
7-
M.lua_format = {
7+
8+
M.lua_format = h.make_builtin({
89
method = FORMATTING,
910
filetypes = {"lua"},
10-
generator = helpers.formatter_factory(
11-
{
12-
command = "lua-format",
13-
args = {"--single-quote-to-double-quote", "-i"},
14-
to_stdin = true,
15-
timeout = 2500
16-
})
17-
}
11+
generator_opts = {
12+
command = "lua-format",
13+
args = {"--single-quote-to-double-quote", "-i"},
14+
to_stdin = true
15+
},
16+
factory = h.formatter_factory
17+
})
1818

19-
M.prettier = {
19+
M.prettier = h.make_builtin({
2020
method = FORMATTING,
2121
filetypes = {
2222
"javascript", "javascriptreact", "typescript", "typescriptreact", "css",
2323
"html", "json", "yaml", "markdown"
2424
},
25-
generator = helpers.formatter_factory(
26-
{
27-
command = "prettier",
28-
args = {"--stdin-filepath", "$FILENAME"},
29-
to_stdin = true
30-
})
31-
}
25+
generator_opts = {
26+
command = "prettier",
27+
args = {"--stdin-filepath", "$FILENAME"},
28+
to_stdin = true
29+
},
30+
factory = h.formatter_factory
31+
})
3232

33-
M.prettier_d_slim = {
33+
M.prettier_d_slim = h.make_builtin({
3434
method = FORMATTING,
3535
filetypes = {
3636
"javascript", "javascriptreact", "typescript", "typescriptreact"
3737
},
38-
generator = helpers.formatter_factory(
39-
{
40-
command = "prettier_d_slim",
41-
args = {"--stdin", "--stdin-filepath", "$FILENAME"},
42-
to_stdin = true
43-
})
44-
}
38+
generator_opts = {
39+
command = "prettier_d_slim",
40+
args = {"--stdin", "--stdin-filepath", "$FILENAME"},
41+
to_stdin = true
42+
},
43+
factory = h.formatter_factory
44+
})
4545

46-
M.eslint_d = {
46+
M.eslint_d = h.make_builtin({
4747
method = FORMATTING,
4848
filetypes = {
4949
"javascript", "javascriptreact", "typescript", "typescriptreact"
5050
},
51-
generator = helpers.formatter_factory(
52-
{
53-
command = "eslint_d",
54-
args = {
55-
"--fix-to-stdout", "--stdin", "--stdin-filename", "$FILENAME"
56-
},
57-
to_stdin = true
58-
})
59-
}
51+
generator_opts = {
52+
command = "eslint_d",
53+
args = {"--fix-to-stdout", "--stdin", "--stdin-filename", "$FILENAME"},
54+
to_stdin = true
55+
},
56+
factory = h.formatter_factory
57+
})
6058

6159
return M

lua/null-ls/builtins/test.lua

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local u = require("null-ls.utils")
2+
local h = require("null-ls.helpers")
23
local methods = require("null-ls.methods")
3-
local helpers = require("null-ls.helpers")
44

55
local api = vim.api
66

@@ -64,29 +64,28 @@ M.mock_code_action = {
6464
filetypes = {"lua"}
6565
}
6666

67-
M.slow_code_action = {
67+
M.slow_code_action = h.make_builtin({
6868
method = methods.internal.CODE_ACTION,
69-
generator = helpers.generator_factory(
70-
{
71-
command = "bash",
72-
args = {"./test/scripts/sleep-and-echo.sh"},
73-
format = "raw",
74-
timeout = 100,
75-
on_output = function(params, done)
76-
if not params.output then return done() end
69+
filetypes = {"lua"},
70+
generator_opts = {
71+
command = "bash",
72+
args = {"./test/scripts/sleep-and-echo.sh"},
73+
timeout = 100,
74+
on_output = function(params, done)
75+
if not params.output then return done() end
7776

78-
return done({
79-
{
80-
title = "Slow mock action",
81-
action = function()
82-
print("I took too long!")
83-
end
84-
}
85-
})
86-
end
87-
}),
88-
filetypes = {"lua"}
89-
}
77+
return done({
78+
{
79+
title = "Slow mock action",
80+
action = function()
81+
print("I took too long!")
82+
end
83+
}
84+
})
85+
end
86+
},
87+
factory = h.generator_factory
88+
})
9089

9190
M.mock_diagnostics = {
9291
method = methods.internal.DIAGNOSTICS,

lua/null-ls/helpers.lua

+28
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ M.formatter_factory = function(opts)
145145
return M.generator_factory(opts)
146146
end
147147

148+
M.make_builtin = function(opts)
149+
local method, filetypes, factory, generator_opts = opts.method,
150+
opts.filetypes,
151+
opts.factory,
152+
opts.generator_opts
153+
154+
local builtin = {
155+
method = method,
156+
filetypes = filetypes,
157+
_opts = generator_opts
158+
}
159+
160+
setmetatable(builtin, {
161+
__index = function(tab, key)
162+
return key == "generator" and factory(tab._opts) or rawget(tab, key)
163+
end
164+
})
165+
166+
builtin.with = function(user_opts)
167+
builtin.filetypes = user_opts.filetypes or builtin.filetypes
168+
builtin._opts = vim.tbl_extend("force", builtin._opts, user_opts)
169+
170+
return builtin
171+
end
172+
173+
return builtin
174+
end
175+
148176
if _G._TEST then
149177
M._json_output_wrapper = json_output_wrapper
150178
M._line_output_wrapper = line_output_wrapper

0 commit comments

Comments
 (0)