diff --git a/lua/hego/commands.lua b/lua/hego/commands.lua new file mode 100644 index 0000000..d71a6b7 --- /dev/null +++ b/lua/hego/commands.lua @@ -0,0 +1,9 @@ +vim.api.nvim_create_user_command("HegoBuild", function() + require("hego").build(false) +end, {}) +vim.api.nvim_create_user_command("HegoBuildPristine", function() + require("hego").build(true) +end, {}) +vim.api.nvim_create_user_command("HegoFlash", function() + require("hego").flash() +end, {}) diff --git a/lua/hego/detect.lua b/lua/hego/detect.lua new file mode 100644 index 0000000..6fe949c --- /dev/null +++ b/lua/hego/detect.lua @@ -0,0 +1,52 @@ +local M = {} + +local function stat(p) + return vim.uv.fs_stat(p) +end + +--- Versión NCS más reciente instalada (p.ej. "v3.4.0"). +function M.ncs_version(root) + local out = {} + for _, d in ipairs(vim.fn.glob(root .. "/v*", false, true)) do + if stat(d .. "/zephyr") then + table.insert(out, vim.fn.fnamemodify(d, ":t")) + end + end + table.sort(out) + return out[#out] +end + +--- Toolchain más reciente: { dir, gcc }. +function M.toolchain(root) + local out = {} + for _, d in ipairs(vim.fn.glob(root .. "/*", false, true)) do + local gcc = d .. "/opt/zephyr-sdk/gnu/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc" + local st = stat(gcc) + if st then + table.insert(out, { dir = d, gcc = gcc, mtime = st.mtime.sec }) + end + end + table.sort(out, function(a, b) + return a.mtime > b.mtime + end) + return out[1] +end + +--- Board: lo lee del CMakeCache si ya hay build; si no, nil. +function M.board(build_dir) + local f = io.open(build_dir .. "/CMakeCache.txt", "r") + if not f then + return nil + end + for line in f:lines() do + local b = line:match("^BOARD:STRING=(.+)$") + if b then + f:close() + return b + end + end + f:close() + return nil +end + +return M diff --git a/lua/hego/exec.lua b/lua/hego/exec.lua new file mode 100644 index 0000000..cad6f57 --- /dev/null +++ b/lua/hego/exec.lua @@ -0,0 +1,53 @@ +local M = {} + +--- Envuelve un binario (west, nrfutil...) en el entorno NCS. +function M.ncs(opts, cmd) + local full = { "nrfutil", "toolchain-manager", "launch" } + if opts.ncs_version then + vim.list_extend(full, { "--ncs-version", opts.ncs_version }) + end + vim.list_extend(full, { "--" }) + vim.list_extend(full, cmd) + return full +end + +--- Ejecuta async y vuelca la salida a un buffer scratch (no bloquea nvim). +function M.run_async(cmd, o) + o = o or {} + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_name(buf, o.title or "hego://run") + vim.cmd("botright split") + vim.api.nvim_win_set_buf(0, buf) + + local function append(data) + if not data or data == "" then + return + end + vim.api.nvim_buf_set_lines(buf, -1, -1, false, vim.split(data, "\n", { plain = true })) + vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(buf), 0 }) + end + + return vim.system(cmd, { + cwd = o.cwd, + text = true, + stdout = function(_, d) + vim.schedule(function() + append(d) + end) + end, + stderr = function(_, d) + vim.schedule(function() + append(d) + end) + end, + }, function(obj) + vim.schedule(function() + append("\n[hego] exit: " .. tostring(obj.code)) + if o.on_exit then + o.on_exit(obj) + end + end) + end) +end + +return M diff --git a/lua/hego/flash.lua b/lua/hego/flash.lua new file mode 100644 index 0000000..3deb106 --- /dev/null +++ b/lua/hego/flash.lua @@ -0,0 +1,49 @@ +local M = {} + +function M.devices() + local out = vim.fn.system({ "nrfutil", "device", "list", "--json" }) + local ok, data = pcall(vim.json.decode, out) + if not ok or type(data) ~= "table" then + return {} + end + local devs = {} + for _, d in ipairs(data) do + if d.serial_number then + table.insert(devs, { + sn = d.serial_number, + name = (d.traits and d.traits.product) or "DK", + }) + end + end + return devs +end + +function M.run(opts) + local devs = M.devices() + if #devs == 0 then + vim.notify("[hego] no hay ninguna placa conectada", vim.log.levels.WARN) + return + end + + local function go(sn) + local exec = require("hego.exec") + local cmd = exec.ncs(opts, { "west", "flash", "-d", opts.build_dir, "--dev-id", sn }) + exec.run_async(cmd, { title = "hego://flash" }) + end + + if #devs == 1 then + return go(devs[1].sn) + end + vim.ui.select(devs, { + prompt = "¿A qué placa flasheo?", + format_item = function(d) + return d.name .. " (" .. d.sn .. ")" + end, + }, function(c) + if c then + go(c.sn) + end + end) +end + +return M diff --git a/lua/hego/init.lua b/lua/hego/init.lua new file mode 100644 index 0000000..e2de3ab --- /dev/null +++ b/lua/hego/init.lua @@ -0,0 +1,48 @@ +local M = {} + +M.defaults = { + ncs_root = vim.fn.expand("~/ncs"), + toolchains_root = vim.fn.expand("~/ncs/toolchains"), + ncs_version = nil, -- nil = autodetectar (p.ej. "v3.4.0") + toolchain_dir = nil, -- nil = autodetectar + board = nil, -- nil = leer de build/CMakeCache.txt + build_dir = "build", + conf_file = "prj.conf", + sysbuild = true, + overlays = {}, -- { "boards/nrf54l15dk_nrf54l15_cpuapp.overlay" } + extra_cmake = {}, -- { "-DCONFIG_DEBUG_THREAD_INFO=y" } + keys = { build = "nb", pristine = "np", flash = "nf" }, +} + +M.options = {} + +function M.setup(opts) + local o = vim.tbl_deep_extend("force", {}, M.defaults, opts or {}) + local detect = require("hego.detect") + + if not o.ncs_version then + o.ncs_version = detect.ncs_version(o.ncs_root) + end + if not o.toolchain_dir then + local tc = detect.toolchain(o.toolchains_root) + o.toolchain_dir = tc and tc.dir or nil + end + if not o.board then + o.board = detect.board(o.build_dir) + end + + M.options = o + require("hego.keymaps").setup(o) + require("hego.commands").setup(o) + return o +end + +-- API que consumen keymaps y comandos de usuario +function M.build(pristine) + return require("hego.west").build(M.options, pristine) +end +function M.flash() + return require("hego.flash").run(M.options) +end + +return M diff --git a/lua/hego/keymaps.lua b/lua/hego/keymaps.lua new file mode 100644 index 0000000..76bdb67 --- /dev/null +++ b/lua/hego/keymaps.lua @@ -0,0 +1,12 @@ +function M.setup(opts) + local k, H = opts.keys, require("hego") + vim.keymap.set("n", k.build, function() + H.build(false) + end, { desc = "NCS: Build" }) + vim.keymap.set("n", k.pristine, function() + H.build(true) + end, { desc = "NCS: Build (pristine)" }) + vim.keymap.set("n", k.flash, function() + H.flash() + end, { desc = "NCS: Flash" }) +end diff --git a/lua/hego/lsp.lua b/lua/hego/lsp.lua new file mode 100644 index 0000000..09fb608 --- /dev/null +++ b/lua/hego/lsp.lua @@ -0,0 +1,39 @@ +local M = {} + +local SKIP = { mcuboot = true, matter_factory_data = true } + +function M.link(opts) + local m = vim.fn.glob(opts.build_dir .. "/*/compile_commands.json", false, true) + if #m == 0 then + m = vim.fn.glob(opts.build_dir .. "/compile_commands.json", false, true) -- sin sysbuild + end + if #m == 0 then + vim.notify("[hego] no encuentro compile_commands.json", vim.log.levels.WARN) + return + end + + local chosen + for _, p in ipairs(m) do + local image = vim.fn.fnamemodify(vim.fn.fnamemodify(p, ":h"), ":t") + if not SKIP[image] then + chosen = p + break + end + end + chosen = chosen or m[1] + + vim.fn.system({ + "ln", + "-sfn", + vim.fn.fnamemodify(chosen, ":p"), + vim.fn.getcwd() .. "/compile_commands.json", + }) + if vim.v.shell_error ~= 0 then + vim.notify("[hego] fallo creando el symlink", vim.log.levels.ERROR) + return + end + vim.cmd("LspRestart") -- clangd solo lo lee al arrancar + vim.notify("[hego] compile_commands.json -> " .. chosen, vim.log.levels.INFO) +end + +return M diff --git a/lua/hego/west.lua b/lua/hego/west.lua new file mode 100644 index 0000000..7e6b12c --- /dev/null +++ b/lua/hego/west.lua @@ -0,0 +1,35 @@ +local M = {} + +function M.build(opts, pristine) + local a = { "build", "-d", opts.build_dir, vim.fn.getcwd(), "--board", opts.board } + if opts.sysbuild then + table.insert(a, "--sysbuild") + end + if pristine then + table.insert(a, "--pristine") + end + table.insert(a, "--") + table.insert(a, "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON") + if opts.conf_file then + table.insert(a, "-DCONF_FILE=" .. opts.conf_file) + end + if #opts.overlays > 0 then + table.insert(a, "-DDTC_OVERLAY_FILE=" .. table.concat(opts.overlays, ";")) + end + for _, e in ipairs(opts.extra_cmake or {}) do + table.insert(a, e) + end + + local exec = require("hego.exec") + local cmd = exec.ncs(opts, vim.list_extend({ "west" }, a)) + return exec.run_async(cmd, { + title = "hego://build", + on_exit = function(obj) + if obj.code == 0 then + require("hego.lsp").link(opts) + end + end, + }) +end + +return M