Skip to content

Commit fd2c378

Browse files
committed
fix(ruby-lsp): Disable onTypeFormatting feature
The Ruby LSP has an issue with Zed and and the `onTypeFormatting` feature which adds odd pipes (`|`) when typing code. Until #38 is fixed upstream, we can explicitly disable this feature if it's not enabled.
1 parent f9a5294 commit fd2c378

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/language_servers/ruby_lsp.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,36 @@ impl RubyLsp {
8686
_ => None,
8787
}
8888
}
89+
90+
pub fn language_server_initialization_options(
91+
&self,
92+
language_server_id: &zed::LanguageServerId,
93+
worktree: &zed::Worktree,
94+
) -> zed::Result<Option<zed::serde_json::Value>> {
95+
let mut initialization_options =
96+
zed::settings::LspSettings::for_worktree(language_server_id.as_ref(), worktree)
97+
.ok()
98+
.and_then(|lsp_settings| lsp_settings.initialization_options.clone())
99+
.unwrap_or_else(|| zed::serde_json::json!({}));
100+
101+
let options_obj = initialization_options
102+
.as_object_mut()
103+
.expect("initialization_options must be an object");
104+
105+
let enabled_features = options_obj
106+
.entry("enabledFeatures")
107+
.or_insert_with(|| zed::serde_json::json!({}));
108+
109+
// Workaround ruby-lsp upstream issue
110+
// https://github.com/zed-extensions/ruby/issues/38
111+
if let Some(features_obj) = enabled_features.as_object_mut() {
112+
features_obj
113+
.entry("onTypeFormatting")
114+
.or_insert(zed::serde_json::Value::Bool(false));
115+
}
116+
117+
Ok(Some(initialization_options))
118+
}
89119
}
90120

91121
#[cfg(test)]

src/ruby.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,18 @@ impl zed::Extension for RubyExtension {
8282
language_server_id: &zed::LanguageServerId,
8383
worktree: &zed::Worktree,
8484
) -> zed::Result<Option<zed::serde_json::Value>> {
85-
let initialization_options =
86-
zed::settings::LspSettings::for_worktree(language_server_id.as_ref(), worktree)
87-
.ok()
88-
.and_then(|lsp_settings| lsp_settings.initialization_options.clone())
89-
.unwrap_or_default();
90-
91-
Ok(Some(zed::serde_json::json!(initialization_options)))
85+
match language_server_id.as_ref() {
86+
RubyLsp::SERVER_ID => {
87+
let ruby = self.ruby_lsp.get_or_insert_with(RubyLsp::new);
88+
ruby.language_server_initialization_options(language_server_id, worktree)
89+
}
90+
_ => Ok(Some(
91+
zed::settings::LspSettings::for_worktree(language_server_id.as_ref(), worktree)
92+
.ok()
93+
.and_then(|lsp_settings| lsp_settings.initialization_options.clone())
94+
.unwrap_or_default(),
95+
)),
96+
}
9297
}
9398

9499
fn label_for_completion(

0 commit comments

Comments
 (0)