Skip to content

Commit

Permalink
Implement code formatting using Biome (#3)
Browse files Browse the repository at this point in the history
* Update runtime package with new `format` module

* Implement initial `brioche fmt` command

* Add `Project.local_module_paths()` method

* Update `brioche fmt` to take a project path instead of a list of files

* Upgrade Biome dependencies

* Remove unused code in `script::format`

* Switch to Biome instead of Prettier for formatting

* Remove `format` module from `runtime` package

* Update LSP to support code formatting

* Update runtime package distribution

* Update `package-lock.json` in runtime package
  • Loading branch information
kylewlacy authored Jan 15, 2024
1 parent 7dbb728 commit 5a86b43
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 34 deletions.
153 changes: 125 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions crates/brioche/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ anyhow = { version = "1.0.75", features = ["backtrace"] }
async-compression = { version = "0.4.5", features = ["tokio", "gzip", "xz", "zstd"] }
async-recursion = "1.0.5"
bincode = "1.3.3"
biome_js_parser = "0.3.1"
biome_js_syntax = "0.3.1"
biome_rowan = "0.3.1"
biome_formatter = "0.4.0"
biome_js_formatter = "0.4.0"
biome_js_parser = "0.4.0"
biome_js_syntax = "0.4.0"
biome_rowan = "0.4.0"
blake3 = "1.5.0"
brioche-pack = { path = "../brioche-pack" }
bstr = { version = "1.8.0", features = ["serde"] }
Expand Down
14 changes: 14 additions & 0 deletions crates/brioche/src/brioche/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub async fn resolve_project_depth(

Ok(Project {
local_path: path.to_owned(),
analysis: project_analysis,
dependencies,
})
}
Expand Down Expand Up @@ -112,9 +113,22 @@ pub async fn find_project_root(path: &Path) -> anyhow::Result<&Path> {
#[derive(Debug, Clone)]
pub struct Project {
pub local_path: PathBuf,
pub analysis: analyze::ProjectAnalysis,
pub dependencies: HashMap<String, Project>,
}

impl Project {
pub fn local_module_paths(&self) -> impl Iterator<Item = &Path> + '_ {
self.analysis
.local_modules
.keys()
.filter_map(|module| match module {
super::script::specifier::BriocheModuleSpecifier::Runtime { .. } => None,
super::script::specifier::BriocheModuleSpecifier::File { path } => Some(&**path),
})
}
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ProjectDefinition {
#[serde(default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/brioche/src/brioche/project/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use biome_rowan::{AstNode as _, AstNodeList as _, AstSeparatedList as _};

use crate::brioche::script::specifier::{BriocheImportSpecifier, BriocheModuleSpecifier};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ProjectAnalysis {
pub definition: super::ProjectDefinition,
pub local_modules: HashMap<BriocheModuleSpecifier, ModuleAnalysis>,
Expand Down
1 change: 1 addition & 0 deletions crates/brioche/src/brioche/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::{
pub mod check;
mod compiler_host;
pub mod evaluate;
pub mod format;
mod js;
pub mod lsp;
pub mod specifier;
Expand Down
4 changes: 2 additions & 2 deletions crates/brioche/src/brioche/script/compiler_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ impl BriocheCompilerHost {

#[derive(Debug, Clone)]
pub struct BriocheDocument {
contents: Arc<String>,
version: u64,
pub contents: Arc<String>,
pub version: u64,
}

deno_core::extension!(brioche_compiler_host,
Expand Down
33 changes: 33 additions & 0 deletions crates/brioche/src/brioche/script/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::brioche::project::Project;

#[tracing::instrument(skip(project), err)]
pub async fn format(project: &Project) -> anyhow::Result<()> {
for path in project.local_module_paths() {
let contents = tokio::fs::read_to_string(path).await?;

let formatted_contents = format_code(&contents)?;

tokio::fs::write(path, &formatted_contents).await?;
}

Ok(())
}

pub fn format_code(contents: &str) -> anyhow::Result<String> {
let file_source =
biome_js_syntax::JsFileSource::ts().with_module_kind(biome_js_syntax::ModuleKind::Module);
let parsed = biome_js_parser::parse(
contents,
file_source,
biome_js_parser::JsParserOptions::default(),
);

let formatted = biome_js_formatter::format_node(
biome_js_formatter::context::JsFormatOptions::new(file_source)
.with_indent_style(biome_formatter::IndentStyle::Space),
&parsed.syntax(),
)?;

let formatted_code = formatted.print()?.into_code();
Ok(formatted_code)
}
Loading

0 comments on commit 5a86b43

Please sign in to comment.