Skip to content
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

feat: add setting require_config_file #29

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ Instead, you can configure biome as an external formatter:
}
```

### Enable biome only when biome.json is present

```jsonc
// settings.json
{
"lsp": {
"biome": {
"settings": {
"require_config_file": true
}
}
}
}
```

### Project based configuration

If you'd like to exclude biome from running in every project,
Expand Down Expand Up @@ -107,4 +122,3 @@ The same can be configured on a per-lanugage bassis with the [`languages`](https
}
}
```

42 changes: 38 additions & 4 deletions src/biome.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::{env, fs, path::Path};
use zed::settings::LspSettings;
use zed_extension_api::{self as zed, serde_json, LanguageServerId, Result};
use zed_extension_api::{
self as zed,
serde_json::{self, Value},
LanguageServerId, Result,
};

const SERVER_PATH: &str = "node_modules/@biomejs/biome/bin/biome";
const PACKAGE_NAME: &str = "@biomejs/biome";

const BIOME_CONFIG_PATHS: &[&str] = &["biome.json", "biome.jsonc"];

struct BiomeExtension;

impl BiomeExtension {
Expand Down Expand Up @@ -74,6 +80,27 @@ impl BiomeExtension {

Ok(fallback_server_path.to_string())
}

// Returns the path if a config file exists
pub fn config_path(&self, worktree: &zed::Worktree, settings: &Value) -> Option<String> {
let config_path_setting = settings.get("config_path").and_then(|value| value.as_str());

if let Some(config_path) = config_path_setting {
if worktree.read_text_file(config_path).is_ok() {
return Some(config_path.to_string());
} else {
return None;
}
}

for config_path in BIOME_CONFIG_PATHS {
if worktree.read_text_file(config_path).is_ok() {
return Some(config_path.to_string());
}
}

None
}
}

impl zed::Extension for BiomeExtension {
Expand All @@ -99,11 +126,18 @@ impl zed::Extension for BiomeExtension {
];

if let Some(settings) = settings.settings {
let config_path = settings.get("config_path").and_then(|value| value.as_str());
let config_path = self.config_path(worktree, &settings);

let require_config_file = settings
.get("require_config_file")
.and_then(|value| value.as_bool())
.unwrap_or(false);

if let Some(path) = config_path {
if let Some(config_path) = config_path {
args.push("--config-path".to_string());
args.push(path.to_string());
args.push(config_path.clone());
} else if require_config_file {
return Err("biome.json is not found but require_config_file is true".to_string());
}
}

Expand Down