Skip to content

Commit c7a7899

Browse files
committed
feat: add documentation
1 parent b156d4e commit c7a7899

File tree

8 files changed

+202
-43
lines changed

8 files changed

+202
-43
lines changed

.editorconfig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,3 @@ end_of_line = lf
66
indent_style = tab
77
insert_final_newline = true
88
trim_trailing_whitespace = true
9-
10-
[*.lua]
11-
indent_size = 3
12-
indent_style = tab

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ clean: clear_dependencies
1616
.PHONY: test
1717
test: install_dependencies
1818
$(NVIM_HEADLESS) -c "call Test()"
19+
20+
.PHONY: docs
21+
docs:
22+
@echo "Generating documentation..."
23+
@nvim --headless --noplugin -u ./scripts/minimal-init.lua -c "luafile scripts/minidoc.lua" -c "qa!"
24+
25+
.PHONY: clean
26+
clean:
27+
@echo "Removing temporary directories..."
28+
@rm -rf "/tmp/nvim/site/pack/test/start/mini.doc"

doc/modes.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
*modes.nvim* Prismatic line decorations for the adventurous vim user
2+
*Modes*
3+
4+
MIT License Copyright (c) mvllow
5+
6+
==============================================================================
7+
8+
Features:
9+
10+
- Highlight UI elements based on the current mode.
11+
12+
# Setup ~
13+
14+
Modes setup can be called with your `config` table to modify default
15+
behaviour.
16+
17+
See |Modes.config| for `config` options and default values.
18+
19+
------------------------------------------------------------------------------
20+
*default_config*
21+
`default_config`
22+
Module config
23+
24+
Default values:
25+
>lua
26+
local default_config = {
27+
colors = {},
28+
line_opacity = {
29+
copy = 0.15,
30+
delete = 0.15,
31+
change = 0.15,
32+
format = 0.15,
33+
insert = 0.15,
34+
visual = 0.15,
35+
},
36+
set_cursor = true,
37+
set_cursorline = true,
38+
set_number = true,
39+
set_signcolumn = true,
40+
ignore = {
41+
'NvimTree',
42+
'lspinfo',
43+
'packer',
44+
'checkhealth',
45+
'help',
46+
'man',
47+
'TelescopePrompt',
48+
'TelescopeResults',
49+
'!minifiles',
50+
},
51+
}
52+
<
53+
------------------------------------------------------------------------------
54+
*H.highlight()*
55+
`H.highlight`({scene})
56+
highlights
57+
Parameters ~
58+
{scene} '`(default)`'|'copy'|'delete'|'change'|'format'|'insert'|'visual'
59+
60+
------------------------------------------------------------------------------
61+
*Modes.setup()*
62+
`Modes.setup`({opts})
63+
Module setup
64+
65+
Parameters ~
66+
{config} `(table|nil)` Module config table. See |Modes.config|.
67+
68+
Usage ~
69+
`require('modes').setup({})` (replace `{}` with your `config` table)
70+
71+
72+
vim:tw=78:ts=8:noet:ft=help:norl:

