Skip to content

Commit a389f62

Browse files
committed
feat: accept functions for ignore_case and smart_case settings
Not because I see users passing functions that do particularly interesting or useful things, but because this is the easiest way to make Command-T look up `vim.o.ignorecase` and `vim.o.smartcase` lazily, if the user does not provide an explicit `ignore_case` or `smart_case` setting. Motivated by: - #431 (comment)
1 parent b6ea600 commit a389f62

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

doc/command-t.txt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,13 @@ whenever they match the search query.
501501
See also |commandt.setup.never_show_dot_files|.
502502

503503
*commandt.setup.ignore_case*
504-
boolean (default: none)
504+
boolean or function (default: none)
505505

506-
Ignore case when searching. When unset (which is the default), the value of
507-
Neovim's |'ignorecase'| setting is used instead. See also
508-
|commandt.setup.smart_case|.
506+
Either a boolean, or a function that returns a boolean, that instructs
507+
Command-T to ignore case when searching. When unset (which is the default),
508+
the value of Neovim's |'ignorecase'| setting is used instead.
509+
510+
See also |commandt.setup.smart_case|.
509511

510512
*commandt.setup.match_listing.icons*
511513
boolean or function (default: true)
@@ -667,12 +669,13 @@ output is buffered, it's possible that slightly more than `max_files` items
667669
may be returned.
668670

669671
*commandt.setup.smart_case*
670-
boolean (default: none)
672+
boolean or function (default: none)
671673

672-
Overrides the |commandt.setup.ignore_case| setting if the search pattern
673-
contains uppercase characters, forcing the match to be case-sensitive. If
674-
unset (which is the default), the value of Neovim's |'smartcase'| setting will
675-
be used instead.
674+
Either a boolean, or a function that returns a boolean, that instructs
675+
Command-T to override the |commandt.setup.ignore_case| setting if the search
676+
pattern contains uppercase characters, forcing the match to be case-sensitive.
677+
If unset (which is the default), the value of Neovim's |'smartcase'| setting
678+
will be used instead.
676679

677680
*commandt.setup.traverse*
678681
string (default: 'none')

lua/wincent/commandt/init.lua

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,16 @@ local options_spec = {
101101
},
102102
height = { kind = 'number' },
103103
ignore_case = {
104-
kind = 'boolean',
104+
kind = {
105+
one_of = {
106+
{
107+
kind = 'boolean',
108+
},
109+
{
110+
kind = 'function',
111+
},
112+
},
113+
},
105114
optional = true,
106115
},
107116
mappings = {
@@ -237,7 +246,16 @@ local options_spec = {
237246
},
238247
selection_highlight = { kind = 'string' },
239248
smart_case = {
240-
kind = 'boolean',
249+
kind = {
250+
one_of = {
251+
{
252+
kind = 'boolean',
253+
},
254+
{
255+
kind = 'function',
256+
},
257+
},
258+
},
241259
optional = true,
242260
},
243261
threads = {
@@ -438,7 +456,11 @@ local default_options = {
438456
},
439457
},
440458
height = 15,
441-
ignore_case = nil, -- If nil, will infer from Neovim's `'ignorecase'`.
459+
460+
-- If nil, will infer from Neovim's `'ignorecase'`.
461+
ignore_case = function()
462+
return vim.o.ignorecase
463+
end,
442464

443465
-- Note that because of the way we merge mappings recursively, you can _add_
444466
-- or _replace_ a mapping easily, but to _remove_ it you have to assign it to
@@ -520,18 +542,21 @@ local default_options = {
520542
},
521543
},
522544
selection_highlight = 'PmenuSel',
523-
smart_case = nil, -- If nil, will infer from Neovim's `'smartcase'`.
545+
546+
-- If nil, will infer from Neovim's `'smartcase'`.
547+
smart_case = function()
548+
return vim.o.smartcase
549+
end,
550+
524551
threads = nil, -- Let heuristic apply.
525552
traverse = 'none', -- 'file', 'pwd' or 'none'.
526553
}
527554

528555
local _options = copy(default_options)
529556

530557
-- Have to add some of these explicitly otherwise the ones with `nil` defaults
531-
-- won't come through (eg. `ignore_case` etc).
558+
-- (eg. `threads`) won't come through.
532559
local allowed_options = concat(keys(default_options), {
533-
'ignore_case',
534-
'smart_case',
535560
'threads',
536561
})
537562

@@ -588,14 +613,6 @@ local sanitize_options = function(options, base)
588613
options = merge(base, options)
589614
end
590615

591-
-- Inferred from Neovim settings if not explicitly set.
592-
if options.ignore_case == nil then
593-
options.ignore_case = vim.o.ignorecase
594-
end
595-
if options.smart_case == nil then
596-
options.smart_case = vim.o.smartcase
597-
end
598-
599616
local validate = require('wincent.commandt.private.validate')
600617
errors = merge(errors, validate('', nil, options, options_spec, default_options))
601618
return options, errors

lua/wincent/commandt/private/lib.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
local ffi = require('ffi')
55

66
local merge = require('wincent.commandt.private.merge')
7+
local toboolean = require('wincent.commandt.private.toboolean')
78

89
local lib = {}
910

@@ -192,6 +193,12 @@ lib.matcher_new = function(scanner, options)
192193
if options.limit < 1 then
193194
error('limit must be > 0')
194195
end
196+
if type(options.ignore_case) == 'function' then
197+
options.ignore_case = toboolean(options.ignore_case())
198+
end
199+
if type(options.smart_case) == 'function' then
200+
options.smart_case = toboolean(options.smart_case())
201+
end
195202

196203
local matcher = c.commandt_matcher_new(
197204
scanner,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
2+
-- SPDX-License-Identifier: BSD-2-Clause
3+
4+
local function toboolean(value)
5+
return not not value
6+
end
7+
8+
return toboolean

0 commit comments

Comments
 (0)