feat: add lint tool
This commit is contained in:
42
lua/chatgpt_nvim/tools/lint.lua
Normal file
42
lua/chatgpt_nvim/tools/lint.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
local uv = vim.loop
|
||||
|
||||
local M = {}
|
||||
|
||||
-- A naive lint approach: if a file ends with .lua, run "luacheck" if available.
|
||||
-- If .go, run "go build" or "go vet", etc. This is just an example; adapt to your needs.
|
||||
-- Return nil if lint tool can't be determined or is not installed.
|
||||
local function guess_linter_command(path)
|
||||
if path:match("%.lua$") then
|
||||
return "luacheck " .. vim.fn.shellescape(path)
|
||||
elseif path:match("%.go$") then
|
||||
-- We'll just do a quick "go build" or "go vet"
|
||||
return "go vet " .. vim.fn.fnamemodify(path, ":h")
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Executes the linter command and returns the output or an error if it fails to run.
|
||||
local function run_command(cmd)
|
||||
local handle = io.popen(cmd)
|
||||
if not handle then
|
||||
return nil, ("Failed to run: %s"):format(cmd)
|
||||
end
|
||||
local output = handle:read("*a") or ""
|
||||
handle:close()
|
||||
return output, nil
|
||||
end
|
||||
|
||||
function M.lint_file(path)
|
||||
local lint_cmd = guess_linter_command(path)
|
||||
if not lint_cmd then
|
||||
return nil, "No known lint command for file: " .. path
|
||||
end
|
||||
|
||||
local output, err = run_command(lint_cmd)
|
||||
if not output then
|
||||
return nil, err or ("Failed to lint: " .. path)
|
||||
end
|
||||
return output, nil
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user