From eab141c8e26563d3ab887feb0a5f5890954e41cb Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Mon, 16 Sep 2024 08:46:50 -0500 Subject: [PATCH 01/21] Add file handler for grit --- Cargo.lock | 12 ++++ crates/biome_grit_formatter/Cargo.toml | 15 ++++- crates/biome_grit_formatter/tests/language.rs | 27 ++++++++ .../biome_grit_formatter/tests/spec_test.rs | 21 ++++++ .../biome_grit_formatter/tests/spec_tests.rs | 8 +++ crates/biome_service/Cargo.toml | 67 ++++++++++--------- .../biome_service/src/file_handlers/grit.rs | 37 ++++++++++ crates/biome_service/src/file_handlers/mod.rs | 1 + 8 files changed, 153 insertions(+), 35 deletions(-) create mode 100644 crates/biome_grit_formatter/tests/language.rs create mode 100644 crates/biome_grit_formatter/tests/spec_test.rs create mode 100644 crates/biome_grit_formatter/tests/spec_tests.rs create mode 100644 crates/biome_service/src/file_handlers/grit.rs diff --git a/Cargo.lock b/Cargo.lock index 47541f804ab7..b02f207cbb24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -592,10 +592,20 @@ name = "biome_grit_formatter" version = "0.0.0" dependencies = [ "biome_formatter", + "biome_formatter_test", + "biome_grit_factory", + "biome_grit_parser", "biome_grit_syntax", + "biome_parser", "biome_rowan", + "biome_service", + "countme", + "iai", + "quickcheck", + "quickcheck_macros", "serde", "serde_json", + "tests_macros", ] [[package]] @@ -1045,7 +1055,9 @@ dependencies = [ "biome_graphql_formatter", "biome_graphql_parser", "biome_graphql_syntax", + "biome_grit_formatter", "biome_grit_patterns", + "biome_grit_syntax", "biome_html_formatter", "biome_html_parser", "biome_html_syntax", diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index 6d79810951d0..e75a5e99f567 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -18,9 +18,18 @@ biome_grit_syntax = { workspace = true } biome_rowan = { workspace = true } [dev-dependencies] -serde = { workspace = true, features = ["derive"] } -serde_json = { workspace = true } - +biome_formatter_test = { path = "../biome_formatter_test" } +biome_grit_factory = { path = "../biome_grit_factory" } +biome_grit_parser = { path = "../biome_grit_parser" } +biome_parser = { path = "../biome_parser" } +biome_service = { path = "../biome_service" } +countme = { workspace = true, features = ["enable"] } +iai = "0.1.1" +quickcheck = { workspace = true } +quickcheck_macros = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +tests_macros = { path = "../tests_macros" } # cargo-workspaces metadata [package.metadata.workspaces] independent = true diff --git a/crates/biome_grit_formatter/tests/language.rs b/crates/biome_grit_formatter/tests/language.rs new file mode 100644 index 000000000000..1e8caa565b28 --- /dev/null +++ b/crates/biome_grit_formatter/tests/language.rs @@ -0,0 +1,27 @@ +use biome_formatter_test::TestFormatLanguage; +use biome_grit_formatter::{context::GritFormatContext, GritFormatLanguage}; +use biome_grit_syntax::GritLanguage; + +#[derive(Default)] + +pub struct GritTestFormatLanguage; + +impl TestFormatLanguage for GritTestFormatLanguage { + type ServiceLanguage = GritLanguage; + + type Context = GritFormatContext; + + type FormatLanguage = GritFormatLanguage; + + fn parse(&self, text: &str) -> biome_parser::AnyParse { + todo!() + } + + fn to_format_language( + &self, + settings: &biome_service::settings::Settings, + file_source: &biome_service::workspace::DocumentFileSource, + ) -> Self::FormatLanguage { + todo!() + } +} diff --git a/crates/biome_grit_formatter/tests/spec_test.rs b/crates/biome_grit_formatter/tests/spec_test.rs new file mode 100644 index 000000000000..a63046226f8d --- /dev/null +++ b/crates/biome_grit_formatter/tests/spec_test.rs @@ -0,0 +1,21 @@ +use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile}; +use std::path::Path; + +mod language { + include!("language.rs"); +} + +pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _file_type: &str) { + let root_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/specs/")); + + let Some(test_file) = SpecTestFile::try_from_file(spec_input_file, root_path, Some(settings)) + else { + return; + }; + + let language = language::GritTestFormatLanguage::default(); + + let snapshot = SpecSnapshot::new(test_file, test_directory, language, ()); + + snapshot.test() +} diff --git a/crates/biome_grit_formatter/tests/spec_tests.rs b/crates/biome_grit_formatter/tests/spec_tests.rs new file mode 100644 index 000000000000..f68ad1f252f7 --- /dev/null +++ b/crates/biome_grit_formatter/tests/spec_tests.rs @@ -0,0 +1,8 @@ +mod spec_test; + +mod formatter { + + mod grit_module { + tests_macros::gen_tests! {"tests/specs/grit/**/*.grit", crate::spec_test::run, ""} + } +} diff --git a/crates/biome_service/Cargo.toml b/crates/biome_service/Cargo.toml index fb3bb96f8035..c900d3ffa32f 100644 --- a/crates/biome_service/Cargo.toml +++ b/crates/biome_service/Cargo.toml @@ -31,39 +31,42 @@ biome_graphql_analyze = { workspace = true } biome_graphql_formatter = { workspace = true } biome_graphql_parser = { workspace = true } biome_graphql_syntax = { workspace = true } +biome_grit_formatter = { workspace = true } biome_grit_patterns = { workspace = true } -biome_html_formatter = { workspace = true } -biome_html_parser = { workspace = true } -biome_html_syntax = { workspace = true } -biome_js_analyze = { workspace = true } -biome_js_factory = { workspace = true, optional = true } -biome_js_formatter = { workspace = true, features = ["serde"] } -biome_js_parser = { workspace = true } -biome_js_semantic = { workspace = true } -biome_js_syntax = { workspace = true, features = ["schema"] } -biome_json_analyze = { workspace = true } -biome_json_formatter = { workspace = true, features = ["serde"] } -biome_json_parser = { workspace = true } -biome_json_syntax = { workspace = true } -biome_parser = { workspace = true } -biome_project = { workspace = true } -biome_rowan = { workspace = true, features = ["serde"] } -biome_string_case = { workspace = true } -biome_text_edit = { workspace = true } -bpaf = { workspace = true } -dashmap = { workspace = true } -enumflags2 = { workspace = true, features = ["serde"] } -getrandom = { workspace = true, features = ["js"] } -ignore = { workspace = true } -indexmap = { workspace = true, features = ["serde"] } -oxc_resolver = { workspace = true } -regex = { workspace = true } -rustc-hash = { workspace = true } -schemars = { workspace = true, features = ["indexmap1"], optional = true } -serde = { workspace = true, features = ["derive"] } -serde_json = { workspace = true, features = ["raw_value"] } -slotmap = { workspace = true, features = ["serde"] } -tracing = { workspace = true, features = ["attributes", "log"] } +biome_grit_syntax = { workspace = true } + +biome_html_formatter = { workspace = true } +biome_html_parser = { workspace = true } +biome_html_syntax = { workspace = true } +biome_js_analyze = { workspace = true } +biome_js_factory = { workspace = true, optional = true } +biome_js_formatter = { workspace = true, features = ["serde"] } +biome_js_parser = { workspace = true } +biome_js_semantic = { workspace = true } +biome_js_syntax = { workspace = true, features = ["schema"] } +biome_json_analyze = { workspace = true } +biome_json_formatter = { workspace = true, features = ["serde"] } +biome_json_parser = { workspace = true } +biome_json_syntax = { workspace = true } +biome_parser = { workspace = true } +biome_project = { workspace = true } +biome_rowan = { workspace = true, features = ["serde"] } +biome_string_case = { workspace = true } +biome_text_edit = { workspace = true } +bpaf = { workspace = true } +dashmap = { workspace = true } +enumflags2 = { workspace = true, features = ["serde"] } +getrandom = { workspace = true, features = ["js"] } +ignore = { workspace = true } +indexmap = { workspace = true, features = ["serde"] } +oxc_resolver = { workspace = true } +regex = { workspace = true } +rustc-hash = { workspace = true } +schemars = { workspace = true, features = ["indexmap1"], optional = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true, features = ["raw_value"] } +slotmap = { workspace = true, features = ["serde"] } +tracing = { workspace = true, features = ["attributes", "log"] } [features] schema = [ diff --git a/crates/biome_service/src/file_handlers/grit.rs b/crates/biome_service/src/file_handlers/grit.rs new file mode 100644 index 000000000000..aeacdf793d5a --- /dev/null +++ b/crates/biome_service/src/file_handlers/grit.rs @@ -0,0 +1,37 @@ +use crate::settings::ServiceLanguage; +use biome_grit_syntax::GritLanguage; + +impl ServiceLanguage for GritLanguage { + type FormatterSettings = (); + type LinterSettings = (); + type OrganizeImportsSettings = (); + type FormatOptions = GritFormatOptions; + type ParserSettings = (); + type EnvironmentSettings = (); + fn lookup_settings( + languages: &crate::settings::LanguageListSettings, + ) -> &crate::settings::LanguageSettings { + todo!() + } + + fn resolve_format_options( + global: Option<&crate::settings::FormatSettings>, + overrides: Option<&crate::settings::OverrideSettings>, + language: Option<&Self::FormatterSettings>, + path: &biome_fs::BiomePath, + file_source: &super::DocumentFileSource, + ) -> Self::FormatOptions { + todo!() + } + + fn resolve_analyzer_options( + global: Option<&crate::settings::Settings>, + linter: Option<&crate::settings::LinterSettings>, + overrides: Option<&crate::settings::OverrideSettings>, + language: Option<&Self::LinterSettings>, + path: &biome_fs::BiomePath, + file_source: &super::DocumentFileSource, + ) -> biome_analyze::AnalyzerOptions { + todo!() + } +} diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 2da2abb62147..4343e957d6e0 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -48,6 +48,7 @@ use tracing::instrument; mod astro; mod css; mod graphql; +mod grit; mod html; mod javascript; mod json; From dec1fe0231b99289db42a2f427909caf17bde0b7 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Mon, 16 Sep 2024 21:08:41 -0500 Subject: [PATCH 02/21] add formatoptions impl for gritformatoptions --- crates/biome_grit_formatter/src/context.rs | 24 +++++++++++++++---- .../biome_service/src/file_handlers/grit.rs | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index b4a8e093459a..d40c4d803327 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -1,9 +1,10 @@ use crate::comments::{FormatGritLeadingComment, GritCommentStyle, GritComments}; use biome_formatter::{ - CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, - LineWidth, QuoteStyle, TransformSourceMap, + AttributePosition, CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, + LineEnding, LineWidth, QuoteStyle, TransformSourceMap, }; use biome_grit_syntax::GritLanguage; +use std::fmt::Display; use std::rc::Rc; #[allow(dead_code)] @@ -50,14 +51,14 @@ impl CstFormatContext for GritFormatContext { } } -#[derive(Debug, Default, Clone, PartialEq)] - +#[derive(Debug, Default, Clone)] pub struct GritFormatOptions { indent_style: IndentStyle, indent_width: IndentWidth, line_ending: LineEnding, line_width: LineWidth, quote_style: QuoteStyle, + attribute_position: AttributePosition, } impl GritFormatOptions { @@ -68,6 +69,7 @@ impl GritFormatOptions { line_ending: LineEnding::default(), line_width: LineWidth::default(), quote_style: QuoteStyle::default(), + attribute_position: AttributePosition::default(), } } pub fn with_indent_style(mut self, indent_style: IndentStyle) -> Self { @@ -118,6 +120,20 @@ impl GritFormatOptions { pub fn quote_style(&self) -> QuoteStyle { self.quote_style } + + pub fn attribute_position(&self) -> AttributePosition { + self.attribute_position + } +} + +impl Display for GritFormatOptions { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "Indent style: {}", self.indent_style)?; + writeln!(f, "Indent width: {}", self.indent_width.value())?; + writeln!(f, "Line ending: {}", self.line_ending)?; + writeln!(f, "Line width: {}", self.line_width.value())?; + writeln!(f, "Attribute Position: {}", self.attribute_position) + } } impl FormatOptions for GritFormatOptions { diff --git a/crates/biome_service/src/file_handlers/grit.rs b/crates/biome_service/src/file_handlers/grit.rs index aeacdf793d5a..0617d6532a09 100644 --- a/crates/biome_service/src/file_handlers/grit.rs +++ b/crates/biome_service/src/file_handlers/grit.rs @@ -1,4 +1,5 @@ use crate::settings::ServiceLanguage; +use biome_grit_formatter::context::GritFormatOptions; use biome_grit_syntax::GritLanguage; impl ServiceLanguage for GritLanguage { From f06d937a211697defcd290d30ede7c6e0570d203 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Tue, 17 Sep 2024 08:25:38 -0500 Subject: [PATCH 03/21] add grit filehandling --- Cargo.lock | 1 + crates/biome_grit_formatter/tests/language.rs | 1 - crates/biome_grit_parser/src/lib.rs | 10 +- crates/biome_service/Cargo.toml | 1 + .../biome_service/src/file_handlers/grit.rs | 108 +++++++++++++++--- 5 files changed, 104 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b02f207cbb24..42e332089752 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1056,6 +1056,7 @@ dependencies = [ "biome_graphql_parser", "biome_graphql_syntax", "biome_grit_formatter", + "biome_grit_parser", "biome_grit_patterns", "biome_grit_syntax", "biome_html_formatter", diff --git a/crates/biome_grit_formatter/tests/language.rs b/crates/biome_grit_formatter/tests/language.rs index 1e8caa565b28..274aa85f3b2d 100644 --- a/crates/biome_grit_formatter/tests/language.rs +++ b/crates/biome_grit_formatter/tests/language.rs @@ -8,7 +8,6 @@ pub struct GritTestFormatLanguage; impl TestFormatLanguage for GritTestFormatLanguage { type ServiceLanguage = GritLanguage; - type Context = GritFormatContext; type FormatLanguage = GritFormatLanguage; diff --git a/crates/biome_grit_parser/src/lib.rs b/crates/biome_grit_parser/src/lib.rs index 69e9c671b466..1aa0cf697e8d 100644 --- a/crates/biome_grit_parser/src/lib.rs +++ b/crates/biome_grit_parser/src/lib.rs @@ -5,8 +5,8 @@ mod token_source; use biome_grit_factory::GritSyntaxFactory; use biome_grit_syntax::{GritLanguage, GritRoot, GritSyntaxNode}; -use biome_parser::diagnostic::ParseDiagnostic; use biome_parser::tree_sink::LosslessTreeSink; +use biome_parser::{diagnostic::ParseDiagnostic, AnyParse}; use biome_rowan::{AstNode, NodeCache}; use parser::{parse_root, GritParser}; @@ -100,3 +100,11 @@ impl GritParse { GritRoot::unwrap_cast(self.syntax()) } } + +impl From for AnyParse { + fn from(parse: GritParse) -> Self { + let root = parse.syntax(); + let diagnostics = parse.into_diagnostics(); + Self::new(root.as_send().unwrap(), diagnostics) + } +} diff --git a/crates/biome_service/Cargo.toml b/crates/biome_service/Cargo.toml index c900d3ffa32f..734b4b762b1b 100644 --- a/crates/biome_service/Cargo.toml +++ b/crates/biome_service/Cargo.toml @@ -32,6 +32,7 @@ biome_graphql_formatter = { workspace = true } biome_graphql_parser = { workspace = true } biome_graphql_syntax = { workspace = true } biome_grit_formatter = { workspace = true } +biome_grit_parser = { workspace = true } biome_grit_patterns = { workspace = true } biome_grit_syntax = { workspace = true } diff --git a/crates/biome_service/src/file_handlers/grit.rs b/crates/biome_service/src/file_handlers/grit.rs index 0617d6532a09..27605e0d9b06 100644 --- a/crates/biome_service/src/file_handlers/grit.rs +++ b/crates/biome_service/src/file_handlers/grit.rs @@ -1,6 +1,19 @@ -use crate::settings::ServiceLanguage; -use biome_grit_formatter::context::GritFormatOptions; +use crate::{ + settings::{ServiceLanguage, Settings, WorkspaceSettingsHandle}, + WorkspaceError, +}; +use biome_formatter::Printed; +use biome_fs::BiomePath; +use biome_grit_formatter::{context::GritFormatOptions, format_node}; +use biome_grit_parser::parse_grit_with_cache; use biome_grit_syntax::GritLanguage; +use biome_parser::AnyParse; +use biome_rowan::NodeCache; + +use super::{ + AnalyzerCapabilities, Capabilities, DebugCapabilities, DocumentFileSource, ExtensionHandler, + FormatterCapabilities, ParseResult, ParserCapabilities, SearchCapabilities, +}; impl ServiceLanguage for GritLanguage { type FormatterSettings = (); @@ -10,29 +23,94 @@ impl ServiceLanguage for GritLanguage { type ParserSettings = (); type EnvironmentSettings = (); fn lookup_settings( - languages: &crate::settings::LanguageListSettings, + _languages: &crate::settings::LanguageListSettings, ) -> &crate::settings::LanguageSettings { todo!() } fn resolve_format_options( - global: Option<&crate::settings::FormatSettings>, - overrides: Option<&crate::settings::OverrideSettings>, - language: Option<&Self::FormatterSettings>, - path: &biome_fs::BiomePath, - file_source: &super::DocumentFileSource, + _global: Option<&crate::settings::FormatSettings>, + _overrides: Option<&crate::settings::OverrideSettings>, + _language: Option<&Self::FormatterSettings>, + _path: &biome_fs::BiomePath, + _file_source: &super::DocumentFileSource, ) -> Self::FormatOptions { - todo!() + GritFormatOptions::default() } fn resolve_analyzer_options( - global: Option<&crate::settings::Settings>, - linter: Option<&crate::settings::LinterSettings>, - overrides: Option<&crate::settings::OverrideSettings>, - language: Option<&Self::LinterSettings>, - path: &biome_fs::BiomePath, - file_source: &super::DocumentFileSource, + _global: Option<&crate::settings::Settings>, + _linter: Option<&crate::settings::LinterSettings>, + _overrides: Option<&crate::settings::OverrideSettings>, + _language: Option<&Self::LinterSettings>, + _path: &biome_fs::BiomePath, + _file_source: &super::DocumentFileSource, ) -> biome_analyze::AnalyzerOptions { todo!() } } + +#[derive(Debug, Default, PartialEq, Eq)] + +pub(crate) struct GritFileHandler; + +impl ExtensionHandler for GritFileHandler { + fn capabilities(&self) -> Capabilities { + Capabilities { + parser: ParserCapabilities { parse: Some(parse) }, + debug: DebugCapabilities { + debug_syntax_tree: None, + debug_control_flow: None, + debug_formatter_ir: None, + }, + analyzer: AnalyzerCapabilities { + lint: None, + code_actions: None, + rename: None, + fix_all: None, + organize_imports: None, + }, + formatter: FormatterCapabilities { + format: Some(format), + format_range: None, + format_on_type: None, + }, + search: SearchCapabilities { search: None }, + } + } +} + +fn parse( + _biome_path: &BiomePath, + file_source: DocumentFileSource, + text: &str, + _settings: Option<&Settings>, + cache: &mut NodeCache, +) -> ParseResult { + let parse = parse_grit_with_cache(text, cache); + + ParseResult { + any_parse: parse.into(), + language: Some(file_source), + } +} + +#[tracing::instrument(level = "debug", skip(parse, settings))] +fn format( + biome_path: &BiomePath, + document_file_source: &DocumentFileSource, + parse: AnyParse, + settings: WorkspaceSettingsHandle, +) -> Result { + let options = settings.format_options::(biome_path, document_file_source); + + tracing::debug!("Format with the following options: \n{}", options); + + let tree = parse.syntax(); + let formatted = format_node(options, &tree)?; + + match formatted.print() { + Ok(printed) => Ok(printed), + Err(error) => Err(WorkspaceError::FormatError(error.into())), + } +} From 3ec03df2472c61c50fc30ee39246e7eb8ee7221a Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Wed, 18 Sep 2024 19:55:35 -0500 Subject: [PATCH 04/21] add tests for grit --- Cargo.lock | 1 + crates/biome_formatter/CONTRIBUTING.md | 8 +++++++- crates/biome_grit_formatter/Cargo.toml | 1 + crates/biome_grit_formatter/tests/spec_test.rs | 12 +++++++++--- .../tests/specs/grit/as_modifier.grit | 4 ++++ 5 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit diff --git a/Cargo.lock b/Cargo.lock index 42e332089752..76a654f7ac99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -591,6 +591,7 @@ dependencies = [ name = "biome_grit_formatter" version = "0.0.0" dependencies = [ + "biome_configuration", "biome_formatter", "biome_formatter_test", "biome_grit_factory", diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index 0d36e8f2bf9a..1a9072c20323 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -424,9 +424,15 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f return; }; + let options = HtmlFormatOptions::default(); let language = language::HtmlTestFormatLanguage::default(); - let snapshot = SpecSnapshot::new(test_file, test_directory, language, ()); + let snapshot = SpecSnapshot::new( + test_file, + test_directory, + language, + HtmlFormatLanguage::new(options), + ); snapshot.test() } diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index e75a5e99f567..2ceb28418a80 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -18,6 +18,7 @@ biome_grit_syntax = { workspace = true } biome_rowan = { workspace = true } [dev-dependencies] +biome_configuration = { path = "../biome_configuration" } biome_formatter_test = { path = "../biome_formatter_test" } biome_grit_factory = { path = "../biome_grit_factory" } biome_grit_parser = { path = "../biome_grit_parser" } diff --git a/crates/biome_grit_formatter/tests/spec_test.rs b/crates/biome_grit_formatter/tests/spec_test.rs index a63046226f8d..fd15d63300d7 100644 --- a/crates/biome_grit_formatter/tests/spec_test.rs +++ b/crates/biome_grit_formatter/tests/spec_test.rs @@ -1,4 +1,5 @@ use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile}; +use biome_grit_formatter::{context::GritFormatOptions, GritFormatLanguage}; use std::path::Path; mod language { @@ -8,14 +9,19 @@ mod language { pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _file_type: &str) { let root_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/specs/")); - let Some(test_file) = SpecTestFile::try_from_file(spec_input_file, root_path, Some(settings)) - else { + let Some(test_file) = SpecTestFile::try_from_file(spec_input_file, root_path, None) else { return; }; + let options = GritFormatOptions::default(); let language = language::GritTestFormatLanguage::default(); - let snapshot = SpecSnapshot::new(test_file, test_directory, language, ()); + let snapshot = SpecSnapshot::new( + test_file, + test_directory, + language, + GritFormatLanguage::new(options), + ); snapshot.test() } diff --git a/crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit b/crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit new file mode 100644 index 000000000000..6582c97bd651 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit @@ -0,0 +1,4 @@ +`function $name ($args) { $body }` as $func where { + $func => `const $name = ($args) => { $body }`, + $args <: contains `apple` => `mango` +} From aa22b364eed189986b3d7e2e9276371f560a46d2 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Wed, 18 Sep 2024 19:58:41 -0500 Subject: [PATCH 05/21] undo format changes to cargo.toml --- crates/biome_service/Cargo.toml | 65 ++++++++++++++++----------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/crates/biome_service/Cargo.toml b/crates/biome_service/Cargo.toml index 734b4b762b1b..77e2048d818d 100644 --- a/crates/biome_service/Cargo.toml +++ b/crates/biome_service/Cargo.toml @@ -35,39 +35,38 @@ biome_grit_formatter = { workspace = true } biome_grit_parser = { workspace = true } biome_grit_patterns = { workspace = true } biome_grit_syntax = { workspace = true } - -biome_html_formatter = { workspace = true } -biome_html_parser = { workspace = true } -biome_html_syntax = { workspace = true } -biome_js_analyze = { workspace = true } -biome_js_factory = { workspace = true, optional = true } -biome_js_formatter = { workspace = true, features = ["serde"] } -biome_js_parser = { workspace = true } -biome_js_semantic = { workspace = true } -biome_js_syntax = { workspace = true, features = ["schema"] } -biome_json_analyze = { workspace = true } -biome_json_formatter = { workspace = true, features = ["serde"] } -biome_json_parser = { workspace = true } -biome_json_syntax = { workspace = true } -biome_parser = { workspace = true } -biome_project = { workspace = true } -biome_rowan = { workspace = true, features = ["serde"] } -biome_string_case = { workspace = true } -biome_text_edit = { workspace = true } -bpaf = { workspace = true } -dashmap = { workspace = true } -enumflags2 = { workspace = true, features = ["serde"] } -getrandom = { workspace = true, features = ["js"] } -ignore = { workspace = true } -indexmap = { workspace = true, features = ["serde"] } -oxc_resolver = { workspace = true } -regex = { workspace = true } -rustc-hash = { workspace = true } -schemars = { workspace = true, features = ["indexmap1"], optional = true } -serde = { workspace = true, features = ["derive"] } -serde_json = { workspace = true, features = ["raw_value"] } -slotmap = { workspace = true, features = ["serde"] } -tracing = { workspace = true, features = ["attributes", "log"] } +biome_html_formatter = { workspace = true } +biome_html_parser = { workspace = true } +biome_html_syntax = { workspace = true } +biome_js_analyze = { workspace = true } +biome_js_factory = { workspace = true, optional = true } +biome_js_formatter = { workspace = true, features = ["serde"] } +biome_js_parser = { workspace = true } +biome_js_semantic = { workspace = true } +biome_js_syntax = { workspace = true, features = ["schema"] } +biome_json_analyze = { workspace = true } +biome_json_formatter = { workspace = true, features = ["serde"] } +biome_json_parser = { workspace = true } +biome_json_syntax = { workspace = true } +biome_parser = { workspace = true } +biome_project = { workspace = true } +biome_rowan = { workspace = true, features = ["serde"] } +biome_string_case = { workspace = true } +biome_text_edit = { workspace = true } +bpaf = { workspace = true } +dashmap = { workspace = true } +enumflags2 = { workspace = true, features = ["serde"] } +getrandom = { workspace = true, features = ["js"] } +ignore = { workspace = true } +indexmap = { workspace = true, features = ["serde"] } +oxc_resolver = { workspace = true } +regex = { workspace = true } +rustc-hash = { workspace = true } +schemars = { workspace = true, features = ["indexmap1"], optional = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true, features = ["raw_value"] } +slotmap = { workspace = true, features = ["serde"] } +tracing = { workspace = true, features = ["attributes", "log"] } [features] schema = [ From f89f1b70749143bbb01e24d7d6f3fee2b154ef73 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Wed, 18 Sep 2024 20:37:05 -0500 Subject: [PATCH 06/21] linting stuff --- crates/biome_grit_formatter/tests/language.rs | 8 +++----- crates/biome_service/src/file_handlers/grit.rs | 1 - 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/biome_grit_formatter/tests/language.rs b/crates/biome_grit_formatter/tests/language.rs index 274aa85f3b2d..752b393ce1e0 100644 --- a/crates/biome_grit_formatter/tests/language.rs +++ b/crates/biome_grit_formatter/tests/language.rs @@ -3,23 +3,21 @@ use biome_grit_formatter::{context::GritFormatContext, GritFormatLanguage}; use biome_grit_syntax::GritLanguage; #[derive(Default)] - pub struct GritTestFormatLanguage; impl TestFormatLanguage for GritTestFormatLanguage { type ServiceLanguage = GritLanguage; type Context = GritFormatContext; - type FormatLanguage = GritFormatLanguage; - fn parse(&self, text: &str) -> biome_parser::AnyParse { + fn parse(&self, _text: &str) -> biome_parser::AnyParse { todo!() } fn to_format_language( &self, - settings: &biome_service::settings::Settings, - file_source: &biome_service::workspace::DocumentFileSource, + _settings: &biome_service::settings::Settings, + _file_source: &biome_service::workspace::DocumentFileSource, ) -> Self::FormatLanguage { todo!() } diff --git a/crates/biome_service/src/file_handlers/grit.rs b/crates/biome_service/src/file_handlers/grit.rs index 27605e0d9b06..d2299c643262 100644 --- a/crates/biome_service/src/file_handlers/grit.rs +++ b/crates/biome_service/src/file_handlers/grit.rs @@ -51,7 +51,6 @@ impl ServiceLanguage for GritLanguage { } #[derive(Debug, Default, PartialEq, Eq)] - pub(crate) struct GritFileHandler; impl ExtensionHandler for GritFileHandler { From 1b501d1684ca2c918267e698e061b8c481ca535a Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 19 Sep 2024 08:21:33 -0500 Subject: [PATCH 07/21] Add file instantiation --- crates/biome_service/src/file_handlers/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 4343e957d6e0..880396d6d59f 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -38,6 +38,7 @@ use biome_parser::AnyParse; use biome_project::PackageJson; use biome_rowan::{FileSourceError, NodeCache}; use biome_string_case::StrExtension; +use grit::GritFileHandler; use html::HtmlFileHandler; pub use javascript::JsFormatterSettings; use std::borrow::Cow; @@ -526,6 +527,8 @@ pub(crate) struct Features { unknown: UnknownFileHandler, graphql: GraphqlFileHandler, html: HtmlFileHandler, + #[allow(unused)] + grit: GritFileHandler, } impl Features { @@ -539,6 +542,7 @@ impl Features { svelte: SvelteFileHandler {}, graphql: GraphqlFileHandler {}, html: HtmlFileHandler {}, + grit: GritFileHandler {}, unknown: UnknownFileHandler::default(), } } From f87f7e38e30747ee1acd8b2974c7422e478fbfd0 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 19 Sep 2024 08:32:50 -0500 Subject: [PATCH 08/21] Fix last linting issue --- crates/biome_grit_formatter/tests/spec_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_grit_formatter/tests/spec_test.rs b/crates/biome_grit_formatter/tests/spec_test.rs index fd15d63300d7..3fd8e25857fa 100644 --- a/crates/biome_grit_formatter/tests/spec_test.rs +++ b/crates/biome_grit_formatter/tests/spec_test.rs @@ -14,7 +14,7 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f }; let options = GritFormatOptions::default(); - let language = language::GritTestFormatLanguage::default(); + let language = language::GritTestFormatLanguage; let snapshot = SpecSnapshot::new( test_file, From f47ed64d382c531162e2cfbb9d4277de4e72a020 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 19 Sep 2024 08:50:25 -0500 Subject: [PATCH 09/21] Remove unused dependencies --- Cargo.lock | 3 --- crates/biome_grit_formatter/Cargo.toml | 3 --- 2 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76a654f7ac99..095d0c890796 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -601,9 +601,6 @@ dependencies = [ "biome_rowan", "biome_service", "countme", - "iai", - "quickcheck", - "quickcheck_macros", "serde", "serde_json", "tests_macros", diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index 2ceb28418a80..df966d11b97f 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -25,9 +25,6 @@ biome_grit_parser = { path = "../biome_grit_parser" } biome_parser = { path = "../biome_parser" } biome_service = { path = "../biome_service" } countme = { workspace = true, features = ["enable"] } -iai = "0.1.1" -quickcheck = { workspace = true } -quickcheck_macros = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } tests_macros = { path = "../tests_macros" } From 8e9b92c9e77e143bafa566869536d4d0ac2b5146 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 19 Sep 2024 09:29:08 -0500 Subject: [PATCH 10/21] Update crates/biome_service/src/file_handlers/mod.rs Co-authored-by: Carson McManus --- crates/biome_service/src/file_handlers/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 880396d6d59f..025881dcd09f 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -527,7 +527,7 @@ pub(crate) struct Features { unknown: UnknownFileHandler, graphql: GraphqlFileHandler, html: HtmlFileHandler, - #[allow(unused)] + #[expect(unused)] grit: GritFileHandler, } From 32d088c6330d6b2eb64d5f403f24cc3901f107dd Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 19 Sep 2024 09:45:13 -0500 Subject: [PATCH 11/21] Add panic to test --- crates/biome_html_formatter/tests/spec_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_html_formatter/tests/spec_test.rs b/crates/biome_html_formatter/tests/spec_test.rs index fea0ee2e56b9..d61107996cb4 100644 --- a/crates/biome_html_formatter/tests/spec_test.rs +++ b/crates/biome_html_formatter/tests/spec_test.rs @@ -28,7 +28,7 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f let root_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/specs/")); let Some(test_file) = SpecTestFile::try_from_file(spec_input_file, root_path, None) else { - return; + panic!("Failed to set up snapshot test"); }; let source_type: HtmlFileSource = test_file.input_file().as_path().try_into().unwrap(); From 56b4c701878ea76cea020baf43aa79c8e4e44b03 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 19 Sep 2024 10:00:20 -0500 Subject: [PATCH 12/21] Add panic to test --- crates/biome_grit_formatter/tests/spec_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_grit_formatter/tests/spec_test.rs b/crates/biome_grit_formatter/tests/spec_test.rs index 3fd8e25857fa..0da896dfc841 100644 --- a/crates/biome_grit_formatter/tests/spec_test.rs +++ b/crates/biome_grit_formatter/tests/spec_test.rs @@ -10,7 +10,7 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f let root_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/specs/")); let Some(test_file) = SpecTestFile::try_from_file(spec_input_file, root_path, None) else { - return; + panic!("Failed to set up snapshot test"); }; let options = GritFormatOptions::default(); From b1bcdd75cd16ea52c33dc966d63a11578e5f1816 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 19 Sep 2024 20:49:21 -0500 Subject: [PATCH 13/21] Add file source for grit --- crates/biome_grit_syntax/src/file_source.rs | 44 +++++++++++++++++++ crates/biome_grit_syntax/src/lib.rs | 1 + crates/biome_service/src/file_handlers/mod.rs | 3 ++ crates/biome_service/src/settings.rs | 2 + 4 files changed, 50 insertions(+) create mode 100644 crates/biome_grit_syntax/src/file_source.rs diff --git a/crates/biome_grit_syntax/src/file_source.rs b/crates/biome_grit_syntax/src/file_source.rs new file mode 100644 index 000000000000..9cc4103fb796 --- /dev/null +++ b/crates/biome_grit_syntax/src/file_source.rs @@ -0,0 +1,44 @@ +use std::{ffi::OsStr, path::Path}; + +use biome_rowan::FileSourceError; + +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[derive( + Debug, Clone, Default, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize, +)] +pub struct GritFileSource { + #[allow(unused)] + variant: GritVariant, +} + +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[derive( + Debug, Clone, Default, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize, +)] +enum GritVariant { + #[default] + Standard, +} + +impl GritFileSource { + pub fn grit() -> Self { + Self { + variant: GritVariant::Standard, + } + } + + pub fn try_from_extension(extension: &OsStr) -> Result { + match extension.as_encoded_bytes() { + b"grit" => Ok(Self::grit()), + _ => Err(FileSourceError::UnknownExtension), + } + } +} + +impl TryFrom<&Path> for GritFileSource { + type Error = FileSourceError; + + fn try_from(value: &Path) -> Result { + todo!() + } +} diff --git a/crates/biome_grit_syntax/src/lib.rs b/crates/biome_grit_syntax/src/lib.rs index 76a7309d1b92..7a03be23f7ee 100644 --- a/crates/biome_grit_syntax/src/lib.rs +++ b/crates/biome_grit_syntax/src/lib.rs @@ -4,6 +4,7 @@ #[macro_use] mod generated; +mod file_source; mod syntax_ext; mod syntax_node; diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 30a811e97019..9b31f41cfc09 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -150,6 +150,9 @@ impl DocumentFileSource { if let Ok(file_source) = HtmlFileSource::try_from_extension(extension) { return Ok(file_source.into()); } + + // #[cfg(feature = "experimental-grit")] + // if let Ok(file_source) = GritFileSource::; Err(FileSourceError::UnknownExtension) } diff --git a/crates/biome_service/src/settings.rs b/crates/biome_service/src/settings.rs index 8eaf70178351..a25b2ffc10fe 100644 --- a/crates/biome_service/src/settings.rs +++ b/crates/biome_service/src/settings.rs @@ -23,6 +23,7 @@ use biome_formatter::{ use biome_fs::BiomePath; use biome_graphql_formatter::context::GraphqlFormatOptions; use biome_graphql_syntax::GraphqlLanguage; +use biome_grit_syntax::GritLanguage; use biome_html_syntax::HtmlLanguage; use biome_js_formatter::context::JsFormatOptions; use biome_js_parser::JsParserOptions; @@ -562,6 +563,7 @@ pub struct LanguageListSettings { pub css: LanguageSettings, pub graphql: LanguageSettings, pub html: LanguageSettings, + pub grit: LanguageSettings, } impl From for LanguageSettings { From 94f38876ff2e5bd7b90f64ce1cebae817aa9fd9b Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Fri, 20 Sep 2024 10:29:42 -0500 Subject: [PATCH 14/21] Add grit file handleres --- .github/workflows/repository_dispatch.yml | 2 +- crates/biome_cli/Cargo.toml | 1 + crates/biome_grit_syntax/src/file_source.rs | 23 +++++++++++++++---- crates/biome_grit_syntax/src/lib.rs | 2 +- crates/biome_service/Cargo.toml | 2 ++ .../biome_service/src/file_handlers/grit.rs | 12 ++++++---- crates/biome_service/src/file_handlers/mod.rs | 16 +++++++++++-- crates/biome_wasm/Cargo.toml | 1 + 8 files changed, 46 insertions(+), 13 deletions(-) diff --git a/.github/workflows/repository_dispatch.yml b/.github/workflows/repository_dispatch.yml index 0321e2e2a412..405ca1ee0204 100644 --- a/.github/workflows/repository_dispatch.yml +++ b/.github/workflows/repository_dispatch.yml @@ -39,7 +39,7 @@ jobs: run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh - name: Build WASM module for the web - run: wasm-pack build --out-dir ../../packages/@biomejs/wasm-web --target web --profiling --scope biomejs crates/biome_wasm --features experimental-html + run: wasm-pack build --out-dir ../../packages/@biomejs/wasm-web --target web --profiling --scope biomejs crates/biome_wasm --features experimental-html, experimental-grit # https://github.com/actions/cache/issues/342 - name: Clear old wasm-pack cache diff --git a/crates/biome_cli/Cargo.toml b/crates/biome_cli/Cargo.toml index 06ad44b835e3..01001244ac15 100644 --- a/crates/biome_cli/Cargo.toml +++ b/crates/biome_cli/Cargo.toml @@ -78,6 +78,7 @@ tokio = { workspace = true, features = ["io-util"] } [features] docgen = ["bpaf/docgen"] +experimental-grit = ["biome_service/experimental-grit"] experimental-html = ["biome_service/experimental-html"] [lints] diff --git a/crates/biome_grit_syntax/src/file_source.rs b/crates/biome_grit_syntax/src/file_source.rs index 9cc4103fb796..7399da2e68ce 100644 --- a/crates/biome_grit_syntax/src/file_source.rs +++ b/crates/biome_grit_syntax/src/file_source.rs @@ -1,7 +1,5 @@ -use std::{ffi::OsStr, path::Path}; - use biome_rowan::FileSourceError; - +use std::{ffi::OsStr, path::Path}; #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[derive( Debug, Clone, Default, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize, @@ -27,6 +25,12 @@ impl GritFileSource { } } + /// Try to return the HTML file source corresponding to this file name from well-known files + pub fn try_from_well_known(_: &Path) -> Result { + // TODO: to be implemented + Err(FileSourceError::UnknownFileName) + } + pub fn try_from_extension(extension: &OsStr) -> Result { match extension.as_encoded_bytes() { b"grit" => Ok(Self::grit()), @@ -38,7 +42,16 @@ impl GritFileSource { impl TryFrom<&Path> for GritFileSource { type Error = FileSourceError; - fn try_from(value: &Path) -> Result { - todo!() + fn try_from(path: &Path) -> Result { + if let Ok(file_source) = Self::try_from_well_known(path) { + return Ok(file_source); + } + + let Some(extension) = path.extension() else { + return Err(FileSourceError::MissingFileExtension); + }; + // We assume the file extensions are case-insensitive + // and we use the lowercase form of them for pattern matching + Self::try_from_extension(&extension.to_ascii_lowercase()) } } diff --git a/crates/biome_grit_syntax/src/lib.rs b/crates/biome_grit_syntax/src/lib.rs index 7a03be23f7ee..364af38a5cb3 100644 --- a/crates/biome_grit_syntax/src/lib.rs +++ b/crates/biome_grit_syntax/src/lib.rs @@ -4,7 +4,7 @@ #[macro_use] mod generated; -mod file_source; +pub mod file_source; mod syntax_ext; mod syntax_node; diff --git a/crates/biome_service/Cargo.toml b/crates/biome_service/Cargo.toml index 79c59a72f057..0363f535628a 100644 --- a/crates/biome_service/Cargo.toml +++ b/crates/biome_service/Cargo.toml @@ -69,7 +69,9 @@ slotmap = { workspace = true, features = ["serde"] } tracing = { workspace = true, features = ["attributes", "log"] } [features] +experimental-grit = [] experimental-html = [] + schema = [ "dep:schemars", "biome_js_analyze/schema", diff --git a/crates/biome_service/src/file_handlers/grit.rs b/crates/biome_service/src/file_handlers/grit.rs index d2299c643262..afb9529d2fe2 100644 --- a/crates/biome_service/src/file_handlers/grit.rs +++ b/crates/biome_service/src/file_handlers/grit.rs @@ -2,6 +2,7 @@ use crate::{ settings::{ServiceLanguage, Settings, WorkspaceSettingsHandle}, WorkspaceError, }; +use biome_analyze::{AnalyzerConfiguration, AnalyzerOptions}; use biome_formatter::Printed; use biome_fs::BiomePath; use biome_grit_formatter::{context::GritFormatOptions, format_node}; @@ -23,9 +24,9 @@ impl ServiceLanguage for GritLanguage { type ParserSettings = (); type EnvironmentSettings = (); fn lookup_settings( - _languages: &crate::settings::LanguageListSettings, + languages: &crate::settings::LanguageListSettings, ) -> &crate::settings::LanguageSettings { - todo!() + &languages.grit } fn resolve_format_options( @@ -43,10 +44,13 @@ impl ServiceLanguage for GritLanguage { _linter: Option<&crate::settings::LinterSettings>, _overrides: Option<&crate::settings::OverrideSettings>, _language: Option<&Self::LinterSettings>, - _path: &biome_fs::BiomePath, + path: &biome_fs::BiomePath, _file_source: &super::DocumentFileSource, ) -> biome_analyze::AnalyzerOptions { - todo!() + AnalyzerOptions { + configuration: AnalyzerConfiguration::default(), + file_path: path.to_path_buf(), + } } } diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 9b31f41cfc09..37e9e18b08e5 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -28,6 +28,7 @@ use biome_formatter::Printed; use biome_fs::BiomePath; use biome_graphql_syntax::{GraphqlFileSource, GraphqlLanguage}; use biome_grit_patterns::{GritQuery, GritQueryResult, GritTargetFile}; +use biome_grit_syntax::file_source::GritFileSource; use biome_html_syntax::HtmlFileSource; use biome_js_parser::{parse, JsParserOptions}; use biome_js_syntax::{ @@ -38,6 +39,7 @@ use biome_parser::AnyParse; use biome_project::PackageJson; use biome_rowan::{FileSourceError, NodeCache}; use biome_string_case::StrExtension; + use grit::GritFileHandler; use html::HtmlFileHandler; pub use javascript::JsFormatterSettings; @@ -67,6 +69,7 @@ pub enum DocumentFileSource { Css(CssFileSource), Graphql(GraphqlFileSource), Html(HtmlFileSource), + Grit(GritFileSource), #[default] Unknown, } @@ -151,8 +154,10 @@ impl DocumentFileSource { return Ok(file_source.into()); } - // #[cfg(feature = "experimental-grit")] - // if let Ok(file_source) = GritFileSource::; + #[cfg(feature = "experimental-grit")] + if let Ok(file_source) = GritFileSource::try_from_extension(extension) { + return Ok(file_source.into()); + } Err(FileSourceError::UnknownExtension) } @@ -180,6 +185,10 @@ impl DocumentFileSource { if let Ok(file_source) = HtmlFileSource::try_from_language_id(language_id) { return Ok(file_source.into()); } + #[cfg(feature = "experimental-grit")] + if let Ok(file_source) = GritFileSource::try_from_language_id(language_id) { + return Ok(file_source.into()); + } Err(FileSourceError::UnknownLanguageId) } @@ -322,6 +331,7 @@ impl DocumentFileSource { | DocumentFileSource::Graphql(_) | DocumentFileSource::Json(_) => true, DocumentFileSource::Html(_) => cfg!(feature = "experimental-html"), + DocumentFileSource::Grit(_) => cfg!(feature = "experimental-grit"), DocumentFileSource::Unknown => false, } } @@ -354,6 +364,7 @@ impl biome_console::fmt::Display for DocumentFileSource { DocumentFileSource::Css(_) => fmt.write_markup(markup! { "CSS" }), DocumentFileSource::Graphql(_) => fmt.write_markup(markup! { "GraphQL" }), DocumentFileSource::Html(_) => fmt.write_markup(markup! { "HTML" }), + DocumentFileSource::Grit(_) => fmt.write_markup(markup! { "GRIT" }), DocumentFileSource::Unknown => fmt.write_markup(markup! { "Unknown" }), } } @@ -569,6 +580,7 @@ impl Features { DocumentFileSource::Css(_) => self.css.capabilities(), DocumentFileSource::Graphql(_) => self.graphql.capabilities(), DocumentFileSource::Html(_) => self.html.capabilities(), + DocumentFileSource::Grit(_) => self.grit.capabilities(), DocumentFileSource::Unknown => self.unknown.capabilities(), } } diff --git a/crates/biome_wasm/Cargo.toml b/crates/biome_wasm/Cargo.toml index 462dbbb6500a..2058da5a419b 100644 --- a/crates/biome_wasm/Cargo.toml +++ b/crates/biome_wasm/Cargo.toml @@ -17,6 +17,7 @@ crate-type = ["cdylib", "rlib"] [features] default = ["console_error_panic_hook"] +experimental-grit = ["biome_service/experimental-grit"] experimental-html = ["biome_service/experimental-html"] [dependencies] From ced50521035bf81c578af179a6880e1e344d55cf Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Fri, 20 Sep 2024 13:50:34 -0500 Subject: [PATCH 15/21] Add grit syntax to schema --- crates/biome_service/Cargo.toml | 2 ++ crates/biome_service/src/file_handlers/mod.rs | 3 +-- xtask/rules_check/src/lib.rs | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/biome_service/Cargo.toml b/crates/biome_service/Cargo.toml index c25d6d6f3354..9ca9261aab40 100644 --- a/crates/biome_service/Cargo.toml +++ b/crates/biome_service/Cargo.toml @@ -82,6 +82,8 @@ schema = [ "biome_json_syntax/schema", "biome_css_syntax/schema", "biome_graphql_syntax/schema", + "biome_grit_syntax/schema", + ] [dev-dependencies] diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 37e9e18b08e5..3998da01e13f 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -364,7 +364,7 @@ impl biome_console::fmt::Display for DocumentFileSource { DocumentFileSource::Css(_) => fmt.write_markup(markup! { "CSS" }), DocumentFileSource::Graphql(_) => fmt.write_markup(markup! { "GraphQL" }), DocumentFileSource::Html(_) => fmt.write_markup(markup! { "HTML" }), - DocumentFileSource::Grit(_) => fmt.write_markup(markup! { "GRIT" }), + DocumentFileSource::Grit(_) => fmt.write_markup(markup! { "Grit" }), DocumentFileSource::Unknown => fmt.write_markup(markup! { "Unknown" }), } } @@ -543,7 +543,6 @@ pub(crate) struct Features { unknown: UnknownFileHandler, graphql: GraphqlFileHandler, html: HtmlFileHandler, - #[expect(unused)] grit: GritFileHandler, } diff --git a/xtask/rules_check/src/lib.rs b/xtask/rules_check/src/lib.rs index 23cf017ce3b8..b33f9359c76d 100644 --- a/xtask/rules_check/src/lib.rs +++ b/xtask/rules_check/src/lib.rs @@ -449,6 +449,8 @@ fn assert_lint( } } DocumentFileSource::Html(..) => todo!("HTML analysis is not yet supported"), + DocumentFileSource::Grit(..) => todo!("Grit analysis is not yet supported"), + // Unknown code blocks should be ignored by tests DocumentFileSource::Unknown => {} } From 6ae146585c31b202637da0b511f8229d8f559e5b Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Fri, 20 Sep 2024 16:06:57 -0500 Subject: [PATCH 16/21] Get file handler for grit working --- crates/biome_css_formatter/src/lib.rs | 2 +- crates/biome_grit_formatter/Cargo.toml | 2 +- crates/biome_grit_formatter/src/context.rs | 29 ++++++++++--------- crates/biome_grit_formatter/src/lib.rs | 13 ++++++--- crates/biome_grit_formatter/tests/language.rs | 5 ++-- crates/biome_grit_syntax/src/file_source.rs | 7 +++++ crates/biome_service/src/file_handlers/mod.rs | 6 ++++ .../@biomejs/backend-jsonrpc/src/workspace.ts | 7 ++++- 8 files changed, 49 insertions(+), 22 deletions(-) diff --git a/crates/biome_css_formatter/src/lib.rs b/crates/biome_css_formatter/src/lib.rs index e14de3cef9dd..a9796554c6fd 100644 --- a/crates/biome_css_formatter/src/lib.rs +++ b/crates/biome_css_formatter/src/lib.rs @@ -274,7 +274,7 @@ impl FormatLanguage for CssFormatLanguage { root: &CssSyntaxNode, source_map: Option, ) -> Self::Context { - let comments = Comments::from_node(root, &CssCommentStyle, source_map.as_ref()); + let comments: Comments = Comments::from_node(root, &CssCommentStyle, source_map.as_ref()); CssFormatContext::new(self.options, comments).with_source_map(source_map) } } diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index df966d11b97f..399c4eda2216 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -23,7 +23,7 @@ biome_formatter_test = { path = "../biome_formatter_test" } biome_grit_factory = { path = "../biome_grit_factory" } biome_grit_parser = { path = "../biome_grit_parser" } biome_parser = { path = "../biome_parser" } -biome_service = { path = "../biome_service" } +biome_service = { path = "../biome_service", features = ["experimental-grit"] } countme = { workspace = true, features = ["enable"] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index d40c4d803327..43812f0b78e5 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -1,7 +1,8 @@ use crate::comments::{FormatGritLeadingComment, GritCommentStyle, GritComments}; +use biome_formatter::printer::PrinterOptions; use biome_formatter::{ - AttributePosition, CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, - LineEnding, LineWidth, QuoteStyle, TransformSourceMap, + AttributePosition, BracketSpacing, CstFormatContext, FormatContext, FormatOptions, IndentStyle, + IndentWidth, LineEnding, LineWidth, QuoteStyle, TransformSourceMap, }; use biome_grit_syntax::GritLanguage; use std::fmt::Display; @@ -10,13 +11,15 @@ use std::rc::Rc; #[allow(dead_code)] #[derive(Debug, Clone)] pub struct GritFormatContext { + options: GritFormatOptions, comments: Rc, source_map: Option, } impl GritFormatContext { - pub fn new(comments: GritComments) -> Self { + pub fn new(options: GritFormatOptions, comments: GritComments) -> Self { Self { + options, comments: Rc::new(comments), source_map: None, } @@ -32,11 +35,11 @@ impl FormatContext for GritFormatContext { type Options = GritFormatOptions; fn options(&self) -> &Self::Options { - todo!() + &self.options } fn source_map(&self) -> Option<&TransformSourceMap> { - todo!() + self.source_map.as_ref() } } impl CstFormatContext for GritFormatContext { @@ -47,7 +50,7 @@ impl CstFormatContext for GritFormatContext { type CommentRule = FormatGritLeadingComment; fn comments(&self) -> &biome_formatter::comments::Comments { - todo!() + &self.comments } } @@ -138,30 +141,30 @@ impl Display for GritFormatOptions { impl FormatOptions for GritFormatOptions { fn indent_style(&self) -> IndentStyle { - todo!() + self.indent_style } fn indent_width(&self) -> IndentWidth { - todo!() + self.indent_width } fn line_width(&self) -> LineWidth { - todo!() + self.line_width } fn line_ending(&self) -> LineEnding { - todo!() + self.line_ending } fn attribute_position(&self) -> biome_formatter::AttributePosition { - todo!() + self.attribute_position } fn bracket_spacing(&self) -> biome_formatter::BracketSpacing { - todo!() + BracketSpacing::default() } fn as_print_options(&self) -> biome_formatter::prelude::PrinterOptions { - todo!() + PrinterOptions::from(self) } } diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index c935554e575c..4e04079ee536 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -6,11 +6,13 @@ mod grit; mod prelude; use biome_formatter::{ + comments::Comments, prelude::*, trivia::{format_dangling_comments, format_leading_comments, format_trailing_comments}, write, CstFormatContext, Format, FormatLanguage, FormatResult, Formatted, }; use biome_grit_syntax::{GritLanguage, GritSyntaxNode}; +use comments::GritCommentStyle; pub(crate) use crate::context::GritFormatContext; @@ -120,15 +122,18 @@ impl FormatLanguage for GritFormatLanguage { } fn options(&self) -> &::Options { - todo!() + &self.options } fn create_context( self, - _root: &biome_rowan::SyntaxNode, - _source_map: Option, + root: &biome_rowan::SyntaxNode, + source_map: Option, ) -> Self::Context { - todo!() + let comments: Comments = + Comments::from_node(root, &GritCommentStyle, source_map.as_ref()); + + GritFormatContext::new(self.options, comments).with_source_map(source_map) } } diff --git a/crates/biome_grit_formatter/tests/language.rs b/crates/biome_grit_formatter/tests/language.rs index 752b393ce1e0..24edd18515ba 100644 --- a/crates/biome_grit_formatter/tests/language.rs +++ b/crates/biome_grit_formatter/tests/language.rs @@ -1,5 +1,6 @@ use biome_formatter_test::TestFormatLanguage; use biome_grit_formatter::{context::GritFormatContext, GritFormatLanguage}; +use biome_grit_parser::parse_grit; use biome_grit_syntax::GritLanguage; #[derive(Default)] @@ -10,8 +11,8 @@ impl TestFormatLanguage for GritTestFormatLanguage { type Context = GritFormatContext; type FormatLanguage = GritFormatLanguage; - fn parse(&self, _text: &str) -> biome_parser::AnyParse { - todo!() + fn parse(&self, text: &str) -> biome_parser::AnyParse { + parse_grit(text).into() } fn to_format_language( diff --git a/crates/biome_grit_syntax/src/file_source.rs b/crates/biome_grit_syntax/src/file_source.rs index 7399da2e68ce..969acd768370 100644 --- a/crates/biome_grit_syntax/src/file_source.rs +++ b/crates/biome_grit_syntax/src/file_source.rs @@ -37,6 +37,13 @@ impl GritFileSource { _ => Err(FileSourceError::UnknownExtension), } } + + pub fn try_from_language_id(language_id: &str) -> Result { + match language_id { + "grit" => Ok(Self::grit()), + _ => Err(FileSourceError::UnknownLanguageId), + } + } } impl TryFrom<&Path> for GritFileSource { diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 3998da01e13f..6507d27e855b 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -104,6 +104,12 @@ impl From for DocumentFileSource { } } +impl From for DocumentFileSource { + fn from(value: GritFileSource) -> Self { + Self::Grit(value) + } +} + impl From<&Path> for DocumentFileSource { fn from(path: &Path) -> Self { Self::from_path(path) diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index 7e015cf3f492..f18de1e9eadf 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -2575,7 +2575,8 @@ export type DocumentFileSource = | { Json: JsonFileSource } | { Css: CssFileSource } | { Graphql: GraphqlFileSource } - | { Html: HtmlFileSource }; + | { Html: HtmlFileSource } + | { Grit: GritFileSource }; export interface JsFileSource { /** * Used to mark if the source is being used for an Astro, Svelte or Vue file @@ -2599,6 +2600,9 @@ export interface GraphqlFileSource { export interface HtmlFileSource { variant: HtmlVariant; } +export interface GritFileSource { + variant: GritVariant; +} export type EmbeddingKind = "Astro" | "Vue" | "Svelte" | "None"; export type Language = | "JavaScript" @@ -2625,6 +2629,7 @@ export type CssVariant = "Standard"; */ export type GraphqlVariant = "Standard"; export type HtmlVariant = "Standard" | "Astro"; +export type GritVariant = "Standard"; export interface ChangeFileParams { content: string; path: BiomePath; From e3d7e13f34e813d56f2a8dceadf6f17258cb6308 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sat, 21 Sep 2024 10:51:12 -0500 Subject: [PATCH 17/21] Undo format change --- crates/biome_css_formatter/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_css_formatter/src/lib.rs b/crates/biome_css_formatter/src/lib.rs index a9796554c6fd..e14de3cef9dd 100644 --- a/crates/biome_css_formatter/src/lib.rs +++ b/crates/biome_css_formatter/src/lib.rs @@ -274,7 +274,7 @@ impl FormatLanguage for CssFormatLanguage { root: &CssSyntaxNode, source_map: Option, ) -> Self::Context { - let comments: Comments = Comments::from_node(root, &CssCommentStyle, source_map.as_ref()); + let comments = Comments::from_node(root, &CssCommentStyle, source_map.as_ref()); CssFormatContext::new(self.options, comments).with_source_map(source_map) } } From 1edaa486e3363f317c5af75f42949b9b90735f54 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 22 Sep 2024 20:51:56 -0500 Subject: [PATCH 18/21] Use different test --- crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit | 4 ---- crates/biome_grit_formatter/tests/specs/grit/file_node.grit | 1 + 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit create mode 100644 crates/biome_grit_formatter/tests/specs/grit/file_node.grit diff --git a/crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit b/crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit deleted file mode 100644 index 6582c97bd651..000000000000 --- a/crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit +++ /dev/null @@ -1,4 +0,0 @@ -`function $name ($args) { $body }` as $func where { - $func => `const $name = ($args) => { $body }`, - $args <: contains `apple` => `mango` -} diff --git a/crates/biome_grit_formatter/tests/specs/grit/file_node.grit b/crates/biome_grit_formatter/tests/specs/grit/file_node.grit new file mode 100644 index 000000000000..4273ffb1159b --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/file_node.grit @@ -0,0 +1 @@ +file(body = contains `console.$method` => `println`) \ No newline at end of file From b68ca00376a106dc05f96dc9e7d38f7ec7a43863 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 22 Sep 2024 20:55:59 -0500 Subject: [PATCH 19/21] Update .github/workflows/repository_dispatch.yml Co-authored-by: Carson McManus --- .github/workflows/repository_dispatch.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/repository_dispatch.yml b/.github/workflows/repository_dispatch.yml index 405ca1ee0204..350d124c4f04 100644 --- a/.github/workflows/repository_dispatch.yml +++ b/.github/workflows/repository_dispatch.yml @@ -39,7 +39,7 @@ jobs: run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh - name: Build WASM module for the web - run: wasm-pack build --out-dir ../../packages/@biomejs/wasm-web --target web --profiling --scope biomejs crates/biome_wasm --features experimental-html, experimental-grit + run: wasm-pack build --out-dir ../../packages/@biomejs/wasm-web --target web --profiling --scope biomejs crates/biome_wasm --features experimental-html,experimental-grit # https://github.com/actions/cache/issues/342 - name: Clear old wasm-pack cache From 694993ca378c92abbbe8ce929aae78a1629168d8 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Mon, 23 Sep 2024 07:38:50 -0500 Subject: [PATCH 20/21] Add snapshot test --- .../tests/specs/grit/file_node.grit.snap | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap diff --git a/crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap new file mode 100644 index 000000000000..6af8139fa4cf --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap @@ -0,0 +1,33 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: grit/file_node.grit +--- +# Input + +```grit +file(body = contains `console.$method` => `println`) +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: LF +Line width: 80 +Attribute Position: Auto +----- + +```grit +file(body = contains `console.$method` => `println`)``` + + + +## Unimplemented nodes/tokens + +"file(body = contains `console.$method` => `println`)" => 0..52 From 1f185f265b98debf18c6d69941340d51775d62ed Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Mon, 23 Sep 2024 07:42:39 -0500 Subject: [PATCH 21/21] Allow to ascii lowercase --- crates/biome_grit_syntax/src/file_source.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/biome_grit_syntax/src/file_source.rs b/crates/biome_grit_syntax/src/file_source.rs index 969acd768370..44d61148a248 100644 --- a/crates/biome_grit_syntax/src/file_source.rs +++ b/crates/biome_grit_syntax/src/file_source.rs @@ -59,6 +59,7 @@ impl TryFrom<&Path> for GritFileSource { }; // We assume the file extensions are case-insensitive // and we use the lowercase form of them for pattern matching + #[allow(clippy::disallowed_methods)] Self::try_from_extension(&extension.to_ascii_lowercase()) } }