Skip to content

Commit

Permalink
feat: use native biome binary without node (#31)
Browse files Browse the repository at this point in the history
* feat: use native biome binary without node

* chore: update formatter configuration in README

* fix: error on unsupported architecture
  • Loading branch information
luckydye authored Jul 24, 2024
1 parent f83ac71 commit be0e886
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,14 @@ Otherwise, it can be configured through the lsp settings:

### Formatting

**Formatting does not work through the extension yet.**

Instead, you can configure biome as an external formatter:
To use the language server as a formatter, specify biome as your formatter in the settings:

```jsonc
// settings.json
{
"formatter": {
"external": {
"command": "./node_modules/@biomejs/biome/bin/biome",
"arguments": ["format", "--write", "--stdin-file-path", "{buffer_path}"]
"language_server": {
"name": "biome"
}
}
}
Expand Down
40 changes: 27 additions & 13 deletions src/biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use zed_extension_api::{
LanguageServerId, Result,
};

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

const BIOME_CONFIG_PATHS: &[&str] = &["biome.json", "biome.jsonc"];
Expand Down Expand Up @@ -35,10 +35,25 @@ impl BiomeExtension {
|| !f["devDependencies"]["@biomejs/biome"].is_null()
});

let (platform, arch) = zed::current_platform();
let binary_path = format!(
"{SERVER_PATH}/cli-{platform}-{arch}/biome",
platform = match platform {
zed::Os::Mac => "darwin",
zed::Os::Linux => "linux",
zed::Os::Windows => "win32",
},
arch = match arch {
zed::Architecture::Aarch64 => "arm64",
zed::Architecture::X8664 => "x64",
_ => return Err(format!("unsupported architecture: {arch:?}")),
},
);

if server_package_exists {
let worktree_root_path = worktree.root_path();
let worktree_server_path = Path::new(worktree_root_path.as_str())
.join(SERVER_PATH)
.join(binary_path)
.to_string_lossy()
.to_string();

Expand All @@ -50,7 +65,7 @@ impl BiomeExtension {
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);

let fallback_server_path = SERVER_PATH.to_string();
let fallback_server_path = binary_path.to_string();
let fallback_server_exist = self.server_exists(fallback_server_path.as_str());
let version = zed::npm_package_latest_version(PACKAGE_NAME)?;

Expand Down Expand Up @@ -116,14 +131,7 @@ impl zed::Extension for BiomeExtension {
let path = self.server_script_path(language_server_id, worktree)?;
let settings = LspSettings::for_worktree(language_server_id.as_ref(), worktree)?;

let mut args = vec![
env::current_dir()
.unwrap()
.join(path)
.to_string_lossy()
.to_string(),
"lsp-proxy".to_string(),
];
let mut args = vec!["lsp-proxy".to_string()];

if let Some(settings) = settings.settings {
let config_path = self.config_path(worktree, &settings);
Expand All @@ -141,16 +149,22 @@ impl zed::Extension for BiomeExtension {
}
}

let bin = env::current_dir()
.unwrap()
.join(path)
.to_string_lossy()
.to_string();

if let Some(binary) = settings.binary {
return Ok(zed::Command {
command: binary.path.map_or(zed::node_binary_path()?, |path| path),
command: binary.path.map_or(bin, |path| path),
args: binary.arguments.map_or(args, |args| args),
env: Default::default(),
});
}

Ok(zed::Command {
command: zed::node_binary_path()?,
command: bin,
args,
env: Default::default(),
})
Expand Down

0 comments on commit be0e886

Please sign in to comment.