Add label_for_symbol
to extension API (#10179)
This PR adds `label_for_symbol` to the extension API. As a motivating example, we implemented `label_for_symbol` for the Haskell extension. Release Notes: - N/A Co-authored-by: Max <max@zed.dev>
This commit is contained in:
parent
5d88d9c0d7
commit
4a325614f0
10 changed files with 238 additions and 43 deletions
|
@ -64,6 +64,15 @@ pub trait Extension: Send + Sync {
|
|||
) -> Option<CodeLabel> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns the label for the given symbol.
|
||||
fn label_for_symbol(
|
||||
&self,
|
||||
_language_server_id: &LanguageServerId,
|
||||
_symbol: Symbol,
|
||||
) -> Option<CodeLabel> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
|
@ -138,11 +147,33 @@ impl wit::Guest for Component {
|
|||
}
|
||||
Ok(labels)
|
||||
}
|
||||
|
||||
fn labels_for_symbols(
|
||||
language_server_id: String,
|
||||
symbols: Vec<Symbol>,
|
||||
) -> Result<Vec<Option<CodeLabel>>, String> {
|
||||
let language_server_id = LanguageServerId(language_server_id);
|
||||
let mut labels = Vec::new();
|
||||
for (ix, symbol) in symbols.into_iter().enumerate() {
|
||||
let label = extension().label_for_symbol(&language_server_id, symbol);
|
||||
if let Some(label) = label {
|
||||
labels.resize(ix + 1, None);
|
||||
*labels.last_mut().unwrap() = Some(label);
|
||||
}
|
||||
}
|
||||
Ok(labels)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
|
||||
pub struct LanguageServerId(String);
|
||||
|
||||
impl AsRef<str> for LanguageServerId {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LanguageServerId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
|
|
|
@ -3,7 +3,7 @@ package zed:extension;
|
|||
world extension {
|
||||
import lsp;
|
||||
|
||||
use lsp.{completion};
|
||||
use lsp.{completion, symbol};
|
||||
|
||||
export init-extension: func();
|
||||
|
||||
|
@ -117,4 +117,5 @@ world extension {
|
|||
}
|
||||
|
||||
export labels-for-completions: func(language-server-id: string, completions: list<completion>) -> result<list<option<code-label>>, string>;
|
||||
export labels-for-symbols: func(language-server-id: string, symbols: list<symbol>) -> result<list<option<code-label>>, string>;
|
||||
}
|
||||
|
|
|
@ -41,4 +41,39 @@ interface lsp {
|
|||
snippet,
|
||||
other(s32),
|
||||
}
|
||||
|
||||
record symbol {
|
||||
kind: symbol-kind,
|
||||
name: string,
|
||||
}
|
||||
|
||||
variant symbol-kind {
|
||||
file,
|
||||
module,
|
||||
namespace,
|
||||
%package,
|
||||
class,
|
||||
method,
|
||||
property,
|
||||
field,
|
||||
%constructor,
|
||||
%enum,
|
||||
%interface,
|
||||
function,
|
||||
variable,
|
||||
constant,
|
||||
%string,
|
||||
number,
|
||||
boolean,
|
||||
array,
|
||||
object,
|
||||
key,
|
||||
null,
|
||||
enum-member,
|
||||
struct,
|
||||
event,
|
||||
operator,
|
||||
type-parameter,
|
||||
other(s32),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue