nvim/lua/tools/cmake.lua
2025-04-06 22:19:03 +08:00

53 lines
1.5 KiB
Lua

-- ~/.config/nvim/lua/myplugin/init.lua
local M = {}
-- Function to copy files from the resource directory to the current directory
local function copy_files(str)
local resource_dir = vim.fn.stdpath("config") .. "/res/template/cmake"
local target_dir = vim.fn.getcwd()
local files = { ".clangd", ".clang-format", "CMakeLists.txt", "main.cpp" }
for _, file in ipairs(files) do
local src_path = resource_dir .. "/" .. file
local dest_path = target_dir .. "/" .. file
local input_file = io.open(src_path, "r")
if input_file then
local content = input_file:read("*all")
input_file:close()
if file == "CMakeLists.txt" and str ~= "" then
-- Replace the placeholder text in CMakeLists.txt
content = content:gsub("replace", str)
end
local output_file = io.open(dest_path, "w")
if output_file then
output_file:write(content)
output_file:close()
else
print("Error writing file: " .. dest_path)
end
else
print("Error reading file: " .. src_path)
end
end
end
function M.setup()
vim.api.nvim_create_user_command("CMakeQuick", function()
local str = vim.fn.input("Enter project's name: ")
if str == "" then
print("Name is empty. Nothing to do.")
else
copy_files(str)
print("Create project successfully.")
end
end, {})
end
return M