doc/tags

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
H.highlight() modes.txt /*H.highlight()*
2+
Modes modes.txt /*Modes*
3+
Modes.setup() modes.txt /*Modes.setup()*
4+
default_config modes.txt /*default_config*
5+
modes.nvim modes.txt /*modes.nvim*

lua/modes.lua

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1-
local utils = require('modes.utils')
1+
--- *modes.nvim* Prismatic line decorations for the adventurous vim user
2+
--- *Modes*
3+
---
4+
--- MIT License Copyright (c) mvllow
5+
---
6+
--- ==============================================================================
7+
---
8+
--- Features:
9+
---
10+
--- - Highlight UI elements based on the current mode.
11+
---
12+
--- # Setup ~
13+
---
14+
--- Modes setup can be called with your `config` table to modify default
15+
--- behaviour.
16+
---
17+
--- See |Modes.config| for `config` options and default values.
18+
19+
---@alias Scene 'copy'|'delete'|'insert'|'normal'|'replace'|'visual'
20+
21+
local Modes = {}
22+
local H = {}
223

3-
local M = {}
424
local config = {}
25+
local utils = require('modes.utils')
26+
27+
--- Module config
28+
---
29+
--- Default values:
30+
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
531
local default_config = {
632
colors = {},
733
line_opacity = {
@@ -28,6 +54,8 @@ local default_config = {
2854
'!minifiles',
2955
},
3056
}
57+
--minidoc_afterlines_end
58+
--
3159
local winhighlight = {
3260
default = {
3361
CursorLine = 'CursorLine',
@@ -81,24 +109,24 @@ local in_ignored_buffer = function()
81109
return config.ignore()
82110
end
83111
return not vim.tbl_contains(config.ignore, '!' .. vim.bo.filetype)
84-
and (vim.api.nvim_get_option_value('buftype', { buf = 0 }) ~= '' -- not a normal buffer
85-
or not vim.api.nvim_get_option_value('buflisted', { buf = 0 }) -- unlisted buffer
86-
or vim.tbl_contains(config.ignore, vim.bo.filetype))
112+
and (vim.api.nvim_get_option_value('buftype', { buf = 0 }) ~= '' -- not a normal buffer
113+
or not vim.api.nvim_get_option_value('buflisted', { buf = 0 }) -- unlisted buffer
114+
or vim.tbl_contains(config.ignore, vim.bo.filetype))
87115
end
88116

89-
M.reset = function()
90-
M.highlight('default')
117+
H.reset = function()
118+
H.highlight('default')
91119
vim.cmd.redraw()
92120
end
93121

94-
M.restore = function()
95-
local scene = M.get_scene()
96-
M.highlight(scene)
122+
H.restore = function()
123+
local scene = H.get_scene()
124+
H.highlight(scene)
97125
end
98126

99127
---Update highlights
100128
---@param scene 'default'|'copy'|'delete'|'change'|'format'|'insert'|'visual'
101-
M.highlight = function(scene)
129+
H.highlight = function(scene)
102130
if in_ignored_buffer() then
103131
return
104132
end
@@ -122,7 +150,7 @@ M.highlight = function(scene)
122150
if not config.set_number then
123151
winhl_map.CursorLineNr = nil
124152
elseif not config.set_cursorline then
125-
local detected_scene = M.get_scene()
153+
local detected_scene = H.get_scene()
126154
if scene == 'default' and detected_scene == 'visual' then
127155
winhl_map.CursorLineNr = 'ModesVisualUnfocusedCursorLineNr'
128156
end
@@ -167,7 +195,7 @@ M.highlight = function(scene)
167195
end
168196
end
169197

170-
M.get_scene = function()
198+
H.get_scene = function()
171199
local mode = vim.api.nvim_get_mode().mode
172200
if mode:match('[iR]') then
173201
return 'insert'
@@ -179,7 +207,7 @@ M.get_scene = function()
179207
return 'default'
180208
end
181209

182-
M.define = function()
210+
H.define = function()
183211
colors = {
184212
bg = config.colors.bg or utils.get_bg('Normal', 'Normal'),
185213
copy = config.colors.copy or utils.get_bg('ModesCopy', '#f5c359'),
@@ -278,7 +306,7 @@ M.define = function()
278306
end
279307
end
280308

281-
M.enable_managed_ui = function()
309+
H.enable_managed_ui = function()
282310
if in_ignored_buffer() then
283311
if config.set_cursorline then
284312
vim.o.cursorline = false
@@ -294,11 +322,11 @@ M.enable_managed_ui = function()
294322
vim.opt.guicursor:append('r-o:ModesOperator')
295323
end
296324

297-
M.restore()
325+
H.restore()
298326
end
299327
end
300328

301-
M.disable_managed_ui = function()
329+
H.disable_managed_ui = function()
302330
if config.set_cursorline then
303331
vim.o.cursorline = false
304332
end
@@ -315,10 +343,15 @@ M.disable_managed_ui = function()
315343
vim.o.guicursor = cursor
316344
end
317345

318-
M.reset()
346+
H.reset()
319347
end
320348

321-
M.setup = function(opts)
349+
--- Module setup
350+
---
351+
---@param config table|nil Module config table. See |Modes.config|.
352+
---
353+
---@usage `require('modes').setup({})` (replace `{}` with your `config` table)
354+
Modes.setup = function(opts)
322355
opts = vim.tbl_extend('keep', opts or {}, default_config)
323356
if opts.focus_only then
324357
vim.notify(
@@ -352,13 +385,13 @@ M.setup = function(opts)
352385
}
353386
end
354387

355-
M.define()
356-
M.enable_managed_ui() -- ensure enabled initially
388+
H.define()
389+
H.enable_managed_ui() -- ensure enabled initially
357390

358391
---Reset normal highlight
359392
vim.api.nvim_create_autocmd('ModeChanged', {
360393
pattern = '*:n,*:ni*',
361-
callback = M.reset,
394+
callback = H.reset,
362395
})
363396

364397
---Set operator highlights
@@ -367,52 +400,52 @@ M.setup = function(opts)
367400
callback = function()
368401
local operator = vim.v.operator
369402
if operator == 'y' then
370-
M.highlight('copy')
403+
H.highlight('copy')
371404
elseif operator == 'd' then
372-
M.highlight('delete')
405+
H.highlight('delete')
373406
elseif operator == 'c' then
374-
M.highlight('change')
407+
H.highlight('change')
375408
elseif operator:match('[=!><g]') then
376-
M.highlight('format')
409+
H.highlight('format')
377410
end
378411
end,
379412
})
380413

381414
---Set highlights when colorscheme changes
382415
vim.api.nvim_create_autocmd('ColorScheme', {
383416
pattern = '*',
384-
callback = M.define,
417+
callback = H.define,
385418
})
386419

387420
---Set insert highlight
388421
vim.api.nvim_create_autocmd('InsertEnter', {
389422
pattern = '*',
390423
callback = function()
391-
M.highlight('insert')
424+
H.highlight('insert')
392425
end,
393426
})
394427

395428
---Set visual highlight
396429
vim.api.nvim_create_autocmd('ModeChanged', {
397430
pattern = '*:[vVsS\x16\x13]',
398431
callback = function()
399-
M.highlight('visual')
432+
H.highlight('visual')
400433
end,
401434
})
402435

403436
---Enable managed UI for current window
404437
vim.api.nvim_create_autocmd({ 'WinEnter', 'FocusGained' }, {
405438
pattern = '*',
406439
callback = function()
407-
vim.schedule(M.enable_managed_ui)
440+
vim.schedule(H.enable_managed_ui)
408441
end,
409442
})
410443

411444
---Disable managed UI
412445
vim.api.nvim_create_autocmd({ 'WinLeave', 'FocusLost' }, {
413446
pattern = '*',
414-
callback = M.disable_managed_ui,
447+
callback = H.disable_managed_ui,
415448
})
416449
end
417450

418-
return M
451+
return Modes

readme.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
## Usage
66

77
```lua
8-
use({
9-
'mvllow/modes.nvim',
10-
tag = 'v0.2.1',
8+
{
9+
"mvllow/modes.nvim",
10+
tag = "v0.2.1",
1111
config = function()
12-
require('modes').setup()
12+
require("modes").setup()
1313
end
14-
})
14+
}
1515
```
1616

1717
![modes.nvim](https://user-images.githubusercontent.com/1474821/127896095-6da221cf-3327-4eed-82be-ce419bdf647c.gif)
@@ -53,7 +53,7 @@ require('modes').setup({
5353
-- Disable modes highlights for specified filetypes
5454
-- or enable with prefix "!" if otherwise disabled (please PR common patterns)
5555
-- Can also be a function fun():boolean that disables modes highlights when true
56-
ignore = { 'NvimTree', 'TelescopePrompt', '!minifiles' }
56+
ignore = { "NvimTree", "TelescopePrompt", "!minifiles" }
5757
})
5858
```
5959

@@ -75,9 +75,21 @@ require('modes').setup({
7575
_Workaround:_
7676

7777
```lua
78-
require('which-key').setup({
78+
require("which-key").setup({
7979
triggers_blacklist = {
8080
n = { "d", "y" }
8181
}
8282
})
8383
```
84+
85+
## Contributing
86+
87+
Pull requests are welcome and appreciated!
88+
89+
### Generating documentation
90+
91+
Run `make docs` or inside of Neovim, with [mini.doc](https://github.com/echasnovski/mini.doc) in your runtimepath:
92+
93+
```lua
94+
:luafile scripts/minidoc.lua
95+
```

scripts/minidoc.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local minidoc = require("mini.doc")
2+
3+
if _G.MiniDoc == nil then
4+
minidoc.setup()
5+
end
6+
7+
local hooks = vim.deepcopy(minidoc.default_hooks)
8+
9+
hooks.write_pre = function(lines)
10+
-- Remove first two lines with `======` and `------` delimiters to comply
11+
-- with `:h local-additions` template
12+
table.remove(lines, 1)
13+
table.remove(lines, 1)
14+
return lines
15+
end
16+
17+
minidoc.generate({ "lua/modes.lua" }, "doc/modes.txt", { hooks = hooks })

0 commit comments

Comments
 (0)