Skip to content

Commit

Permalink
Merge pull request #24 from WHForks/label-completions
Browse files Browse the repository at this point in the history
Add highlights to the completions
  • Loading branch information
tgodzik authored Oct 9, 2024
2 parents 1e2bf5c + 022d365 commit 1e33d7e
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use zed_extension_api::{self as zed, serde_json, settings::LspSettings, Result};
use zed_extension_api::{
self as zed,
lsp::{Completion, CompletionKind, Symbol, SymbolKind},
serde_json,
settings::LspSettings,
CodeLabel, CodeLabelSpan, Result,
};

struct ScalaExtension;

Expand Down Expand Up @@ -58,6 +64,61 @@ impl zed::Extension for ScalaExtension {
"metals": settings
})))
}

fn label_for_completion(
&self,
_language_server_id: &zed_extension_api::LanguageServerId,
completion: Completion,
) -> Option<zed_extension_api::CodeLabel> {
let prefix = match completion.kind? {
CompletionKind::Method | CompletionKind::Function => "def ",
CompletionKind::Constructor
| CompletionKind::Class
| CompletionKind::Interface
| CompletionKind::Module => "class ",
CompletionKind::Variable => "var ",
CompletionKind::Field
| CompletionKind::Constant
| CompletionKind::Value
| CompletionKind::Property => "val ",
CompletionKind::Enum => "enum ",
CompletionKind::Keyword => "",
_ => return None,
};
let name = completion.label;
let code = format!("{prefix}{name}");
let code_len = code.len();
Some(CodeLabel {
code,
spans: vec![CodeLabelSpan::code_range(prefix.len()..code_len)],
filter_range: (0..name.len()).into(),
})
}

fn label_for_symbol(
&self,
_language_server_id: &zed_extension_api::LanguageServerId,
symbol: Symbol,
) -> Option<CodeLabel> {
let prefix = match symbol.kind {
SymbolKind::Module
| SymbolKind::Class
| SymbolKind::Interface
| SymbolKind::Constructor => "class ",
SymbolKind::Method | SymbolKind::Function => "def ",
SymbolKind::Variable => "var ",
SymbolKind::Property | SymbolKind::Field | SymbolKind::Constant => "val ",
_ => "",
};
let name = symbol.name;
let code = format!("{prefix}{name}");
let code_len = code.len();
Some(CodeLabel {
code,
spans: vec![CodeLabelSpan::code_range(prefix.len()..code_len)],
filter_range: (0..name.len()).into(),
})
}
}

zed::register_extension!(ScalaExtension);

0 comments on commit 1e33d7e

Please sign in to comment.