Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ tests/

**Config merging:** `vim.tbl_deep_extend('force', global_config, user_config or {})`

**Profiles:** `JavaProfile` persists vm args, program args, inline env entries, and an env file; inline env overrides env file values for both DAP and built-in runner

**Config type sync:** When modifying `lua/java/config.lua` (add/update/delete properties), update both `java.Config` type and `java.PartialConfig` in `lua/java.lua` to keep types in sync

**Complex types:** If type contains complex object, create class type instead of inlining type everywhere
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Yep! That's all :)

### Profiles

- `JavaProfile` - Opens the profiles UI
- `JavaProfile` - Opens the profiles UI for VM args, program args, env, and env file

### Refactor

Expand Down Expand Up @@ -245,6 +245,9 @@ require('java').test.view_last_report()
require('java').profile.ui()
```

Profiles can store VM args, program args, `KEY=VALUE` env entries, and an env file path.
Inline env overrides env file values. Relative env file paths resolve from the current project cwd.

### Refactor

- `extract_variable` - Create a variable from value at cursor/selection
Expand Down
5 changes: 4 additions & 1 deletion doc/nvim-java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ TEST ~

PROFILES ~

- `JavaProfile` - Opens the profiles UI
- `JavaProfile` - Opens the profiles UI for vm args, program args, env, and env file


REFACTOR ~
Expand Down Expand Up @@ -262,6 +262,9 @@ PROFILES ~
require('java').profile.ui()
<

Profiles can store vm args, program args, `KEY=VALUE` env entries, and an env file path.
Inline env overrides env file values. Relative env file paths resolve from the current project cwd.


REFACTOR ~

Expand Down
31 changes: 29 additions & 2 deletions lua/java-dap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ end
---Configure dap
function M.config_dap()
local get_error_handler = require('java-core.utils.error_handler')
local notify = require('java-core.utils.notify')
local runner = require('async.runner')

return runner(function()
local lsp_utils = require('java-core.utils.lsp')
local nvim_dap = require('dap')
local profile_config = require('java.api.profile_config')
local env_utils = require('java.utils.env')
local DapSetup = require('java-dap.setup')

local client = lsp_utils.get_jdtls()
Expand All @@ -51,21 +53,46 @@ function M.config_dap()
----------------------------------------------------------------------

local dap_config = dap:get_dap_config()
local applied_dap_config = {}
local preserved_dap_config = {}
for _, config in ipairs(nvim_dap.configurations.java or {}) do
if not config._nvim_java_managed then
table.insert(preserved_dap_config, config)
end
end

for _, config in ipairs(dap_config) do
local is_valid = true
local profile = profile_config.get_active_profile(config.name)
if profile then
config.vmArgs = profile.vm_args
config.args = profile.prog_args

local env, err = env_utils.load_profile_env(
profile,
profile_config.current_project_path
)
if err then
notify.error(err)
is_valid = false
else
config.env = env
config.envFile = profile.env_file
end
end

if is_valid then
config._nvim_java_managed = true
table.insert(applied_dap_config, config)
end
end

if nvim_dap.session then
nvim_dap.terminate()
end

nvim_dap.configurations.java = nvim_dap.configurations.java or {}
vim.list_extend(nvim_dap.configurations.java, dap_config)
nvim_dap.configurations.java = preserved_dap_config
vim.list_extend(nvim_dap.configurations.java, applied_dap_config)
end)
.catch(get_error_handler('dap configuration failed'))
.run()
Expand Down
4 changes: 3 additions & 1 deletion lua/java-runner/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ function Run:_init(dap_config)
end

---@param cmd string[]
function Run:start(cmd)
---@param env table<string, string>|nil
function Run:start(cmd, env)
local merged_cmd = table.concat(cmd, ' ')
self.is_running = true
self:send_term(merged_cmd)

self.job_chan_id = vim.fn.jobstart(merged_cmd, {
env = env,
pty = true,
on_stdout = function(_, data)
self:send_term(data)
Expand Down
22 changes: 18 additions & 4 deletions lua/java-runner/runner.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local ui = require('java.ui.utils')
local class = require('java-core.utils.class')
local lsp_utils = require('java-core.utils.lsp')
local notify = require('java-core.utils.notify')
local profile_config = require('java.api.profile_config')
local Run = require('java-runner.run')
local RunLogger = require('java-runner.run-logger')
local DapSetup = require('java-dap.setup')
local env_utils = require('java.utils.env')

---@class java.Runner
---@field runs table<string, java.Run>
Expand All @@ -20,7 +22,7 @@ end
---Starts a new run
---@param args string
function Runner:start_run(args)
local cmd, dap_config = self:select_dap_config(args)
local cmd, dap_config, env = self:select_dap_config(args)

if not cmd or not dap_config then
return
Expand All @@ -41,7 +43,7 @@ function Runner:start_run(args)
self.curr_run = run
self.logger:set_buffer(run.buffer)

run:start(cmd)
run:start(cmd, env)
end

---Stops the user selected run
Expand Down Expand Up @@ -100,6 +102,7 @@ end
---@param args string additional program arguments to pass
---@return string[] | nil
---@return java-dap.DapLauncherConfig | nil
---@return table<string, string> | nil
function Runner:select_dap_config(args)
local dap = DapSetup(lsp_utils.get_jdtls())
local dap_config_list = dap:get_dap_config()
Expand All @@ -109,7 +112,7 @@ function Runner:select_dap_config(args)
end)

if not selected_dap_config then
return nil, nil
return nil, nil, nil
end

local enriched_config = dap:enrich_config(selected_dap_config)
Expand All @@ -122,10 +125,21 @@ function Runner:select_dap_config(args)

local vm_args = ''
local prog_args = args
local env = nil

if active_profile then
prog_args = (active_profile.prog_args or '') .. ' ' .. (args or '')
vm_args = active_profile.vm_args or ''

local err
env, err = env_utils.load_profile_env(
active_profile,
profile_config.current_project_path
)
if err then
notify.error(err)
return nil, nil, nil
end
end

local cmd = {
Expand All @@ -137,7 +151,7 @@ function Runner:select_dap_config(args)
prog_args,
}

return cmd, selected_dap_config
return cmd, selected_dap_config, env
end

return Runner
23 changes: 21 additions & 2 deletions lua/java/api/profile_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ local config_path = vim.fn.stdpath('data') .. '/nvim-java-profiles.json'
--- @field prog_args string
--- @field name string
--- @field is_active boolean
--- @field env table<string, string>
--- @field env_file string|nil
local Profile = class()

--- @param vm_args string
--- @param prog_args string
--- @param name string
--- @param is_active boolean
function Profile:_init(vm_args, prog_args, name, is_active)
--- @param env table<string, string>|nil
--- @param env_file string|nil
function Profile:_init(vm_args, prog_args, name, is_active, env, env_file)
self.vm_args = vm_args
self.prog_args = prog_args
self.name = name
self.is_active = is_active or false
self.env = env or {}
self.env_file = env_file
end

-- palin config structure
Expand All @@ -30,12 +36,18 @@ end
-- "vm_args": "-Xmx1024m",
-- "prog_args": "arg1 arg2",
-- "name": "profile_name1",
-- "env": {
-- "SPRING_PROFILES_ACTIVE": "dev"
-- },
-- "env_file": ".env.dev",
-- "is_active": true
-- },
-- {
-- "vm_args": "-Xmx1024m",
-- "prog_args": "arg1 arg2",
-- "name": "profile_name2",
-- "env": {},
-- "env_file": ".env.local",
-- "is_active": false
-- }
-- ],
Expand Down Expand Up @@ -90,7 +102,14 @@ function M.load_current_project_profiles()
result[dap_config_name] = {}
for _, profile in pairs(dap_config_name_val) do
result[dap_config_name][profile.name] =
Profile(profile.vm_args, profile.prog_args, profile.name, profile.is_active)
Profile(
profile.vm_args,
profile.prog_args,
profile.name,
profile.is_active,
profile.env,
profile.env_file
)
end
end
return result
Expand Down
Loading