Skip to content

Commit

Permalink
catalog: add indoc! example
Browse files Browse the repository at this point in the history
fix #667
  • Loading branch information
HerringtonDarkholme committed Feb 1, 2025
1 parent e1451a9 commit 207d3e6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions website/catalog/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
* [Avoid Duplicated Exports](/catalog/rust/#avoid-duplicated-exports)
* [Get number of digits in a `usize`](/catalog/rust/#get-number-of-digits-in-a-usize)
* [Beware of char offset when iterate over a string](/catalog/rust/#beware-of-char-offset-when-iterate-over-a-string)
* [Rewrite `indoc!` macro](/catalog/rust/#rewrite-indoc-macro)
* [TypeScript](/catalog/typescript/)
* [Repository of ESLint rules 🔗](https://github.com/ast-grep/eslint/)
* [No `await` in `Promise.all`](/catalog/typescript/#no-await-in-promise-all-array)
Expand Down
3 changes: 2 additions & 1 deletion website/catalog/rust/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ This page curates a list of example ast-grep rules to check and to rewrite Rust

<!--@include: ./avoid-duplicated-exports.md-->
<!--@include: ./boshen-footgun.md-->
<!--@include: ./get-digit-count-in-usize.md-->
<!--@include: ./get-digit-count-in-usize.md-->
<!--@include: ./rewrite-indoc-macro.md-->
71 changes: 71 additions & 0 deletions website/catalog/rust/rewrite-indoc-macro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## Rewrite `indoc!` macro <Badge type="tip" text="Has Fix" />


* [Playground Link](/playground.html#eyJtb2RlIjoiUGF0Y2giLCJsYW5nIjoicnVzdCIsInF1ZXJ5IjoiaW5kb2MhIHsgciNcIiQkJEFcIiMgfSIsInJld3JpdGUiOiJgJCQkQWAiLCJzdHJpY3RuZXNzIjoicmVsYXhlZCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoicnVsZTogXG4gYW55OlxuIC0gcGF0dGVybjogJFYgPT09ICRTRU5TRVRJVkVXT1JEXG4gLSBwYXR0ZXJuOiAkU0VOU0VUSVZFV09SRCA9PT0gJFZcbmNvbnN0cmFpbnRzOlxuICBTRU5TRVRJVkVXT1JEOlxuICAgIHJlZ2V4OiBwYXNzd29yZCIsInNvdXJjZSI6ImZuIG1haW4oKSB7XG4gICAgaW5kb2MhIHtyI1wiXG4gICAgICAgIC5mb28ge1xuICAgICAgICAgICAgb3JkZXI6IDE7XG4gICAgICAgIH1cbiAgICBcIiN9O1xufSJ9)

### Description

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.

Previously, the same refactor is implemented by a _unreadable monster regex_ in vim syntax.

:::details Click to see the original regex (neovim, btw)

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

Let Claude break it down piece by piece:

- `:%s/` - This is a vim/sed substitution command
- `\v` - Very magic mode in vim, making special characters active by default
- `(indoc!|)` - Matches either "indoc!" or nothing (optional indoc!)
- `(| )([\{)` - Matches any spaces, parentheses, or curly braces
- `r#"` - Matches the literal raw string prefix in Rust
- `(([^#]+|\n+)+)` - This is the capture group that matches:
- `[^#]+` - Any characters that aren't '#'
- `|\n+` - Or one or more newlines
- The outer `()+` makes it match one or more of these sequences
- `"#` - Matches the raw string suffix
- `/\4` - Replaces the entire match with the contents of the 4th capture group

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.

:::

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

```shell
ast-grep --pattern 'indoc! { r#"$$$A"# }' --rewrite '$$$A' sgtest.rs
```

### Example

<!-- highlight matched code in curly-brace {lineNum} -->
```rs {2-6}
fn main() {
indoc! {r#"
.foo {
order: 1;
}
"#};
}
```

### Diff
<!-- use // [!code --] and // [!code ++] to annotate diff -->
```rs
fn main() {
indoc! {r#" // [!code --]
`.foo { // [!code ++]
order: 1;
}
"#}; // [!code --]
`; // [!code ++]
}
```

### Contributed by
[Zack in SF](https://x.com/zack_overflow)

0 comments on commit 207d3e6

Please sign in to comment.