mirror of
https://github.com/pojokcodeid/nvim-lazy.git
synced 2025-07-11 18:04:25 +02:00
enc: add create go project whijart
This commit is contained in:
parent
68cf1f8bc4
commit
82392943e2
2 changed files with 225 additions and 3 deletions
|
@ -32,7 +32,7 @@
|
|||
"menu": { "branch": "main", "commit": "7a0a4a2896b715c066cfbe320bdc048091874cc6" },
|
||||
"mini.indentscope": { "branch": "main", "commit": "5fdc3edf7bb1b6365980c2c47dac2f19ec93c97b" },
|
||||
"minty": { "branch": "main", "commit": "aafc9e8e0afe6bf57580858a2849578d8d8db9e0" },
|
||||
"neotest": { "branch": "master", "commit": "ef492755730e59e1d8122c461abbd086bee4c76b" },
|
||||
"neotest": { "branch": "master", "commit": "1d4b3bd89afa8bfa12fffd2bb1ccd26ac3c92ce5" },
|
||||
"neotest-golang": { "branch": "main", "commit": "0a0acff3cf9a0bf140c84ea9e66d818df7c2608f" },
|
||||
"neotest-jest": { "branch": "main", "commit": "dd82016c01704d9bbd5188749fe0586b3b620693" },
|
||||
"neotest-plenary": { "branch": "master", "commit": "3523adcf9ffaad1911960c5813b0136c1b63a2ec" },
|
||||
|
@ -49,11 +49,11 @@
|
|||
"nvim-dap-virtual-text": { "branch": "master", "commit": "fbdb48c2ed45f4a8293d0d483f7730d24467ccb6" },
|
||||
"nvim-dap-vscode-js": { "branch": "main", "commit": "03bd29672d7fab5e515fc8469b7d07cc5994bbf6" },
|
||||
"nvim-lint": { "branch": "master", "commit": "2b0039b8be9583704591a13129c600891ac2c596" },
|
||||
"nvim-lspconfig": { "branch": "master", "commit": "69a1624aff5dc30dddd0c59d99a947b63c80bf2a" },
|
||||
"nvim-lspconfig": { "branch": "master", "commit": "6bba673aa8993eceec233be17b42ddfb9540794b" },
|
||||
"nvim-material-icon": { "branch": "main", "commit": "38fc13fe4811c4bf62533180ff5e7bbd237c5ef5" },
|
||||
"nvim-navic": { "branch": "master", "commit": "f887d794a0f4594882814d7780980a949200a238" },
|
||||
"nvim-nio": { "branch": "master", "commit": "21f5324bfac14e22ba26553caf69ec76ae8a7662" },
|
||||
"nvim-notify": { "branch": "master", "commit": "b5825cf9ee881dd8e43309c93374ed5b87b7a896" },
|
||||
"nvim-notify": { "branch": "master", "commit": "a22f5d7ac511c2df2fd3290a9f04c48d5a822e2e" },
|
||||
"nvim-scrollview": { "branch": "main", "commit": "095181bc2adb64af670dae73208871a731f0bb86" },
|
||||
"nvim-tree.lua": { "branch": "master", "commit": "b0b49552c9462900a882fe772993b01d780445fe" },
|
||||
"nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" },
|
||||
|
@ -69,6 +69,7 @@
|
|||
"telescope.nvim": { "branch": "master", "commit": "b4da76be54691e854d3e0e02c36b0245f945c2c7" },
|
||||
"tiny-devicons-auto-colors.nvim": { "branch": "main", "commit": "51f548421f8a74680eff27d283c9d5ea6e8d0074" },
|
||||
"toggleterm.nvim": { "branch": "main", "commit": "9a88eae817ef395952e08650b3283726786fb5fb" },
|
||||
"transparent.nvim": { "branch": "main", "commit": "8ac59883de84e9cd1850ea25cf087031c5ba7d54" },
|
||||
"vim-illuminate": { "branch": "master", "commit": "0d1e93684da00ab7c057410fecfc24f434698898" },
|
||||
"vim-visual-multi": { "branch": "master", "commit": "a6975e7c1ee157615bbc80fc25e4392f71c344d4" },
|
||||
"virt-column.nvim": { "branch": "master", "commit": "b87e3e0864211a32724a2ebf3be37e24e9e2fa99" },
|
||||
|
|
|
@ -51,6 +51,227 @@ return {
|
|||
|
||||
go_runner:toggle()
|
||||
end, {})
|
||||
|
||||
-- Fungsi untuk membuat proyek Go
|
||||
local function go_new_project()
|
||||
local function create_notif(message, level)
|
||||
local notif_ok, notify = pcall(require, "notify")
|
||||
if notif_ok then
|
||||
notify(message, level)
|
||||
else
|
||||
print(message)
|
||||
end
|
||||
end
|
||||
|
||||
local function get_user_input(prompt, default_value)
|
||||
vim.fn.inputsave()
|
||||
local result = vim.fn.input(prompt, default_value)
|
||||
vim.fn.inputrestore()
|
||||
|
||||
if result == "" then
|
||||
create_notif("Input canceled.", "info")
|
||||
return nil, true
|
||||
end
|
||||
return result, false
|
||||
end
|
||||
|
||||
local project_name, canceled = get_user_input("Enter project name: ", "myapp")
|
||||
if canceled then
|
||||
return
|
||||
end
|
||||
|
||||
local file_name, canceled = get_user_input("Enter file name (without .go): ", "main")
|
||||
if canceled then
|
||||
return
|
||||
end
|
||||
|
||||
local function_name, canceled = get_user_input("Enter function name: ", "main")
|
||||
if canceled then
|
||||
return
|
||||
end
|
||||
|
||||
local cwd = vim.fn.getcwd()
|
||||
local project_path = cwd .. "/" .. project_name
|
||||
if vim.fn.isdirectory(project_path) == 1 then
|
||||
create_notif("Project directory already exists: " .. project_path, "error")
|
||||
return
|
||||
end
|
||||
|
||||
local mkdir_cmd = string.format("mkdir -p '%s'", project_path)
|
||||
local gomod_cmd = string.format("cd '%s' && go mod init %s", project_path, project_name)
|
||||
local entry_file = string.format("%s/%s.go", project_path, file_name)
|
||||
local main_code = string.format(
|
||||
[[
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func %s() {
|
||||
fmt.Println("Hello, world!")
|
||||
}
|
||||
]],
|
||||
function_name
|
||||
)
|
||||
|
||||
vim.fn.system(mkdir_cmd)
|
||||
vim.fn.system(gomod_cmd)
|
||||
|
||||
local file = io.open(entry_file, "w")
|
||||
if file then
|
||||
file:write(main_code)
|
||||
file:close()
|
||||
create_notif("Go project created at " .. project_path, "info")
|
||||
vim.cmd("cd " .. project_path)
|
||||
|
||||
-- Jika ada NvimTreeOpen, jalankan lalu kembalikan fokus ke file
|
||||
if vim.fn.exists(":NvimTreeOpen") == 2 then
|
||||
vim.cmd("NvimTreeOpen")
|
||||
-- Penjadwalan agar kembali ke buffer file Go setelah NvimTree terbuka
|
||||
vim.schedule(function()
|
||||
vim.cmd("edit " .. entry_file)
|
||||
-- Cari baris function
|
||||
local lines = {}
|
||||
for line in main_code:gmatch("([^\n]*)\n?") do
|
||||
table.insert(lines, line)
|
||||
end
|
||||
local func_line = 1
|
||||
for i, line in ipairs(lines) do
|
||||
if line:find("func%s+" .. function_name .. "%s*%(") then
|
||||
func_line = i
|
||||
break
|
||||
end
|
||||
end
|
||||
local target_line = func_line + 1
|
||||
vim.api.nvim_win_set_cursor(0, { target_line, 4 })
|
||||
vim.cmd("startinsert")
|
||||
end)
|
||||
else
|
||||
vim.cmd("edit " .. entry_file)
|
||||
local lines = {}
|
||||
for line in main_code:gmatch("([^\n]*)\n?") do
|
||||
table.insert(lines, line)
|
||||
end
|
||||
local func_line = 1
|
||||
for i, line in ipairs(lines) do
|
||||
if line:find("func%s+" .. function_name .. "%s*%(") then
|
||||
func_line = i
|
||||
break
|
||||
end
|
||||
end
|
||||
local target_line = func_line + 1
|
||||
vim.api.nvim_win_set_cursor(0, { target_line, 4 })
|
||||
vim.cmd("startinsert")
|
||||
end
|
||||
else
|
||||
create_notif("Failed to create file: " .. entry_file, "error")
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_create_user_command("GoNewProject", go_new_project, {})
|
||||
|
||||
-- Fungsi untuk membuat file Go
|
||||
local function go_new_file()
|
||||
local function create_notif(message, level)
|
||||
local notif_ok, notify = pcall(require, "notify")
|
||||
if notif_ok then
|
||||
notify(message, level)
|
||||
else
|
||||
print(message)
|
||||
end
|
||||
end
|
||||
|
||||
local function get_user_input(prompt, default_value)
|
||||
vim.fn.inputsave()
|
||||
local result = vim.fn.input(prompt, default_value)
|
||||
vim.fn.inputrestore()
|
||||
if result == "" then
|
||||
create_notif("Input canceled.", "info")
|
||||
return nil, true
|
||||
end
|
||||
return result, false
|
||||
end
|
||||
|
||||
-- Ambil nama folder
|
||||
local folder_name, canceled = get_user_input("Enter folder name (dot for current folder): ", ".")
|
||||
if canceled then
|
||||
return
|
||||
end
|
||||
|
||||
-- Ambil nama file (tanpa .go)
|
||||
local file_name, canceled = get_user_input("Enter file name (without .go): ", "newfile")
|
||||
if canceled then
|
||||
return
|
||||
end
|
||||
|
||||
-- Ambil nama package
|
||||
local package_name, canceled = get_user_input("Enter package name: ", "main")
|
||||
if canceled then
|
||||
return
|
||||
end
|
||||
|
||||
-- Ambil nama function
|
||||
local function_name, canceled = get_user_input("Enter function name: ", "main")
|
||||
if canceled then
|
||||
return
|
||||
end
|
||||
|
||||
local cwd = vim.fn.getcwd()
|
||||
local folder_path = (folder_name == "." or folder_name == "") and cwd or cwd .. "/" .. folder_name
|
||||
|
||||
-- Cek dan buat folder jika perlu
|
||||
if vim.fn.isdirectory(folder_path) == 0 then
|
||||
local mkdir_cmd = string.format("mkdir -p '%s'", folder_path)
|
||||
vim.fn.system(mkdir_cmd)
|
||||
end
|
||||
|
||||
local file_path = string.format("%s/%s.go", folder_path, file_name)
|
||||
|
||||
if vim.fn.filereadable(file_path) == 1 then
|
||||
create_notif("File already exists: " .. file_path, "error")
|
||||
return
|
||||
end
|
||||
|
||||
local code = string.format(
|
||||
[[
|
||||
package %s
|
||||
|
||||
func %s() {
|
||||
|
||||
}
|
||||
]],
|
||||
package_name,
|
||||
function_name
|
||||
)
|
||||
|
||||
-- Buat file dan isi konten
|
||||
local file = io.open(file_path, "w")
|
||||
if file then
|
||||
file:write(code)
|
||||
file:close()
|
||||
create_notif("Go file created: " .. file_path, "info")
|
||||
|
||||
-- Buka file dan tempatkan kursor di bawah function, mode insert
|
||||
vim.cmd("edit " .. file_path)
|
||||
local lines = {}
|
||||
for line in code:gmatch("([^\n]*)\n?") do
|
||||
table.insert(lines, line)
|
||||
end
|
||||
local func_line = 1
|
||||
for i, line in ipairs(lines) do
|
||||
if line:find("func%s+" .. function_name .. "%s*%(") then
|
||||
func_line = i
|
||||
break
|
||||
end
|
||||
end
|
||||
local target_line = func_line + 1
|
||||
vim.api.nvim_win_set_cursor(0, { target_line, 4 })
|
||||
vim.cmd("startinsert")
|
||||
else
|
||||
create_notif("Failed to create file: " .. file_path, "error")
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_create_user_command("GoNewFile", go_new_file, {})
|
||||
end,
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue