|
1 | 1 | local utils = require('cheatsheet.utils')
|
| 2 | +local config = require('cheatsheet.config') |
2 | 3 | -- plenary is only used for telescope specific code
|
3 | 4 | local has_path, path = pcall(require, "plenary.path")
|
4 | 5 |
|
5 | 6 | local M = {}
|
| 7 | + |
| 8 | +M.setup = function(opts) |
| 9 | + config.setup(opts) |
| 10 | +end |
| 11 | + |
6 | 12 | -- 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 |
7 | 15 | -- @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") |
10 | 34 |
|
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 |
13 | 51 | 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 | + |
14 | 60 | -- https://github.com/neovim/neovim/issues/14294
|
15 | 61 | -- returned table may have duplicated entries
|
16 | 62 | return utils.dedupe_array(cheats)
|
|
0 commit comments