From b5591291fd0f7dad8fea82046f4e1b87e702aff3 Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Thu, 30 Mar 2023 16:29:59 +0200 Subject: [PATCH] add option to hide snippets --- book/src/configuration.md | 1 + helix-lsp/src/lib.rs | 8 ++++++++ helix-term/src/ui/completion.rs | 16 +++++++++------- helix-term/src/ui/editor.rs | 1 + helix-view/src/editor.rs | 3 +++ 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/book/src/configuration.md b/book/src/configuration.md index 2af0e6326c880..04a2e7093ff5a 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -127,6 +127,7 @@ The following statusline elements can be configured: | `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` | | `display-inlay-hints` | Display inlay hints[^2] | `false` | | `display-signature-help-docs` | Display docs under signature help popup | `true` | +| `show-snippet-completions` | Whether to show snippets as completion options | `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/lib.rs b/helix-lsp/src/lib.rs index d56148a415609..db253581450ed 100644 --- a/helix-lsp/src/lib.rs +++ b/helix-lsp/src/lib.rs @@ -334,6 +334,14 @@ pub mod util { transaction.with_selection(selection) } + pub fn is_snippet(item: &lsp::CompletionItem) -> bool { + matches!(item.kind, Some(lsp::CompletionItemKind::SNIPPET)) + || matches!( + item.insert_text_format, + Some(lsp::InsertTextFormat::SNIPPET) + ) + } + /// Creates a [Transaction] from the [snippet::Snippet] in a completion response. /// The transaction applies the edit to all cursors. #[allow(clippy::too_many_arguments)] diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index da6b5ddcbc1fb..8b91a44936f53 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -15,7 +15,10 @@ use helix_view::{graphics::Rect, Document, Editor}; use crate::commands; use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent}; -use helix_lsp::{lsp, util}; +use helix_lsp::{ + lsp, + util::{self, is_snippet}, +}; use lsp::CompletionItem; impl menu::Item for CompletionItem { @@ -107,8 +110,12 @@ impl Completion { offset_encoding: helix_lsp::OffsetEncoding, start_offset: usize, trigger_offset: usize, + show_snippets: bool, ) -> Self { let replace_mode = editor.config().completion_replace; + if !show_snippets { + items.retain(|completion| !is_snippet(completion)) + } // Sort completion items according to their preselect status (given by the LSP server) items.sort_by_key(|item| !item.preselect.unwrap_or(false)); @@ -166,12 +173,7 @@ impl Completion { (None, new_text) }; - if matches!(item.kind, Some(lsp::CompletionItemKind::SNIPPET)) - || matches!( - item.insert_text_format, - Some(lsp::InsertTextFormat::SNIPPET) - ) - { + if is_snippet(item) { match snippet::parse(&new_text) { Ok(snippet) => util::generate_transaction_from_snippet( doc.text(), diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index d4b141a041af6..dc89473bcb149 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -960,6 +960,7 @@ impl EditorView { offset_encoding, start_offset, trigger_offset, + editor.config().lsp.show_snippet_completions, ); if completion.is_empty() { diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 727e1261d54c6..1f532c6dcb3ac 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -352,6 +352,8 @@ pub struct LspConfig { pub display_signature_help_docs: bool, /// Display inlay hints pub display_inlay_hints: bool, + /// Whether to enable snippet support + pub show_snippet_completions: bool, } impl Default for LspConfig { @@ -362,6 +364,7 @@ impl Default for LspConfig { auto_signature_help: true, display_signature_help_docs: true, display_inlay_hints: false, + show_snippet_completions: true, } } }