Skip to content

Commit bb84d4c

Browse files
committed
fix: use better defaults when position = 'bottom'
1. Swap the default borders, because they assumed the prompt at the top and the match listing underneath: ┌ CommandT [fd] ─────────────┐ prompt border characters: │> │ { '┌', '─', '┐', '│', '┤', '─', '├', '│' } ├────────────────────────────┤ │> CODE_OF_CONDUCT.md │ match listing border │ CONTRIBUTING.md │ characters: │ doc/command-t-ruby.txt │ { '', '', '', '│', '┘', '─', '└', '│' } │ doc/command-t.txt │ └────────────────────────────┘ When the prompt is at the bottom and the match listing is above (ie. when `position = 'bottom'` is set), the borders need to get swapped: ┌────────────────────────────┐ │> CODE_OF_CONDUCT.md │ match listing border │ CONTRIBUTING.md │ characters: │ doc/command-t-ruby.txt │ { '┌', '─', '┐', '│', '┤', '─', '├', '│' } │ doc/command-t.txt │ ├────────────────────────────┤ │> │ prompt border characters: └ CommandT [fd] ─────────────┘ { '', '', '', '│', '┘', '─', '└', '│' } 2. Likewise, when `position = 'bottom'` is set, we need to change the prompt's `title` to be a `footer` instead. Closes: #436
1 parent c153217 commit bb84d4c

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

lua/wincent/commandt/init.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ local default_options = {
630630
traverse = 'none', -- 'file', 'pwd' or 'none'.
631631
}
632632

633-
local _options = copy(default_options)
633+
local _options = nil
634634

635635
-- Have to add some of these explicitly otherwise the ones with `nil` defaults
636636
-- (eg. `threads`) won't come through.
@@ -639,7 +639,7 @@ local allowed_options = concat(keys(default_options), {
639639
})
640640

641641
local get_directory = function()
642-
local options = _options -- No need for deep copy provided by `commandt.options()`.
642+
local options = commandt.options()
643643
if options.traverse == 'file' then
644644
local file = vim.fn.expand('%:p:h') -- If no current file, returns current dir.
645645
return require('wincent.commandt.private.find_root')(file, options.root_markers)
@@ -652,8 +652,16 @@ end
652652

653653
commandt._directory = get_directory
654654

655-
commandt.default_options = function()
656-
return copy(default_options)
655+
commandt.default_options = function(options)
656+
local result = copy(default_options)
657+
658+
-- Swap border definitions if `position = 'bottom'` is set.
659+
if type(options) == 'table' and options.position == 'bottom' then
660+
result.match_listing.border = default_options.prompt.border
661+
result.prompt.border = default_options.match_listing.border
662+
end
663+
664+
return result
657665
end
658666

659667
commandt.file_finder = function(directory)
@@ -702,7 +710,7 @@ local sanitize_options = function(options, base)
702710
end
703711

704712
local validate = require('wincent.commandt.private.validate')
705-
errors = merge(errors, validate('', nil, options, options_spec, default_options))
713+
errors = merge(errors, validate('', nil, options, options_spec, commandt.default_options(options)))
706714
return options, errors
707715
end
708716

@@ -765,11 +773,11 @@ commandt.on_open = on_open
765773
commandt.open = smart_open
766774

767775
commandt.options = function()
768-
return copy(_options)
776+
return copy(_options or commandt.default_options())
769777
end
770778

771779
commandt.setup = function(options)
772-
local sanitized_options, errors = sanitize_options(options, _options)
780+
local sanitized_options, errors = sanitize_options(options, _options or commandt.default_options(options))
773781
_options = sanitized_options
774782

775783
if vim.g.CommandTPreferredImplementation == 'ruby' then

lua/wincent/commandt/private/match_listing.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ function MatchListing:show()
174174
self:update(self._results, { selected = self._selected })
175175
end
176176
end,
177+
position = self._position,
177178
selection_highlight = self._selection_highlight,
178179
title = '',
179180
top = top,

lua/wincent/commandt/private/prompt.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function Prompt:show()
9999
self._window = nil
100100
end,
101101
on_leave = self._on_leave,
102+
position = self._position,
102103
title = self:title(),
103104
top = top,
104105
})

lua/wincent/commandt/private/window.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ local validate_options = function(options)
4848
if options.on_resize ~= nil and type(options.on_resize) ~= 'function' then
4949
error('Window.new(): `on_resize` must be a function')
5050
end
51+
if options.position ~= 'bottom' and options.position ~= 'center' and options.position ~= 'top' then
52+
error("Window.new(): `position` must be 'bottom', 'center', or 'top'")
53+
end
5154
if options.selection_highlight ~= nil and type(options.selection_highlight) ~= 'string' then
5255
error('Window.new(): `selection_highlight` must be a string')
5356
end
@@ -68,6 +71,7 @@ function Window.new(options)
6871
on_close = nil,
6972
on_leave = nil,
7073
on_resize = nil,
74+
position = 'top',
7175
prompt = '> ', -- Has no effect unless `buftype` is 'prompt'.
7276
selection_highlight = 'PmenuSel',
7377
title = 'Command-T', -- Set to '' to suppress.
@@ -90,6 +94,7 @@ function Window.new(options)
9094
_on_leave = options.on_leave,
9195
_on_resize = options.on_resize,
9296
_padded_title = options.title ~= '' and (' ' .. options.title .. ' ') or '',
97+
_position = options.position,
9398
_prompt = options.prompt,
9499
_resize_autocmd = nil,
95100
_selection_highlight = options.selection_highlight,
@@ -203,7 +208,8 @@ function Window:set_title(title)
203208
self:_reposition()
204209
if self._main_window then
205210
vim.api.nvim_win_set_config(self._main_window, {
206-
title = { { self._padded_title, 'FloatBorder' } },
211+
title = self._position ~= 'bottom' and { { self._padded_title, 'FloatBorder' } } or nil,
212+
footer = self._position == 'bottom' and { { self._padded_title, 'FloatBorder' } } or nil,
207213
})
208214
end
209215
end
@@ -269,7 +275,8 @@ function Window:show()
269275
noautocmd = true,
270276
relative = 'editor',
271277
style = 'minimal',
272-
title = { { self._padded_title, 'FloatBorder' } },
278+
title = self._position ~= 'bottom' and { { self._padded_title, 'FloatBorder' } } or nil,
279+
footer = self._position == 'bottom' and { { self._padded_title, 'FloatBorder' } } or nil,
273280
}, position)
274281
)
275282
if self._main_window == 0 then

0 commit comments

Comments
 (0)