Skip to content

Commit

Permalink
refactor: add utilities to easily insert/extend lists with unique values
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter authored and luxus committed Mar 9, 2023
1 parent 2f1164a commit 5306679
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"bennypowers/nvim-regexplainer",
Expand All @@ -15,7 +16,7 @@ return {
return
end
-- Add the "regex" parser to opts.ensure_installed.
table.insert(opts.ensure_installed, "regex")
utils.list_insert_unique(opts.ensure_installed, "regex")
end,
},
}
9 changes: 5 additions & 4 deletions lua/astrocommunity/pack/bash/bash.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -9,7 +10,7 @@ return {
return
end
-- Add the "julia" and "toml" language to opts.ensure_installed.
table.insert(opts.ensure_installed, "bash")
utils.list_insert_unique(opts.ensure_installed, "bash")
end,
},
{
Expand All @@ -18,7 +19,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add julia lsp and toml lsp to ensure_installed
table.insert(opts.ensure_installed, "bashls")
utils.list_insert_unique(opts.ensure_installed, "bashls")
end,
},
{
Expand All @@ -27,7 +28,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add julia lsp and toml lsp to ensure_installed
vim.list_extend(opts.ensure_installed, { "shellcheck", "shfmt" })
utils.list_insert_unique(opts.ensure_installed, { "shellcheck", "shfmt" })
end,
},
{
Expand All @@ -36,7 +37,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add julia lsp and toml lsp to ensure_installed
table.insert(opts.ensure_installed, "bash")
utils.list_insert_unique(opts.ensure_installed, "bash")
end,
},
}
11 changes: 6 additions & 5 deletions lua/astrocommunity/pack/go/go.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
-- Golang support
{
Expand All @@ -10,7 +11,7 @@ return {
return
end
-- Add the "go" language to opts.ensure_installed.
table.insert(opts.ensure_installed, "go")
utils.list_insert_unique(opts.ensure_installed, "go")
end,
},

Expand All @@ -20,15 +21,15 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add go lsps to opts.ensure_installed using vim.list_extend.
vim.list_extend(opts.ensure_installed, { "gomodifytags", "gofumpt", "iferr", "impl", "goimports" })
utils.list_insert_unique(opts.ensure_installed, { "gomodifytags", "gofumpt", "iferr", "impl", "goimports" })
end,
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
table.insert(opts.ensure_installed, "gopls")
utils.list_insert_unique(opts.ensure_installed, "gopls")
end,
},
{
Expand All @@ -42,14 +43,14 @@ return {
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
table.insert(opts.ensure_installed, "delve")
utils.list_insert_unique(opts.ensure_installed, "delve")
end,
},
},
},
{
"olexsmir/gopher.nvim",
init = function() table.insert(astronvim.lsp.skip_setup, "gopls") end,
init = function() utils.list_insert_unique(astronvim.lsp.skip_setup, "gopls") end,
opts = function() return { server = require("astronvim.utils.lsp").config "gopls" } end,
ft = "go",
dependencies = {
Expand Down
14 changes: 12 additions & 2 deletions lua/astrocommunity/pack/json/json.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -8,15 +9,24 @@ return {
elseif opts.ensure_installed == "all" then
return
end
vim.list_extend(opts.ensure_installed, { "json", "jsonc" })
utils.list_insert_unique(opts.ensure_installed, { "json", "jsonc" })
end,
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table
if not opts.ensure_installed then opts.ensure_installed = {} end
table.insert(opts.ensure_installed, "jsonls")
utils.list_insert_unique(opts.ensure_installed, "jsonls")
end,
},
{
"jay-babu/mason-null-ls.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add go lsps to opts.ensure_installed using vim.list_extend.
utils.list_insert_unique(opts.ensure_installed, "prettier")
end,
},
}
5 changes: 3 additions & 2 deletions lua/astrocommunity/pack/julia/julia.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -9,7 +10,7 @@ return {
return
end
-- Add the "julia" and "toml" language to opts.ensure_installed.
vim.list_extend(opts.ensure_installed, { "julia", "toml" })
utils.list_insert_unique(opts.ensure_installed, { "julia", "toml" })
end,
},
{
Expand All @@ -18,7 +19,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add julia lsp and toml lsp to ensure_installed
vim.list_extend(opts.ensure_installed, { "julials", "taplo" })
utils.list_insert_unique(opts.ensure_installed, { "julials", "taplo" })
end,
},
{
Expand Down
7 changes: 4 additions & 3 deletions lua/astrocommunity/pack/lua/lua.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -8,23 +9,23 @@ return {
elseif opts.ensure_installed == "all" then
return
end
vim.list_extend(opts.ensure_installed, { "lua", "luap" })
utils.list_insert_unique(opts.ensure_installed, { "lua", "luap" })
end,
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table
if not opts.ensure_installed then opts.ensure_installed = {} end
table.insert(opts.ensure_installed, "lua_ls")
utils.list_insert_unique(opts.ensure_installed, "lua_ls")
end,
},
{
"jay-babu/mason-null-ls.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table
if not opts.ensure_installed then opts.ensure_installed = {} end
table.insert(opts.ensure_installed, "stylua")
utils.list_insert_unique(opts.ensure_installed, "stylua")
end,
},
}
5 changes: 3 additions & 2 deletions lua/astrocommunity/pack/nix/nix.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -9,15 +10,15 @@ return {
return
end
-- Add the "nix" language to opts.ensure_installed.
table.insert(opts.ensure_installed, "nix")
utils.list_insert_unique(opts.ensure_installed, "nix")
end,
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
table.insert(opts.ensure_installed, "rnix")
utils.list_insert_unique(opts.ensure_installed, "rnix")
end,
},
{
Expand Down
9 changes: 5 additions & 4 deletions lua/astrocommunity/pack/python/python.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -9,7 +10,7 @@ return {
return
end
-- Add the "python" and "toml" language to opts.ensure_installed.
vim.list_extend(opts.ensure_installed, { "python", "toml" })
utils.list_insert_unique(opts.ensure_installed, { "python", "toml" })
end,
},
{
Expand All @@ -18,7 +19,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add pyright lsp to opts.ensure_installed using table.insert.
vim.list_extend(opts.ensure_installed, { "pyright", "ruff_lsp" })
utils.list_insert_unique(opts.ensure_installed, { "pyright", "ruff_lsp" })
end,
},
{
Expand All @@ -27,7 +28,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add to opts.ensure_installed using vim.list_extend.
vim.list_extend(opts.ensure_installed, { "isort", "black" })
utils.list_insert_unique(opts.ensure_installed, { "isort", "black" })
end,
},
{
Expand All @@ -36,7 +37,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add to opts.ensure_installed using table.insert.
table.insert(opts.ensure_installed, "python")
utils.list_insert_unique(opts.ensure_installed, "python")
end,
},
{
Expand Down
9 changes: 5 additions & 4 deletions lua/astrocommunity/pack/rust/rust.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -9,21 +10,21 @@ return {
return
end
-- Add the "rust" and "toml" language to opts.ensure_installed.
table.insert(opts.ensure_installed, { "rust", "toml" })
utils.list_insert_unique(opts.ensure_installed, { "rust", "toml" })
end,
},
{
"simrat39/rust-tools.nvim",
ft = { "rust" },
init = function() table.insert(astronvim.lsp.skip_setup, "rust_analyzer") end,
init = function() utils.list_insert_unique(astronvim.lsp.skip_setup, "rust_analyzer") end,
opts = function() return { server = require("astronvim.utils.lsp").config "rust_analyzer" } end,
dependencies = {
{
"jay-babu/mason-nvim-dap.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
table.insert(opts.ensure_installed, "codelldb")
utils.list_insert_unique(opts.ensure_installed, "codelldb")
end,
},
},
Expand All @@ -34,7 +35,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add rust and taplo lsps to opts.ensure_installed using vim.list_extend.
vim.list_extend(opts.ensure_installed, { "rust_analyzer", "taplo" })
utils.list_insert_unique(opts.ensure_installed, { "rust_analyzer", "taplo" })
end,
},
{
Expand Down
18 changes: 8 additions & 10 deletions lua/astrocommunity/pack/typescript/typescript.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
Expand All @@ -9,7 +10,7 @@ return {
return
end
-- Add the required file types to opts.ensure_installed.
vim.list_extend(opts.ensure_installed, { "javascript", "json", "typescript", "tsx" })
utils.list_insert_unique(opts.ensure_installed, { "javascript", "json", "typescript", "tsx" })
end,
},
{
Expand All @@ -18,7 +19,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add tsserver to opts.ensure_installed using vim.list_extend.
table.insert(opts.ensure_installed, "tsserver")
utils.list_insert_unique(opts.ensure_installed, "tsserver")
end,
},
{
Expand All @@ -27,7 +28,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add to opts.ensure_installed using vim.list_extend.
table.insert(opts.ensure_installed, "prettier")
utils.list_insert_unique(opts.ensure_installed, "prettier")
end,
},
{
Expand All @@ -36,7 +37,7 @@ return {
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add to opts.ensure_installed using table.insert.
table.insert(opts.ensure_installed, "js")
utils.list_insert_unique(opts.ensure_installed, "js")
end,
},
{
Expand All @@ -50,12 +51,9 @@ return {
ft = { "ts", "js", "tsx", "jsx" },
enabled = true,
dependencies = {
{
"mxsdev/nvim-dap-vscode-js",
opts = { debugger_cmd = { "js-debug-adapter" }, adapters = { "pwa-node" } },
},
{ "mxsdev/nvim-dap-vscode-js", opts = { debugger_cmd = { "js-debug-adapter" }, adapters = { "pwa-node" } } },
{ "theHamsta/nvim-dap-virtual-text", config = true },
{ "rcarriga/nvim-dap-ui", config = true },
{ "rcarriga/nvim-dap-ui", config = true },
},
config = function()
local dap = require "dap"
Expand Down Expand Up @@ -101,7 +99,7 @@ return {
},
{
"jose-elias-alvarez/typescript.nvim",
init = function() table.insert(astronvim.lsp.skip_setup, "tsserver") end,
init = function() utils.list_insert_unique(astronvim.lsp.skip_setup, "tsserver") end,
ft = {
"typescript",
"typescriptreact",
Expand Down
3 changes: 2 additions & 1 deletion lua/astrocommunity/utility/noice-nvim/noice-nvim.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local utils = require "astrocommunity.utils"
return {
{
"nvim-treesitter/nvim-treesitter",
opts = function(_, opts)
if not opts.ensure_installed then opts.ensure_installed = {} end
if type(opts.ensure_installed) == "table" then
vim.list_extend(opts.ensure_installed, { "bash", "markdown", "markdown_inline", "regex", "vim" })
utils.list_insert_unique(opts.ensure_installed, { "bash", "markdown", "markdown_inline", "regex", "vim" })
end
return opts
end,
Expand Down
10 changes: 10 additions & 0 deletions lua/astrocommunity/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local M = {}

function M.list_insert_unique(tbl, vals)
if type(vals) ~= "table" then vals = { vals } end
for _, val in ipairs(vals) do
if not vim.tbl_contains(tbl, val) then table.insert(tbl, val) end
end
end

return M

0 comments on commit 5306679

Please sign in to comment.