From 5e0f479163f5b9d0d9bc88776e2d8b745c38be35 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Tue, 3 Feb 2026 20:07:39 -0500 Subject: [PATCH] feat(linter): add cpplint C++ linter --- lua/guard-collection/linter/cpplint.lua | 33 +++++++++++++++++++++++ lua/guard-collection/linter/init.lua | 1 + test/linter/cpplint_spec.lua | 36 +++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lua/guard-collection/linter/cpplint.lua create mode 100644 test/linter/cpplint_spec.lua diff --git a/lua/guard-collection/linter/cpplint.lua b/lua/guard-collection/linter/cpplint.lua new file mode 100644 index 0000000..4054c95 --- /dev/null +++ b/lua/guard-collection/linter/cpplint.lua @@ -0,0 +1,33 @@ +local lint = require('guard.lint') + +return { + fn = function(_, fname) + local co = assert(coroutine.running()) + vim.system({ 'cpplint', '--filter=-legal/copyright', fname }, {}, function(result) + coroutine.resume(co, result.stderr or '') + end) + return coroutine.yield() + end, + parse = function(result, bufnr) + local diags = {} + for line in result:gmatch('[^\n]+') do + local lnum, msg, cat, sev = line:match('^[^:]+:(%d+):%s*(.-)%s+%[([^%]]+)%]%s+%[(%d+)%]$') + if lnum then + local severity = tonumber(sev) >= 3 and vim.diagnostic.severity.ERROR + or vim.diagnostic.severity.WARN + table.insert( + diags, + lint.diag_fmt( + bufnr, + tonumber(lnum) - 1, + 0, + ('[%s] %s'):format(cat, msg), + severity, + 'cpplint' + ) + ) + end + end + return diags + end, +} diff --git a/lua/guard-collection/linter/init.lua b/lua/guard-collection/linter/init.lua index 51a6599..ecdfe15 100644 --- a/lua/guard-collection/linter/init.lua +++ b/lua/guard-collection/linter/init.lua @@ -3,6 +3,7 @@ return { checkmake = require('guard-collection.linter.checkmake'), ['clang-tidy'] = require('guard-collection.linter.clang-tidy'), codespell = require('guard-collection.linter.codespell'), + cpplint = require('guard-collection.linter.cpplint'), detekt = require('guard-collection.linter.detekt'), eslint = require('guard-collection.linter.eslint'), eslint_d = require('guard-collection.linter.eslint_d'), diff --git a/test/linter/cpplint_spec.lua b/test/linter/cpplint_spec.lua new file mode 100644 index 0000000..dcc7165 --- /dev/null +++ b/test/linter/cpplint_spec.lua @@ -0,0 +1,36 @@ +describe('cpplint', function() + it('can lint', function() + local helper = require('test.linter.helper') + local ns = helper.namespace + local ft = require('guard.filetype') + ft('cpp'):lint('cpplint') + + local buf, diagnostics = helper.test_with('cpp', { + [[int main(){int x=1;}]], + }) + assert.are.same({ + { + bufnr = buf, + col = 0, + end_col = 0, + end_lnum = 0, + lnum = 0, + message = '[whitespace/operators] Missing spaces around =', + namespace = ns, + severity = 1, + source = 'cpplint', + }, + { + bufnr = buf, + col = 0, + end_col = 0, + end_lnum = 0, + lnum = 0, + message = '[whitespace/braces] Missing space before {', + namespace = ns, + severity = 1, + source = 'cpplint', + }, + }, diagnostics) + end) +end)