From 762d49f60749eac75979202194ad0ee177977a74 Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Sat, 9 Apr 2022 13:17:57 +0200 Subject: [PATCH] feat(lsp_references): include current line option close: #1821 --- doc/telescope.txt | 16 ++++++++++------ lua/telescope/builtin/init.lua | 3 ++- lua/telescope/builtin/lsp.lua | 17 +++++++++++------ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/doc/telescope.txt b/doc/telescope.txt index c3189d0..e86e6ed 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1342,6 +1342,12 @@ builtin.lsp_references({opts}) *builtin.lsp_references()* Parameters: ~ {opts} (table) options to pass to the picker + Options: ~ + {include_declaration} (boolean) include symbol declaration in the + lsp references (default: true) + {include_current_line} (boolean) include current line (default: + false) + builtin.lsp_definitions({opts}) *builtin.lsp_definitions()* Goto the definition of the word under the cursor, if there's only one, @@ -1367,12 +1373,10 @@ builtin.lsp_type_definitions({opts}) *builtin.lsp_type_definitions()* {opts} (table) options to pass to the picker Options: ~ - {jump_type} (string) how to goto definition if there is - only one, values: "tab", "split", - "vsplit", "never" - {ignore_filename} (boolean) dont show filenames (default: true) - {include_declaration} (boolean) include symbol declaration in the lsp - references (default: true) + {jump_type} (string) how to goto definition if there is only + one, values: "tab", "split", "vsplit", + "never" + {ignore_filename} (boolean) dont show filenames (default: true) builtin.lsp_implementations({opts}) *builtin.lsp_implementations()* diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 95e293d..9c434dd 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -352,6 +352,8 @@ builtin.jumplist = require_on_exported_call("telescope.builtin.internal").jumpli --- Lists LSP references for word under the cursor, jumps to reference on `` ---@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) builtin.lsp_references = require_on_exported_call("telescope.builtin.lsp").references --- Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope @@ -365,7 +367,6 @@ builtin.lsp_definitions = require_on_exported_call("telescope.builtin.lsp").defi ---@param opts table: options to pass to the picker ---@field jump_type string: how to goto definition if there is only one, values: "tab", "split", "vsplit", "never" ---@field ignore_filename boolean: dont show filenames (default: true) ----@field include_declaration boolean: include symbol declaration in the lsp references (default: true) builtin.lsp_type_definitions = require("telescope.builtin.lsp").type_definitions --- Goto the implementation of the word under the cursor if there's only one, otherwise show all options in Telescope diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index fbb8484..76284e0 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -16,9 +16,10 @@ lsp.references = function(opts) local filepath = vim.api.nvim_buf_get_name(opts.bufnr) local lnum = vim.api.nvim_win_get_cursor(opts.winnr)[1] local params = vim.lsp.util.make_position_params(opts.winnr) + local include_current_line = vim.F.if_nil(opts.include_current_line, false) params.context = { includeDeclaration = vim.F.if_nil(opts.include_declaration, true) } - vim.lsp.buf_request(opts.bufnr, "textDocument/references", params, function(err, result, ctx, _config) + vim.lsp.buf_request(opts.bufnr, "textDocument/references", params, function(err, result, ctx, _) if err then vim.api.nvim_err_writeln("Error when finding references: " .. err.message) return @@ -27,10 +28,14 @@ lsp.references = function(opts) local locations = {} if result then local results = vim.lsp.util.locations_to_items(result, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding) - locations = vim.tbl_filter(function(v) - -- Remove current line from result - return not (v.filename == filepath and v.lnum == lnum) - end, results or {}) + if include_current_line then + locations = vim.tbl_filter(function(v) + -- Remove current line from result + return not (v.filename == filepath and v.lnum == lnum) + end, vim.F.if_nil(results, {})) + else + locations = vim.F.if_nil(results, {}) + end end if vim.tbl_isempty(locations) then @@ -54,7 +59,7 @@ local function list_or_jump(action, title, opts) opts = opts or {} local params = vim.lsp.util.make_position_params(opts.winnr) - vim.lsp.buf_request(opts.bufnr, action, params, function(err, result, ctx, _config) + vim.lsp.buf_request(opts.bufnr, action, params, function(err, result, ctx, _) if err then vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err.message) return