From cd62e60eba6b4dbb69955f128ecdaa9303c6fa3a Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 22 Aug 2022 11:43:26 +0200 Subject: [PATCH] Implement `ElixirLspAdapter::label_for_completion` --- crates/zed/src/languages/elixir.rs | 54 +++++++++++++++++++ .../zed/src/languages/elixir/highlights.scm | 4 +- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/crates/zed/src/languages/elixir.rs b/crates/zed/src/languages/elixir.rs index 32abbec4c6..c6c7c8d161 100644 --- a/crates/zed/src/languages/elixir.rs +++ b/crates/zed/src/languages/elixir.rs @@ -4,6 +4,7 @@ use async_trait::async_trait; use client::http::HttpClient; use futures::StreamExt; pub use language::*; +use lsp::CompletionItemKind; use smol::fs::{self, File}; use std::{any::Any, path::PathBuf, sync::Arc}; use util::ResultExt; @@ -108,4 +109,57 @@ impl LspAdapter for ElixirLspAdapter { .await .log_err() } + + async fn label_for_completion( + &self, + completion: &lsp::CompletionItem, + language: &Language, + ) -> Option { + match completion.kind.zip(completion.detail.as_ref()) { + Some((_, detail)) if detail.starts_with("(function)") => { + let text = detail.strip_prefix("(function) ")?; + let filter_range = 0..text.find('(').unwrap_or(text.len()); + let source = Rope::from(format!("def {text}").as_str()); + let runs = language.highlight_text(&source, 4..4 + text.len()); + return Some(CodeLabel { + text: text.to_string(), + runs, + filter_range, + }); + } + Some((_, detail)) if detail.starts_with("(macro)") => { + let text = detail.strip_prefix("(macro) ")?; + let filter_range = 0..text.find('(').unwrap_or(text.len()); + let source = Rope::from(format!("defmacro {text}").as_str()); + let runs = language.highlight_text(&source, 9..9 + text.len()); + return Some(CodeLabel { + text: text.to_string(), + runs, + filter_range, + }); + } + Some(( + CompletionItemKind::MODULE + | CompletionItemKind::INTERFACE + | CompletionItemKind::STRUCT, + _, + )) => { + let filter_range = 0..completion + .label + .find(" (") + .unwrap_or(completion.label.len()); + let text = &completion.label[filter_range.clone()]; + let source = Rope::from(format!("defmodule {text}").as_str()); + let runs = language.highlight_text(&source, 10..10 + text.len()); + return Some(CodeLabel { + text: completion.label.clone(), + runs, + filter_range, + }); + } + _ => {} + } + + None + } } diff --git a/crates/zed/src/languages/elixir/highlights.scm b/crates/zed/src/languages/elixir/highlights.scm index 9324990391..5c256f341c 100644 --- a/crates/zed/src/languages/elixir/highlights.scm +++ b/crates/zed/src/languages/elixir/highlights.scm @@ -43,11 +43,11 @@ (float) ] @number -(alias) @module +(alias) @type (call target: (dot - left: (atom) @module)) + left: (atom) @type)) (char) @constant