From cc77713294892c5bbce26adee665114250624e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gutyina=20Gerg=C5=91?= Date: Sun, 6 Nov 2022 17:43:01 +0100 Subject: [PATCH] feat: add jump_type option for lsp_references (#2218) --- doc/telescope.txt | 3 +++ lua/telescope/builtin/__lsp.lua | 14 ++++++++++++++ lua/telescope/builtin/init.lua | 1 + 3 files changed, 18 insertions(+) diff --git a/doc/telescope.txt b/doc/telescope.txt index 23f87bf..42e1006 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1420,6 +1420,9 @@ builtin.lsp_references({opts}) *telescope.builtin.lsp_references()* lsp references (default: true) {include_current_line} (boolean) include current line (default: false) + {jump_type} (string) how to goto reference if there is + only one, values: "tab", "split", + "vsplit", "never" {fname_width} (number) defines the width of the filename section (default: 30) {show_line} (boolean) show results text (default: true) diff --git a/lua/telescope/builtin/__lsp.lua b/lua/telescope/builtin/__lsp.lua index 21aa8ed..ab2cd7d 100644 --- a/lua/telescope/builtin/__lsp.lua +++ b/lua/telescope/builtin/__lsp.lua @@ -39,6 +39,20 @@ lsp.references = function(opts) return end + if #locations == 1 and opts.jump_type ~= "never" then + if opts.jump_type == "tab" then + vim.cmd "tabedit" + elseif opts.jump_type == "split" then + vim.cmd "new" + elseif opts.jump_type == "vsplit" then + vim.cmd "vnew" + end + -- jump to location + vim.api.nvim_win_set_buf(0, opts.bufnr) + vim.api.nvim_win_set_cursor(0, { locations[1].lnum, locations[1].col - 1 }) + return + end + pickers .new(opts, { prompt_title = "LSP References", diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 4736412..b912c80 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -372,6 +372,7 @@ builtin.jumplist = require_on_exported_call("telescope.builtin.__internal").jump ---@param opts table: options to pass to the picker ---@field include_declaration boolean: include symbol declaration in the lsp references (default: true) ---@field include_current_line boolean: include current line (default: false) +---@field jump_type string: how to goto reference if there is only one, values: "tab", "split", "vsplit", "never" ---@field fname_width number: defines the width of the filename section (default: 30) ---@field show_line boolean: show results text (default: true) ---@field trim_text boolean: trim results text (default: false)