Skip to content

Commit 4cc5502

Browse files
committed
feat(cli)!: new user command interface
Replaced `TSContextEnable`, `TSContextDisable`, `TSContextToggle` with `TSContext` with `enable`, `disable` and `toggle` as subcommands. Also moved init code into `plugin/`.
1 parent ebee603 commit 4cc5502

File tree

9 files changed

+84
-50
lines changed

9 files changed

+84
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ require'treesitter-context'.setup{
378378

379379
## Commands
380380

381-
`TSContextEnable`, `TSContextDisable` and `TSContextToggle`.
381+
`TSContext` with subcommands `enable`, `disable` and `toggle`.
382382

383383
## Appearance
384384

doc/nvim-treesitter-context.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ not required.
6464
==============================================================================
6565
COMMANDS *nvim-treesitter-context-commands*
6666

67-
`TSContextEnable`, `TSContextDisable` and `TSContextToggle`.
67+
`TSContext` with subcommands `enable`, `disable` and `toggle`
6868

6969
==============================================================================
7070
HIGHLIGHTS *nvim-treesitter-context-highlights*

lua/treesitter-context.lua

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local api = vim.api
33
local config = require('treesitter-context.config')
44

55
local augroup = api.nvim_create_augroup
6-
local command = api.nvim_create_user_command
76

87
local enabled = false
98

@@ -266,24 +265,6 @@ function M.enabled()
266265
return enabled
267266
end
268267

269-
local function init()
270-
command('TSContextEnable', M.enable, {})
271-
command('TSContextDisable', M.disable, {})
272-
command('TSContextToggle', M.toggle, {})
273-
274-
api.nvim_set_hl(0, 'TreesitterContext', { link = 'NormalFloat', default = true })
275-
api.nvim_set_hl(0, 'TreesitterContextLineNumber', { link = 'LineNr', default = true })
276-
api.nvim_set_hl(0, 'TreesitterContextBottom', { link = 'NONE', default = true })
277-
api.nvim_set_hl(
278-
0,
279-
'TreesitterContextLineNumberBottom',
280-
{ link = 'TreesitterContextBottom', default = true }
281-
)
282-
api.nvim_set_hl(0, 'TreesitterContextSeparator', { link = 'FloatBorder', default = true })
283-
end
284-
285-
local did_init = false
286-
287268
--- @param options? TSContext.UserConfig
288269
function M.setup(options)
289270
-- NB: setup may be called several times.
@@ -297,11 +278,6 @@ function M.setup(options)
297278
else
298279
M.disable()
299280
end
300-
301-
if not did_init then
302-
init()
303-
did_init = true
304-
end
305281
end
306282

307283
--- @param depth integer? default 1

lua/treesitter-context/cli.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local subcmds = { 'enable', 'disable', 'toggle' }
2+
3+
local M = {}
4+
5+
--- @param command string
6+
local function do_subcmd(command)
7+
local TSContext = require('treesitter-context')
8+
if TSContext[command] then
9+
TSContext[command]()
10+
else
11+
vim.notify('TSContext: Unknown command ' .. command, vim.log.levels.ERROR)
12+
end
13+
end
14+
15+
--- @param args string[]
16+
function M.run(args)
17+
if #args == 0 then
18+
vim.ui.select(subcmds, {
19+
prompt = 'Treesitter Context: ',
20+
format_item = function(item)
21+
return item:sub(1, 1):upper() .. item:sub(2)
22+
end,
23+
}, function(choice)
24+
if choice then
25+
do_subcmd(choice)
26+
end
27+
end)
28+
return
29+
end
30+
31+
do_subcmd(args[1])
32+
end
33+
34+
function M.complete()
35+
return subcmds
36+
end
37+
38+
return M

plugin/treesitter-context.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
require('treesitter-context').setup()
1+
vim.api.nvim_create_user_command('TSContext', function(args)
2+
require('treesitter-context.cli').run(args.fargs)
3+
end, {
4+
complete = function()
5+
return require('treesitter-context.cli').complete()
6+
end,
7+
nargs = '*',
8+
desc = 'Manage Treesitter Context',
9+
})
10+
11+
vim.api.nvim_set_hl(0, 'TreesitterContext', { link = 'NormalFloat', default = true })
12+
vim.api.nvim_set_hl(0, 'TreesitterContextLineNumber', { link = 'LineNr', default = true })
13+
vim.api.nvim_set_hl(0, 'TreesitterContextBottom', { link = 'NONE', default = true })
14+
vim.api.nvim_set_hl(
15+
0,
16+
'TreesitterContextLineNumberBottom',
17+
{ link = 'TreesitterContextBottom', default = true }
18+
)
19+
vim.api.nvim_set_hl(0, 'TreesitterContextSeparator', { link = 'FloatBorder', default = true })

test/contexts_spec.lua

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ local api = helpers.api
66
local fn = helpers.fn
77

88
local tc_helpers = require('test.helpers')
9-
local install_langs = tc_helpers.install_langs
10-
local get_langs = tc_helpers.get_langs
119

1210
--- @param line string
1311
--- @return string?
@@ -70,7 +68,7 @@ local function install_langs_for_file(filename, root_lang)
7068
if seen_langs[current_lang] then
7169
goto continue
7270
end
73-
exec_lua(install_langs, current_lang)
71+
exec_lua(tc_helpers.install_langs, current_lang)
7472

7573
-- Query for injections in the current language, and queue them for installation.
7674
--- @diagnostic disable-next-line: redefined-local Not actually redefining locals
@@ -110,7 +108,7 @@ local function install_langs_for_file(filename, root_lang)
110108
end
111109
end
112110

113-
local langs = get_langs()
111+
local langs = tc_helpers.get_langs()
114112
local langs_with_queries = {} --- @type string[]
115113
for _, lang in ipairs(langs) do
116114
if vim.uv.fs_stat('queries/' .. lang .. '/context.scm') then
@@ -122,8 +120,7 @@ local lang_to_test_files = {} --- @type table<string,string[]>
122120
setup(function()
123121
helpers.clear()
124122
exec_lua(tc_helpers.setup)
125-
126-
exec_lua(install_langs, 'lua')
123+
exec_lua(tc_helpers.install_langs, 'lua')
127124

128125
local test_files = fn.globpath('test/lang', '*', true, true) --- @type string[]
129126
for _, test_file in ipairs(test_files) do

test/helpers.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ function M.get_langs()
4343
return langs
4444
end
4545

46-
function M.setup()
46+
--- @param opts? TSContext.UserConfig
47+
function M.setup(opts)
4748
-- Do not pull in parsers from /usr/local/share/ as they may
4849
-- be the wrong ABI
4950
vim.opt.runtimepath = {
@@ -55,6 +56,10 @@ function M.setup()
5556
install_dir = vim.fs.joinpath('deps', 'nvim-treesitter-data'),
5657
})
5758

59+
require('treesitter-context').setup(opts)
60+
-- Need to source plugin to define highlights for screen tests
61+
vim.cmd.source(vim.api.nvim_get_runtime_file('plugin/treesitter-context.lua', false)[1])
62+
5863
vim.env.XDG_CACHE_HOME = 'scratch/cache'
5964
vim.opt.packpath = ''
6065
end

test/queries_spec.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ local helpers = require('nvim-test.helpers')
33
local exec_lua = helpers.exec_lua
44

55
local tc_helpers = require('test.helpers')
6-
local install_langs = tc_helpers.install_langs
7-
local get_langs = tc_helpers.get_langs
86

97
describe('query:', function()
108
local readme_lines = {} --- @type string[]
@@ -20,7 +18,7 @@ describe('query:', function()
2018
f:close()
2119
end)
2220

23-
for _, lang in ipairs(get_langs()) do
21+
for _, lang in ipairs(tc_helpers.get_langs()) do
2422
it(lang, function()
2523
local lang_index --- @type integer
2624
local last_supported_lang_index --- @type integer
@@ -50,16 +48,18 @@ describe('query:', function()
5048
if not vim.uv.fs_stat('queries/' .. lang .. '/context.scm') then
5149
table.insert(readme_lines, last_lang_index, (' - [ ] `%s`'):format(lang))
5250
pending('no queries/' .. lang .. '/context.scm')
53-
else
54-
exec_lua(install_langs, lang)
55-
local ok = exec_lua([[return pcall(vim.treesitter.query.get, ...)]], lang, 'context')
56-
table.insert(
57-
readme_lines,
58-
last_supported_lang_index,
59-
(' - [x] `%s`%s'):format(lang, ok and '' or ' (broken)')
60-
)
61-
assert(ok)
51+
return
6252
end
53+
exec_lua(tc_helpers.install_langs, lang)
54+
local ok = exec_lua(function(...)
55+
return (pcall(vim.treesitter.query.get, ...))
56+
end, lang, 'context')
57+
table.insert(
58+
readme_lines,
59+
last_supported_lang_index,
60+
(' - [x] `%s`%s'):format(lang, ok and '' or ' (broken)')
61+
)
62+
assert(ok)
6363
end)
6464
end
6565

test/ts_context_spec.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ describe('ts_context', function()
5858
end)
5959

6060
it('load the plugin', function()
61-
exec_lua([[require'treesitter-context'.setup{}]])
61+
exec_lua(tc_helpers.setup)
6262
end)
6363

6464
it('edit a file', function()
6565
exec_lua(install_langs, 'lua')
66-
exec_lua([[require'treesitter-context'.setup{}]])
66+
exec_lua(tc_helpers.setup)
6767
cmd('edit test/test_file.lua')
6868
exec_lua([[vim.treesitter.start()]])
6969
feed('<C-e>')
@@ -107,9 +107,9 @@ describe('ts_context', function()
107107

108108
describe('language:', function()
109109
before_each(function()
110-
exec_lua([[require'treesitter-context'.setup{
110+
exec_lua(tc_helpers.setup, {
111111
mode = 'topline',
112-
}]])
112+
})
113113
cmd('set scrolloff=5')
114114
cmd('set nowrap')
115115
end)

0 commit comments

Comments
 (0)