add option to enable/disable lsp snippets

This commit is contained in:
Pascal Kuthe 2023-03-31 04:26:20 +02:00 committed by Blaž Hrastnik
parent a48d1a4abc
commit 9fe3adcff9
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640
5 changed files with 35 additions and 12 deletions

View File

@ -127,6 +127,7 @@ ### `[editor.lsp]` Section
| `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` | | `auto-signature-help` | Enable automatic popup of signature help (parameter hints) | `true` |
| `display-inlay-hints` | Display inlay hints[^2] | `false` | | `display-inlay-hints` | Display inlay hints[^2] | `false` |
| `display-signature-help-docs` | Display docs under signature help popup | `true` | | `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` |
[^1]: By default, a progress spinner is shown in the statusline beside the file path. [^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. [^2]: You may also have to activate them in the LSP config for them to appear, not just in Helix.

View File

@ -411,7 +411,7 @@ pub fn reply(
// General messages // General messages
// ------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------
pub(crate) async fn initialize(&self) -> Result<lsp::InitializeResult> { pub(crate) async fn initialize(&self, enable_snippets: bool) -> Result<lsp::InitializeResult> {
if let Some(config) = &self.config { if let Some(config) = &self.config {
log::info!("Using custom LSP config: {}", config); log::info!("Using custom LSP config: {}", config);
} }
@ -459,7 +459,7 @@ pub(crate) async fn initialize(&self) -> Result<lsp::InitializeResult> {
text_document: Some(lsp::TextDocumentClientCapabilities { text_document: Some(lsp::TextDocumentClientCapabilities {
completion: Some(lsp::CompletionClientCapabilities { completion: Some(lsp::CompletionClientCapabilities {
completion_item: Some(lsp::CompletionItemCapability { completion_item: Some(lsp::CompletionItemCapability {
snippet_support: Some(true), snippet_support: Some(enable_snippets),
resolve_support: Some(lsp::CompletionItemCapabilityResolveSupport { resolve_support: Some(lsp::CompletionItemCapabilityResolveSupport {
properties: vec![ properties: vec![
String::from("documentation"), String::from("documentation"),

View File

@ -647,6 +647,7 @@ pub fn restart(
language_config: &LanguageConfiguration, language_config: &LanguageConfiguration,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool,
) -> Result<Option<Arc<Client>>> { ) -> Result<Option<Arc<Client>>> {
let config = match &language_config.language_server { let config = match &language_config.language_server {
Some(config) => config, Some(config) => config,
@ -661,8 +662,14 @@ pub fn restart(
// initialize a new client // initialize a new client
let id = self.counter.fetch_add(1, Ordering::Relaxed); let id = self.counter.fetch_add(1, Ordering::Relaxed);
let NewClientResult(client, incoming) = let NewClientResult(client, incoming) = start_client(
start_client(id, language_config, config, doc_path, root_dirs)?; id,
language_config,
config,
doc_path,
root_dirs,
enable_snippets,
)?;
self.incoming.push(UnboundedReceiverStream::new(incoming)); self.incoming.push(UnboundedReceiverStream::new(incoming));
let old_clients = entry.insert(vec![(id, client.clone())]); let old_clients = entry.insert(vec![(id, client.clone())]);
@ -695,6 +702,7 @@ pub fn get(
language_config: &LanguageConfiguration, language_config: &LanguageConfiguration,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool,
) -> Result<Option<Arc<Client>>> { ) -> Result<Option<Arc<Client>>> {
let config = match &language_config.language_server { let config = match &language_config.language_server {
Some(config) => config, Some(config) => config,
@ -711,8 +719,14 @@ pub fn get(
// initialize a new client // initialize a new client
let id = self.counter.fetch_add(1, Ordering::Relaxed); let id = self.counter.fetch_add(1, Ordering::Relaxed);
let NewClientResult(client, incoming) = let NewClientResult(client, incoming) = start_client(
start_client(id, language_config, config, doc_path, root_dirs)?; id,
language_config,
config,
doc_path,
root_dirs,
enable_snippets,
)?;
clients.push((id, client.clone())); clients.push((id, client.clone()));
self.incoming.push(UnboundedReceiverStream::new(incoming)); self.incoming.push(UnboundedReceiverStream::new(incoming));
Ok(Some(client)) Ok(Some(client))
@ -811,6 +825,7 @@ fn start_client(
ls_config: &LanguageServerConfiguration, ls_config: &LanguageServerConfiguration,
doc_path: Option<&std::path::PathBuf>, doc_path: Option<&std::path::PathBuf>,
root_dirs: &[PathBuf], root_dirs: &[PathBuf],
enable_snippets: bool,
) -> Result<NewClientResult> { ) -> Result<NewClientResult> {
let (client, incoming, initialize_notify) = Client::start( let (client, incoming, initialize_notify) = Client::start(
&ls_config.command, &ls_config.command,
@ -834,7 +849,7 @@ fn start_client(
.capabilities .capabilities
.get_or_try_init(|| { .get_or_try_init(|| {
_client _client
.initialize() .initialize(enable_snippets)
.map_ok(|response| response.capabilities) .map_ok(|response| response.capabilities)
}) })
.await; .await;

View File

@ -1378,9 +1378,12 @@ fn lsp_restart(
.context("LSP not defined for the current document")?; .context("LSP not defined for the current document")?;
let scope = config.scope.clone(); let scope = config.scope.clone();
cx.editor cx.editor.language_servers.restart(
.language_servers config,
.restart(config, doc.path(), &editor_config.workspace_lsp_roots)?; doc.path(),
&editor_config.workspace_lsp_roots,
editor_config.lsp.snippets,
)?;
// This collect is needed because refresh_language_server would need to re-borrow editor. // This collect is needed because refresh_language_server would need to re-borrow editor.
let document_ids_to_refresh: Vec<DocumentId> = cx let document_ids_to_refresh: Vec<DocumentId> = cx

View File

@ -352,6 +352,8 @@ pub struct LspConfig {
pub display_signature_help_docs: bool, pub display_signature_help_docs: bool,
/// Display inlay hints /// Display inlay hints
pub display_inlay_hints: bool, pub display_inlay_hints: bool,
/// Whether to enable snippet support
pub snippets: bool,
} }
impl Default for LspConfig { impl Default for LspConfig {
@ -362,6 +364,7 @@ fn default() -> Self {
auto_signature_help: true, auto_signature_help: true,
display_signature_help_docs: true, display_signature_help_docs: true,
display_inlay_hints: false, display_inlay_hints: false,
snippets: true,
} }
} }
} }
@ -1092,12 +1095,13 @@ fn launch_language_server(&mut self, doc_id: DocumentId) -> Option<()> {
// if doc doesn't have a URL it's a scratch buffer, ignore it // if doc doesn't have a URL it's a scratch buffer, ignore it
let doc = self.document(doc_id)?; let doc = self.document(doc_id)?;
let (lang, path) = (doc.language.clone(), doc.path().cloned()); let (lang, path) = (doc.language.clone(), doc.path().cloned());
let root_dirs = &doc.config.load().workspace_lsp_roots; let config = doc.config.load();
let root_dirs = &config.workspace_lsp_roots;
// try to find a language server based on the language name // try to find a language server based on the language name
let language_server = lang.as_ref().and_then(|language| { let language_server = lang.as_ref().and_then(|language| {
self.language_servers self.language_servers
.get(language, path.as_ref(), root_dirs) .get(language, path.as_ref(), root_dirs, config.lsp.snippets)
.map_err(|e| { .map_err(|e| {
log::error!( log::error!(
"Failed to initialize the LSP for `{}` {{ {} }}", "Failed to initialize the LSP for `{}` {{ {} }}",