Skip to content

Commit

Permalink
feat: add line-through plugin by improving has_subsegments check
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Nov 30, 2022
1 parent 09625f7 commit 0d92ef5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
15 changes: 10 additions & 5 deletions crates/tailwind-parse-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(drain_filter)]
#![feature(box_patterns)]

use std::cmp::max;
use std::{cmp::max, collections::HashSet};

use convert_case::{Case, Casing};
use proc_macro::TokenStream;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn parser(_attr: TokenStream, input: TokenStream) -> TokenStream {

let mut max_dashes = 0;
let mut subcommands = vec![];
let mut subcommands_rootless = vec![];
let mut has_subsegments = HashSet::new();

let x = root
.variants
Expand Down Expand Up @@ -96,14 +96,17 @@ pub fn parser(_attr: TokenStream, input: TokenStream) -> TokenStream {
} else {
subcommands.push(ident.clone());
if optional.is_none() {
subcommands_rootless.push(kebab.clone());
has_subsegments.insert(kebab.clone());
}
fmt_regular
};

let subcommands = subcommand.variants.iter().map(|v| {
let kebab = format(v, &kebab);
let sub_name = subcommand.ident.clone();
if let Some((l, _)) = kebab.split_once('-') {
has_subsegments.insert(l.to_string());
}
let sub_ident = v.ident.clone();
if optional.is_some() {
quote! {
Expand Down Expand Up @@ -135,6 +138,8 @@ pub fn parser(_attr: TokenStream, input: TokenStream) -> TokenStream {
})
.collect::<Vec<_>>();

let has_subsegments = has_subsegments.iter();

TokenStream::from(quote! {
mod plugin {
#root
Expand All @@ -161,8 +166,8 @@ pub fn parser(_attr: TokenStream, input: TokenStream) -> TokenStream {
}
}

pub fn is_rootless_subcommand(name: &str) -> bool {
[#(#subcommands_rootless),*].contains(&name)
pub fn has_subsegments(name: &str) -> bool {
[#(#has_subsegments),*].contains(&name)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/tailwind-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ mod test {
#[test_case("border-[10px]", Plugin::Border(None), Some(SubjectValue::Css(Css("10px"))) ; "arbitrary css")]
#[test_case("border-[repeat(6,1fr)]", Plugin::Border(None), Some(SubjectValue::Css(Css("repeat(6,1fr)"))) ; "when braces are in arbitrary css")]
#[test_case("border-[min-content min-content]", Plugin::Border(None), Some(SubjectValue::Css(Css("min-content min-content"))) ; "when spaces are in arbitrary css")]
#[test_case("line-through", Plugin::TextDecoration(TextDecoration::LineThrough), None ; "when we have a transparent plugin")]
fn plugin(s: &str, p: Plugin, v: Option<SubjectValue>) {
let (rest, s) = Subject::parse(LocatedSpan::new_extra(s, DUMMY_SP)).unwrap();
let lit = match s {
Expand Down
6 changes: 3 additions & 3 deletions crates/tailwind-parse/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ mod plugin {
impl<'a> Plugin {
/// At a hight level, this algorithm:
///
/// 1. attempts to parse a valid plugin from the input
/// 2. if it fails, checks to see if the input is a 'rootless subcommand' (ie, one where the command on its own is not valid)
/// 1. attempts to parse a plugin from the first segment in the text
/// 2. if it fails, it then checks the rootless map to determine whether it should keep searching
/// 3. if there is a subcommand, it attempts to parse the subcommand
/// 4. if the subcommand fails to parse, it falls back to the first discovered error
///
Expand All @@ -306,7 +306,7 @@ mod plugin {
// step 2
let rest = if let Ok((rest, p)) = cmd && p.has_subcommand() {
rest
} else if cmd.is_err() && let Ok((rest, cmd)) = parse_cmd()(s) && Plugin::is_rootless_subcommand(&cmd) {
} else if cmd.is_err() && let Ok((rest, cmd)) = parse_cmd()(s) && Plugin::has_subsegments(&cmd) {
rest
} else {
// this is not a subcommand, return early
Expand Down

0 comments on commit 0d92ef5

Please sign in to comment.