Skip to content

Commit 207d3e6

Browse files
catalog: add indoc! example
fix #667
1 parent e1451a9 commit 207d3e6

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

website/catalog/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
3434
* [Avoid Duplicated Exports](/catalog/rust/#avoid-duplicated-exports)
3535
* [Get number of digits in a `usize`](/catalog/rust/#get-number-of-digits-in-a-usize)
3636
* [Beware of char offset when iterate over a string](/catalog/rust/#beware-of-char-offset-when-iterate-over-a-string)
37+
* [Rewrite `indoc!` macro](/catalog/rust/#rewrite-indoc-macro)
3738
* [TypeScript](/catalog/typescript/)
3839
* [Repository of ESLint rules 🔗](https://github.com/ast-grep/eslint/)
3940
* [No `await` in `Promise.all`](/catalog/typescript/#no-await-in-promise-all-array)

website/catalog/rust/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ This page curates a list of example ast-grep rules to check and to rewrite Rust
44

55
<!--@include: ./avoid-duplicated-exports.md-->
66
<!--@include: ./boshen-footgun.md-->
7-
<!--@include: ./get-digit-count-in-usize.md-->
7+
<!--@include: ./get-digit-count-in-usize.md-->
8+
<!--@include: ./rewrite-indoc-macro.md-->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Rewrite `indoc!` macro <Badge type="tip" text="Has Fix" />
2+
3+
4+
* [Playground Link](/playground.html#eyJtb2RlIjoiUGF0Y2giLCJsYW5nIjoicnVzdCIsInF1ZXJ5IjoiaW5kb2MhIHsgciNcIiQkJEFcIiMgfSIsInJld3JpdGUiOiJgJCQkQWAiLCJzdHJpY3RuZXNzIjoicmVsYXhlZCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoicnVsZTogXG4gYW55OlxuIC0gcGF0dGVybjogJFYgPT09ICRTRU5TRVRJVkVXT1JEXG4gLSBwYXR0ZXJuOiAkU0VOU0VUSVZFV09SRCA9PT0gJFZcbmNvbnN0cmFpbnRzOlxuICBTRU5TRVRJVkVXT1JEOlxuICAgIHJlZ2V4OiBwYXNzd29yZCIsInNvdXJjZSI6ImZuIG1haW4oKSB7XG4gICAgaW5kb2MhIHtyI1wiXG4gICAgICAgIC5mb28ge1xuICAgICAgICAgICAgb3JkZXI6IDE7XG4gICAgICAgIH1cbiAgICBcIiN9O1xufSJ9)
5+
6+
### Description
7+
8+
This example, created from [a Tweet](https://x.com/zack_overflow/status/1885065128590401551), shows a refactoring operation being performed on Rust source code. The changes involve removing `indoc!` macro declarations while preserving the CSS-like content within them.
9+
10+
Previously, the same refactor is implemented by a _unreadable monster regex_ in vim syntax.
11+
12+
:::details Click to see the original regex (neovim, btw)
13+
14+
```vimscript
15+
:%s/\v(indoc!|)(| )([|\{)r#"(([^#]+|\n+)+)"#/\4
16+
```
17+
I have to confess that I don't understand this regex even if I use neovim, btw.
18+
19+
Let Claude break it down piece by piece:
20+
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.
34+
35+
:::
36+
37+
<!-- Use pattern in the example. Delete this section if use YAML. -->
38+
### Pattern
39+
40+
```shell
41+
ast-grep --pattern 'indoc! { r#"$$$A"# }' --rewrite '$$$A' sgtest.rs
42+
```
43+
44+
### Example
45+
46+
<!-- highlight matched code in curly-brace {lineNum} -->
47+
```rs {2-6}
48+
fn main() {
49+
indoc! {r#"
50+
.foo {
51+
order: 1;
52+
}
53+
"#};
54+
}
55+
```
56+
57+
### Diff
58+
<!-- use // [!code --] and // [!code ++] to annotate diff -->
59+
```rs
60+
fn main() {
61+
indoc! {r#" // [!code --]
62+
`.foo { // [!code ++]
63+
order: 1;
64+
}
65+
"#}; // [!code --]
66+
`; // [!code ++]
67+
}
68+
```
69+
70+
### Contributed by
71+
[Zack in SF](https://x.com/zack_overflow)

0 commit comments

Comments
 (0)