Skip to content

Commit 320923c

Browse files
committed
Add group option hint
With custom changers, the cycle result might not obviously explain its origin. This option let user to add a static "hint" for each group item. Used in select UI or conflict UI, to help user see more information of the item.
1 parent a256030 commit 320923c

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ CHANGES
1111

1212
* Add `naming` group option, cycle between naming conventions.
1313

14+
* Add `hints` group option, display a static hint for each item in select or conflict UI.
15+
1416
* `sub_pairs` now supports using an identical character on both begin and end sides.
1517

1618
* `Cycle()` and `CycleSelect()` functions now accept extra groups argument,

autoload/cycle.vim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
" naming
1919
" year
2020
" cond
21+
" hints
2122
"
2223
" Sub options for 'regex':
2324
" to
@@ -113,9 +114,12 @@ function! cycle#select(class_name, ...) "{{{
113114

114115
let options = []
115116
for match in matches
117+
let hints = get(match.group.options, 'hints', [])
118+
let hint = empty(hints) ? '' : get(hints, match.index)
116119
call add(options, {
117120
\ "group_name": get(match.group.options, 'name', ''),
118-
\ "text": match.pairs.after.text
121+
\ "text": match.pairs.after.text,
122+
\ "hint": hint,
119123
\ })
120124
endfor
121125

@@ -269,9 +273,12 @@ function! s:conflict(ctx) "{{{
269273

270274
let options = []
271275
for match in matches
276+
let hints = get(match.group.options, 'hints', [])
277+
let hint = empty(hints) ? '' : get(hints, match.index)
272278
call add(options, {
273279
\ "group_name": get(match.group.options, 'name', ''),
274-
\ "text": match.pairs.after.text
280+
\ "text": match.pairs.after.text,
281+
\ "hint": hint,
275282
\ })
276283
endfor
277284

autoload/cycle/select.vim

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let s:funcs = {}
22
let s:loaders = {}
3+
let s:hint_pad = 4
34

45

56
" Select UI Loaders: {{{
@@ -64,13 +65,17 @@ function! s:open_inputlist(options) "{{{
6465
let index = 0
6566
let candidates = []
6667
let max_length = max(map(copy(a:options), 'strlen(v:val.text)'))
68+
let max_hint_len = max(map(copy(a:options), 'strlen(v:val.hint)'))
69+
let hint_pad = max_hint_len > 0 ? s:hint_pad : 0
6770
for option in a:options
6871
let group = get(option, 'group_name', '')
6972
let line = printf(
70-
\ '%2S => %-*S %S',
73+
\ '%2S => %-*S ' . repeat(' ', hint_pad) . ' %-*S %S',
7174
\ index + 1,
7275
\ max_length,
7376
\ option.text,
77+
\ max_hint_len,
78+
\ option.hint,
7479
\ len(group) ? printf(' (%s)', group) : ''
7580
\ )
7681
call add(candidates, line)
@@ -98,14 +103,19 @@ function! s:open_confirm(options) "{{{
98103
let captions = []
99104
let candidates = []
100105
let max_length = max(map(copy(a:options), 'strlen(v:val.text)'))
106+
let max_hint_len = max(map(copy(a:options), 'strlen(v:val.hint)'))
107+
let hint_pad = max_hint_len > 0 ? s:hint_pad : 0
108+
101109
for option in a:options
102110
let caption = nr2char(char2nr('A') + index)
103111
let group = get(option, 'group_name', '')
104112
let line = printf(
105-
\ ' %2S) => %-*S %S',
113+
\ ' %2S) => %-*S ' . repeat(' ', hint_pad) . ' %-*S %S',
106114
\ caption,
107115
\ max_length,
108116
\ option.text,
117+
\ max_hint_len,
118+
\ option.hint,
109119
\ len(group) ? printf(' (%s)', group) : ''
110120
\ )
111121
call add(candidates, line)

doc/cycle.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,18 @@ By default no options are set.
584584
\ [['==', '!='], { 'cond': function('s:not_lua_context') }],
585585
\ ]
586586
<
587+
"hints" List (default: none) ~
588+
589+
UI hint for each item.
590+
Will be displayed in |CycleSelect()| or a conflict prompt. The list contains
591+
string values, each corresponding to a group item. >
592+
\ [['snake_case', 'SCREAMING_SNAKE_CASE'],
593+
\ {'hints': ['snake_case', 'MACRO_CASE'}]}],
594+
< results in select UI like: >
595+
Cycle to:
596+
A) => foo_bar_baz snake_case
597+
B) => FOO_BAR_BAZ MACRO_CASE
598+
<
587599
------------------------------------------------------------------------------
588600
Phased Search *cycle-phased-search*
589601

lua/vim_cycle/init.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local M = {}
2+
local hint_pad_size = 4
23

34
local make_select_options = function(options)
45
local max_length = math.max(
@@ -8,7 +9,21 @@ local make_select_options = function(options)
89
end, options)
910
)
1011
)
11-
local format_str = string.format("%%-%dS", max_length)
12+
13+
local max_hint_length = math.max(
14+
unpack(
15+
vim.tbl_map(function(opt)
16+
return string.len(opt.hint)
17+
end, options)
18+
)
19+
)
20+
21+
local hint_pad = ''
22+
if max_hint_length > 0 then
23+
hint_pad = string.rep(' ', hint_pad_size)
24+
end
25+
26+
local format_str = string.format("%%-%dS" .. hint_pad .. " %%-%dS", max_length, max_hint_length)
1227

1328
local select_options = {
1429
prompt = 'Cycle to:',
@@ -20,7 +35,7 @@ local make_select_options = function(options)
2035
group = ''
2136
end
2237

23-
return vim.fn.printf(format_str, opt.text) .. group
38+
return vim.fn.printf(format_str, opt.text, opt.hint) .. group
2439
end,
2540
}
2641

0 commit comments

Comments
 (0)