Skip to content

Commit

Permalink
feat: add history menu & cache, gradle support, luacheck
Browse files Browse the repository at this point in the history
  • Loading branch information
tvntvn13 committed Aug 22, 2024
1 parent e127d4c commit da92e55
Show file tree
Hide file tree
Showing 19 changed files with 742 additions and 66 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
name: luacheck

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
luacheck:
name: luacheck
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4
- name: lint
uses: lunarmodules/luacheck@v1
8 changes: 8 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
std = "min+busted"
globals = {
"vim",
"T_Type",
"_Toggle_term",
"A",
}

4 changes: 4 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.workspace.checkThirdParty": false
}
22 changes: 20 additions & 2 deletions lua/java_test_util/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
---@class Config
---@field use_wrapper boolean
---@field hide_terminal boolean
---@field terminal_height number
---@field terminal_width number
---@field terminal_border string
---@field display_name string
---@field title_pos string
---@field direction string
---@field auto_scroll boolean
---@field close_on_exit boolean
---@field timeout_len number
---@field toggle_key string
---@field close_key string
---@field max_history_size number

---@type Config
local config = {
use_maven_wrapper = false,
use_wrapper = false,
hide_terminal = true,
terminal_height = 25,
terminal_width = 90,
Expand All @@ -9,9 +26,10 @@ local config = {
direction = "float",
auto_scroll = true,
close_on_exit = false,
timeout_len = 5000,
timeout_len = 2000,
toggle_key = "<leader>Mm",
close_key = "q",
max_history_size = 12,
}

return config
32 changes: 14 additions & 18 deletions lua/java_test_util/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,42 @@ function M.run_mvn_test_for_current_method()
return
end

local test_command = util.build_test_command_string(M.config, "method", nil, class_name, method_name)
local test_command = util.build_test_command_string(T_Type.METHOD, nil, class_name, method_name)

term.run_command_in_terminal(test_command, method_name)

vim.notify("󰂓 running test: " .. method_name)
term.run_command_in_terminal(test_command, method_name, T_Type.METHOD)
util.notify_tests_running(method_name, T_Type.METHOD)
end

function M.run_mvn_test_for_current_class()
local file_path = util.get_filepath()
local class_name = util.get_class_name_from_path(file_path)
local test_command = util.build_test_command_string(M.config, "class", nil, class_name)

term.run_command_in_terminal(test_command, class_name)
local test_command = util.build_test_command_string(T_Type.CLASS, nil, class_name)

vim.notify("󰂓 running tests for class: " .. class_name)
term.run_command_in_terminal(test_command, class_name, T_Type.CLASS)
util.notify_tests_running(class_name, T_Type.CLASS)
end

function M.run_mvn_test_for_current_package()
local package_name = util.get_package_name()
local test_command = util.build_test_command_string(M.config, "package", package_name)
local test_command = util.build_test_command_string(T_Type.PACKAGE, package_name)

term.run_command_in_terminal(test_command, package_name)

vim.notify("󰂓 running tests for package: " .. package_name)
term.run_command_in_terminal(test_command, package_name, T_Type.PACKAGE)
util.notify_tests_running(package_name, T_Type.PACKAGE)
end

function M.run_mvn_previous_test()
if term.last_test_command and term.last_test_component then
vim.notify("Rerunning tests for: " .. term.last_test_component)
term.run_command_in_terminal(term.last_test_command, term.last_test_component)
vim.notify("󰂓 Re-running tests for: " .. term.last_test_component)
term.run_command_in_terminal(term.last_test_command, term.last_test_component, term.last_test_type)
else
util.show_message_until("No previous test to run", 2000)
end
end

function M.run_mvn_test_for_all()
local test_command = util.build_test_command_string(M.config, "all")
term.run_command_in_terminal(test_command, "all")

vim.notify("󰂓 running All tests")
local test_command = util.build_test_command_string(T_Type.ALL, T_Type.ALL)
term.run_command_in_terminal(test_command, T_Type.ALL, T_Type.ALL)
util.notify_tests_running(T_Type.ALL, T_Type.ALL)
end

return M
151 changes: 151 additions & 0 deletions lua/java_test_util/history.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
local M = {}

---@class CommandHistoryItem
---@field command? string
---@field component? string
---@field type? string

local config = require("java_test_util.config")
local util = require("java_test_util.util")

---@type CommandHistoryItem[]|nil
M.cmd_history = {}


local max_size = config.max_history_size or 10
local CACHE_PATH = "/java-test-util/"
local CACHE_SUFFIX = "_history.lua"

---@param command string
---@param component string
---@return boolean
function M.check_for_duplicate(command, component, type)
for _, item in ipairs(M.cmd_history) do
if item.command == command and item.component == component and item.type == type then
return true
end
end
return false
end

---@return string|nil
local function get_cache_path()
local project_root

if util.root_dir then
project_root = util.root_dir
else
project_root = util.get_project_root()
end

if project_root then
local cache_path = vim.fn.stdpath("cache") .. CACHE_PATH .. vim.fn.fnamemodify(project_root, ":t") .. CACHE_SUFFIX
return cache_path
end
vim.notify("no project root found!", vim.log.levels.ERROR)
return nil
end

function M.load_cached_history()
local cache_path = get_cache_path()

if not cache_path then
vim.notify("no cache found", vim.log.levels.WARN)
return nil
end

if cache_path and vim.fn.filereadable(cache_path) == 1 then
local chunk = loadfile(cache_path)
if chunk then
local loaded_history = chunk()
if type(loaded_history) == "table" then
M.cmd_history = loaded_history
end
end
end
end

function M.write_history_to_cache()
local cache_path = get_cache_path()
if not cache_path then
vim.notify("no cache_path")
return
end

if cache_path then
vim.fn.mkdir(vim.fn.fnamemodify(cache_path, ":h"), "p")
local serialized_history = "return " .. vim.inspect(M.cmd_history)
vim.fn.writefile(vim.split(serialized_history, "\n"), cache_path)
end
end

function M.remove_cache_file()
local cache_path = get_cache_path()
if cache_path and vim.fn.filereadable(cache_path) == 1 then
vim.fn.delete(cache_path)
M.cmd_history = {}
vim.notify("history file removed from: " .. cache_path)
else
vim.notify("no history file found for the current project", vim.log.levels.WARN)
end
end

---@param command string
---@param component string
---@param type string
function M.save_to_history(command, component, type)
if M.check_for_duplicate(command, component, type) then
return
end

if #M.cmd_history >= max_size then
table.remove(M.cmd_history, 1)
end

table.insert(M.cmd_history, { command = command, component = component, type = type })
M.write_history_to_cache()
end

---@param component string
function M.remove_from_history(component)
for i, item in ipairs(M.cmd_history) do
if item.component == component then
table.remove(M.cmd_history, i)
M.write_history_to_cache()
break
end
end
end

---@return table<string>
function M.get_all_descriptions()
local descriptions = {}
for _, item in ipairs(M.cmd_history) do
table.insert(descriptions, item.component)
end
return descriptions
end

---@param component string
---@return string|nil
function M.get_command_by_component(component)
for _, item in ipairs(M.cmd_history) do
if item.component == component then
return item.command
end
end
return nil
end

---@param component string
---@return string
function M.get_type_by_component(component)
for _, item in ipairs(M.cmd_history) do
if item.component == component then
return item.type
end
end
return ""
end

return M
16 changes: 16 additions & 0 deletions lua/java_test_util/init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
local default_config = require("java_test_util.config")
local core = require("java_test_util.core")
local term = require("java_test_util.terminal")
local menu = require("java_test_util.menu")
local util = require("java_test_util.util")
local history = require("java_test_util.history")

local M = {}

function M.setup(opts)
---@type Config
M.config = vim.tbl_deep_extend("force", default_config, opts or {})
core.config = M.config
term.config = M.config
history.config = M.config
menu.config = M.config
util.config = M.config

util.root_dir = util.get_project_root()

if util.root_dir then
util.build_tool = util.detect_build_tool()
history.load_cached_history()
end
end

M.run_mvn_test_for_current_method = core.run_mvn_test_for_current_method
M.run_mvn_test_for_current_class = core.run_mvn_test_for_current_class
M.run_mvn_test_for_current_package = core.run_mvn_test_for_current_package
M.run_mvn_previous_test = core.run_mvn_previous_test
M.run_mvn_test_for_all = core.run_mvn_test_for_all
M.create_history_menu = menu.create_history_menu
M.detect_build_tool = util.detect_build_tool

return M
Loading

0 comments on commit da92e55

Please sign in to comment.