Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions crates/language/src/target_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,48 @@ impl PatternLanguage {
Self::from_string(lang, flavor.as_deref())
}

impl PatternLanguage {
pub fn from_string_or_alias(name: &str, flavor: Option<&str>) -> Option<Self> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xonx4l This should be removed, you don't need both.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!!!

match name {
"py" | "python" => Some(Self::Python),
"js" | "javascript" => match flavor {
Some("jsx") => Some(Self::Tsx),
Some("flow") => Some(Self::Tsx),
Some("FlowComments") => Some(Self::Tsx),
Some("typescript") => Some(Self::TypeScript),
Some("js_do_not_use") => Some(Self::JavaScript),
_ => Some(Self::Tsx),
},
"ts" | "typescript" => Some(Self::Typescript),
"html" => Some(Self::Html),
"css" => Some(Self::Css),
"json" => Some(Self::Json),
"java" => Some(Self::Java),
"cs" | "csharp" => Some(Self::CSharp),
"md" | "markdown" => match flavor {
Some("block") => Some(Self::MarkdownBlock),
Some("inline") => Some(Self::MarkdownInline),
_ => Some(Self::MarkdownInline),
},
"go" => Some(Self::Go),
"rs" | "rust" => Some(Self::Rust),
"rb" | "ruby" => Some(Self::Ruby),
"sol" | "solidity" => Some(Self::Solidity),
"hcl" | "tf" | "terraform" => Some(Self::Hcl),
"yml" | "yaml" => Some(Self::Yaml),
"sql" => Some(Self::Sql),
"vue" => Some(Self::Vue),
"toml" => Some(Self::Toml),
"php" => match flavor {
Some("html") => Some(Self::Php),
Some("only") => Some(self::PhpOnly),
_ => Some(Self::Php),
},
"universal" => Some(Self::Universal),
_ => None,
}
}
Comment on lines 132 to 172
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve the implementation of from_string_or_alias.

The new function from_string_or_alias is well-implemented and covers a comprehensive range of languages and aliases. It effectively centralizes the logic for language identification, which enhances maintainability and code reuse.

Suggestion: Add documentation.
It would be beneficial to add documentation comments to this function to explain its purpose, parameters, and the handling of different cases, especially the use of the flavor parameter.


#[cfg(not(feature = "builtin-parser"))]
pub fn get_language_with_parser(_parser: &mut MarzanoGritParser, _body: &str) -> Option<Self> {
unimplemented!("grit_parser is unavailable when feature flag [builtin-parser] is off.")
Expand All @@ -146,6 +188,7 @@ impl PatternLanguage {
}

pub fn from_string(name: &str, flavor: Option<&str>) -> Option<Self> {
Self::from_string_or_alias(name, flavor)
match name {
"js" => match flavor {
Some("jsx") => Some(Self::Tsx),
Expand Down Expand Up @@ -245,6 +288,7 @@ impl PatternLanguage {
}

pub fn from_extension(extension: &str) -> Option<Self> {
Self::from_string_or_alias(extension, None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call isn't doing anything, I think you probably meant to delegate this entirely?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed the from_extension function which now uses the updated from_string function, which handles both full language names and aliases.

match extension {
"js" | "jsx" | "cjs" | "mjs" => Some(Self::Tsx),
"ts" | "tsx" | "cts" | "mts" => Some(Self::Tsx),
Expand Down