Skip to content

Commit 740571d

Browse files
authored
feat: add blackd support (#1606)
* feat: add blackd * fix: format only on 200 http status * fix: headers should start with "X-" * fix: use factory instead allowing args * fix: let users choose the parameters based on official docs * perf: use async generator * fix: user config instead of args * fix: use generator key instead of factory key * feat: add descriptions and default values * fix: missing default * docs: add info about config in description * fix: move curl require inside the callback
1 parent 608547d commit 740571d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
local h = require("null-ls.helpers")
2+
local methods = require("null-ls.methods")
3+
4+
local FORMATTING = methods.internal.FORMATTING
5+
6+
return h.make_builtin({
7+
name = "blackd",
8+
meta = {
9+
url = "https://github.com/psf/black",
10+
description = "blackd is a small HTTP server that exposes Black’s functionality over a simple protocol. The main benefit of using it is to avoid the cost of starting up a new Black process every time you want to blacken a file. The only way to configure the formatter is by using the provided config options, it will not pick up on config files.",
11+
12+
config = {
13+
{
14+
key = "hostname",
15+
type = "string",
16+
description = "Address to bind the server to. Defaults to localhost.",
17+
},
18+
{ key = "port", type = "string", description = "Port to listen on. Defaults to 45484." },
19+
{
20+
key = "line_length",
21+
type = "number",
22+
description = "Set how many characters per line to allow. Defaults to 88.",
23+
},
24+
{
25+
key = "skip_source_first_line",
26+
type = "boolean",
27+
description = "If set to true, the first line of the source code will be ignored. Defaults to false.",
28+
},
29+
{
30+
key = "skip_string_normalization",
31+
type = "boolean",
32+
description = "If set to true, no string normalization will be performed. Defaults to false.",
33+
},
34+
{
35+
key = "skip_magic_trailing_comma",
36+
type = "boolean",
37+
description = "If set to true, trailing commas will not be used as a reason to split lines. Defaults to false.",
38+
},
39+
{
40+
key = "preview",
41+
type = "boolean",
42+
description = "If set to true, experimental and potentially disruptive style changes will be used. Defaults to false.",
43+
},
44+
{
45+
key = "fast",
46+
type = "boolean",
47+
description = "If set to true, Black will not perform an AST safety check after formatting. Defaults to false.",
48+
},
49+
{
50+
key = "python_variant",
51+
type = "string",
52+
description = "If set to pyi, Black will format all input files like typing stubs regardless of the file extension. Otherwise, its value must correspond to a Python version or a set of comma-separated Python versions, optionally prefixed with py. (e.g. py3.5,py3.6). Defaults to empty string.",
53+
},
54+
},
55+
},
56+
method = FORMATTING,
57+
filetypes = { "python" },
58+
generator = {
59+
async = true,
60+
fn = function(params, done)
61+
local config = params:get_config()
62+
local hostname = config.hostname or "localhost"
63+
local port = config.port or 45484
64+
require("plenary.curl").post(hostname .. ":" .. port, {
65+
body = table.concat(params.content, "\n"),
66+
headers = {
67+
["X-Line-Length"] = config.line_length or 88,
68+
["X-Skip-Source-First-Line"] = config.skip_source_first_line or nil,
69+
["X-Skip-String-Normalization"] = config.skip_string_normalization or nil,
70+
["X-Skip-Magic-Trailing-Comma"] = config.skip_magic_trailing_comma or nil,
71+
["X-Preview"] = config.preview or nil,
72+
["X-Fast-Or-Safe"] = config.fast == "fast" and "fast" or nil,
73+
["X-Python-Variant"] = config.python_variant or "",
74+
},
75+
callback = function(response)
76+
if response.status == 200 then
77+
return done({ { text = response.body } })
78+
end
79+
return done()
80+
end,
81+
})
82+
end,
83+
},
84+
})

0 commit comments

Comments
 (0)