feat: added java and refactored how context is utilized

This commit is contained in:
2026-10-27 14:35:19 -04:00
parent fcde51bc05
commit e8c55ec18f
18 changed files with 253 additions and 33 deletions
+6 -2
View File
@@ -30,12 +30,16 @@ M.create_file = function(opts)
end end
local raw_path = vim.fs.joinpath(mod.path, mod.name) 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 path = utils.resolve_path(raw_path, opts.c.dirs or {}, context)
local root = utils.proj_root() local root = utils.proj_root()
utils.dump( utils.dump(
vim.fs.joinpath(root, path), 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.cmd.edit(vim.fn.expand(path))
vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" })
+6 -2
View File
@@ -30,12 +30,16 @@ M.create_file = function(opts)
end end
local raw_path = vim.fs.joinpath(mod.path, mod.name) 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 path = utils.resolve_path(raw_path, opts.cpp.dirs or {}, context)
local root = utils.proj_root() local root = utils.proj_root()
utils.dump( utils.dump(
vim.fs.joinpath(root, path), 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.cmd.edit(vim.fn.expand(path))
vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" })
+10 -3
View File
@@ -26,7 +26,7 @@ M.create_project = function(opts)
end end
local root = local root =
vim.fs.joinpath(vim.fn.expand(args.workspace_root), args[ft].root, pname) 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_git(root, ft)
utils.init_readme(root) utils.init_readme(root)
if ok and actions and type(actions.create_project) == "function" then 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 {} mod = actions.create_file(args[ft]) or args[ft].templates.file.source or {}
end end
local raw_path = vim.fs.joinpath(mod.path, mod.name) 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 path = utils.resolve_path(raw_path, args[ft].dirs or {}, context)
local root = utils.proj_root() 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.cmd.edit(vim.fn.expand(path))
vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" })
end) end)
+58
View File
@@ -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
+6 -2
View File
@@ -13,11 +13,15 @@ M.create_file = function() end
function M.run(opts) function M.run(opts)
opts = opts or {} opts = opts or {}
local root = utils.proj_root() 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 bin = opts.python.templates.project.primary
local exe = vim.fs.joinpath(bin.path, bin.name) or "src/{project_name}/main.py" 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) local full_path = vim.fs.joinpath(vim.fn.expand(root), exe_path)
utils.term({ "python3 " .. full_path }, opts.terminal or {}, false) utils.term({ "python3 " .. full_path }, opts.terminal or {}, false)
end end
+9 -4
View File
@@ -4,11 +4,16 @@ local utils = require("vern-nvim.utils")
local templates = require("vern-nvim.templates") local templates = require("vern-nvim.templates")
---@param cfg table Language config ---@param cfg table Language config
---@param author string Author of project
---@param pname string Name of project ---@param pname string Name of project
---@param root string Project location ---@param root string Project location
---@return string bin Location of executable ---@return string bin Location of executable
function M.create(cfg, pname, root) function M.create(cfg, author, pname, root)
local ctx = { project_name = pname } local ctx = {
project_name = pname,
company_name = author,
file_name = "",
}
vim.fn.mkdir(root, "p") 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 if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then
vim.fn.mkdir(abs_dir_path, "p") vim.fn.mkdir(abs_dir_path, "p")
end 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
for _, tbl in pairs(cfg.templates.extras) do for _, tbl in pairs(cfg.templates.extras) do
if tbl.enabled then 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 if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then
vim.fn.mkdir(abs_dir_path, "p") vim.fn.mkdir(abs_dir_path, "p")
end 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
end end
+2 -2
View File
@@ -45,13 +45,13 @@ M.c = {
file = { file = {
header = { header = {
path = "{dirs.include}", path = "{dirs.include}",
name = "{name}.h", name = "{file_name}.h",
kind = "header", kind = "header",
kind_path = "vern-nvim.templates.c", kind_path = "vern-nvim.templates.c",
}, },
source = { source = {
path = "{dirs.src}", path = "{dirs.src}",
name = "{name}.c", name = "{file_name}.c",
kind = "source", kind = "source",
kind_path = "vern-nvim.templates.c", kind_path = "vern-nvim.templates.c",
}, },
+2 -2
View File
@@ -45,13 +45,13 @@ M.cpp = {
file = { file = {
header = { header = {
path = "{dirs.include}", path = "{dirs.include}",
name = "{name}.hpp", name = "{file_name}.hpp",
kind = "header", kind = "header",
kind_path = "vern-nvim.templates.cpp", kind_path = "vern-nvim.templates.cpp",
}, },
source = { source = {
path = "{dirs.src}", path = "{dirs.src}",
name = "{name}.cpp", name = "{file_name}.cpp",
kind = "source", kind = "source",
kind_path = "vern-nvim.templates.cpp", kind_path = "vern-nvim.templates.cpp",
}, },
+4
View File
@@ -3,6 +3,10 @@ local actions = require("vern-nvim.actions")
---@type VernCoreConfig ---@type VernCoreConfig
return { return {
workspace_root = "~/personal/Projects", workspace_root = "~/personal/Projects",
env = {
--- In documentation, make recommendation to change to username or name instead of company
author = "company",
},
keybinds = { keybinds = {
["<leader>pc"] = { ["<leader>pc"] = {
user_command = "VernCreateProject", user_command = "VernCreateProject",
+45
View File
@@ -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 = {
["<leader>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
+1 -1
View File
@@ -33,7 +33,7 @@ M.lua = {
file = { file = {
source = { source = {
path = "{dirs.src}", path = "{dirs.src}",
name = "{name}.lua", name = "{file_name}.lua",
kind = "source", kind = "source",
kind_path = "vern-nvim.templates.lua", kind_path = "vern-nvim.templates.lua",
}, },
+1 -1
View File
@@ -59,7 +59,7 @@ M.python = {
file = { file = {
source = { source = {
path = "{dirs.src}", path = "{dirs.src}",
name = "{name}.py", name = "{file_name}.py",
kind = "module", kind = "module",
kind_path = "vern-nvim.templates.python", kind_path = "vern-nvim.templates.python",
}, },
+3 -3
View File
@@ -87,12 +87,12 @@ endif()
Target: include/{project_name}/{name}.h Target: include/{project_name}/{name}.h
===================================================================== --]] ===================================================================== --]]
header = vim.trim([=[ header = vim.trim([=[
#ifndef __PROJECT_NAME___H #ifndef __FILE_NAME___H
#define __PROJECT_NAME___H #define __FILE_NAME___H
// Code // Code
#endif /* __PROJECT_NAME___H */ #endif /* __FILE_NAME___H */
]=]) .. "\n", ]=]) .. "\n",
--[[ ===================================================================== --[[ =====================================================================
+3 -3
View File
@@ -87,12 +87,12 @@ endif()
Target: include/{project_name}/{name}.hpp Target: include/{project_name}/{name}.hpp
===================================================================== --]] ===================================================================== --]]
header = vim.trim([=[ header = vim.trim([=[
#ifndef __PROJECT_NAME___HPP #ifndef __FILE_NAME___HPP
#define __PROJECT_NAME___HPP #define __FILE_NAME___HPP
// Code // Code
#endif /* __PROJECT_NAME___HPP */ #endif /* __FILE_NAME___HPP */
]=]) .. "\n", ]=]) .. "\n",
--[[ ===================================================================== --[[ =====================================================================
+25 -7
View File
@@ -1,19 +1,37 @@
local M = {} 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_path string Filetype of template
---@param kind string Template in language table ---@param kind string Template in language table
---@param name string A string to replace temp values in template ---@param context table<string, string> A string to replace temp values in template
---@return string Content Formatted string ---@return string Content Formatted string
function M.fetch(kind_path, kind, name) function M.fetch(kind_path, kind, context)
kind, name = kind or "", name or "" kind, context = kind or "", context or {}
if (kind == "" or nil) or (name == "" or nil) then if not kind or kind == "" then
return "" return ""
end end
local ok, mod = pcall(require, kind_path) local ok, mod = pcall(require, kind_path)
if ok and type(mod) == "table" then if not ok or type(mod) ~= "table" or not mod[kind] then
return (string.gsub(mod[kind], "__PROJECT_NAME__", name))
end
return "" return ""
end
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 end
return M return M
+64
View File
@@ -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",
}
+1 -1
View File
@@ -53,7 +53,7 @@ __PROJECT_NAME__ = "__PROJECT_NAME__.main:main"
===================================================================== --]] ===================================================================== --]]
module = vim.trim([=[ module = vim.trim([=[
""" """
Module: __PROJECT_NAME__ Module: __FILE_NAME__
""" """
]=]) .. "\n", ]=]) .. "\n",
} }
+7
View File
@@ -27,8 +27,12 @@
---@class VernTerminalConfig ---@class VernTerminalConfig
---@field mode VernModeConfig ---@field mode VernModeConfig
---@class VernEnvItem
---@field author string
---@class VernCoreConfig ---@class VernCoreConfig
---@field workspace_root string ---@field workspace_root string
---@field env VernEnvItem
---@field keybinds table<string, VernKeybindItem> ---@field keybinds table<string, VernKeybindItem>
---@field terminal VernTerminalConfig ---@field terminal VernTerminalConfig
@@ -63,3 +67,6 @@
---@class VernLuaConfig : VernCoreConfig ---@class VernLuaConfig : VernCoreConfig
---@field lua? VernLanguageConfig ---@field lua? VernLanguageConfig
---
---@class VernJavaConfig : VernCoreConfig
---@field java? VernLanguageConfig