Skip to content

Commit b29420d

Browse files
1 parent 968a35d commit b29420d

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

lua/lint/linters/api-linter.lua

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
13+
-- search for the buf config, searching upwards from the current buffer's directory until $HOME.
14+
local buffer_parent_dir = vim.fn.expand("%:p:h") -- the path to the folder of the opened .proto file.
15+
local buf_config_filepaths = vim.fs.find(
16+
{ "buf.yaml", "buf.yml" },
17+
{ path = buffer_parent_dir, upward = true, stop = vim.fs.normalize("~"), type = "file", limit = 1 }
18+
)
19+
if #buf_config_filepaths == 0 then
20+
error("Buf config file not found")
21+
end
22+
local buf_config_filepath = buf_config_filepaths[1]
23+
24+
-- build the descriptor file.
25+
local buf_config_folderpath = vim.fn.fnamemodify(buf_config_filepath, ":h")
26+
local buf_cmd = { "buf", "build", "-o", descriptor_filepath }
27+
local buf_cmd_opts = { cwd = buf_config_folderpath }
28+
local obj = vim.system(buf_cmd, buf_cmd_opts):wait()
29+
if obj.code ~= 0 then
30+
error("Command failed: " .. vim.inspect(buf_cmd) .. "\n" .. obj.stderr)
31+
end
32+
33+
-- return the argument to be passed to the linter.
34+
return "--descriptor-set-in=" .. descriptor_filepath
35+
end
36+
37+
return {
38+
cmd = "api-linter",
39+
stdin = false,
40+
append_fname = true,
41+
args = {
42+
"--output-format=json",
43+
"--disable-rule=core::0191::java-multiple-files",
44+
"--disable-rule=core::0191::java-package",
45+
"--disable-rule=core::0191::java-outer-classname",
46+
descriptor_set_in,
47+
},
48+
stream = "stdout",
49+
ignore_exitcode = true,
50+
env = nil,
51+
parser = function(output)
52+
if output == "" then
53+
return {}
54+
end
55+
local json_output = vim.json.decode(output)
56+
local diagnostics = {}
57+
if json_output == nil then
58+
return diagnostics
59+
end
60+
for _, item in ipairs(json_output) do
61+
for _, problem in ipairs(item.problems) do
62+
table.insert(diagnostics, {
63+
message = problem.message,
64+
file = item.file,
65+
code = problem.rule_id .. " " .. problem.rule_doc_uri,
66+
severity = vim.diagnostic.severity.WARN,
67+
lnum = problem.location.start_position.line_number - 1,
68+
col = problem.location.start_position.column_number - 1,
69+
end_lnum = problem.location.end_position.line_number - 1,
70+
end_col = problem.location.end_position.column_number - 1,
71+
})
72+
end
73+
end
74+
cleanup_descriptor()
75+
return diagnostics
76+
end,
77+
}

0 commit comments

Comments
 (0)