Skip to content

Commit ce26336

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 ce26336

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/language_servers/ruby_lsp.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ 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_default();
100+
101+
if !initialization_options.is_object() {
102+
initialization_options = zed::serde_json::json!({});
103+
}
104+
105+
let options_obj = initialization_options
106+
.as_object_mut()
107+
.expect("initialization_options must be an object");
108+
109+
let enabled_features = options_obj
110+
.entry("enabledFeatures")
111+
.or_insert_with(|| zed::serde_json::json!({}));
112+
113+
// Workaround ruby-lsp upstream issue
114+
// https://github.com/zed-extensions/ruby/issues/38
115+
if let Some(features_obj) = enabled_features.as_object_mut() {
116+
features_obj
117+
.entry("onTypeFormatting")
118+
.or_insert(zed::serde_json::Value::Bool(false));
119+
}
120+
121+
Ok(Some(initialization_options))
122+
}
89123
}
90124

91125
#[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)