Skip to content

Commit 4da66da

Browse files
tjdevriesGithub Actions
and
Github Actions
authored
feat: add ivy-style layout strategy (#771)
* feat: add new layout strategy * [docgen] Update doc/telescope.txt skip-checks: true Co-authored-by: Github Actions <actions@github>
1 parent b78d4ef commit 4da66da

File tree

6 files changed

+220
-40
lines changed

6 files changed

+220
-40
lines changed

doc/telescope.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,37 @@ telescope.extensions() *telescope.extensions()*
9191

9292

9393

94+
================================================================================
95+
*telescope.themes*
96+
97+
Themes are ways to combine several elements of styling together.
98+
99+
They are helpful for managing the several differnt UI aspects for telescope and
100+
provide a simple interface for users to get a particular "style" of picker.
101+
102+
themes.get_dropdown() *themes.get_dropdown()*
103+
Dropdown style theme.
104+
105+
Usage:
106+
107+
`local builtin = require('telescope.builtin')`
108+
`local themes = require('telescope.themes')`
109+
`builtin.find_files(themes.get_dropdown())`
110+
111+
112+
113+
themes.get_ivy() *themes.get_ivy()*
114+
Ivy style theme.
115+
116+
Usage:
117+
118+
`local builtin = require('telescope.builtin')`
119+
`local themes = require('telescope.themes')`
120+
`builtin.find_files(themes.get_ivy())`
121+
122+
123+
124+
94125
================================================================================
95126
*telescope.actions.set*
96127

lua/telescope/pickers/layout_strategies.lua

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,38 +61,7 @@
6161
local config = require('telescope.config')
6262
local resolve = require("telescope.config.resolve")
6363

64-
local function get_initial_window_options(picker)
65-
local popup_border = resolve.win_option(picker.window.border)
66-
local popup_borderchars = resolve.win_option(picker.window.borderchars)
67-
68-
local preview = {
69-
title = picker.preview_title,
70-
border = popup_border.preview,
71-
borderchars = popup_borderchars.preview,
72-
enter = false,
73-
highlight = false
74-
}
75-
76-
local results = {
77-
title = picker.results_title,
78-
border = popup_border.results,
79-
borderchars = popup_borderchars.results,
80-
enter = false,
81-
}
82-
83-
local prompt = {
84-
title = picker.prompt_title,
85-
border = popup_border.prompt,
86-
borderchars = popup_borderchars.prompt,
87-
enter = true
88-
}
89-
90-
return {
91-
preview = preview,
92-
results = results,
93-
prompt = prompt,
94-
}
95-
end
64+
local p_window = require('telescope.pickers.window')
9665

9766

9867
-- Check if there are any borders. Right now it's a little raw as
@@ -139,7 +108,7 @@ layout_strategies.horizontal = function(self, max_columns, max_lines)
139108
scroll_speed = "The speed when scrolling through the previewer",
140109
})
141110

142-
local initial_options = get_initial_window_options(self)
111+
local initial_options = p_window.get_initial_window_options(self)
143112
local preview = initial_options.preview
144113
local results = initial_options.results
145114
local prompt = initial_options.prompt
@@ -237,7 +206,7 @@ end
237206
--- +--------------+
238207
--- </pre>
239208
layout_strategies.center = function(self, columns, lines)
240-
local initial_options = get_initial_window_options(self)
209+
local initial_options = p_window.get_initial_window_options(self)
241210
local preview = initial_options.preview
242211
local results = initial_options.results
243212
local prompt = initial_options.prompt
@@ -307,7 +276,7 @@ layout_strategies.vertical = function(self, max_columns, max_lines)
307276
scroll_speed = "The speed when scrolling through the previewer",
308277
})
309278

310-
local initial_options = get_initial_window_options(self)
279+
local initial_options = p_window.get_initial_window_options(self)
311280
local preview = initial_options.preview
312281
local results = initial_options.results
313282
local prompt = initial_options.prompt
@@ -447,4 +416,67 @@ layout_strategies.current_buffer = function(self, _, _)
447416
}
448417
end
449418

419+
layout_strategies.bottom_pane = function(self, max_columns, max_lines)
420+
local layout_config = validate_layout_config(self.layout_config or {}, {
421+
height = "The height of the layout",
422+
})
423+
424+
local initial_options = p_window.get_initial_window_options(self)
425+
local results = initial_options.results
426+
local prompt = initial_options.prompt
427+
local preview = initial_options.preview
428+
429+
local result_height = layout_config.height or 25
430+
431+
local prompt_width = max_columns
432+
local col = 0
433+
434+
local has_border = not not self.window.border
435+
if has_border then
436+
col = 1
437+
prompt_width = prompt_width - 2
438+
end
439+
440+
local result_width
441+
if self.previewer then
442+
result_width = math.floor(prompt_width / 2)
443+
444+
local base_col = result_width + 1
445+
if has_border then
446+
preview = vim.tbl_deep_extend("force", {
447+
col = base_col + 2,
448+
line = max_lines - result_height + 1,
449+
width = prompt_width - result_width - 2,
450+
height = result_height - 1,
451+
}, preview)
452+
else
453+
preview = vim.tbl_deep_extend("force", {
454+
col = base_col,
455+
line = max_lines - result_height,
456+
width = prompt_width - result_width,
457+
height = result_height,
458+
}, preview)
459+
end
460+
else
461+
preview = nil
462+
result_width = prompt_width
463+
end
464+
465+
return {
466+
preview = preview,
467+
prompt = vim.tbl_deep_extend("force", prompt, {
468+
line = max_lines - result_height - 1,
469+
col = col,
470+
height = 1,
471+
width = prompt_width,
472+
}),
473+
results = vim.tbl_deep_extend("force", results, {
474+
line = max_lines - result_height,
475+
col = col,
476+
height = result_height,
477+
width = result_width,
478+
}),
479+
}
480+
end
481+
450482
return layout_strategies

