Skip to content
68 changes: 57 additions & 11 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

-- Set to true if you have a Nerd Font installed and selected in the terminal
vim.g.have_nerd_font = false
-- vim.g.have_nerd_font = false
vim.g.have_nerd_font = true

-- [[ Setting options ]]
-- See `:help vim.opt`
Expand Down Expand Up @@ -393,11 +394,18 @@ require('lazy').setup({
-- You can put your default mappings / updates / etc. in here
-- All the info you're looking for is in `:help telescope.setup()`
--
-- defaults = {
-- mappings = {
-- i = { ['<c-enter>'] = 'to_fuzzy_refine' },
-- },
-- },
defaults = {
mappings = {
n = {
['<c-d>'] = require('telescope.actions').delete_buffer,
},
i = {
['<C-h>'] = 'which_key',
['<c-d>'] = require('telescope.actions').delete_buffer,
},
-- i = { ['<c-enter>'] = 'to_fuzzy_refine' },
},
},
-- pickers = {}
extensions = {
['ui-select'] = {
Expand Down Expand Up @@ -554,10 +562,25 @@ require('lazy').setup({
-- or a suggestion from your LSP for this to activate.
map('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction', { 'n', 'x' })

-- Organize imports (Python via Pyright code actions)
map('<leader>co', function()
vim.lsp.buf.code_action({
context = {
only = { 'source.organizeImports' },
},
apply = true,
})
end, '[C]ode [O]rganize Imports')

-- WARN: This is not Goto Definition, this is Goto Declaration.
-- For example, in C this would take you to the header.
map('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')

map('gDef', vim.lsp.buf.hover, '[G]oto [Def]inition')

-- Show LSP message
map('gM', vim.diagnostic.open_float, '[G]oto [M]essage')

-- This function resolves a difference between neovim nightly (version 0.11) and stable (version 0.10)
---@param client vim.lsp.Client
---@param method vim.lsp.protocol.Method
Expand Down Expand Up @@ -660,7 +683,21 @@ require('lazy').setup({
local servers = {
-- clangd = {},
-- gopls = {},
-- pyright = {},
pyright = {
settings = {
python = {
analysis = {
autoSearchPaths = true,
useLibraryCodeForTypes = true,
diagnosticMode = 'workspace',
typeCheckingMode = 'basic',
autoImportCompletions = true,
},
},
},
},
-- NOTE: jdtls is configured separately via nvim-jdtls plugin (see custom/plugins/jdtls.lua)
-- Do not configure it here as it requires special handling
-- rust_analyzer = {},
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
--
Expand Down Expand Up @@ -703,6 +740,11 @@ require('lazy').setup({
local ensure_installed = vim.tbl_keys(servers or {})
vim.list_extend(ensure_installed, {
'stylua', -- Used to format Lua code
'jdtls', -- Java language server
'java-debug-adapter', -- Java debugger
'java-test', -- Java test runner
'isort', -- Python import organizer
'black', -- Python formatter
})
require('mason-tool-installer').setup { ensure_installed = ensure_installed }

Expand All @@ -711,6 +753,10 @@ require('lazy').setup({
automatic_installation = false,
handlers = {
function(server_name)
-- Skip jdtls as it's configured separately via nvim-jdtls plugin
if server_name == 'jdtls' then
return
end
local server = servers[server_name] or {}
-- This handles overriding only values explicitly passed
-- by the server configuration above. Useful when disabling
Expand Down Expand Up @@ -758,7 +804,7 @@ require('lazy').setup({
formatters_by_ft = {
lua = { 'stylua' },
-- Conform can also run multiple formatters sequentially
-- python = { "isort", "black" },
python = { "isort", "black" },
--
-- You can use 'stop_after_first' to run the first available formatter from the list
-- javascript = { "prettierd", "prettier", stop_after_first = true },
Expand Down Expand Up @@ -985,14 +1031,14 @@ require('lazy').setup({
-- require 'kickstart.plugins.indent_line',
-- require 'kickstart.plugins.lint',
-- require 'kickstart.plugins.autopairs',
-- require 'kickstart.plugins.neo-tree',
-- require 'kickstart.plugins.gitsigns', -- adds gitsigns recommend keymaps
require 'kickstart.plugins.neo-tree',
require 'kickstart.plugins.gitsigns', -- adds gitsigns recommend keymaps

-- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
-- This is the easiest way to modularize your config.
--
-- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going.
-- { import = 'custom.plugins' },
{ import = 'custom.plugins' },
--
-- For additional information with loading, sourcing and examples see `:help lazy.nvim-🔌-plugin-spec`
-- Or use telescope!
Expand Down
195 changes: 195 additions & 0 deletions lua/custom/maven.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
-- Maven command runner with UI
local M = {}

-- Store last command for quick re-run
M.last_command = nil

-- Common Maven phases and goals
M.maven_commands = {
-- Lifecycle Phases
{ name = "clean", desc = "Clean the project (remove target/)" },
{ name = "validate", desc = "Validate project structure" },
{ name = "compile", desc = "Compile source code" },
{ name = "test", desc = "Run unit tests" },
{ name = "test-compile", desc = "Compile test sources" },
{ name = "package", desc = "Package compiled code (JAR/WAR)" },
{ name = "verify", desc = "Run integration tests" },
{ name = "install", desc = "Install package to local repository" },
{ name = "deploy", desc = "Deploy to remote repository" },

-- Common Combined Commands
{ name = "clean compile", desc = "Clean and compile" },
{ name = "clean test", desc = "Clean and test" },
{ name = "clean package", desc = "Clean and package" },
{ name = "clean install", desc = "Clean and install" },

-- Plugin Goals
{ name = "dependency:tree", desc = "Display dependency tree" },
{ name = "dependency:analyze", desc = "Analyze dependencies" },
{ name = "versions:display-dependency-updates", desc = "Check for dependency updates" },
{ name = "help:effective-pom", desc = "Show effective POM" },

-- Spring Boot specific (if applicable)
{ name = "spring-boot:run", desc = "Run Spring Boot application" },

-- Testing specific
{ name = "test -Dtest=", desc = "Run specific test class (specify after =)", custom_input = true },
{ name = "test -Dtest=*Test", desc = "Run all test classes" },
{ name = "surefire-report:report", desc = "Generate test report" },
}

-- Execute Maven command
function M.execute_maven(cmd, opts)
opts = opts or {}

-- Check if mvn exists
local mvn_cmd = vim.fn.executable("mvn") == 1 and "mvn" or nil
if not mvn_cmd then
vim.notify("Maven (mvn) not found in PATH. Install with: brew install maven", vim.log.levels.ERROR)
return
end

-- Store last command
M.last_command = cmd

-- Determine if we're in a Maven project
local pom_exists = vim.fn.filereadable(vim.fn.getcwd() .. "/pom.xml") == 1
if not pom_exists then
vim.notify("No pom.xml found in current directory: " .. vim.fn.getcwd(), vim.log.levels.WARN)
local proceed = vim.fn.confirm("Run Maven anyway?", "&Yes\n&No", 2)
if proceed ~= 1 then
return
end
end

local full_cmd = "mvn " .. cmd

-- Choose display method based on preference
if opts.background then
-- Run in background, show in quickfix
vim.notify("Running: " .. full_cmd, vim.log.levels.INFO)
vim.fn.setqflist({}, 'r', { title = full_cmd, lines = {} })
vim.cmd("copen")

vim.fn.jobstart(full_cmd, {
on_stdout = function(_, data)
if data then
vim.fn.setqflist({}, 'a', { lines = data })
end
end,
on_stderr = function(_, data)
if data then
vim.fn.setqflist({}, 'a', { lines = data })
end
end,
on_exit = function(_, exit_code)
if exit_code == 0 then
vim.notify("Maven command completed successfully", vim.log.levels.INFO)
else
vim.notify("Maven command failed with exit code: " .. exit_code, vim.log.levels.ERROR)
end
end,
})
else
-- Run in terminal split
vim.cmd("botright 15split | terminal " .. full_cmd)
vim.cmd("startinsert")
end
end

-- Show Maven menu using vim.ui.select
function M.show_menu()
local choices = {}
for i, cmd in ipairs(M.maven_commands) do
choices[i] = string.format("%-40s | %s", cmd.name, cmd.desc)
end

vim.ui.select(choices, {
prompt = "Select Maven command:",
format_item = function(item)
return item
end,
}, function(choice, idx)
if not choice then
return
end

local selected = M.maven_commands[idx]

-- Handle custom input commands
if selected.custom_input then
vim.ui.input({
prompt = "Enter Maven command: ",
default = selected.name,
}, function(input)
if input and input ~= "" then
M.execute_maven(input)
end
end)
else
M.execute_maven(selected.name)
end
end)
end

-- Run last Maven command
function M.run_last()
if M.last_command then
vim.notify("Re-running: mvn " .. M.last_command, vim.log.levels.INFO)
M.execute_maven(M.last_command)
else
vim.notify("No previous Maven command to run", vim.log.levels.WARN)
M.show_menu()
end
end

-- Quick command to run specific test
function M.run_test(test_name)
if test_name then
M.execute_maven("test -Dtest=" .. test_name)
else
-- Try to infer test name from current file
local current_file = vim.fn.expand("%:t:r") -- filename without extension
if current_file:match("Test$") or current_file:match("Tests$") then
vim.ui.input({
prompt = "Test class name:",
default = current_file,
}, function(input)
if input and input ~= "" then
M.execute_maven("test -Dtest=" .. input)
end
end)
else
M.execute_maven("test")
end
end
end

-- User command to run Maven
vim.api.nvim_create_user_command("Maven", function(opts)
if opts.args and opts.args ~= "" then
M.execute_maven(opts.args)
else
M.show_menu()
end
end, {
nargs = "*",
desc = "Run Maven command",
complete = function()
local completions = {}
for _, cmd in ipairs(M.maven_commands) do
table.insert(completions, cmd.name)
end
return completions
end,
})

-- User command to run tests
vim.api.nvim_create_user_command("MavenTest", function(opts)
M.run_test(opts.args ~= "" and opts.args or nil)
end, {
nargs = "?",
desc = "Run Maven tests",
})

return M
Loading
Loading