Skip to content

Commit 5e0f479

Browse files
committed
feat(linter): add cpplint C++ linter
1 parent a55f21b commit 5e0f479

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local lint = require('guard.lint')
2+
3+
return {
4+
fn = function(_, fname)
5+
local co = assert(coroutine.running())
6+
vim.system({ 'cpplint', '--filter=-legal/copyright', fname }, {}, function(result)
7+
coroutine.resume(co, result.stderr or '')
8+
end)
9+
return coroutine.yield()
10+
end,
11+
parse = function(result, bufnr)
12+
local diags = {}
13+
for line in result:gmatch('[^\n]+') do
14+
local lnum, msg, cat, sev = line:match('^[^:]+:(%d+):%s*(.-)%s+%[([^%]]+)%]%s+%[(%d+)%]$')
15+
if lnum then
16+
local severity = tonumber(sev) >= 3 and vim.diagnostic.severity.ERROR
17+
or vim.diagnostic.severity.WARN
18+
table.insert(
19+
diags,
20+
lint.diag_fmt(
21+
bufnr,
22+
tonumber(lnum) - 1,
23+
0,
24+
('[%s] %s'):format(cat, msg),
25+
severity,
26+
'cpplint'
27+
)
28+
)
29+
end
30+
end
31+
return diags
32+
end,
33+
}

lua/guard-collection/linter/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ return {
33
checkmake = require('guard-collection.linter.checkmake'),
44
['clang-tidy'] = require('guard-collection.linter.clang-tidy'),
55
codespell = require('guard-collection.linter.codespell'),
6+
cpplint = require('guard-collection.linter.cpplint'),
67
detekt = require('guard-collection.linter.detekt'),
78
eslint = require('guard-collection.linter.eslint'),
89
eslint_d = require('guard-collection.linter.eslint_d'),

test/linter/cpplint_spec.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
describe('cpplint', function()
2+
it('can lint', function()
3+
local helper = require('test.linter.helper')
4+
local ns = helper.namespace
5+
local ft = require('guard.filetype')
6+
ft('cpp'):lint('cpplint')
7+
8+
local buf, diagnostics = helper.test_with('cpp', {
9+
[[int main(){int x=1;}]],
10+
})
11+
assert.are.same({
12+
{
13+
bufnr = buf,
14+
col = 0,
15+
end_col = 0,
16+
end_lnum = 0,
17+
lnum = 0,
18+
message = '[whitespace/operators] Missing spaces around =',
19+
namespace = ns,
20+
severity = 1,
21+
source = 'cpplint',
22+
},
23+
{
24+
bufnr = buf,
25+
col = 0,
26+
end_col = 0,
27+
end_lnum = 0,
28+
lnum = 0,
29+
message = '[whitespace/braces] Missing space before {',
30+
namespace = ns,
31+
severity = 1,
32+
source = 'cpplint',
33+
},
34+
}, diagnostics)
35+
end)
36+
end)

0 commit comments

Comments
 (0)