lua/telescope/pickers/window.lua

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local p_layouts = require('telescope.pickers.layout_strategies')
1+
local resolve = require("telescope.config.resolve")
22

33
local p_window = {}
44

55
function p_window.get_window_options(picker, max_columns, max_lines)
66
local layout_strategy = picker.layout_strategy
7-
local getter = p_layouts[layout_strategy]
7+
local getter = require('telescope.pickers.layout_strategies')[layout_strategy]
88

99
if not getter then
1010
error("Not a valid layout strategy: " .. layout_strategy)
@@ -13,5 +13,38 @@ function p_window.get_window_options(picker, max_columns, max_lines)
1313
return getter(picker, max_columns, max_lines)
1414
end
1515

16+
function p_window.get_initial_window_options(picker)
17+
local popup_border = resolve.win_option(picker.window.border)
18+
local popup_borderchars = resolve.win_option(picker.window.borderchars)
19+
20+
local preview = {
21+
title = picker.preview_title,
22+
border = popup_border.preview,
23+
borderchars = popup_borderchars.preview,
24+
enter = false,
25+
highlight = false
26+
}
27+
28+
local results = {
29+
title = picker.results_title,
30+
border = popup_border.results,
31+
borderchars = popup_borderchars.results,
32+
enter = false,
33+
}
34+
35+
local prompt = {
36+
title = picker.prompt_title,
37+
border = popup_border.prompt,
38+
borderchars = popup_borderchars.prompt,
39+
enter = true
40+
}
41+
42+
return {
43+
preview = preview,
44+
results = results,
45+
prompt = prompt,
46+
}
47+
end
48+
1649

1750
return p_window

lua/telescope/themes.lua

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@
22
-- Currently certain designs need a number of parameters.
33
--
44
-- local opts = themes.get_dropdown { winblend = 3 }
5-
--
5+
6+
---@tag telescope.themes
7+
8+
---@brief [[
9+
--- Themes are ways to combine several elements of styling together.
10+
---
11+
--- They are helpful for managing the several differnt UI aspects for telescope and provide
12+
--- a simple interface for users to get a particular "style" of picker.
13+
---@brief ]]
614

715
local themes = {}
816

17+
--- Dropdown style theme.
18+
--- <pre>
19+
---
20+
--- Usage:
21+
---
22+
--- `local builtin = require('telescope.builtin')`
23+
--- `local themes = require('telescope.themes')`
24+
--- `builtin.find_files(themes.get_dropdown())`
25+
--- </pre>
926
function themes.get_dropdown(opts)
1027
opts = opts or {}
1128

@@ -21,14 +38,49 @@ function themes.get_dropdown(opts)
2138
width = 80,
2239
results_height = 15,
2340
borderchars = {
24-
{ '', '', '', '', '', '', '', ''},
41+
{ "", "", "", "", "", "", "", ""},
2542
prompt = {"", "", " ", "", "", "", "", ""},
2643
results = {"", "", "", "", "", "", "", ""},
27-
preview = { '', '', '', '', '', '', '', ''},
44+
preview = { "", "", "", "", "", "", "", ""},
2845
},
2946
}
3047

3148
return vim.tbl_deep_extend("force", theme_opts, opts)
3249
end
3350

51+
--- Ivy style theme.
52+
--- <pre>
53+
---
54+
--- Usage:
55+
---
56+
--- `local builtin = require('telescope.builtin')`
57+
--- `local themes = require('telescope.themes')`
58+
--- `builtin.find_files(themes.get_ivy())`
59+
--- </pre>
60+
function themes.get_ivy(opts)
61+
opts = opts or {}
62+
63+
return vim.tbl_deep_extend("force", {
64+
theme = "ivy",
65+
66+
sorting_strategy = "ascending",
67+
68+
preview_title = "",
69+
70+
layout_strategy = "bottom_pane",
71+
layout_config = {
72+
height = 25,
73+
},
74+
75+
border = true,
76+
borderchars = {
77+
"z",
78+
prompt = { "", " ", " ", " ", "", "", " ", " " },
79+
results = { " " },
80+
-- results = { "a", "b", "c", "d", "e", "f", "g", "h" },
81+
preview = { "", "", "", "", "", "", "", ""},
82+
},
83+
}, opts)
84+
end
85+
3486
return themes

scratch/ivy.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
RELOAD('telescope')
3+
require('telescope.builtin').find_files(require('telescope.themes').get_ivy { previewer = false })
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+

scripts/gendocs.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ docs.test = function()
1515
"./lua/telescope/actions/state.lua",
1616
"./lua/telescope/actions/set.lua",
1717
"./lua/telescope/previewers/init.lua",
18+
"./lua/telescope/themes.lua",
1819
}
1920

2021
table.sort(input_files, function(a, b)

0 commit comments

Comments
 (0)