Skip to content
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

No flavor array #36

Merged
merged 9 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions crates/cli_bin/tests/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ fn basic_js_in_vue_apply() -> Result<()> {
let output = apply_cmd.output()?;

// Assert that the command executed successfully
println!("OUTPUT: {:#?}", output);
assert!(
output.status.success(),
"Command didn't finish successfully: {}",
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern/code_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ fn language_specific_snippet(
.child_by_field_name("language")
.ok_or_else(|| anyhow!("missing language of languageSpecificSnippet"))?;
let lang_name = lang_node.utf8_text(src.as_bytes())?.trim().to_string();
let _snippet_lang = TargetLanguage::from_string(&lang_name, &[])
let _snippet_lang = TargetLanguage::from_string(&lang_name, None)
.ok_or_else(|| anyhow!("invalid language: {}", lang_name))?;
let snippet_node = node
.child_by_field_name("snippet")
Expand Down
27 changes: 11 additions & 16 deletions crates/language/src/target_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::{
};
use anyhow::Result;
use enum_dispatch::enum_dispatch;
use itertools::Itertools;
use std::borrow::Cow;
use std::fmt;
use std::hash::Hash;
Expand Down Expand Up @@ -159,14 +158,10 @@ impl PatternLanguage {
.utf8_text(src.as_bytes())
.ok()?;
let lang = lang.trim();
let mut cursor = langdecl.walk();
let flavors = langdecl
.children_by_field_name("flavor", &mut cursor)
.filter(|f| f.is_named())
.map(|f| f.utf8_text(src.as_bytes()).ok())
.collect::<Option<Vec<Cow<str>>>>()?;
let flavors = flavors.iter().map(|s| s.as_ref()).collect_vec();
Self::from_string(lang, &flavors)
let flavor = langdecl
.child_by_field_name("flavor")
.and_then(|f| f.utf8_text(src.as_bytes()).ok());
Self::from_string(lang, flavor.as_deref())
}

#[cfg(not(feature = "builtin-parser"))]
Expand All @@ -188,12 +183,12 @@ impl PatternLanguage {
Self::get_language_with_parser(&mut parser, src)
}

pub fn from_string(name: &str, flavor: &[&str]) -> Option<Self> {
pub fn from_string(name: &str, flavor: Option<&str>) -> Option<Self> {
match name {
"js" => match flavor {
["jsx"] => Some(Self::Tsx),
["typescript"] => Some(Self::TypeScript),
["js_do_not_use"] => Some(Self::JavaScript),
Some("jsx") => Some(Self::Tsx),
Some("typescript") => Some(Self::TypeScript),
Some("js_do_not_use") => Some(Self::JavaScript),
_ => Some(Self::Tsx),
},
"html" => Some(Self::Html),
Expand All @@ -202,8 +197,8 @@ impl PatternLanguage {
"java" => Some(Self::Java),
"csharp" => Some(Self::CSharp),
"markdown" => match flavor {
["block"] => Some(Self::MarkdownBlock),
["inline"] => Some(Self::MarkdownInline),
Some("block") => Some(Self::MarkdownBlock),
Some("inline") => Some(Self::MarkdownInline),
_ => Some(Self::MarkdownInline),
},
"python" => Some(Self::Python),
Expand Down Expand Up @@ -580,7 +575,7 @@ impl TargetLanguage {
PatternLanguage::get_language(src).map(|l| l.try_into().ok())?
}

pub fn from_string(name: &str, flavor: &[&str]) -> Option<Self> {
pub fn from_string(name: &str, flavor: Option<&str>) -> Option<Self> {
PatternLanguage::from_string(name, flavor).map(|l| l.try_into().ok())?
}

Expand Down
2 changes: 1 addition & 1 deletion crates/lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl GritServer {
})?;
let range = convert_lsp_range_to_grit_range(&range, &document.text);
let grit_files = get_grit_files_from_uri(document.uri.as_str(), true).await;
let language = PatternLanguage::from_string(language.as_str(), &[]).unwrap_or_default();
let language = PatternLanguage::from_string(language.as_str(), None).unwrap_or_default();

apply_pattern_body(
&document,
Expand Down
Loading