integrations/editors/zed/src/lib.rs
1
use zed_extension_api::{
2
self as zed,
3
settings::{ContextServerSettings, LspSettings},
4
ContextServerId, LanguageServerId, Project, Result, Worktree,
5
};
7
const BINARY: &str = "koment";
9
struct KomentExtension {
10
found: Option<String>,
11
}
13
impl KomentExtension {
14
fn configured_for_language_server(
15
id: &LanguageServerId,
16
worktree: &Worktree,
17
) -> Option<String> {
18
LspSettings::for_worktree(id.as_ref(), worktree)
19
.ok()?
20
.binary?
21
.path
22
}
24
fn configured_for_context_server(id: &ContextServerId, project: &Project) -> Option<String> {
25
ContextServerSettings::for_project(id.as_ref(), project)
26
.ok()?
27
.command?
28
.path
29
}
30
}
32
fn nowhere_to_be_found() -> String {
33
format!(
34
"koment is not on $PATH. Install it, or set the {BINARY} binary path in your Zed settings \
35
under lsp.{BINARY}.binary.path. A Zed launched from the Finder or Dock does not inherit \
36
the $PATH your shell sets."
37
)
38
}
40
impl zed::Extension for KomentExtension {
41
fn new() -> Self {
42
Self { found: None }
43
}
45
fn language_server_command(
46
&mut self,
47
language_server_id: &LanguageServerId,
48
worktree: &Worktree,
49
) -> Result<zed::Command> {
50
let binary = match Self::configured_for_language_server(language_server_id, worktree) {
51
Some(configured) => configured,
52
None => worktree.which(BINARY).ok_or_else(nowhere_to_be_found)?,
53
};
54
self.found = Some(binary.clone());
56
Ok(zed::Command {
57
command: binary,
58
args: vec!["lsp".to_string()],
59
env: worktree.shell_env(),
60
})
61
}
63
fn context_server_command(
64
&mut self,
65
context_server_id: &ContextServerId,
66
project: &Project,
67
) -> Result<zed::Command> {
68
let binary = Self::configured_for_context_server(context_server_id, project)
69
.or_else(|| self.found.clone())
70
.unwrap_or_else(|| BINARY.to_string());
72
Ok(zed::Command {
73
command: binary,
74
args: vec!["mcp".to_string(), "--write".to_string()],
75
env: Vec::new(),
76
})
77
}
78
}
80
zed::register_extension!(KomentExtension);