-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.lua
127 lines (114 loc) · 3.08 KB
/
config.lua
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
---@tag dap-go.config
local open = require('plenary.context_manager').open
local with = require('plenary.context_manager').with
local util = require('dap-go.util')
local uv = vim.loop
---@brief [[
--- Config holds all the configuration to operate the extension.
---
---@brief ]]
---@class Config @Expose configuration for the |dap-go.nvim| extension
local config = {}
config._options = {}
local mt = {}
function mt:__index(k)
return config._options[k]
end
local defaults = {
delay = 100, -- ms
external_config = {
--- Enable external config
enabled = false,
--- File with the config definitions.
path = (function()
local root_dir = require('lspconfig.util').find_git_ancestor(uv.fs_realpath('.')) or '.'
return root_dir .. '/dap-go.json'
end)(),
},
--- nvim-dap configuration for go.
dap = {
configurations = {
{
type = 'go',
name = 'Debug',
request = 'launch',
program = '${file}',
},
{
type = 'go',
name = 'Attach',
mode = 'local',
request = 'attach',
processId = require('dap.utils').pick_process,
},
},
},
}
--- Load configurations from file and merge it with current ones.
---@private
local function load_dap_configurations_from_file()
---@diagnostic disable-next-line: undefined-field
if vim.fn.filereadable(config.external_config.path) == 0 then
return
end
---@diagnostic disable-next-line: undefined-field
local result = with(open(config.external_config.path), function(f)
return f:read('*all')
end)
if result then
local ok, c = util.json_decode(result)
if ok then
for _, v in pairs(c) do
---@diagnostic disable-next-line: undefined-field
table.insert(config.dap.configurations, v)
end
end
end
end
--- Setup function, this function is internally called by |dap-go.setup()| and
--- depending on the configuration will load an external config file.
---
--- Usage:
--- <code>
--- require('dap-go').setup{
--- delay = 100, -- ms
--- external_config = {
--- --- Enable external config
--- enabled = false,
--- --- File with the config definitions.
--- path = (function()
--- local root_dir = require('lspconfig.util').find_git_ancestor(uv.fs_realpath('.')) or '.'
--- return root_dir .. '/dap-go.json'
--- end)(),
--- },
---
--- --- nvim-dap configuration for go.
--- dap = {
--- configurations = {
--- {
--- type = 'go',
--- name = 'Debug',
--- request = 'launch',
--- program = '${file}',
--- },
--- {
--- type = 'go',
--- name = 'Attach',
--- mode = 'local',
--- request = 'attach',
--- processId = require('dap.utils').pick_process,
--- },
--- },
--- },
--- },
--- </code>
---@param options table: Custom options.
function config.setup(options)
config._options = vim.tbl_deep_extend('force', {}, defaults, options or {})
---@diagnostic disable-next-line: undefined-field
if config.external_config.enabled then
load_dap_configurations_from_file()
end
end
setmetatable(config, mt)
return config