Skip to content

Commit 01ebfcf

Browse files
1 parent 968a35d commit 01ebfcf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

lua/lint/linters/api-linter.lua

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
local descriptor_filepath = os.tmpname()
2+
local cleanup_descriptor = function()
3+
os.remove(descriptor_filepath)
4+
end
5+
6+
--- Function to set the `--descriptor-set-in` argument.
7+
--- This requires the buf CLI, which will first build the descriptor file.
8+
local function descriptor_set_in()
9+
if vim.fn.executable("buf") == 0 then
10+
error("buf CLI not found")
11+
end
12+
local buf_config_filepaths = vim.fs.find(
13+
{ "buf.yaml", "buf.yml" },
14+
{ limit = math.huge, type = "file", path = vim.fn.getcwd() }
15+
)
16+
if #buf_config_filepaths == 0 then
17+
error("Buf config file not found")
18+
end
19+
local buf_config_filepath = buf_config_filepaths[1]
20+
local buf_config_folderpath = vim.fn.fnamemodify(buf_config_filepath, ":h")
21+
local buf_cmd = { "buf", "build", "-o", descriptor_filepath }
22+
local buf_cmd_opts = { cwd = buf_config_folderpath }
23+
local obj = vim.system(buf_cmd, buf_cmd_opts):wait()
24+
if obj.code ~= 0 then
25+
error("Command failed: " .. vim.inspect(buf_cmd) .. "\n" .. obj.stderr)
26+
end
27+
local descriptor_arg = "--descriptor-set-in=" .. descriptor_filepath
28+
return descriptor_arg
29+
end
30+
31+
return {
32+
cmd = "api-linter",
33+
stdin = false,
34+
append_fname = true,
35+
args = {
36+
"--output-format=json",
37+
"--disable-rule=core::0191::java-multiple-files",
38+
"--disable-rule=core::0191::java-package",
39+
"--disable-rule=core::0191::java-outer-classname",
40+
descriptor_set_in,
41+
},
42+
stream = "stdout",
43+
ignore_exitcode = true,
44+
env = nil,
45+
parser = function(output)
46+
if output == "" then
47+
return {}
48+
end
49+
local json_output = vim.json.decode(output)
50+
local diagnostics = {}
51+
if json_output == nil then
52+
return diagnostics
53+
end
54+
for _, item in ipairs(json_output) do
55+
for _, problem in ipairs(item.problems) do
56+
table.insert(diagnostics, {
57+
message = problem.message,
58+
file = item.file,
59+
code = problem.rule_id .. " " .. problem.rule_doc_uri,
60+
severity = vim.diagnostic.severity.WARN,
61+
lnum = problem.location.start_position.line_number - 1,
62+
col = problem.location.start_position.column_number - 1,
63+
end_lnum = problem.location.end_position.line_number - 1,
64+
end_col = problem.location.end_position.column_number - 1,
65+
})
66+
end
67+
end
68+
cleanup_descriptor()
69+
return diagnostics
70+
end,
71+
}

0 commit comments

Comments
 (0)