-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Detect classes inside Elixir charlist, word list, and string sigils #18432
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
Merged
+161
−6
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9edd5d
Add Elixir pre-processor
thecrypticace fcc6722
Run `cargo fmt`
thecrypticace 666c81f
Update changelog
thecrypticace aea72d9
Apply suggestions from code review
thecrypticace 21196ed
Run `cargo clippy`
thecrypticace bc29cd8
Add tests
thecrypticace 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
This file contains hidden or 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 hidden or 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 hidden or 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,150 @@ | ||
| use std::str::from_utf8; | ||
|
|
||
| use crate::cursor; | ||
| use crate::extractor::bracket_stack::BracketStack; | ||
| use crate::extractor::pre_processors::pre_processor::PreProcessor; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Elixir; | ||
|
|
||
| impl PreProcessor for Elixir { | ||
| fn process(&self, content: &[u8]) -> Vec<u8> { | ||
| let mut cursor = cursor::Cursor::new(&content); | ||
thecrypticace marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut result = content.to_vec(); | ||
| let mut bracket_stack = BracketStack::default(); | ||
|
|
||
| while cursor.pos < content.len() { | ||
| // Look for a sigil marker | ||
| if cursor.curr != b'~' { | ||
| cursor.advance(); | ||
| continue; | ||
| } | ||
|
|
||
| // Scan charlists, strings, and wordlists | ||
| if !matches!(cursor.next, b'c' | b'C' | b's' | b'S' | b'w' | b'W') { | ||
| cursor.advance(); | ||
| continue; | ||
| } | ||
|
|
||
| cursor.advance_twice(); | ||
|
|
||
| // Match the opening for a sigil | ||
| if !matches!(cursor.curr, b'(' | b'[' | b'{') { | ||
| continue; | ||
| } | ||
|
|
||
| // Replace the opening bracket with a space | ||
| result[cursor.pos] = b' '; | ||
|
|
||
| // Scan until we find a balanced closing one and replace it too | ||
| bracket_stack.push(cursor.curr); | ||
|
|
||
| while cursor.pos < content.len() { | ||
| cursor.advance(); | ||
|
|
||
| match cursor.curr { | ||
| // Escaped character, skip ahead to the next character | ||
| b'\\' => cursor.advance_twice(), | ||
| b'(' | b'[' | b'{' => { | ||
| bracket_stack.push(cursor.curr); | ||
| } | ||
| b')' | b']' | b'}' if !bracket_stack.is_empty() => { | ||
| bracket_stack.pop(cursor.curr); | ||
|
|
||
| if bracket_stack.is_empty() { | ||
| // Replace the closing bracket with a space | ||
| result[cursor.pos] = b' '; | ||
| break; | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| result | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::Elixir; | ||
| use crate::extractor::pre_processors::pre_processor::PreProcessor; | ||
|
|
||
| #[test] | ||
| fn test_elixir_pre_processor() { | ||
| for (input, expected) in [ | ||
| // Simple sigils | ||
| ("~W(flex underline)", "~W flex underline "), | ||
| ("~W[flex underline]", "~W flex underline "), | ||
| ("~W{flex underline}", "~W flex underline "), | ||
| // Sigils with nested brackets | ||
| ( | ||
| "~W(text-(--my-color) bg-(--my-color))", | ||
| "~W text-(--my-color) bg-(--my-color) ", | ||
| ), | ||
| ("~W[text-[red] bg-[red]]", "~W text-[red] bg-[red] "), | ||
| // Word sigils with modifiers | ||
| ("~W(flex underline)a", "~W flex underline a"), | ||
| ("~W(flex underline)c", "~W flex underline c"), | ||
| ("~W(flex underline)s", "~W flex underline s"), | ||
| // Other sigil types | ||
| ("~w(flex underline)", "~w flex underline "), | ||
| ("~c(flex underline)", "~c flex underline "), | ||
| ("~C(flex underline)", "~C flex underline "), | ||
| ("~s(flex underline)", "~s flex underline "), | ||
| ("~S(flex underline)", "~S flex underline "), | ||
| ] { | ||
| Elixir::test(input, expected); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_extract_candidates() { | ||
| let input = r#" | ||
| ~W(c1 c2) | ||
| ~W[c3 c4] | ||
| ~W{c5 c6} | ||
| ~W(text-(--c7) bg-(--c8)) | ||
| ~W[text-[c9] bg-[c10]] | ||
| ~W(c13 c14)a | ||
| ~W(c15 c16)c | ||
| ~W(c17 c18)s | ||
| ~w(c19 c20) | ||
| ~c(c21 c22) | ||
| ~C(c23 c24) | ||
| ~s(c25 c26) | ||
| ~S(c27 c28) | ||
| "#; | ||
|
|
||
| Elixir::test_extract_contains( | ||
| input, | ||
| vec![ | ||
| "c1", | ||
| "c2", | ||
| "c3", | ||
| "c4", | ||
| "c5", | ||
| "c6", | ||
| "text-(--c7)", | ||
| "bg-(--c8)", | ||
| "c13", | ||
| "c14", | ||
| "c15", | ||
| "c16", | ||
| "c17", | ||
| "c18", | ||
| "c19", | ||
| "c20", | ||
| "c21", | ||
| "c22", | ||
| "c23", | ||
| "c24", | ||
| "c25", | ||
| "c26", | ||
| "c27", | ||
| "c28", | ||
| ], | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.