diff --git a/lua/vern-nvim/actions/c.lua b/lua/vern-nvim/actions/c.lua index 86193ae..6f106e7 100644 --- a/lua/vern-nvim/actions/c.lua +++ b/lua/vern-nvim/actions/c.lua @@ -30,12 +30,16 @@ M.create_file = function(opts) end local raw_path = vim.fs.joinpath(mod.path, mod.name) - local context = { project_name = utils.proj_name(), name = fname } + local context = { + project_name = utils.proj_name(), + company_name = opts.env.author, + file_name = fname, + } local path = utils.resolve_path(raw_path, opts.c.dirs or {}, context) local root = utils.proj_root() utils.dump( vim.fs.joinpath(root, path), - require("vern-nvim.templates").fetch(mod.kind_path, mod.kind, fname) + require("vern-nvim.templates").fetch(mod.kind_path, mod.kind, context) ) vim.cmd.edit(vim.fn.expand(path)) vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) diff --git a/lua/vern-nvim/actions/cpp.lua b/lua/vern-nvim/actions/cpp.lua index a495146..f9bb867 100644 --- a/lua/vern-nvim/actions/cpp.lua +++ b/lua/vern-nvim/actions/cpp.lua @@ -30,12 +30,16 @@ M.create_file = function(opts) end local raw_path = vim.fs.joinpath(mod.path, mod.name) - local context = { project_name = utils.proj_name(), name = fname } + local context = { + project_name = utils.proj_name(), + company_name = opts.env.author, + file_name = fname, + } local path = utils.resolve_path(raw_path, opts.cpp.dirs or {}, context) local root = utils.proj_root() utils.dump( vim.fs.joinpath(root, path), - require("vern-nvim.templates").fetch(mod.kind_path, mod.kind, fname) + require("vern-nvim.templates").fetch(mod.kind_path, mod.kind, context) ) vim.cmd.edit(vim.fn.expand(path)) vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) diff --git a/lua/vern-nvim/actions/init.lua b/lua/vern-nvim/actions/init.lua index 95dd0c5..5ea1a0d 100644 --- a/lua/vern-nvim/actions/init.lua +++ b/lua/vern-nvim/actions/init.lua @@ -26,7 +26,7 @@ M.create_project = function(opts) end local root = vim.fs.joinpath(vim.fn.expand(args.workspace_root), args[ft].root, pname) - local bin = action_utils.create(args[ft], pname, root) + local bin = action_utils.create(args[ft], args.env.author, pname, root) utils.init_git(root, ft) utils.init_readme(root) if ok and actions and type(actions.create_project) == "function" then @@ -74,10 +74,17 @@ M.create_file = function(opts) mod = actions.create_file(args[ft]) or args[ft].templates.file.source or {} end local raw_path = vim.fs.joinpath(mod.path, mod.name) - local context = { project_name = utils.proj_name(), name = fname } + local context = { + project_name = utils.proj_name(), + company_name = opts.env.author, + file_name = fname, + } local path = utils.resolve_path(raw_path, args[ft].dirs or {}, context) local root = utils.proj_root() - utils.dump(vim.fs.joinpath(root, path), templates.fetch(mod.kind_path, mod.kind, fname)) + utils.dump( + vim.fs.joinpath(root, path), + templates.fetch(mod.kind_path, mod.kind, context) + ) vim.cmd.edit(vim.fn.expand(path)) vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) end) diff --git a/lua/vern-nvim/actions/java.lua b/lua/vern-nvim/actions/java.lua new file mode 100644 index 0000000..4cd456b --- /dev/null +++ b/lua/vern-nvim/actions/java.lua @@ -0,0 +1,58 @@ +local M = {} + +local utils = require("vern-nvim.utils") + +M.create_project = function() end + +M.create_file = function() end + +---@param opts VernJavaConfig Takes a table for the runtime +function M.run(opts) + opts = opts or {} + local root = utils.proj_root() or "." + local context = { + project_name = utils.proj_name(), + company_name = opts.env.author, + file_name = "", + } + + local src_root = + vim.fs.joinpath(root, utils.resolve_path(opts.java.dirs.src, opts.java.dirs, context)) + local build_dir = root .. "/bin" + local build_cmd = string.format( + "javac -d %s -sourcepath %s $(find %s -name '*java')", + build_dir, + src_root, + src_root + ) + + 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 = string.format( + "java -cp %s com.%s.%s.Main", + build_dir, + context.company_name, + context.project_name + ) + utils.term({ run_cmd }, opts.terminal, false) + end) + end + end) +end + +return M diff --git a/lua/vern-nvim/actions/python.lua b/lua/vern-nvim/actions/python.lua index 4709cd3..be43d52 100644 --- a/lua/vern-nvim/actions/python.lua +++ b/lua/vern-nvim/actions/python.lua @@ -13,11 +13,15 @@ M.create_file = function() end function M.run(opts) opts = opts or {} local root = utils.proj_root() or "." - local pname = vim.fs.basename(root) + local context = { + project_name = utils.proj_name(), + company_name = opts.env.author, + file_name = "", + } local bin = opts.python.templates.project.primary local exe = vim.fs.joinpath(bin.path, bin.name) or "src/{project_name}/main.py" - local exe_path = utils.resolve_path(exe, opts.python.dirs, { project_name = pname }) + local exe_path = utils.resolve_path(exe, opts.python.dirs, context) local full_path = vim.fs.joinpath(vim.fn.expand(root), exe_path) utils.term({ "python3 " .. full_path }, opts.terminal or {}, false) end diff --git a/lua/vern-nvim/actions/utils.lua b/lua/vern-nvim/actions/utils.lua index 21c4a0e..c373b6b 100644 --- a/lua/vern-nvim/actions/utils.lua +++ b/lua/vern-nvim/actions/utils.lua @@ -4,11 +4,16 @@ local utils = require("vern-nvim.utils") local templates = require("vern-nvim.templates") ---@param cfg table Language config +---@param author string Author of project ---@param pname string Name of project ---@param root string Project location ---@return string bin Location of executable -function M.create(cfg, pname, root) - local ctx = { project_name = pname } +function M.create(cfg, author, pname, root) + local ctx = { + project_name = pname, + company_name = author, + file_name = "", + } vim.fn.mkdir(root, "p") @@ -29,7 +34,7 @@ function M.create(cfg, pname, root) if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then vim.fn.mkdir(abs_dir_path, "p") end - utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) + utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, ctx)) end for _, tbl in pairs(cfg.templates.extras) do if tbl.enabled then @@ -40,7 +45,7 @@ function M.create(cfg, pname, root) if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then vim.fn.mkdir(abs_dir_path, "p") end - utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) + utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, ctx)) end end diff --git a/lua/vern-nvim/configs/c.lua b/lua/vern-nvim/configs/c.lua index 039da33..53325f2 100644 --- a/lua/vern-nvim/configs/c.lua +++ b/lua/vern-nvim/configs/c.lua @@ -45,13 +45,13 @@ M.c = { file = { header = { path = "{dirs.include}", - name = "{name}.h", + name = "{file_name}.h", kind = "header", kind_path = "vern-nvim.templates.c", }, source = { path = "{dirs.src}", - name = "{name}.c", + name = "{file_name}.c", kind = "source", kind_path = "vern-nvim.templates.c", }, diff --git a/lua/vern-nvim/configs/cpp.lua b/lua/vern-nvim/configs/cpp.lua index 12325e3..80339ab 100644 --- a/lua/vern-nvim/configs/cpp.lua +++ b/lua/vern-nvim/configs/cpp.lua @@ -45,13 +45,13 @@ M.cpp = { file = { header = { path = "{dirs.include}", - name = "{name}.hpp", + name = "{file_name}.hpp", kind = "header", kind_path = "vern-nvim.templates.cpp", }, source = { path = "{dirs.src}", - name = "{name}.cpp", + name = "{file_name}.cpp", kind = "source", kind_path = "vern-nvim.templates.cpp", }, diff --git a/lua/vern-nvim/configs/init.lua b/lua/vern-nvim/configs/init.lua index 3ecc6bc..4b5155c 100644 --- a/lua/vern-nvim/configs/init.lua +++ b/lua/vern-nvim/configs/init.lua @@ -3,6 +3,10 @@ local actions = require("vern-nvim.actions") ---@type VernCoreConfig return { workspace_root = "~/personal/Projects", + env = { + --- In documentation, make recommendation to change to username or name instead of company + author = "company", + }, keybinds = { ["pc"] = { user_command = "VernCreateProject", diff --git a/lua/vern-nvim/configs/java.lua b/lua/vern-nvim/configs/java.lua new file mode 100644 index 0000000..161e53d --- /dev/null +++ b/lua/vern-nvim/configs/java.lua @@ -0,0 +1,45 @@ +local M = {} + +M.attach = function() end + +---@type VernLanguageConfig +M.java = { + root = "Java", + dirs = { + src = "src/com/company/app", + bin = "bin", + }, + keybinds = { + ["pr"] = { + user_command = "JavaRun", + desc = "Executes java code", + action = require("vern-nvim.actions.java").run, + }, + }, + templates = { + project = { + primary = { + path = "{dirs.src}", + name = "Main.java", + kind = "main", + kind_path = "vern-nvim.templates.java", + }, + style = { + path = ".", + name = ".clang-format", + kind = "format", + kind_path = "vern-nvim.templates.java", + }, + }, + file = { + source = { + path = "{dirs.src}", + name = "{file_name}.java", + kind = "module", + kind_path = "vern-nvim.templates.java", + }, + }, + extras = {}, + }, +} +return M diff --git a/lua/vern-nvim/configs/lua.lua b/lua/vern-nvim/configs/lua.lua index cb5f570..c0d049e 100644 --- a/lua/vern-nvim/configs/lua.lua +++ b/lua/vern-nvim/configs/lua.lua @@ -33,7 +33,7 @@ M.lua = { file = { source = { path = "{dirs.src}", - name = "{name}.lua", + name = "{file_name}.lua", kind = "source", kind_path = "vern-nvim.templates.lua", }, diff --git a/lua/vern-nvim/configs/python.lua b/lua/vern-nvim/configs/python.lua index 61bb7e0..9ba202a 100644 --- a/lua/vern-nvim/configs/python.lua +++ b/lua/vern-nvim/configs/python.lua @@ -59,7 +59,7 @@ M.python = { file = { source = { path = "{dirs.src}", - name = "{name}.py", + name = "{file_name}.py", kind = "module", kind_path = "vern-nvim.templates.python", }, diff --git a/lua/vern-nvim/templates/c.lua b/lua/vern-nvim/templates/c.lua index ff9906a..6c5d664 100644 --- a/lua/vern-nvim/templates/c.lua +++ b/lua/vern-nvim/templates/c.lua @@ -87,12 +87,12 @@ endif() Target: include/{project_name}/{name}.h ===================================================================== --]] header = vim.trim([=[ -#ifndef __PROJECT_NAME___H -#define __PROJECT_NAME___H +#ifndef __FILE_NAME___H +#define __FILE_NAME___H // Code -#endif /* __PROJECT_NAME___H */ +#endif /* __FILE_NAME___H */ ]=]) .. "\n", --[[ ===================================================================== diff --git a/lua/vern-nvim/templates/cpp.lua b/lua/vern-nvim/templates/cpp.lua index a3dd079..25b9d57 100644 --- a/lua/vern-nvim/templates/cpp.lua +++ b/lua/vern-nvim/templates/cpp.lua @@ -87,12 +87,12 @@ endif() Target: include/{project_name}/{name}.hpp ===================================================================== --]] header = vim.trim([=[ -#ifndef __PROJECT_NAME___HPP -#define __PROJECT_NAME___HPP +#ifndef __FILE_NAME___HPP +#define __FILE_NAME___HPP // Code -#endif /* __PROJECT_NAME___HPP */ +#endif /* __FILE_NAME___HPP */ ]=]) .. "\n", --[[ ===================================================================== diff --git a/lua/vern-nvim/templates/init.lua b/lua/vern-nvim/templates/init.lua index 74587eb..ee2a127 100644 --- a/lua/vern-nvim/templates/init.lua +++ b/lua/vern-nvim/templates/init.lua @@ -1,19 +1,37 @@ local M = {} +local registers = { + project_name = "__PROJECT_NAME__", + company_name = "__COMPANY_NAME__", + file_name = "__FILE_NAME__", +} + ---@param kind_path string Filetype of template ---@param kind string Template in language table ----@param name string A string to replace temp values in template +---@param context table A string to replace temp values in template ---@return string Content Formatted string -function M.fetch(kind_path, kind, name) - kind, name = kind or "", name or "" - if (kind == "" or nil) or (name == "" or nil) then +function M.fetch(kind_path, kind, context) + kind, context = kind or "", context or {} + if not kind or kind == "" then return "" end local ok, mod = pcall(require, kind_path) - if ok and type(mod) == "table" then - return (string.gsub(mod[kind], "__PROJECT_NAME__", name)) + if not ok or type(mod) ~= "table" or not mod[kind] then + return "" end - return "" + + local template = mod[kind] or "" + context = context or {} + + if ok and type(mod) == "table" then + for key, placeholder in pairs(registers) do + local value = context[key] + if value then + template = string.gsub(template, placeholder, value) + end + end + end + return template end return M diff --git a/lua/vern-nvim/templates/java.lua b/lua/vern-nvim/templates/java.lua new file mode 100644 index 0000000..3193307 --- /dev/null +++ b/lua/vern-nvim/templates/java.lua @@ -0,0 +1,64 @@ +return { + --[[ ===================================================================== + TEMPLATE: MAIN (Entrypoint) + File: Main.java + Target: src/com/company/app/Main.java + ===================================================================== --]] + main = vim.trim([=[ +package com.__COMPANY_NAME__.__PROJECT_NAME__; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } +} + +]=]) .. "\n", + + --[[ ===================================================================== + TEMPLATE: CLANG-FORMAT (Code formatter) + File: .clang-format + Target: .clang-format + ===================================================================== --]] + format = vim.trim([=[ +--- +Language: Java +BasedOnStyle: Google +Standard: Latest +UseTab: Always +TabWidth: 8 +IndentWidth: 8 +BreakBeforeBraces: Allman +SeparateDefinitionBlocks: Always +BreakAfterReturnType: TopLevel +ReflowComments: Always +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortLoopsOnASingleLine: true +SortIncludes: CaseSensitive +IncludeBlocks: Preserve +]=]) .. "\n", + + --[[ ===================================================================== + TEMPLATE: SOURCE (Standalone File) + File: {name}.java + Target: src/com/company/app/{name}.java + ===================================================================== --]] + source = vim.trim([=[ +public class __FILE_NAME__ { + package com.__COMPANY_NAME__.__PROJECT_NAME__; + + // Fields / Attributes + private String data; + + // Constructor + public __FILE_NAME__() { + } + + // Methods + public void doSomething() { + + } +} +]=]) .. "\n", +} diff --git a/lua/vern-nvim/templates/python.lua b/lua/vern-nvim/templates/python.lua index 2ef20df..6089acb 100644 --- a/lua/vern-nvim/templates/python.lua +++ b/lua/vern-nvim/templates/python.lua @@ -53,7 +53,7 @@ __PROJECT_NAME__ = "__PROJECT_NAME__.main:main" ===================================================================== --]] module = vim.trim([=[ """ -Module: __PROJECT_NAME__ +Module: __FILE_NAME__ """ ]=]) .. "\n", } diff --git a/lua/vern-nvim/types.lua b/lua/vern-nvim/types.lua index 6b26c61..651d516 100644 --- a/lua/vern-nvim/types.lua +++ b/lua/vern-nvim/types.lua @@ -27,8 +27,12 @@ ---@class VernTerminalConfig ---@field mode VernModeConfig +---@class VernEnvItem +---@field author string + ---@class VernCoreConfig ---@field workspace_root string +---@field env VernEnvItem ---@field keybinds table ---@field terminal VernTerminalConfig @@ -63,3 +67,6 @@ ---@class VernLuaConfig : VernCoreConfig ---@field lua? VernLanguageConfig +--- +---@class VernJavaConfig : VernCoreConfig +---@field java? VernLanguageConfig