Skip to content

Commit bc19b7d

Browse files
committed
feat: add match_listing / truncate setting
For: #429 Some disgusting arithmetic in here, so hopefully it's correct. Note that this setting takes string values: - 'beginning' - 'middle' (the default) - 'end' (the old behavior prior to this commit) But also booleans: - true (same as 'middle') - false (same as 'end') And for foolproof-ness, stringy booleans: - 'true' - 'false' Note that 'middle' is a reasonable default because that's what the old Ruby implementation did (and it wasn't even configurable): - https://github.com/wincent/command-t/blob/3e937f91260acca0061d884d0ff3c9a521c1fde9/ruby/command-t/lib/command-t/match_window.rb#L478-L487 Also note that when 'end', we don't show an actual ellipsis; we just cut off the text at the right edge.
1 parent 3e937f9 commit bc19b7d

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

doc/command-t.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ are documented and they could change without notice at any time; for example:
421421
-- 'double', 'none', 'rounded', 'shadow', 'single', 'solid', or a
422422
-- list of strings.
423423
border = { '', '', '', '│', '┘', '─', '└', '│' },
424+
truncate = 'middle', -- 'beginning', 'end', true, false.
424425
},
425426
never_show_dot_files = false,
426427
order = 'forward', -- 'forward' or 'reverse'.
@@ -610,6 +611,20 @@ Possible vaules are:
610611
(above), but instead of starting from the file currently being edited, start
611612
from Neovim's present working directory.
612613

614+
*command-t-truncate*
615+
string (default: 'middle')
616+
617+
Controls how entries that are too wide to fit inside the match listing are
618+
truncated. Possible values are:
619+
620+
- `'beginning'`: entries are truncated at the beginning (ie. "...bazqux").
621+
- `'middle'`: entries are truncated in the middle (ie. "foo...qux").
622+
- `'end'`: entries are truncated at the end (ie. "foobarbaz").
623+
- `true`: same as `'middle'`.
624+
- `false`: same as `'end'`).
625+
626+
Note that when set to `'end'` (or `false`), no ellipsis is shown.
627+
613628

614629
MAPPINGS *command-t-mappings*
615630

@@ -822,6 +837,8 @@ main (not yet released) ~
822837
- feat: add `match_listing` / `border` and `prompt` / `border` settings.
823838
- feat: add `traverse` and `root_markers` settings
824839
(https://github.com/wincent/command-t/issues/416).
840+
- feat: add `match_listing` / `truncate` setting
841+
(https://github.com/wincent/command-t/discussions/429).
825842

826843
6.0.0-b.1 (16 December 2022) ~
827844

lua/wincent/commandt/init.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ local options_spec = {
133133
},
134134
},
135135
},
136+
truncate = {
137+
kind = {
138+
one_of = {
139+
'beginning',
140+
'middle',
141+
'end',
142+
'true',
143+
'false',
144+
{ kind = 'boolean' },
145+
},
146+
},
147+
},
136148
},
137149
},
138150
never_show_dot_files = { kind = 'boolean' },
@@ -461,6 +473,7 @@ local default_options = {
461473
margin = 10,
462474
match_listing = {
463475
border = { '', '', '', '', '', '', '', '' }, -- 'double', 'none', 'rounded', 'shadow', 'single', 'solid', or a list of strings.
476+
truncate = 'middle',
464477
},
465478
never_show_dot_files = false,
466479
order = 'forward', -- 'forward', 'reverse'.

lua/wincent/commandt/private/match_listing.lua

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function MatchListing.new(options)
2222
margin = 0,
2323
position = 'bottom',
2424
selection_highlight = 'PmenuSel',
25+
truncate = 'middle',
2526
}, options or {})
2627
-- TODO: validate options
2728
local m = {
@@ -33,6 +34,7 @@ function MatchListing.new(options)
3334
_results = nil,
3435
_selected = nil,
3536
_selection_highlight = options.selection_highlight,
37+
_truncate = options.truncate,
3638
_window = nil,
3739
}
3840
setmetatable(m, mt)
@@ -45,7 +47,7 @@ function MatchListing:close()
4547
end
4648
end
4749

48-
local format_line = function(line, width, selected)
50+
local format_line = function(line, width, selected, truncate)
4951
local prefix = selected and '> ' or ' '
5052

5153
-- Sanitize some control characters, plus blackslashes.
@@ -58,6 +60,21 @@ local format_line = function(line, width, selected)
5860
:gsub('\t', '\\t')
5961
:gsub('\v', '\\v')
6062

63+
if #line + #prefix < width then
64+
-- Line fits without trimming.
65+
elseif #line < 5 then
66+
-- Line is so short that adding an ellipsis is not practical.
67+
elseif truncate == true or truncate == 'true' or truncate == 'middle' then
68+
local half = math.floor((width - 2) / 2)
69+
local left = line:sub(1, half - 2 + width % 2)
70+
local right = line:sub(2 - half)
71+
line = left .. '...' .. right
72+
elseif truncate == 'beginning' then
73+
line = '...' .. line:sub(-width + #prefix + 3)
74+
elseif truncate == false or truncate == 'false' or truncate == 'end' then
75+
-- Fall through; truncation will happen before the final `return`.
76+
end
77+
6178
-- Right pad so that selection highlighting is shown across full width.
6279
if width < 102 and #line > 99 then
6380
-- No padding needed.
@@ -73,7 +90,7 @@ local format_line = function(line, width, selected)
7390
end
7491
end
7592

76-
-- Trim right to make sure we never wrap.
93+
-- Trim to make sure we never wrap.
7794
return line:sub(1, width)
7895
end
7996

@@ -84,12 +101,12 @@ function MatchListing:select(selected)
84101
if self._window then
85102
local width = self._window:width() or vim.o.columns -- BUG: width may be cached/stale
86103

87-
local previous_selection = format_line(self._results[self._selected], width, false)
104+
local previous_selection = format_line(self._results[self._selected], width, false, self._truncate)
88105
self._window:replace_line(previous_selection, self._selected)
89106
self._window:unhighlight_line(self._selected)
90107

91108
self._selected = selected
92-
local new_selection = format_line(self._results[self._selected], width, true)
109+
local new_selection = format_line(self._results[self._selected], width, true, self._truncate)
93110
self._window:replace_line(new_selection, self._selected)
94111
self._window:highlight_line(self._selected)
95112
end
@@ -150,7 +167,7 @@ function MatchListing:update(results, options)
150167
self._lines = {}
151168
for i, result in ipairs(results) do
152169
local selected = i == self._selected
153-
local line = format_line(result, width, selected)
170+
local line = format_line(result, width, selected, self._truncate)
154171
table.insert(self._lines, line)
155172
end
156173
self._window:unhighlight()

lua/wincent/commandt/private/ui.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ ui.show = function(finder, options)
9191
margin = options.margin,
9292
position = options.position,
9393
selection_highlight = options.selection_highlight,
94+
truncate = options.match_listing.truncate,
9495
})
9596
match_listing:show()
9697

0 commit comments

Comments
 (0)