Skip to content

Commit 153a076

Browse files
isakbmlewis6991
authored andcommitted
feat(config): max_lines accept % win.height
Fixes #275 Update lua/treesitter-context/context.lua Co-authored-by: Lewis Russell <[email protected]> Update lua/treesitter-context/context.lua Co-authored-by: Lewis Russell <[email protected]> terser initialization of max_lines
1 parent 404502e commit 153a076

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/nvim-treesitter-context.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ not required.
2727
multiwindow = false,
2828

2929
-- How many lines the window should span. Values <= 0 mean no limit.
30+
-- Can be '<int>%' like '30%' - to specify percentage of win.height
3031
max_lines = 0,
3132

3233
-- Minimum editor window height to enable context. Values <= 0 mean no

lua/treesitter-context/config.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--- @class (exact) TSContext.Config
22
--- @field enable boolean
33
--- @field multiwindow boolean
4-
--- @field max_lines integer
4+
--- @field max_lines integer | string
55
--- @field min_window_height integer
66
--- @field line_numbers boolean
77
--- @field multiline_threshold integer
@@ -20,7 +20,8 @@
2020
--- @field multiwindow? boolean
2121
---
2222
--- How many lines the window should span. Values <= 0 mean no limit.
23-
--- @field max_lines? integer
23+
--- Can be '<int>%' like '30%' - to specify percentage of win.height.
24+
--- @field max_lines? integer | string
2425
---
2526
--- Minimum editor window height to enable context. Values <= 0 mean no limit.
2627
--- @field min_window_height? integer

lua/treesitter-context/context.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,29 @@ local function get_parent_nodes(langtree, range)
4545
return ret
4646
end
4747

48+
--- @param winid integer
49+
--- @param percent string
50+
--- @return integer
51+
local function max_lines_from_string(winid, percent)
52+
local win_height = api.nvim_win_get_height(winid)
53+
local percent_s = percent:match('^(%d+)%%$')
54+
local percent = percent_s and tonumber(percent_s, 10) or 0
55+
return math.ceil((percent / 100.0) * win_height)
56+
end
57+
4858
--- @param winid integer
4959
--- @return integer
5060
local function calc_max_lines(winid)
51-
local max_lines = config.max_lines == 0 and -1 or config.max_lines
61+
local max_lines --- @type integer
62+
63+
if type(config.max_lines) == 'string' then
64+
max_lines = max_lines_from_string(winid, config.max_lines)
65+
else
66+
max_lines = config.max_lines --- @type integer
67+
end
68+
69+
-- ensure we never have zero as max lines
70+
max_lines = max_lines == 0 and -1 or max_lines
5271

5372
local wintop = fn.line('w0', winid)
5473
local cursor = fn.line('.', winid)

0 commit comments

Comments
 (0)