Skip to content

feat: handle workspace/configuration requests for config path in v2 #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/added_support_for_biome_v20.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Added support for Biome v2.0.
60 changes: 48 additions & 12 deletions src/biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use zed::settings::LspSettings;
use zed_extension_api::{
self as zed,
serde_json::{self, Value},
LanguageServerId, Result,
LanguageServerId, Result, Worktree,
};

const WORKTREE_SERVER_PATH: &str = "node_modules/@biomejs/biome/bin/biome";
Expand Down Expand Up @@ -115,6 +115,20 @@ impl BiomeExtension {

None
}

fn require_config_file(&self, settings: &Value) -> bool {
settings
.get("require_config_file")
.and_then(|value| value.as_bool())
.unwrap_or(false)
}

fn is_biome_v2(&self) -> bool {
zed::npm_package_installed_version(PACKAGE_NAME)
.ok()
.flatten()
.is_none_or(|version| version.starts_with("2."))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest checking v1 instead, so even future versions like V3, v4, etc. will be compatible.

It's more future proof

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it makes sense, fixed in 4054066

}
}

impl zed::Extension for BiomeExtension {
Expand All @@ -131,17 +145,14 @@ impl zed::Extension for BiomeExtension {

let mut args = vec!["lsp-proxy".to_string()];

// evaluate lsp settings
if let Some(settings) = settings.settings {
let require_config_file = settings
.get("require_config_file")
.and_then(|value| value.as_bool())
.unwrap_or(false);

if let Some(config_path) = self.config_path(worktree, &settings) {
args.append(&mut vec!["--config-path".to_string(), config_path.clone()]);
} else if require_config_file {
return Err("biome.json is not found but require_config_file is true".to_string());
// evaluate lsp settings for v1 compatibility
if !self.is_biome_v2() {
if let Some(settings) = settings.settings {
if let Some(config_path) = self.config_path(worktree, &settings) {
args.append(&mut vec!["--config-path".to_string(), config_path.clone()]);
} else if self.require_config_file(&settings) {
return Err("biome.json is not found but require_config_file is true".to_string());
}
}
}

Expand Down Expand Up @@ -185,6 +196,31 @@ impl zed::Extension for BiomeExtension {
env: Default::default(),
})
}

fn language_server_workspace_configuration(
&mut self,
language_server_id: &LanguageServerId,
worktree: &Worktree,
) -> Result<Option<Value>> {
let lsp_settings = LspSettings::for_worktree(language_server_id.as_ref(), worktree)?;

let Some(settings) = lsp_settings.settings else {
return Ok(Some(serde_json::json!({
"biome": {},
})));
};

let config_path = self
.config_path(worktree, &settings)
.map(|p| Path::new(&worktree.root_path()).join(p));

Ok(Some(serde_json::json!({
"biome": {
"requireConfiguration": self.require_config_file(&settings),
"configurationPath": config_path,
},
})))
}
}

zed::register_extension!(BiomeExtension);