-
-
Notifications
You must be signed in to change notification settings - Fork 475
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
test(format/grit): add tests for grit formatter #3937
Merged
Merged
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
eab141c
Add file handler for grit
branberry dec1fe0
add formatoptions impl for gritformatoptions
branberry f06d937
add grit filehandling
branberry 3ec03df
add tests for grit
branberry aa22b36
undo format changes to cargo.toml
branberry f89f1b7
linting stuff
branberry 1b501d1
Add file instantiation
branberry f87f7e3
Fix last linting issue
branberry f47ed64
Remove unused dependencies
branberry 8e9b92c
Update crates/biome_service/src/file_handlers/mod.rs
branberry 32d088c
Add panic to test
branberry 56b4c70
Add panic to test
branberry 13172aa
Merge branch 'main' into feat/grit-formatter-tests
branberry b1bcdd7
Add file source for grit
branberry 94f3887
Add grit file handleres
branberry 1bdd46c
Merge branch 'main' into feat/grit-formatter-tests
branberry ced5052
Add grit syntax to schema
branberry 6ae1465
Get file handler for grit working
branberry e3d7e13
Undo format change
branberry 1edaa48
Use different test
branberry b68ca00
Update .github/workflows/repository_dispatch.yml
branberry 34e50a7
Merge branch 'main' into feat/grit-formatter-tests
dyc3 694993c
Add snapshot test
branberry 1f185f2
Allow to ascii lowercase
branberry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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!() | ||
} | ||
} |
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,27 @@ | ||
use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile}; | ||
use biome_grit_formatter::{context::GritFormatOptions, GritFormatLanguage}; | ||
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, None) else { | ||
panic!("Failed to set up snapshot test"); | ||
}; | ||
|
||
let options = GritFormatOptions::default(); | ||
let language = language::GritTestFormatLanguage; | ||
|
||
let snapshot = SpecSnapshot::new( | ||
test_file, | ||
test_directory, | ||
language, | ||
GritFormatLanguage::new(options), | ||
); | ||
|
||
snapshot.test() | ||
} |
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,8 @@ | ||
mod spec_test; | ||
|
||
mod formatter { | ||
|
||
mod grit_module { | ||
tests_macros::gen_tests! {"tests/specs/grit/**/*.grit", crate::spec_test::run, ""} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/biome_grit_formatter/tests/specs/grit/as_modifier.grit
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,4 @@ | ||
`function $name ($args) { $body }` as $func where { | ||
$func => `const $name = ($args) => { $body }`, | ||
$args <: contains `apple` => `mango` | ||
} |
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,115 @@ | ||
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 = (); | ||
type LinterSettings = (); | ||
type OrganizeImportsSettings = (); | ||
type FormatOptions = GritFormatOptions; | ||
type ParserSettings = (); | ||
type EnvironmentSettings = (); | ||
fn lookup_settings( | ||
_languages: &crate::settings::LanguageListSettings, | ||
) -> &crate::settings::LanguageSettings<Self> { | ||
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 { | ||
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, | ||
) -> 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<Printed, WorkspaceError> { | ||
let options = settings.format_options::<GritLanguage>(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())), | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that we're adding a file handler for Grit 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad to hear! Haha. I've been following some of the other examples, and I've seen that we typically create a file handler struct as a part of the testing process.
Not sure if I'm doing too much work here, but it sounds like I'm working in the right direction!
As a side note, I'm a Rust newbie so any pointers on how I can improve my Rust code is welcome and appreciated :D