Skip to content

Commit b156d4e

Browse files
authored
feat: add highlighting for formatting operations (=!><g) (#82)
* feat: add highlighting for formatting operations (`=!><g`) * docs: include format color * refactor: consistent mode order
1 parent 03d69d3 commit b156d4e

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

lua/modes.lua

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local default_config = {
88
copy = 0.15,
99
delete = 0.15,
1010
change = 0.15,
11+
format = 0.15,
1112
insert = 0.15,
1213
visual = 0.15,
1314
},
@@ -41,12 +42,6 @@ local winhighlight = {
4142
CursorLineSign = 'ModesCopyCursorLineSign',
4243
CursorLineFold = 'ModesCopyCursorLineFold',
4344
},
44-
insert = {
45-
CursorLine = 'ModesInsertCursorLine',
46-
CursorLineNr = 'ModesInsertCursorLineNr',
47-
CursorLineSign = 'ModesInsertCursorLineSign',
48-
CursorLineFold = 'ModesInsertCursorLineFold',
49-
},
5045
delete = {
5146
CursorLine = 'ModesDeleteCursorLine',
5247
CursorLineNr = 'ModesDeleteCursorLineNr',
@@ -59,6 +54,18 @@ local winhighlight = {
5954
CursorLineSign = 'ModesChangeCursorLineSign',
6055
CursorLineFold = 'ModesChangeCursorLineFold',
6156
},
57+
format = {
58+
CursorLine = 'ModesFormatCursorLine',
59+
CursorLineNr = 'ModesFormatCursorLineNr',
60+
CursorLineSign = 'ModesFormatCursorLineSign',
61+
CursorLineFold = 'ModesFormatCursorLineFold',
62+
},
63+
insert = {
64+
CursorLine = 'ModesInsertCursorLine',
65+
CursorLineNr = 'ModesInsertCursorLineNr',
66+
CursorLineSign = 'ModesInsertCursorLineSign',
67+
CursorLineFold = 'ModesInsertCursorLineFold',
68+
},
6269
visual = {
6370
CursorLine = 'ModesVisualCursorLine',
6471
CursorLineNr = 'ModesVisualCursorLineNr',
@@ -90,7 +97,7 @@ M.restore = function()
9097
end
9198

9299
---Update highlights
93-
---@param scene 'default'|'insert'|'visual'|'copy'|'delete'|'change'
100+
---@param scene 'default'|'copy'|'delete'|'change'|'format'|'insert'|'visual'
94101
M.highlight = function(scene)
95102
if in_ignored_buffer() then
96103
return
@@ -132,26 +139,28 @@ M.highlight = function(scene)
132139
vim.api.nvim_set_option_value('winhighlight', table.concat(new_value, ','), { win = 0 })
133140

134141
if vim.api.nvim_get_option_value('showmode', { scope = 'global' }) then
135-
if scene == 'visual' then
136-
utils.set_hl('ModeMsg', { link = 'ModesVisualModeMsg' })
137-
elseif scene == 'insert' then
142+
if scene == 'insert' then
138143
utils.set_hl('ModeMsg', { link = 'ModesInsertModeMsg' })
144+
elseif scene == 'visual' then
145+
utils.set_hl('ModeMsg', { link = 'ModesVisualModeMsg' })
139146
else
140147
utils.set_hl('ModeMsg', { link = 'ModesDefaultModeMsg' })
141148
end
142149
end
143150

144151
if config.set_cursor then
145-
if scene == 'delete' then
146-
utils.set_hl('ModesOperator', { link = 'ModesDelete' })
147-
elseif scene == 'copy' then
152+
if scene == 'copy' then
148153
utils.set_hl('ModesOperator', { link = 'ModesCopy' })
149-
elseif scene == 'visual' then
150-
utils.set_hl('ModesOperator', { link = 'ModesVisual' })
151-
elseif scene == 'insert' then
152-
utils.set_hl('ModesOperator', { link = 'ModesInsert' })
154+
elseif scene == 'delete' then
155+
utils.set_hl('ModesOperator', { link = 'ModesDelete' })
153156
elseif scene == 'change' then
154157
utils.set_hl('ModesOperator', { link = 'ModesChange' })
158+
elseif scene == 'format' then
159+
utils.set_hl('ModesOperator', { link = 'ModesFormat' })
160+
elseif scene == 'insert' then
161+
utils.set_hl('ModesOperator', { link = 'ModesInsert' })
162+
elseif scene == 'visual' then
163+
utils.set_hl('ModesOperator', { link = 'ModesVisual' })
155164
else
156165
utils.set_hl('ModesOperator', { link = 'ModesDefault' })
157166
end
@@ -175,6 +184,7 @@ M.define = function()
175184
bg = config.colors.bg or utils.get_bg('Normal', 'Normal'),
176185
copy = config.colors.copy or utils.get_bg('ModesCopy', '#f5c359'),
177186
delete = config.colors.delete or utils.get_bg('ModesDelete', '#c75c6a'),
187+
format = config.colors.format or utils.get_bg('ModesFormat', '#c79585'),
178188
insert = config.colors.insert or utils.get_bg('ModesInsert', '#78ccc5'),
179189
visual = config.colors.visual or utils.get_bg('ModesVisual', '#9745be'),
180190
}
@@ -192,6 +202,11 @@ M.define = function()
192202
colors.bg,
193203
config.line_opacity.change
194204
),
205+
format = utils.blend(
206+
colors.format,
207+
colors.bg,
208+
config.line_opacity.format
209+
),
195210
insert = utils.blend(
196211
colors.insert,
197212
colors.bg,
@@ -211,15 +226,18 @@ M.define = function()
211226
if colors.delete ~= '' then
212227
vim.cmd('hi ModesDelete guibg=' .. colors.delete)
213228
end
229+
if colors.change ~= '' then
230+
vim.cmd('hi ModesChange guibg=' .. colors.change)
231+
end
232+
if colors.format ~= '' then
233+
vim.cmd('hi ModesFormat guibg=' .. colors.format)
234+
end
214235
if colors.insert ~= '' then
215236
vim.cmd('hi ModesInsert guibg=' .. colors.insert)
216237
end
217238
if colors.visual ~= '' then
218239
vim.cmd('hi ModesVisual guibg=' .. colors.visual)
219240
end
220-
if colors.change ~= '' then
221-
vim.cmd('hi ModesChange guibg=' .. colors.change)
222-
end
223241

224242
local default_operator = utils.get_bg('Cursor', '#524f67')
225243
utils.set_hl('ModesDefault', { bg = default_operator })
@@ -234,7 +252,7 @@ M.define = function()
234252
end
235253

236254
local line_nr_gui = utils.get_gui('CursorLineNr', 'none')
237-
for _, mode in ipairs({ 'Copy', 'Delete', 'Insert', 'Visual', 'Change' }) do
255+
for _, mode in ipairs({ 'Copy', 'Delete', 'Change', 'Format', 'Insert', 'Visual' }) do
238256
local mode_fg = colors[mode:lower()]
239257
if mode_fg ~= '' then
240258
local mode_bg = (mode:lower() == 'visual') and 'none' or blended_colors[mode:lower()]
@@ -328,6 +346,7 @@ M.setup = function(opts)
328346
copy = config.line_opacity,
329347
delete = config.line_opacity,
330348
change = config.line_opacity,
349+
format = config.line_opacity,
331350
insert = config.line_opacity,
332351
visual = config.line_opacity,
333352
}
@@ -346,12 +365,15 @@ M.setup = function(opts)
346365
vim.api.nvim_create_autocmd('ModeChanged', {
347366
pattern = '*:no*',
348367
callback = function()
349-
if vim.v.operator == 'y' then
368+
local operator = vim.v.operator
369+
if operator == 'y' then
350370
M.highlight('copy')
351-
elseif vim.v.operator == 'd' then
371+
elseif operator == 'd' then
352372
M.highlight('delete')
353-
elseif vim.v.operator == 'c' then
373+
elseif operator == 'c' then
354374
M.highlight('change')
375+
elseif operator:match('[=!><g]') then
376+
M.highlight('format')
355377
end
356378
end,
357379
})

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require('modes').setup({
2929
copy = "#f5c359",
3030
delete = "#c75c6a",
3131
change = "#c75c6a", -- Optional param, defaults to delete
32+
format = "#c79585",
3233
insert = "#78ccc5",
3334
visual = "#9745be",
3435
},
@@ -63,6 +64,7 @@ require('modes').setup({
6364
| `ModesCopy` | `guibg=#f5c359` |
6465
| `ModesDelete` | `guibg=#c75c6a` |
6566
| `ModesChange` | `ModesDelete` |
67+
| `ModesFormat` | `guibg=#c79585` |
6668
| `ModesInsert` | `guibg=#78ccc5` |
6769
| `ModesVisual` | `guibg=#9745be` |
6870

0 commit comments

Comments
 (0)