Skip to content

Commit

Permalink
chore(nvim): Replace all vim.tbl_filter and vim.tbl_map with vim.iter…
Browse files Browse the repository at this point in the history
… variants
  • Loading branch information
mrjones2014 committed Dec 15, 2023
1 parent 482e18d commit c3f6eb6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 72 deletions.
42 changes: 24 additions & 18 deletions nvim/lua/my/configure/heirline/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ local diagnostics_base = {
init = function(self)
local all_diagnostics = vim.diagnostic.get(0)
for key, severity in pairs(severities) do
self[key] = #vim.tbl_filter(function(d)
return d.severity == severity
end, all_diagnostics)
self[key] = #vim
.iter(all_diagnostics)
:filter(function(d)
return d.severity == severity
end)
:totable()
end
end,
}
Expand All @@ -65,22 +68,25 @@ function M.Diagnostics(is_winbar, bg)
bg = bg or 'bg_statusline'
return utils.insert(
diagnostics_base,
unpack(vim.tbl_map(function(severity)
local component = {
provider = function(self)
return string.format('%s%s ', icons[severity], self[severity])
end,
hl = function()
return { fg = utils.get_highlight(string.format('DiagnosticSign%s', severity_hl[severity])).fg, bg = bg }
end,
}
if is_winbar then
component.condition = function(self)
return self[severity] > 0
unpack(vim
.iter(severities_order)
:map(function(severity)
local component = {
provider = function(self)
return string.format('%s%s ', icons[severity], self[severity])
end,
hl = function()
return { fg = utils.get_highlight(string.format('DiagnosticSign%s', severity_hl[severity])).fg, bg = bg }
end,
}
if is_winbar then
component.condition = function(self)
return self[severity] > 0
end
end
end
return component
end, severities_order))
return component
end)
:totable())
)
end

Expand Down
17 changes: 10 additions & 7 deletions nvim/lua/my/configure/heirline/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@ local function unsaved_count()
if #vim.fn.expand('%') == 0 then
return 0
else
return #vim.tbl_filter(function(buf)
return vim.bo[buf].ft ~= 'minifiles'
and vim.bo[buf].ft ~= 'dap-repl'
and vim.bo[buf].bt ~= 'acwrite'
and vim.bo[buf].modifiable
and vim.bo[buf].modified
end, vim.api.nvim_list_bufs())
return #vim
.iter(vim.api.nvim_list_bufs())
:filter(function(buf)
return vim.bo[buf].ft ~= 'minifiles'
and vim.bo[buf].ft ~= 'dap-repl'
and vim.bo[buf].bt ~= 'acwrite'
and vim.bo[buf].modifiable
and vim.bo[buf].modified
end)
:totable()
end
end

Expand Down
43 changes: 25 additions & 18 deletions nvim/lua/my/configure/heirline/winbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,45 @@ local conditions = require('heirline.conditions')
local sep = require('my.configure.heirline.separators')

local function get_current_filenames()
local listed_buffers = vim.tbl_filter(function(bufnr)
return vim.bo[bufnr].buflisted and vim.api.nvim_buf_is_loaded(bufnr)
end, vim.api.nvim_list_bufs())
local listed_buffers = vim
.iter(vim.api.nvim_list_bufs())
:filter(function(bufnr)
return vim.bo[bufnr].buflisted and vim.api.nvim_buf_is_loaded(bufnr)
end)
:totable()

return vim.tbl_map(vim.api.nvim_buf_get_name, listed_buffers)
return vim.iter(listed_buffers):map(vim.api.nvim_buf_get_name):totable()
end

-- Get unique name for the current buffer
local function get_unique_filename(filename)
local filenames = vim.tbl_filter(function(filename_other)
return filename_other ~= filename
end, get_current_filenames())

-- Reverse filenames in order to compare their names
local filenames = vim
.iter(get_current_filenames())
:filter(function(filename_other)
return filename_other ~= filename
end)
:map(string.reverse)
:totable() -- Reverse filenames in order to compare their names
filename = string.reverse(filename)
filenames = vim.tbl_map(string.reverse, filenames)

local index

