-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathprofile_config.lua
More file actions
232 lines (211 loc) · 5.79 KB
/
profile_config.lua
File metadata and controls
232 lines (211 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
local runner = require('async.runner')
local get_error_handler = require('java-core.utils.error_handler')
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 runner(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 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)
if not M.has_profile(profile_name, dap_config_name) then
return
end
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)
if not M.has_profile(profile_name, dap_config_name) then
return
end
M.project_profiles[dap_config_name][profile_name] = nil
M.save()
end
---@private
---Returns true if a profile exists by given name
---@param profile_name string
---@param dap_config_name string
function M.has_profile(profile_name, dap_config_name)
if M.project_profiles[dap_config_name][profile_name] then
return true
end
return false
end
--- @type Profile
M.Profile = Profile
M.setup = function()
runner(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