rubocop.rs

 1use zed_extension_api::{self as zed, settings::LspSettings, LanguageServerId, Result};
 2
 3pub struct RubocopBinary {
 4    pub path: String,
 5    pub args: Option<Vec<String>>,
 6}
 7
 8pub struct Rubocop {}
 9
10impl Rubocop {
11    pub const LANGUAGE_SERVER_ID: &'static str = "rubocop";
12
13    pub fn new() -> Self {
14        Self {}
15    }
16
17    pub fn language_server_command(
18        &mut self,
19        language_server_id: &LanguageServerId,
20        worktree: &zed::Worktree,
21    ) -> Result<zed::Command> {
22        let binary = self.language_server_binary(language_server_id, worktree)?;
23
24        Ok(zed::Command {
25            command: binary.path,
26            args: binary.args.unwrap_or_else(|| vec!["--lsp".to_string()]),
27            env: worktree.shell_env(),
28        })
29    }
30
31    fn language_server_binary(
32        &self,
33        _language_server_id: &LanguageServerId,
34        worktree: &zed::Worktree,
35    ) -> Result<RubocopBinary> {
36        let binary_settings = LspSettings::for_worktree("rubocop", worktree)
37            .ok()
38            .and_then(|lsp_settings| lsp_settings.binary);
39        let binary_args = binary_settings
40            .as_ref()
41            .and_then(|binary_settings| binary_settings.arguments.clone());
42
43        if let Some(path) = binary_settings.and_then(|binary_settings| binary_settings.path) {
44            return Ok(RubocopBinary {
45                path,
46                args: binary_args,
47            });
48        }
49
50        if let Some(path) = worktree.which("rubocop") {
51            return Ok(RubocopBinary {
52                path,
53                args: binary_args,
54            });
55        }
56
57        Err("rubocop must be installed manually. Install it with `gem install rubocop` or specify the 'binary' path to it via local settings.".to_string())
58    }
59}