-
-
Notifications
You must be signed in to change notification settings - Fork 83
feat: run/debug profile (#110, #131) #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| local async = require('java-core.utils.async').sync | ||
| local get_error_handler = require('java.handlers.error') | ||
| local class = require('java-core.utils.class') | ||
|
|
||
| local config_path = vim.fn.stdpath('data') .. '/nvim-java-profiles.json' | ||
|
|
||
| --- @class Profile | ||
| --- @field vm_args string | ||
| --- @field prog_args string | ||
| --- @field name string | ||
| --- @field is_active boolean | ||
| 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) | ||
| self.vm_args = vm_args | ||
| self.prog_args = prog_args | ||
| self.name = name | ||
| self.is_active = is_active or false | ||
| end | ||
|
|
||
| -- palin config structure | ||
| -- { | ||
| -- "roo_dir" : { | ||
| -- "mod1 -> main_class" : [ | ||
| -- { | ||
| -- "vm_args": "-Xmx1024m", | ||
| -- "prog_args": "arg1 arg2", | ||
| -- "name": "profile_name1", | ||
| -- "is_active": true | ||
| -- }, | ||
| -- { | ||
| -- "vm_args": "-Xmx1024m", | ||
| -- "prog_args": "arg1 arg2", | ||
| -- "name": "profile_name2", | ||
| -- "is_active": false | ||
| -- } | ||
| -- ], | ||
| -- "mod2 -> main_class" : [ | ||
| -- ... | ||
| -- ] | ||
| -- } | ||
| -- } | ||
| --- @return table | ||
| local function read_full_config() | ||
| local file = io.open(config_path, 'r') | ||
| if not file then | ||
| return {} | ||
| end | ||
| local data = file:read('*a') | ||
| file:close() | ||
| local ok, config = pcall(vim.fn.json_decode, data) | ||
| if not ok or not config then | ||
| vim.notify('Failed to read config') | ||
| return {} | ||
| end | ||
| return config | ||
| end | ||
|
|
||
| local M = {} | ||
|
|
||
| -- loaded profiles | ||
| -- { | ||
| -- ['mod1 -> dap_config_name'] = { | ||
| -- profile_name1 = { | ||
| -- vm_args = '-Xmx1024m', | ||
| -- prog_args = 'arg1 arg2', | ||
| -- name = 'profile_name1', | ||
| -- is_active = true, | ||
| -- }, | ||
| -- profile_name2 = { | ||
| -- vm_args = '-Xmx1024m', | ||
| -- prog_args = 'arg1 arg2', | ||
| -- name = 'profile_name2', | ||
| -- is_active = false, | ||
| -- }, | ||
| -- }, | ||
| -- ['mod2 -> dap_config_name'] = { | ||
| -- .... | ||
| -- }, | ||
| -- } | ||
| --- @return table<string, table<string, Profile>> | ||
| function M.load_current_project_profiles() | ||
| local result = {} | ||
| local current = read_full_config()[M.current_project_path] or {} | ||
| for dap_config_name, dap_config_name_val in pairs(current) do | ||
| 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 | ||
| ) | ||
| end | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| function M.save() | ||
| return async(function() | ||
| local full_config = read_full_config() | ||
| local updated_profiles = {} | ||
| for dap_config_name, val in pairs(M.project_profiles) do | ||
| updated_profiles[dap_config_name] = {} | ||
| for _, profile in pairs(val) do | ||
| table.insert(updated_profiles[dap_config_name], profile) | ||
| end | ||
| end | ||
| full_config[M.current_project_path] = updated_profiles | ||
| local ok, json = pcall(vim.fn.json_encode, full_config) | ||
| assert(ok, 'Failed to encode json') | ||
| local file = io.open(config_path, 'w') | ||
| assert(file, 'Failed to open file') | ||
| file:write(json) | ||
| file:close() | ||
| end) | ||
| .catch(get_error_handler('Failed to save profile')) | ||
| .run() | ||
| end | ||
| --- @param dap_config_name string | ||
| --- @return Profile|nil | ||
| function M.get_active_profile(dap_config_name) | ||
| if M.project_profiles[dap_config_name] == nil then | ||
| return nil | ||
| end | ||
| local number_of_profiles = 0 | ||
| for _, profile in pairs(M.project_profiles[dap_config_name]) do | ||
| number_of_profiles = number_of_profiles + 1 | ||
| if profile.is_active then | ||
| return profile | ||
| end | ||
| end | ||
| if number_of_profiles > 0 then | ||
| error('No active profile') | ||
| end | ||
| end | ||
|
|
||
| --- @param dap_config_name string | ||
| --- @param profile_name string | ||
| --- @return Profile|nil | ||
| function M.get_profile(profile_name, dap_config_name) | ||
| if M.project_profiles[dap_config_name] == nil then | ||
| return nil | ||
| end | ||
| return M.project_profiles[dap_config_name][profile_name] | ||
| end | ||
|
|
||
| --- @param dap_config_name_key string | ||
| --- @return table<string, Profile> | ||
| function M.get_all_profiles(dap_config_name) | ||
| return M.project_profiles[dap_config_name] or {} | ||
| end | ||
| --- @param dap_config_name string | ||
| --- @param profile_name string | ||
| function M.set_active_profile(profile_name, dap_config_name) | ||
| for _, profile in pairs(M.project_profiles[dap_config_name]) do | ||
| if profile.is_active then | ||
| profile.is_active = false | ||
| break | ||
| end | ||
| end | ||
| M.project_profiles[dap_config_name][profile_name].is_active = true | ||
| M.save() | ||
| end | ||
|
|
||
| --- @param dap_config_name string | ||
| --- @param current_profile_name string|nil | ||
| --- @param new_profile Profile | ||
| function M.add_or_update_profile( | ||
| new_profile, | ||
| current_profile_name, | ||
| dap_config_name | ||
| ) | ||
| assert(new_profile.name, 'Profile name is required') | ||
| if current_profile_name then | ||
| M.project_profiles[dap_config_name][current_profile_name] = nil | ||
| end | ||
| assert( | ||
| M.get_profile(new_profile.name, dap_config_name) == nil, | ||
| "Profile with name '" .. new_profile.name .. "' already exists" | ||
| ) | ||
|
|
||
| if M.project_profiles[dap_config_name] == nil then | ||
| M.project_profiles[dap_config_name] = {} | ||
| else | ||
| for _, profile in pairs(M.project_profiles[dap_config_name]) do | ||
| profile.is_active = false | ||
| end | ||
| end | ||
|
|
||
| new_profile.is_active = true | ||
| M.project_profiles[dap_config_name][new_profile.name] = new_profile | ||
| M.save() | ||
| end | ||
|
|
||
| --- @param dap_config_name string | ||
| --- @param profile_name string | ||
| function M.delete_profile(profile_name, dap_config_name) | ||
| M.project_profiles[dap_config_name][profile_name] = nil | ||
| M.save() | ||
| end | ||
|
|
||
| --- @type Profile | ||
| M.Profile = Profile | ||
|
|
||
| M.setup = function() | ||
| async(function() | ||
| M.current_project_path = vim.fn.getcwd() | ||
| M.project_profiles = M.load_current_project_profiles() | ||
| end) | ||
| .catch(get_error_handler('Failed to read profile config')) | ||
| .run() | ||
| end | ||
|
|
||
| return M | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.