Skip to content

Commit 49db279

Browse files
fix: correct typo
1 parent 207d3e6 commit 49db279

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

website/catalog/rust/rewrite-indoc-macro.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,34 @@ Previously, the same refactor is implemented by a _unreadable monster regex_ in
1212
:::details Click to see the original regex (neovim, btw)
1313

1414
```vimscript
15-
:%s/\v(indoc!|)(| )([|\{)r#"(([^#]+|\n+)+)"#/\4
15+
:%s/\v(indoc!|)(| )([|\{)r#"(([^#]+|\n+)+)"#/`\4`
1616
```
1717
I have to confess that I don't understand this regex even if I use neovim, btw.
1818

1919
Let Claude break it down piece by piece:
2020

21-
- `:%s/` - This is a vim/sed substitution command
22-
- `\v` - Very magic mode in vim, making special characters active by default
23-
- `(indoc!|)` - Matches either "indoc!" or nothing (optional indoc!)
24-
- `(| )([\{)` - Matches any spaces, parentheses, or curly braces
25-
- `r#"` - Matches the literal raw string prefix in Rust
26-
- `(([^#]+|\n+)+)` - This is the capture group that matches:
27-
- `[^#]+` - Any characters that aren't '#'
28-
- `|\n+` - Or one or more newlines
29-
- The outer `()+` makes it match one or more of these sequences
30-
- `"#` - Matches the raw string suffix
31-
- `/\4` - Replaces the entire match with the contents of the 4th capture group
32-
33-
This regex appears to be designed to extract the content from within Rust raw string literals that may or may not be wrapped in the `indoc!` macro.
21+
- `:%s/` - Vim substitution command for all lines
22+
- `\v` - Very magic mode in vim for simpler regex syntax
23+
- `(indoc!|)` - First capture group: matches either "indoc!" or nothing
24+
- `(| )` - Second capture group: matches either empty string or a space
25+
- `([|\{)` - Third capture group: matches either `[` or `{`
26+
- `r#"` - Matches literal `r#"` (Rust raw string delimiter)
27+
- `(([^#]+|\n+)+)` - Fourth capture group (nested):
28+
- `[^#]+` - One or more non-# characters
29+
- `|\n+` - OR one or more newlines
30+
- Outer `()+` makes this repeat one or more times
31+
- `"#` - Matches the closing raw string delimiter
32+
- \`\4\` - Replaces with the fourth capture group wrapped in backticks
33+
34+
This regex is designed to find Rust raw string literals (possibly wrapped in `indoc!` macro), capture their content, and replace the entire match with just the content wrapped in backticks. It's more precise than my previous explanation and matches the pattern you're showing.
3435

3536
:::
3637

3738
<!-- Use pattern in the example. Delete this section if use YAML. -->
3839
### Pattern
3940

4041
```shell
41-
ast-grep --pattern 'indoc! { r#"$$$A"# }' --rewrite '$$$A' sgtest.rs
42+
ast-grep --pattern 'indoc! { r#"$$$A"# }' --rewrite '`$$$A`' sgtest.rs
4243
```
4344

4445
### Example

0 commit comments

Comments
 (0)