summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorhaorenW1025 <whz861025@gmail.com>2020-09-17 00:20:45 +0800
committerGitHub <noreply@github.com>2020-09-16 12:20:45 -0400
commit7d1a8292b6c7db7401af05f75e6d9992fc8ed28a (patch)
treed0d3a0c34b5ad8e70583c2a1c8c67d87abda89a5 /lua
parent9bdf5b3d4af99b76d3cd25466af708d57d6a1483 (diff)
feat: add scrolling in preview window (#47)
* feat: add scrolling in preview window refactor into two layers * remove redundant code * add error message for scroll_fn and send_input * consider count for preview scrolling
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/actions.lua8
-rw-r--r--lua/telescope/pickers.lua6
-rw-r--r--lua/telescope/previewers.lua36
3 files changed, 49 insertions, 1 deletions
diff --git a/lua/telescope/actions.lua b/lua/telescope/actions.lua
index 50c9fe2..224a3e4 100644
--- a/lua/telescope/actions.lua
+++ b/lua/telescope/actions.lua
@@ -34,6 +34,14 @@ function actions.get_selected_entry(prompt_bufnr)
return actions.get_current_picker(prompt_bufnr):get_selection()
end
+function actions.preview_scrolling_up(prompt_bufnr)
+ actions.get_current_picker(prompt_bufnr).previewer:scroll_fn(-30)
+end
+
+function actions.preview_scrolling_down(prompt_bufnr)
+ actions.get_current_picker(prompt_bufnr).previewer:scroll_fn(30)
+end
+
-- TODO: It seems sometimes we get bad styling.
local function goto_file_selection(prompt_bufnr, command)
local picker = actions.get_current_picker(prompt_bufnr)
diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua
index 66973f7..bf88b63 100644
--- a/lua/telescope/pickers.lua
+++ b/lua/telescope/pickers.lua
@@ -42,6 +42,9 @@ local default_mappings = {
["<C-x>"] = actions.goto_file_selection_split,
["<C-v>"] = actions.goto_file_selection_vsplit,
["<C-t>"] = actions.goto_file_selection_tabedit,
+
+ ["<C-u>"] = actions.preview_scrolling_up,
+ ["<C-d>"] = actions.preview_scrolling_down,
},
n = {
@@ -57,6 +60,9 @@ local default_mappings = {
["<Down>"] = actions.move_selection_next,
["<Up>"] = actions.move_selection_previous,
+
+ ["<C-u>"] = actions.preview_scrolling_up,
+ ["<C-d>"] = actions.preview_scrolling_down,
},
}
diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua
index 5798bac..462b65e 100644
--- a/lua/telescope/previewers.lua
+++ b/lua/telescope/previewers.lua
@@ -11,7 +11,7 @@ local Previewer = {}
Previewer.__index = Previewer
-- TODO: Should play with these some more, ty @clason
-local bat_options = " --style=plain --paging=never --color=always "
+local bat_options = " --style=plain --color=always "
local previewer_ns = vim.api.nvim_create_namespace('telescope.previewers')
@@ -28,6 +28,8 @@ function Previewer:new(opts)
state = nil,
_setup_func = opts.setup,
_teardown_func = opts.teardown,
+ _send_input = opts.send_input,
+ _scroll_fn = opts.scroll_fn,
preview_fn = opts.preview_fn,
}, Previewer)
end
@@ -55,6 +57,22 @@ function Previewer:teardown()
end
end
+function Previewer:send_input(input)
+ if self._send_input then
+ self:_send_input(input)
+ else
+ vim.api.nvim_err_writeln("send_input is not defined for this previewer")
+ end
+end
+
+function Previewer:scroll_fn(direction)
+ if self._scroll_fn then
+ self:_scroll_fn(direction)
+ else
+ vim.api.nvim_err_writeln("scroll_fn is not defined for this previewer")
+ end
+end
+
previewers.new = function(...)
return Previewer:new(...)
end
@@ -186,6 +204,22 @@ previewers.cat = defaulter(function(opts)
}
end,
+ send_input = function(self, input)
+ termcode = vim.api.nvim_replace_termcodes(input, true, false, true)
+ if self.state.termopen_id then
+ pcall(vim.fn.chansend, self.state.termopen_id, termcode)
+ end
+ end,
+
+ scroll_fn = function(self, direction)
+ if not self.state then
+ return
+ end
+ if direction > 0 then input = "d" else input = "u" end
+ local count = math.abs(direction)
+ self:send_input(count..input)
+ end,
+
teardown = function(self)
if not self.state then
return