-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement code formatting using Biome (#3)
* 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
Showing
9 changed files
with
283 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.