From 7d1a8292b6c7db7401af05f75e6d9992fc8ed28a Mon Sep 17 00:00:00 2001 From: haorenW1025 Date: Thu, 17 Sep 2020 00:20:45 +0800 Subject: [PATCH] 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 --- README.md | 3 +++ lua/telescope/actions.lua | 8 ++++++++ lua/telescope/pickers.lua | 6 ++++++ lua/telescope/previewers.lua | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7573484..77f717b 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,9 @@ j k next | previous (in normal mode) go to file selection as a vertical split go to a file in a new tab + scroll up in preview window + scroll down in preview window + close telescope close telescope (in normal mode) ``` 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 = { [""] = actions.goto_file_selection_split, [""] = actions.goto_file_selection_vsplit, [""] = actions.goto_file_selection_tabedit, + + [""] = actions.preview_scrolling_up, + [""] = actions.preview_scrolling_down, }, n = { @@ -57,6 +60,9 @@ local default_mappings = { [""] = actions.move_selection_next, [""] = actions.move_selection_previous, + + [""] = actions.preview_scrolling_up, + [""] = 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