Skip to content

Commit b13306e

Browse files
LuxedGithub Actionscbrunel
authored
feat: add cursor layout (#878)
* Add basic implementation of "cursor" layout strategy * Update cursor layout strategy to properly follow cursor * feat(cursor_layout): handle previewer * Refactor cursor layout code * Add cursor theme * Update readme * Improve cursor theme and layout documentation * [docgen] Update doc/telescope.txt skip-checks: true * Remove trailing whitespace * Fix issues related with neovim and plugin api changes * [docgen] Update doc/telescope.txt skip-checks: true * Allow preview width to be configured * [docgen] Update doc/telescope.txt skip-checks: true Co-authored-by: Github Actions <actions@github> Co-authored-by: cbrunel <[email protected]>
1 parent 7473962 commit b13306e

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ We have some built in themes but are looking for more cool options.
545545
| Themes | Description |
546546
|--------------------------|---------------------------------------------------------------------------------------------|
547547
| `themes.get_dropdown` | A list like centered list. [dropdown](https://i.imgur.com/SorAcXv.png) |
548+
| `themes.get_cursor` | A cursor relative list. |
548549
| `themes.get_ivy` | Bottom panel overlay. [Ivy #771](https://github.com/nvim-telescope/telescope.nvim/pull/771) |
549550

550551
To use a theme, simply append it to a built-in function:

doc/telescope.txt

+37
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ telescope.setup({opts}) *telescope.setup()*
9494
center = {
9595
preview_cutoff = 40
9696
},
97+
cursor = {
98+
preview_cutoff = 40
99+
},
97100
height = 0.9,
98101
horizontal = {
99102
preview_cutoff = 120,
@@ -912,6 +915,17 @@ themes.get_dropdown() *themes.get_dropdown()*
912915

913916

914917

918+
themes.get_cursor() *themes.get_cursor()*
919+
Cursor style theme.
920+
921+
Usage:
922+
923+
`local builtin = require('telescope.builtin')`
924+
`local themes = require('telescope.themes')`
925+
`builtin.lsp_code_actions(themes.get_cursor())`
926+
927+
928+
915929
themes.get_ivy() *themes.get_ivy()*
916930
Ivy style theme.
917931

@@ -1034,6 +1048,29 @@ layout_strategies.center() *layout_strategies.center()*
10341048
- preview_cutoff: When lines are less than this value, the preview will be disabled
10351049

10361050

1051+
layout_strategies.cursor() *layout_strategies.cursor()*
1052+
Cursor layout dynamically positioned below the cursor if possible. If there
1053+
is no place below the cursor it will be placed above.
1054+
1055+
┌──────────────────────────────────────────────────┐
1056+
│ │
1057+
│ █ │
1058+
│ ┌──────────────┐┌─────────────────────┐ │
1059+
│ │ Prompt ││ Preview │ │
1060+
│ ├──────────────┤│ Preview │ │
1061+
│ │ Result ││ Preview │ │
1062+
│ │ Result ││ Preview │ │
1063+
│ └──────────────┘└─────────────────────┘ │
1064+
│ █ │
1065+
│ │
1066+
│ │
1067+
│ │
1068+
│ │
1069+
│ │
1070+
└──────────────────────────────────────────────────┘
1071+
1072+
1073+
10371074
layout_strategies.vertical() *layout_strategies.vertical()*
10381075
Vertical layout stacks the items on top of each other. Particularly useful
10391076
with thinner windows.

lua/telescope/config.lua

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ local layout_config_defaults = {
8080
center = {
8181
preview_cutoff = 40,
8282
},
83+
84+
cursor = {
85+
preview_cutoff = 40,
86+
}
8387
}
8488

8589
local layout_config_description = string.format([[

lua/telescope/pickers/layout_strategies.lua

+93
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,99 @@ layout_strategies.center = make_documented_layout("center", vim.tbl_extend("erro
391391
}
392392
end)
393393

394+
--- Cursor layout dynamically positioned below the cursor if possible.
395+
--- If there is no place below the cursor it will be placed above.
396+
---
397+
--- <pre>
398+
--- ┌──────────────────────────────────────────────────┐
399+
--- │ │
400+
--- │ █ │
401+
--- │ ┌──────────────┐┌─────────────────────┐ │
402+
--- │ │ Prompt ││ Preview │ │
403+
--- │ ├──────────────┤│ Preview │ │
404+
--- │ │ Result ││ Preview │ │
405+
--- │ │ Result ││ Preview │ │
406+
--- │ └──────────────┘└─────────────────────┘ │
407+
--- │ █ │
408+
--- │ │
409+
--- │ │
410+
--- │ │
411+
--- │ │
412+
--- │ │
413+
--- └──────────────────────────────────────────────────┘
414+
--- </pre>
415+
layout_strategies.cursor = make_documented_layout("cursor", vim.tbl_extend("error", shared_options, {
416+
preview_width = { "Change the width of Telescope's preview window", "See |resolver.resolve_width()|", },
417+
preview_cutoff = "When columns are less than this value, the preview will be disabled",
418+
}), function(self, max_columns, max_lines, layout_config)
419+
local initial_options = p_window.get_initial_window_options(self)
420+
local preview = initial_options.preview
421+
local results = initial_options.results
422+
local prompt = initial_options.prompt
423+
424+
local height_opt = layout_config.height
425+
local height = resolve.resolve_height(height_opt)(self, max_columns, max_lines)
426+
427+
local width_opt = layout_config.width
428+
local width = resolve.resolve_width(width_opt)(self, max_columns, max_lines)
429+
430+
local max_width = (width > max_columns and max_columns or width)
431+
432+
local bs = get_border_size(self)
433+
434+
prompt.height = 1
435+
results.height = height
436+
preview.height = results.height + prompt.height + bs
437+
438+
if self.previewer and max_columns >= layout_config.preview_cutoff then
439+
preview.width = resolve.resolve_width(if_nil(layout_config.preview_width, function(_, cols)
440+
-- By default, previewer takes 2/3 of the layout
441+
return 2 * math.floor(max_width / 3)
442+
end))(self, max_width, max_lines)
443+
else
444+
preview.width = 0
445+
end
446+
447+
prompt.width = max_width - preview.width
448+
results.width = prompt.width
449+
450+
local total_height = preview.height + (bs*2)
451+
local total_width = prompt.width + (bs*2) + preview.width + bs
452+
453+
local position = vim.api.nvim_win_get_position(0)
454+
local top_left = {
455+
line = vim.fn.winline() + position[1] + bs,
456+
col = vim.fn.wincol() + position[2]
457+
}
458+
local bot_right = {
459+
line = top_left.line + total_height - 1,
460+
col = top_left.col + total_width - 1
461+
}
462+
463+
if bot_right.line > max_lines then
464+
-- position above current line
465+
top_left.line = top_left.line - total_height - 1
466+
end
467+
if bot_right.col >= max_columns then
468+
-- cap to the right of the screen
469+
top_left.col = max_columns - total_width
470+
end
471+
472+
prompt.line = top_left.line
473+
results.line = prompt.line + bs + 1
474+
preview.line = prompt.line
475+
476+
prompt.col = top_left.col
477+
results.col = prompt.col
478+
preview.col = results.col + (bs*2) + results.width
479+
480+
return {
481+
preview = self.previewer and preview.width > 0 and preview,
482+
results = results,
483+
prompt = prompt
484+
}
485+
end)
486+
394487
--- Vertical layout stacks the items on top of each other.
395488
--- Particularly useful with thinner windows.
396489
---

lua/telescope/themes.lua

+38
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@ function themes.get_dropdown(opts)
5858
return vim.tbl_deep_extend("force", theme_opts, opts)
5959
end
6060

61+
--- Cursor style theme.
62+
--- <pre>
63+
---
64+
--- Usage:
65+
---
66+
--- `local builtin = require('telescope.builtin')`
67+
--- `local themes = require('telescope.themes')`
68+
--- `builtin.lsp_code_actions(themes.get_cursor())`
69+
--- </pre>
70+
function themes.get_cursor(opts)
71+
opts = opts or {}
72+
73+
local theme_opts = {
74+
theme = 'cursor',
75+
76+
sorting_strategy = 'ascending',
77+
results_title = false,
78+
layout_strategy = 'cursor',
79+
layout_config = {
80+
width = function(_, _, _)
81+
return 80
82+
end,
83+
84+
height = function(_, _, _)
85+
return 6
86+
end,
87+
},
88+
borderchars = {
89+
{ '', '', '', '', '', '', '', ''},
90+
prompt = {'', '', ' ', '', '', '', '', ''},
91+
results = {'', '', '', '', '', '', '', ''},
92+
preview = { '', '', '', '', '', '', '', ''},
93+
},
94+
}
95+
96+
return vim.tbl_deep_extend('force', theme_opts, opts)
97+
end
98+
6199
--- Ivy style theme.
62100
--- <pre>
63101
---

0 commit comments

Comments
 (0)