From 2836ea2ac40bd54ec1b00ffcd5927cdb4b7724d3 Mon Sep 17 00:00:00 2001
From: Vitalii Solodilov <Mcdkr@yandex.ru>
Date: Thu, 27 Apr 2023 17:30:15 +0300
Subject: [PATCH] feat: add a config option to exclude declaration from LSP
 references (#6886)

* feat: added the config option to exclude declaration from reference query

Fixes: #5344

* fix: review

* fix: review
---
 book/src/configuration.md      | 1 +
 helix-lsp/src/client.rs        | 3 ++-
 helix-term/src/commands/lsp.rs | 8 +++++++-
 helix-view/src/editor.rs       | 3 +++
 4 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/book/src/configuration.md b/book/src/configuration.md
index 1fdbf005..253a0726 100644
--- a/book/src/configuration.md
+++ b/book/src/configuration.md
@@ -128,6 +128,7 @@ The following statusline elements can be configured:
 | `display-inlay-hints` | Display inlay hints[^2]                                     | `false` |
 | `display-signature-help-docs` | Display docs under signature help popup             | `true`  |
 | `snippets`      | Enables snippet completions. Requires a server restart (`:lsp-restart`) to take effect after `:config-reload`/`:set`. | `true`  |
+| `goto-reference-include-declaration` | Include declaration in the goto references popup. | `true`  |
 
 [^1]: By default, a progress spinner is shown in the statusline beside the file path.
 [^2]: You may also have to activate them in the LSP config for them to appear, not just in Helix.
diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs
index 93e822c4..89b714e2 100644
--- a/helix-lsp/src/client.rs
+++ b/helix-lsp/src/client.rs
@@ -1167,6 +1167,7 @@ impl Client {
         &self,
         text_document: lsp::TextDocumentIdentifier,
         position: lsp::Position,
+        include_declaration: bool,
         work_done_token: Option<lsp::ProgressToken>,
     ) -> Option<impl Future<Output = Result<Value>>> {
         let capabilities = self.capabilities.get().unwrap();
@@ -1183,7 +1184,7 @@ impl Client {
                 position,
             },
             context: lsp::ReferenceContext {
-                include_declaration: true,
+                include_declaration,
             },
             work_done_progress_params: lsp::WorkDoneProgressParams { work_done_token },
             partial_result_params: lsp::PartialResultParams {
diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs
index b5d1d337..0ad6fb7e 100644
--- a/helix-term/src/commands/lsp.rs
+++ b/helix-term/src/commands/lsp.rs
@@ -1078,13 +1078,19 @@ pub fn goto_implementation(cx: &mut Context) {
 }
 
 pub fn goto_reference(cx: &mut Context) {
+    let config = cx.editor.config();
     let (view, doc) = current!(cx.editor);
     let language_server = language_server!(cx.editor, doc);
     let offset_encoding = language_server.offset_encoding();
 
     let pos = doc.position(view.id, offset_encoding);
 
-    let future = match language_server.goto_reference(doc.identifier(), pos, None) {
+    let future = match language_server.goto_reference(
+        doc.identifier(),
+        pos,
+        config.lsp.goto_reference_include_declaration,
+        None,
+    ) {
         Some(future) => future,
         None => {
             cx.editor
diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs
index 52f86f2d..fd0abe91 100644
--- a/helix-view/src/editor.rs
+++ b/helix-view/src/editor.rs
@@ -354,6 +354,8 @@ pub struct LspConfig {
     pub display_inlay_hints: bool,
     /// Whether to enable snippet support
     pub snippets: bool,
+    /// Whether to include declaration in the goto reference query
+    pub goto_reference_include_declaration: bool,
 }
 
 impl Default for LspConfig {
@@ -365,6 +367,7 @@ impl Default for LspConfig {
             display_signature_help_docs: true,
             display_inlay_hints: false,
             snippets: true,
+            goto_reference_include_declaration: true,
         }
     }
 }