Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(init): improve speed via build index when initializing #37

Merged
merged 7 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ It allow you do any switches with:
- [Requirement](#-requirement)
- [Install](#-install)
- [lazy.nvim](#lazynvim)
- [pckr.nvim](#pckrnvim)
- [Configuration](#-configuration)
- [Development](#-development)
- [Contribute](#-contribute)
Expand All @@ -59,6 +60,12 @@ It allow you do any switches with:

## 📦 Install

**Warning:** if this plugin provides the main colorscheme (e.g. the `colorscheme` command right after nvim start), then make sure:

1. Don't lazy this plugin, it only takes ~4 ms to load.
2. Load this plugin before all other start plugins.
3. Hold your wifi, it `clone` and `pull` a lot of git repos!

### [lazy.nvim](https://github.com/folke/lazy.nvim)

```lua
Expand All @@ -73,12 +80,17 @@ require('lazy').setup({
})
```

> Note:
>
> If this plugin provides the main colorscheme (e.g. the `colorscheme` command right after nvim start), then make sure:
>
> 1. Don't lazy this plugin.
> 2. Load this plugin before all other start plugins.
### [pckr.nvim](https://github.com/lewis6991/pckr.nvim)

```lua
require('pckr').add{
{
'linrongbin16/colorbox.nvim',
run = function() require('colorbox').update() end,
config = function() require('colorbox').setup() end,
}
}
```

## 🔧 Configuration

Expand Down
44 changes: 14 additions & 30 deletions lua/colorbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ local Defaults = {
--- @type colorbox.Options
local Configs = {}

-- color names list
-- filtered color names list
--- @type string[]
local ColorNamesList = {}
local FilteredColorNamesList = {}

--- @param color_name string
--- @param spec colorbox.ColorSpec
Expand Down Expand Up @@ -100,41 +100,25 @@ local function _init()
-- vim.opt.packpath:append(cwd .. "/pack/colorbox/opt")
-- vim.cmd([[packadd catppuccin-nvim]])

local HandleToColorSpecsMap =
require("colorbox.db").get_handle_to_color_specs_map()
logger.debug(
"|colorbox._init| HandleToColorSpecsMap:%s",
vim.inspect(HandleToColorSpecsMap)
)
for _, spec in pairs(HandleToColorSpecsMap) do
if
vim.fn.isdirectory(spec.full_pack_path) > 0
and vim.fn.isdirectory(spec.full_pack_path .. "/.git") > 0
then
for _, color_name in ipairs(spec.color_names) do
if not _should_filter(color_name, spec) then
table.insert(ColorNamesList, color_name)
end
end
local ColorNameToColorSpecsMap =
require("colorbox.db").get_color_name_to_color_specs_map()
local ColorNamesList = require("colorbox.db").get_color_names_list()
for _, color_name in pairs(ColorNamesList) do
local spec = ColorNameToColorSpecsMap[color_name]
if not _should_filter(color_name, spec) then
table.insert(FilteredColorNamesList, color_name)
end
end
-- logger.debug(
-- "|colorbox._init| before sort ColorNamesList:%s",
-- vim.inspect(ColorNamesList)
-- )
table.sort(ColorNamesList, function(a, b)
return a:lower() < b:lower()
end)
logger.debug(
"|colorbox._init| ColorNamesList:%s",
vim.inspect(ColorNamesList)
"|colorbox._init| FilteredColorNamesList:%s",
vim.inspect(FilteredColorNamesList)
)
end

local function _policy_shuffle()
if #ColorNamesList > 0 then
local r = utils.randint(#ColorNamesList)
local color = ColorNamesList[r + 1]
if #FilteredColorNamesList > 0 then
local r = utils.randint(#FilteredColorNamesList)
local color = FilteredColorNamesList[r + 1]
-- logger.debug(
-- "|colorbox._policy_shuffle| color:%s, ColorNames:%s (%d), r:%d",
-- vim.inspect(color),
Expand Down
21 changes: 21 additions & 0 deletions lua/colorbox/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ local GitPathToColorSpecsMap = nil
--- @type table<string, colorbox.ColorSpec>
local ColorNameToColorSpecsMap = nil

-- color names list
--- @type string[]
local ColorNamesList = nil

do
if type(HandleToColorSpecsMap) ~= "table" then
local cwd = vim.fn["colorbox#base_dir"]()
Expand Down Expand Up @@ -127,6 +131,17 @@ do
GitPathToColorSpecsMap[spec.git_path] = spec
end
end
if type(ColorNamesList) ~= "table" then
ColorNamesList = {}
for _, spec in pairs(HandleToColorSpecsMap) do
for _, color_name in ipairs(spec.color_names) do
table.insert(ColorNamesList, color_name)
end
end
table.sort(ColorNamesList, function(a, b)
return a:lower() < b:lower()
end)
end
end

--- @return table<string, colorbox.ColorSpec>
Expand All @@ -144,10 +159,16 @@ local function get_color_name_to_color_specs_map()
return ColorNameToColorSpecsMap
end

--- @return string[]
local function get_color_names_list()
return ColorNamesList
end

local M = {
get_handle_to_color_specs_map = get_handle_to_color_specs_map,
get_git_path_to_color_specs_map = get_git_path_to_color_specs_map,
get_color_name_to_color_specs_map = get_color_name_to_color_specs_map,
get_color_names_list = get_color_names_list,
}

return M
32 changes: 32 additions & 0 deletions test/db_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local cwd = vim.fn.getcwd()

describe("db", function()
local assert_eq = assert.is_equal
local assert_true = assert.is_true
local assert_false = assert.is_false

before_each(function()
vim.api.nvim_command("cd " .. cwd)
end)

local db = require("colorbox.db")
describe("[get_xxxapi]", function()
it("get_handle_to_color_specs_map", function()
local HandleToColorSpecsMap = db.get_handle_to_color_specs_map()
assert_eq(type(HandleToColorSpecsMap), "table")
end)
it("get_git_path_to_color_specs_map", function()
local GitPathToColorSpecsMap = db.get_git_path_to_color_specs_map()
assert_eq(type(GitPathToColorSpecsMap), "table")
end)
it("get_git_path_to_color_specs_map", function()
local ColorNameToColorSpecsMap =
db.get_color_name_to_color_specs_map()
assert_eq(type(ColorNameToColorSpecsMap), "table")
end)
it("get_color_names_list", function()
local ColorNamesList = db.get_color_names_list()
assert_eq(type(ColorNamesList), "table")
end)
end)
end)