@@ -6,29 +6,45 @@ local defaults = {
6
6
debounce = 250 ,
7
7
keep_alive_interval = 60000 , -- 60 seconds,
8
8
save_after_format = true ,
9
- on_attach = nil ,
10
- generators = {},
11
- filetypes = {},
12
- names = {}
9
+ _generators = {} ,
10
+ _filetypes = {},
11
+ _names = {},
12
+ _setup = false
13
13
}
14
14
15
+ local type_overrides = {
16
+ on_attach = {" function" , " nil" },
17
+ sources = {" table" , " nil" }
18
+ }
19
+
20
+ local wanted_type = function (k )
21
+ if vim .startswith (k , " _" ) then return " nil" , true end
22
+
23
+ local override = type_overrides [k ]
24
+ if type (override ) == " string" then return override , true end
25
+ if type (override ) == " table" then
26
+ return function (a ) return vim .tbl_contains (override , type (a )) end ,
27
+ table.concat (override , " , " )
28
+ end
29
+
30
+ return type (defaults [k ]), true
31
+ end
32
+
15
33
local config = vim .deepcopy (defaults )
16
34
17
35
-- allow plugins to call register multiple times without duplicating sources
18
36
local is_registered = function (name , insert )
19
37
if not name then return false end
38
+ if vim .tbl_contains (config ._names , name ) then return true end
20
39
21
- if vim .tbl_contains (config .names , name ) then return true end
22
-
23
- if insert then table.insert (config .names , name ) end
24
-
40
+ if insert then table.insert (config ._names , name ) end
25
41
return false
26
42
end
27
43
28
44
local register_filetypes = function (filetypes )
29
45
for _ , filetype in pairs (filetypes ) do
30
- if not vim .tbl_contains (config .filetypes , filetype ) then
31
- table.insert (config .filetypes , filetype )
46
+ if not vim .tbl_contains (config ._filetypes , filetype ) then
47
+ table.insert (config ._filetypes , filetype )
32
48
end
33
49
end
34
50
end
@@ -49,11 +65,11 @@ local register_source = function(source, filetypes)
49
65
local fn , async = generator .fn , generator .async
50
66
validate ({fn = {fn , " function" }, async = {async , " boolean" , true }})
51
67
52
- if not config .generators [method ] then config .generators [method ] = {} end
68
+ if not config ._generators [method ] then config ._generators [method ] = {} end
53
69
register_filetypes (filetypes )
54
70
55
71
generator .filetypes = filetypes
56
- table.insert (config .generators [method ], generator )
72
+ table.insert (config ._generators [method ], generator )
57
73
58
74
-- plugins that register sources after BufEnter may need to call try_attach() again,
59
75
-- after filetypes have been registered
@@ -79,51 +95,48 @@ local register = function(to_register)
79
95
if is_registered (name , true ) then return end
80
96
81
97
validate ({sources = {sources , " table" }, name = {name , " string" , true }})
82
-
83
98
for _ , source in pairs (sources ) do register_source (source , filetypes ) end
84
99
end
85
100
86
101
local M = {}
87
102
88
103
M .get = function () return config end
89
-
90
104
M .reset = function () config = vim .deepcopy (defaults ) end
91
105
92
106
M .is_registered = is_registered
93
-
94
107
M .register = register
95
-
96
- M .reset_sources = function () config .generators = {} end
108
+ M .reset_sources = function () config ._generators = {} end
97
109
98
110
M .generators = function (method )
99
- if method then return config .generators [method ] end
100
- return config .generators
111
+ return method and config ._generators [method ] or config ._generators
101
112
end
102
113
103
- M .setup = function (user_config )
104
- local on_attach , debounce , user_sources , save_after_format ,
105
- keep_alive_interval = user_config .on_attach , user_config .debounce ,
106
- user_config .sources ,
107
- user_config .save_after_format ,
108
- user_config .keep_alive_interval
114
+ local validate_config = function (user_config )
115
+ local to_validate , validated = {}, {}
109
116
110
- validate ({
111
- on_attach = {on_attach , " function" , true },
112
- debounce = {debounce , " number" , true },
113
- sources = {user_sources , " table" , true },
114
- save_after_format = {save_after_format , " boolean" , true },
115
- keep_alive_interval = {keep_alive_interval , " number" , true }
116
- })
117
+ local get_wanted = function (config_table )
118
+ for k in pairs (config_table ) do
119
+ local wanted , optional = wanted_type (k )
120
+ to_validate [k ] = {user_config [k ], wanted , optional }
117
121
118
- if on_attach then config .on_attach = on_attach end
119
- if debounce then config .debounce = debounce end
120
- if save_after_format ~= nil then
121
- config .save_after_format = save_after_format
122
- end
123
- if keep_alive_interval then
124
- config .keep_alive_interval = keep_alive_interval
122
+ validated [k ] = user_config [k ]
123
+ end
125
124
end
126
- if user_sources then register (user_sources ) end
125
+ get_wanted (config )
126
+ get_wanted (type_overrides )
127
+
128
+ validate (to_validate )
129
+ return validated
130
+ end
131
+
132
+ M .setup = function (user_config )
133
+ if config ._setup then return end
134
+
135
+ local validated = validate_config (user_config )
136
+ config = vim .tbl_extend (" force" , config , validated )
137
+
138
+ if config .sources then register (config .sources ) end
139
+ config ._setup = true
127
140
end
128
141
129
142
return M
0 commit comments