protobuf_language_server.rs

 1use zed_extension_api::{self as zed, Result, settings::LspSettings};
 2
 3pub(crate) struct ProtobufLanguageServer {
 4    cached_binary_path: Option<String>,
 5}
 6
 7impl ProtobufLanguageServer {
 8    pub(crate) const SERVER_NAME: &str = "protobuf-language-server";
 9
10    pub(crate) fn new() -> Self {
11        ProtobufLanguageServer {
12            cached_binary_path: None,
13        }
14    }
15
16    pub(crate) fn language_server_binary(
17        &mut self,
18        worktree: &zed::Worktree,
19    ) -> Result<zed::Command> {
20        let binary_settings = LspSettings::for_worktree(Self::SERVER_NAME, worktree)
21            .ok()
22            .and_then(|lsp_settings| lsp_settings.binary);
23
24        let args = binary_settings
25            .as_ref()
26            .and_then(|binary_settings| binary_settings.arguments.clone())
27            .unwrap_or_else(|| vec!["-logs".into(), "".into()]);
28
29        if let Some(path) = binary_settings.and_then(|binary_settings| binary_settings.path) {
30            Ok(zed::Command {
31                command: path,
32                args,
33                env: Default::default(),
34            })
35        } else if let Some(path) = self.cached_binary_path.clone() {
36            Ok(zed::Command {
37                command: path,
38                args,
39                env: Default::default(),
40            })
41        } else if let Some(path) = worktree.which(Self::SERVER_NAME) {
42            self.cached_binary_path = Some(path.clone());
43            Ok(zed::Command {
44                command: path,
45                args,
46                env: Default::default(),
47            })
48        } else {
49            Err(format!("{} not found in PATH", Self::SERVER_NAME))
50        }
51    }
52}