Skip to content

Commit 23041ec

Browse files
committed
Add proper system for bundled cheatsheets
1 parent b2820f5 commit 23041ec

File tree

6 files changed

+141
-40
lines changed

6 files changed

+141
-40
lines changed

README.md

+38-7
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,9 @@ Your cheatsheet file is a simple text file with the name `cheatsheet.txt` found
6767
`~/.config/nvim/` (`~/AppData/Local/nvim/` if you're on Windows) alongside your
6868
`init.vim`. Use the `:CheatsheetEdit` command to open it in a buffer to edit.
6969

70-
A [builtin cheatsheet](./doc/cheatsheet-default.txt) lists most of the useful
71-
inbuilt vim mappings and commands, useful for people still finding their way
72-
around the editor (and for others wanting to scratch the occasional
73-
I've-been-using-vim-for-years-but-forgot-how-to-scroll-horizontally itch).
74-
Disable it with `let g:cheatsheet_use_default = v:false`.
75-
7670
| Telescope mappings | Description |
7771
| --- | --- |
7872
| `<C-E>` | Edit user cheatsheet à la `:CheatsheetEdit` |
79-
| `<C-D>` | Toggle `g:cheatsheet_use_default` |
8073
| `<C-Y>` | Yank the cheatcode |
8174
| `Enter` | Fill in the command line; see below |
8275

@@ -90,6 +83,44 @@ Since `cheatsheet.nvim` provides it's own commands, it is not required to
9083
"load" `cheatsheet.nvim` with Telescope which is usually required for plugins
9184
using Telescope.
9285

86+
## Configuration
87+
88+
```lua
89+
local defaults = {
90+
-- Whether to show bundled cheatsheets
91+
92+
-- For generic cheatsheets like default, unicode, nerd-fonts, etc
93+
bundled_cheatsheets = true,
94+
-- bundled_cheatsheets = {
95+
-- enabled = {},
96+
-- disabled = {},
97+
-- },
98+
99+
-- For plugin specific cheatsheets
100+
bundled_plugin_cheatsheets = true,
101+
-- bundled_plugin_cheatsheets = {
102+
-- enabled = {},
103+
-- disabled = {},
104+
-- }
105+
}
106+
```
107+
108+
`bundled_cheatsheets` and `bundled_plugin_cheatsheets` can also be tables to
109+
have more fine grained control:
110+
111+
```lua
112+
local defaults = {
113+
bundled_cheatsheets = {
114+
-- only show the default cheatsheet
115+
enabled = { "default" },
116+
},
117+
bundled_plugin_cheatsheets = {
118+
-- show cheatsheets for all plugins except gitsigns
119+
disabled = { "gitsigns.nvim" },
120+
}
121+
}
122+
```
123+
93124
## `cheatsheet.txt` File Format
94125

95126
- `#` starts a normal comment, blank lines are ignored.

lua/cheatsheet/config.lua

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local M = {}
2+
3+
-- NOTE: Watch out for bugs in cheatsheet.get_cheatsheet_files if
4+
-- defaults is modified in the future
5+
local defaults = {
6+
-- Whether to show bundled cheatsheets
7+
8+
-- For generic cheatsheets like default, unicode, nerd-fonts, etc
9+
-- bundled_cheatsheets = {
10+
-- enabled = {},
11+
-- disabled = {},
12+
-- },
13+
bundled_cheatsheets = true,
14+
-- For plugin specific cheatsheets
15+
-- bundled_plugin_cheatsheets = {
16+
-- enabled = {},
17+
-- disabled = {},
18+
-- }
19+
bundled_plugin_cheatsheets = true,
20+
}
21+
22+
M.options = {}
23+
24+
function M.setup(opts)
25+
opts = opts or {}
26+
for key, val in pairs(defaults) do
27+
if opts[key] == nil then
28+
opts[key] = val
29+
end
30+
end
31+
M.options = opts
32+
end
33+
34+
M.setup()
35+
36+
return M

lua/cheatsheet/init.lua

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,62 @@
11
local utils = require('cheatsheet.utils')
2+
local config = require('cheatsheet.config')
23
-- plenary is only used for telescope specific code
34
local has_path, path = pcall(require, "plenary.path")
45

56
local M = {}
7+
8+
M.setup = function(opts)
9+
config.setup(opts)
10+
end
11+
612
-- Get `cheatsheet.txt` files from any directory in runtimepath
13+
-- Inlcudes bundled cheatsheets if configured to do so.
14+
-- @param *opts* config.optsa like table
715
-- @return array of filepaths
8-
M.get_cheatsheet_files = function()
9-
local cheats = vim.api.nvim_get_runtime_file("cheatsheet.txt", true)
16+
M.get_cheatsheet_files = function(opts)
17+
opts = opts or config.options
18+
19+
-- Insert elements from `from_tbl` into `ins_tbl`. If `include` is a bool,
20+
-- it controls appending everything in `from_tbl` to `ins_tbl`. If it is a
21+
-- table, Use `pattern` to extract a match from an element in `from_tbl`
22+
-- and insert only if match is present in `include.enabled` or not present
23+
-- in `include.disabled`.
24+
local function filter_insert(ins_tbl, from_tbl, pattern, include)
25+
if include == false then return end
26+
27+
if include == true then
28+
for _, file in ipairs(from_tbl) do
29+
table.insert(ins_tbl, file)
30+
end
31+
return
32+
end
33+
assert(type(include) == "table", "Invalid table format")
1034

11-
if utils.is_using_default_cheatsheet() then
12-
table.insert(cheats, utils.get_default_cheatsheet())
35+
if include.enabled ~= nil then
36+
for _, element in ipairs(from_tbl) do
37+
local match = element:match(pattern)
38+
if utils.has_value(include.enabled, match) then
39+
table.insert(ins_tbl, element)
40+
end
41+
end
42+
elseif include.disabled ~= nil then
43+
for _, element in ipairs(from_tbl) do
44+
local match = element:match(pattern)
45+
print(match)
46+
if not utils.has_value(include.disabled, match) then
47+
table.insert(ins_tbl, element)
48+
end
49+
end
50+
end
1351
end
52+
53+
local cheats = vim.api.nvim_get_runtime_file("cheatsheet.txt", true)
54+
local bundled = utils.get_bundled_cheatsheets()
55+
local bundled_plugins = utils.get_bundled_plugin_cheatsheets()
56+
57+
filter_insert(cheats, bundled, '.+/cheatsheets/cheatsheet%-(.+)%.txt', opts.bundled_cheatsheets)
58+
filter_insert(cheats, bundled_plugins, '.+/cheatsheets/plugins/cheatsheet%-(.+)%.txt', opts.bundled_plugin_cheatsheets)
59+
1460
-- https://github.com/neovim/neovim/issues/14294
1561
-- returned table may have duplicated entries
1662
return utils.dedupe_array(cheats)

lua/cheatsheet/telescope.lua

-9
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,6 @@ M.pick_cheat = function(opts)
121121
utils.edit_user_cheatsheet()
122122
end
123123
)
124-
map(
125-
'i', '<C-D>', function()
126-
-- _close with keepinsert=true to support opening another
127-
-- telescope window (here same window) from current one
128-
actions._close(prompt_bufnr, true)
129-
utils.toggle_use_default_cheatsheet()
130-
M.pick_cheat()
131-
end
132-
)
133124
map(
134125
'i', '<C-Y>', function()
135126
actions.close(prompt_bufnr)

lua/cheatsheet/utils.lua

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ M.dedupe_array = function(array)
1919
return result
2020
end
2121

22+
M.has_value = function(tbl, value)
23+
for _, val in pairs(tbl) do
24+
if val == value then return true end
25+
end
26+
return false
27+
end
28+
2229
-- Get the user's cheatsheet.txt located in config directory (~/.config/nvim)
2330
M.get_user_cheatsheet = function()
2431
return vim.fn.stdpath("config") .. "/cheatsheet.txt"
@@ -29,25 +36,18 @@ M.edit_user_cheatsheet = function()
2936
vim.api.nvim_command(":edit " .. M.get_user_cheatsheet())
3037
end
3138

32-
-- Get the cheatsheet included with the plugin containing a generic vim cheatsheet
33-
-- @return path to defautl cheatsheet
34-
M.get_default_cheatsheet = function()
35-
return vim.api.nvim_get_runtime_file("doc/cheatsheet-default.txt", false)[1]
36-
end
37-
38-
M.is_using_default_cheatsheet = function()
39-
local var_is_defined, use_default = pcall(
40-
vim.api.nvim_get_var, 'cheatsheet_use_default'
41-
)
42-
if var_is_defined and use_default then return true end
43-
return false
44-
39+
-- Get the list of cheatsheets bundled with this plugin,
40+
-- from cheatsheets/. Does not include plugin cheatsheets.
41+
-- @return Array of cheatsheet filepaths
42+
M.get_bundled_cheatsheets = function()
43+
return vim.api.nvim_get_runtime_file("cheatsheets/cheatsheet-*.txt", true)
4544
end
4645

47-
M.toggle_use_default_cheatsheet = function()
48-
vim.api.nvim_set_var(
49-
'cheatsheet_use_default', not M.is_using_default_cheatsheet()
50-
)
46+
-- Get the list of plugin cheatsheets bundled with this plugin,
47+
-- from cheatsheets/plugins/
48+
-- @return Array of cheatsheet filepaths
49+
M.get_bundled_plugin_cheatsheets = function()
50+
return vim.api.nvim_get_runtime_file("cheatsheets/plugins/cheatsheet-*.txt", true)
5151
end
5252

5353
return M

plugin/cheatsheet.vim

-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ if mapcheck("<leader>?", "n") == ""
1414
nnoremap <unique> <leader>? :<C-U>Cheatsheet<CR>
1515
endif
1616

17-
" Include doc/cheatsheet-default.txt
18-
let g:cheatsheet_use_default = v:true
19-
2017
highlight default link cheatComment Comment
2118
highlight default link cheatMetadataComment Comment
2219
highlight default link cheatMetadataTag Include

0 commit comments

Comments
 (0)