local M = {} local utils = require("vern.utils") local templates = require("vern.templates") local action_utils = require("vern.actions.utils") local join = vim.fs.joinpath ---@param opts VernCConfig function M.create_project(opts) vim.ui.input({ prompt = "Enter project name: " }, function(pname) pname = pname and vim.trim(pname) if not pname or pname == "" then return end local configs = require("vern.configs.c") ---@type VernCConfig local cfg = vim.tbl_deep_extend("force", {}, configs, configs, opts or {}) local root = join(vim.fn.expand(cfg.workspace_root), cfg.c.root, pname) local bin = action_utils.create(cfg.c, pname, root) local build_cmd = "cd " .. root .. " && cmake -S . -B build" local _ = vim.system({ "sh", "-c", build_cmd }):wait() utils.init_git(root, "c") vim.cmd.edit(vim.fn.fnameescape(bin)) end) end function M.create_file(opts) vim.ui.input({ prompt = "Enter type (c/h): " }, function(ft) ft = ft and vim.trim(ft) if not ft or ft == "" then return end local configs = require("vern.configs.c") ---@type VernCConfig local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {}) vim.ui.input({ prompt = "Enter file name: " }, function(fname) local root = join(vim.fn.expand(cfg.workspace_root), cfg.c.root, utils.proj_name()) local input_txt = fname ft = ft and vim.trim(ft) if not ft or ft == "" then return end local mod if ft == "c" then mod = cfg.c.templates.file.source or {} end if ft == "h" then mod = cfg.c.templates.file.header or {} input_txt = fname:upper() end local raw_path = join(mod.path, mod.name) local context = { project_name = utils.proj_name(), name = fname } local path = utils.resolve_path(raw_path, cfg.c.dirs or {}, context) local abs_file_path = vim.fs.normalize(join(root, path)) if ft == "h" then local abs_dir_path = vim.fs.dirname(abs_file_path) if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then vim.fn.mkdir(abs_dir_path, "p") end end utils.dump(abs_file_path, templates.fetch(mod.kind_path, mod.kind, input_txt)) vim.cmd.edit(vim.fn.expand(abs_file_path)) end) end) end ---@param opts? VernCConfig Takes a table for the runtime function M.run(opts) opts = opts or {} local root = utils.proj_root() or "." local pname = vim.fs.basename(root) local build_dir = root .. "/build" local build_cmd = "cd " .. build_dir .. " && cmake --build ." vim.system({ "sh", "-c", build_cmd }, { text = true }, function(obj) if obj.code ~= 0 then vim.schedule(function() local error_obj = (obj.stderr ~= "" and obj.stderr) or obj.stdout or "" local error_lines = vim.list_extend( { "Error During compilation" }, vim.split(error_obj, "\n", { trimempty = true }) ) local buf, _ = utils.term({ "echo 'compilation failed'" }, opts.terminal, true) if buf and vim.api.nvim_buf_is_valid(buf) then vim.api.nvim_set_option_value("modifiable", true, { buf = buf }) vim.api.nvim_buf_set_lines(buf, 0, -1, false, error_lines) vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) end end) else vim.schedule(function() local run_cmd = "cd '" .. build_dir .. "' && ./" .. pname utils.term({ run_cmd }, opts.terminal, false) end) end end) end return M