-- For every other filename, compare it with the name of the current file char-by-char to
-- find the minimum index `i` where the i-th character is different for the two filenames
-- After doing it for every filename, get the maximum value of `i`
if next(filenames) then
index = math.max(unpack(vim.tbl_map(function(filename_other)
for i = 1, #filename do
-- Compare i-th character of both names until they aren't equal
if filename:sub(i, i) ~= filename_other:sub(i, i) then
return i
index = math.max(unpack(vim
.iter(filenames)
:map(function(filename_other)
for i = 1, #filename do
-- Compare i-th character of both names until they aren't equal
if filename:sub(i, i) ~= filename_other:sub(i, i) then
return i
end
end
end
return 1
end, filenames)))
return 1
end)
:totable()))
else
index = 1
end
Expand Down
18 changes: 12 additions & 6 deletions nvim/lua/my/configure/mini_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ return {
-- like I would have to in `content.filter` above is too slow. Here we can give it _all_ the entries
-- at once, which is much more performant.
local all_paths = table.concat(
vim.tbl_map(function(entry)
return entry.path
end, entries),
vim
.iter(entries)
:map(function(entry)
return entry.path
end)
:totable(),
'\n'
)
local output_lines = {}
Expand All @@ -72,9 +75,12 @@ return {
vim.fn.chansend(job_id, all_paths)
vim.fn.chanclose(job_id, 'stdin')
vim.fn.jobwait({ job_id })
return require('mini.files').default_sort(vim.tbl_filter(function(entry)
return not vim.tbl_contains(output_lines, entry.path)
end, entries))
return require('mini.files').default_sort(vim
.iter(entries)
:filter(function(entry)
return not vim.tbl_contains(output_lines, entry.path)
end)
:totable())
end,
},
mappings = {
Expand Down
14 changes: 7 additions & 7 deletions nvim/lua/my/legendary/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ function M.lsp_autocmds(bufnr, server_name)
}

if
#vim.tbl_filter(function(autocmd)
return autocmd.buflocal == true and autocmd.buffer == bufnr and autocmd.event == 'CursorHold'
end, autocmds) == 0
not vim.iter(autocmds):find(function(autocmd)
return autocmd.buflocal == true and autocmd.buffer == bufnr and autocmd.event == 'CursorHold'
end)
then
table.insert(augroup, {
'CursorHold',
Expand All @@ -71,10 +71,10 @@ function M.lsp_autocmds(bufnr, server_name)
})
end

if #vim.tbl_filter(function(autocmd)
return autocmd.buflocal == true and autocmd.buffer == bufnr and autocmd.event == 'BufWritePost'
end, autocmds)
== 0
if
not vim.iter(autocmds):find(function(autocmd)
return autocmd.buflocal == true and autocmd.buffer == bufnr and autocmd.event == 'BufWritePost'
end)
then
table.insert(augroup, {
'BufWritePost',
Expand Down
4 changes: 2 additions & 2 deletions nvim/lua/my/legendary/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ function M.default_commands()
{
':Plugins',
function()
local plugin_shorthands = vim.tbl_map(function(plugin)
local plugin_shorthands = vim.iter(require('lazy').plugins()):map(function(plugin)
return plugin[1]
end, require('lazy').plugins())
end)
vim.ui.select(plugin_shorthands, { prompt = 'Select Plugin' }, function(selected)
if not selected then
return
Expand Down
4 changes: 2 additions & 2 deletions nvim/lua/my/legendary/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ end
function M.lsp_keymaps(bufnr)
-- if the buffer already has LSP keymaps bound, do nothing
if
#vim.tbl_filter(function(keymap)
vim.iter(vim.api.nvim_buf_get_keymap(bufnr, 'n')):find(function(keymap)
return (keymap.desc or ''):lower() == 'rename symbol'
end, vim.api.nvim_buf_get_keymap(bufnr, 'n')) > 0
end)
then
return {}
end
Expand Down
27 changes: 15 additions & 12 deletions nvim/lua/my/lsp/filetypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,21 @@ local function load_efm_modules(mods, mod_type)

-- normalize type to string[]
mods = type(mods) == 'string' and { mods } or mods
return vim.tbl_map(function(mod)
if efm_customizations[mod] then
return efm_customizations[mod]()
end

local ok, module = pcall(require, string.format('efmls-configs.%s.%s', mod_type, mod))
if not ok then
vim.notify(string.format('Module efmls-configs.%s.%s not found', mod_type, mod))
return nil
end
return module
end, mods)
return vim
.iter(mods)
:map(function(mod)
if efm_customizations[mod] then
return efm_customizations[mod]()
end

local ok, module = pcall(require, string.format('efmls-configs.%s.%s', mod_type, mod))
if not ok then
vim.notify(string.format('Module efmls-configs.%s.%s not found', mod_type, mod))
return nil
end
return module
end)
:totable()
end

local function load_linters(linters)
Expand Down

0 comments on commit c3f6eb6

Please sign in to comment.