Skip to content

Commit f073cba

Browse files
committed
legacy generate handles action =
1 parent 90ea114 commit f073cba

File tree

1 file changed

+49
-89
lines changed

1 file changed

+49
-89
lines changed

lua/nvim-tree/keymap-legacy.lua

Lines changed: 49 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -168,94 +168,6 @@ local function generate_on_attach_function(list, unmapped_keys, remove_defaults)
168168
end
169169
end
170170

171-
local function generate_legacy_default_mappings()
172-
local mappings = {}
173-
174-
for a, m in pairs(LEGACY_MAPPINGS) do
175-
table.insert(mappings, {
176-
action = a,
177-
desc = m.desc,
178-
key = m.key,
179-
})
180-
end
181-
182-
return mappings
183-
end
184-
185-
-- taken from actions/init merge_mappings and filter_mappings
186-
local function generate_legacy_active_mappings(user_mappings, defaults, remove_keys, remove_defaults)
187-
local filtered_defaults
188-
189-
--
190-
-- filter_mappings
191-
--
192-
if remove_defaults then
193-
filtered_defaults = {}
194-
else
195-
filtered_defaults = vim.tbl_filter(function(m)
196-
if type(m.key) == "table" then
197-
m.key = vim.tbl_filter(function(k)
198-
return not vim.tbl_contains(remove_keys, k)
199-
end, m.key)
200-
return #m.key > 0
201-
else
202-
return not vim.tbl_contains(remove_keys, m.key)
203-
end
204-
end, vim.deepcopy(defaults))
205-
end
206-
207-
--
208-
-- merge_mappings
209-
--
210-
if #user_mappings == 0 then
211-
return filtered_defaults
212-
end
213-
214-
local function is_empty(s)
215-
return s == ""
216-
end
217-
218-
local user_keys = {}
219-
local removed_keys = {}
220-
-- remove default mappings if action is a empty string
221-
for _, map in pairs(user_mappings) do
222-
if type(map.key) == "table" then
223-
for _, key in pairs(map.key) do
224-
table.insert(user_keys, key)
225-
if is_empty(map.action) then
226-
table.insert(removed_keys, key)
227-
end
228-
end
229-
else
230-
table.insert(user_keys, map.key)
231-
if is_empty(map.action) then
232-
table.insert(removed_keys, map.key)
233-
end
234-
end
235-
end
236-
237-
local default_map = vim.tbl_filter(function(map)
238-
if type(map.key) == "table" then
239-
local filtered_keys = {}
240-
for _, key in pairs(map.key) do
241-
if not vim.tbl_contains(user_keys, key) and not vim.tbl_contains(removed_keys, key) then
242-
table.insert(filtered_keys, key)
243-
end
244-
end
245-
map.key = filtered_keys
246-
return not vim.tbl_isempty(map.key)
247-
else
248-
return not vim.tbl_contains(user_keys, map.key) and not vim.tbl_contains(removed_keys, map.key)
249-
end
250-
end, filtered_defaults)
251-
252-
local user_map = vim.tbl_filter(function(map)
253-
return not is_empty(map.action)
254-
end, user_mappings)
255-
256-
return vim.fn.extend(default_map, user_map)
257-
end
258-
259171
local function generate_on_attach_lua(list, unmapped_keys, remove_defaults)
260172
local lua = [[
261173
local api = require('nvim-tree.api')
@@ -312,6 +224,54 @@ local on_attach = function(bufnr)
312224
return lua
313225
end
314226

227+
local function generate_legacy_default_mappings()
228+
local mappings = {}
229+
230+
for a, m in pairs(LEGACY_MAPPINGS) do
231+
table.insert(mappings, {
232+
action = a,
233+
desc = m.desc,
234+
key = m.key,
235+
})
236+
end
237+
238+
return mappings
239+
end
240+
241+
local function generate_legacy_active_mappings(list, defaults, unmapped_keys, remove_defaults)
242+
local filtered_defaults
243+
244+
--
245+
-- remove explicitly unmapped defaults
246+
--
247+
if remove_defaults then
248+
filtered_defaults = {}
249+
else
250+
filtered_defaults = vim.tbl_filter(function(m)
251+
if type(m.key) == "table" then
252+
m.key = vim.tbl_filter(function(k)
253+
return not vim.tbl_contains(unmapped_keys, k)
254+
end, m.key)
255+
return #m.key > 0
256+
else
257+
return not vim.tbl_contains(unmapped_keys, m.key)
258+
end
259+
end, vim.deepcopy(defaults))
260+
end
261+
262+
--
263+
-- remove user action = ""
264+
--
265+
local user_map = vim.tbl_filter(function(map)
266+
return map.action ~= ""
267+
end, list)
268+
269+
--
270+
-- merge
271+
--
272+
return vim.fn.extend(filtered_defaults, user_map)
273+
end
274+
315275
function M.generate_legacy_on_attach(opts)
316276
if type(opts.on_attach) == "function" then
317277
return
@@ -333,7 +293,7 @@ function M.generate_legacy_on_attach(opts)
333293
M.on_attach_lua = generate_on_attach_lua(list, unmapped_keys, remove_defaults)
334294

335295
M.legacy_default = generate_legacy_default_mappings()
336-
M.legacy_active = generate_legacy_active_mappings(list, M.legacy_default, remove_keymaps, remove_defaults)
296+
M.legacy_active = generate_legacy_active_mappings(list, M.legacy_default, unmapped_keys, remove_defaults)
337297
end
338298

339299
function M.generate_on_attach()

0 commit comments

Comments
 (0)