Skip to content

Commit d54053d

Browse files
author
Jose Alvarez
authored
Merge pull request #386 from meijieru/fix_python_formatter
feature(yapf): allow range_format
2 parents 2af1e1e + 32fc7c2 commit d54053d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

doc/BUILTINS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,8 @@ local sources = { null_ls.builtins.formatting.uncrustify }
13741374
##### About
13751375

13761376
Formatter for `python` files
1377+
- Supports both `textDocument/formatting` and `textDocument/rangeFormatting`.
1378+
- `textDocument/rangeFormatting` is line-based.
13771379

13781380
##### Usage
13791381

lua/null-ls/builtins/formatting/yapf.lua

+23-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,35 @@ local h = require("null-ls.helpers")
22
local methods = require("null-ls.methods")
33

44
local FORMATTING = methods.internal.FORMATTING
5+
local RANGE_FORMATTING = methods.internal.RANGE_FORMATTING
6+
7+
local function range_formatting_args_factory(base_args, start_arg)
8+
vim.validate({
9+
base_args = { base_args, "table" },
10+
start_arg = { start_arg, "string" },
11+
})
12+
13+
return function(params)
14+
local args = vim.deepcopy(base_args)
15+
if params.method == FORMATTING then
16+
return args
17+
end
18+
19+
local range = params.range
20+
table.insert(args, start_arg)
21+
table.insert(args, range.row .. "-" .. range.end_row) -- range of lines to reformat, one-based
22+
return args
23+
end
24+
end
525

626
return h.make_builtin({
7-
method = FORMATTING,
27+
method = { FORMATTING, RANGE_FORMATTING },
828
filetypes = { "python" },
929
generator_opts = {
1030
command = "yapf",
11-
args = {
31+
args = range_formatting_args_factory({
1232
"--quiet",
13-
},
33+
}, "--lines"),
1434
to_stdin = true,
1535
},
1636
factory = h.formatter_factory,

0 commit comments

Comments
 (0)