Skip to content

Commit 0a93c31

Browse files
authored
Add config to ignore formatting (#72)
* feat: add config to ignore formatting * docs: add config to ignore formatting * docs: improve & refactor * docs: align table * docs: update formatting * test: add test for ignore formatting * refactor: Refactor ERB Formatter/Beautify tests * refactor: Update HtmlBeautifierProvider to format the entire document * test: refactor * refactor: Add return types to configuration change functions * refactor: Update HtmlBeautifier class to use public logChannel * refactor: Improve error handling in HtmlBeautifier class * refactor: Improve error handling in HtmlBeautifier class * refactor: Simplify error handling in HtmlBeautifier class * refactor: Simplify HtmlBeautifier command execution
1 parent 9ca0ecc commit 0a93c31

7 files changed

+261
-209
lines changed

README.md

+75-34
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,114 @@
44
 
55
[![Release](https://github.com/aliariff/vscode-erb-beautify/actions/workflows/release.yaml/badge.svg)](https://github.com/aliariff/vscode-erb-beautify/actions/workflows/release.yaml)
66

7-
Most of solution that exist in the internet is tell you to create a task and call it using ctrl-shift-p menu.
7+
## Overview
88

9-
This extension basically using [htmlbeautifier](https://github.com/threedaymonk/htmlbeautifier) to format your file using the Formatter API from the vscode, so no need to create a hack using Task, etc.
9+
The **ERB Formatter/Beautify** extension for Visual Studio Code uses the [htmlbeautifier](https://github.com/threedaymonk/htmlbeautifier) gem to format ERB files, providing a seamless experience with the VS Code Formatter API. Unlike other solutions that require setting up tasks or manual formatting, this extension integrates directly with VS Code to format your ERB files automatically.
1010

1111
## Features
1212

13+
- Formats ERB files using `htmlbeautifier`.
14+
- Works with the VS Code Formatter API for a native experience.
15+
- Supports custom configuration options for indentation, blank lines, and error handling.
16+
- Ability to ignore specific files or patterns.
17+
1318
![Demo GIF](./images/demo.gif)
1419

15-
## Requirements
20+
## Installation
1621

17-
```
22+
### Requirements
23+
24+
To use this extension, you must have `htmlbeautifier` installed on your system. You can install it globally or add it to your project's Gemfile.
25+
26+
#### Install Globally
27+
28+
```bash
1829
gem install htmlbeautifier
1930
```
2031

21-
or
32+
#### Install with Bundler
2233

23-
To use the gem with Bundler, add to your `Gemfile`:
34+
Add the following to your `Gemfile`:
2435

2536
```ruby
2637
gem 'htmlbeautifier'
2738
```
2839

29-
NOTE: For you that have a filename with extension `.html.erb`, your file might be recognized as `html` file, not as `erb` file. In that case, add a setting in your `settings.json` like below:
40+
Then run:
41+
42+
```bash
43+
bundle install
44+
```
45+
46+
## Configuration
47+
48+
### Resolving File Recognition Issues
49+
50+
If `.html.erb` files are recognized as HTML instead of ERB, add the following to your `settings.json` to associate `.html.erb` files with the ERB language:
51+
52+
```json
53+
"files.associations": {
54+
"*.html.erb": "erb"
55+
}
56+
```
57+
58+
### Setting Default Formatter and Enabling Format-on-Save
59+
60+
To set the default formatter for ERB files and enable format-on-save, add the following to your `settings.json`:
3061

3162
```json
3263
"[erb]": {
3364
"editor.defaultFormatter": "aliariff.vscode-erb-beautify",
3465
"editor.formatOnSave": true
35-
},
36-
"files.associations": {
37-
"*.html.erb": "erb"
3866
}
3967
```
4068

41-
## Known Issues
69+
This ensures that the extension formats ERB files automatically whenever they are saved.
4270

43-
- `invalid byte sequence in US-ASCII`
71+
### Disabling Formatting for Specific Files
4472

45-
Add below setting. [Reference](https://github.com/aliariff/vscode-erb-beautify/issues/47)
73+
To disable formatting for specific ERB files, such as email templates, use the `ignoreFormatFilePatterns` setting. Add the following to your `settings.json`:
4674

47-
```json
48-
"vscode-erb-beautify.customEnvVar": {
49-
"LC_ALL": "en_US.UTF-8"
50-
}
51-
```
75+
```json
76+
"vscode-erb-beautify.ignoreFormatFilePatterns": ["**/email_templates/**/*.erb"]
77+
```
5278

53-
## Settings
79+
This configuration ignores all `.erb` files inside the `email_templates` directory.
5480

55-
| Setting | Description | Default |
56-
| ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------- | -------------- |
57-
| `vscode-erb-beautify.executePath` | Path to the htmlbeautifier executable, set this to absolute path when you have different htmlbeautifier location | htmlbeautifier |
58-
| `vscode-erb-beautify.useBundler` | Execute htmlbeautifier using bundler (ie 'bundle exec htmlbeautifier'). If this true, vscode-erb-beautify.executePath is ignored | false |
59-
| `vscode-erb-beautify.bundlerPath` | Path to the bundler executable, set this to absolute path when you have different bundler location | bundle |
60-
| `vscode-erb-beautify.tabStops` | Set number of spaces per indent | 2 |
61-
| `vscode-erb-beautify.tab` | Indent using tabs | false |
62-
| `vscode-erb-beautify.indentBy` | Indent the output by NUMBER steps | 0 |
63-
| `vscode-erb-beautify.stopOnErrors` | Stop when invalid nesting is encountered in the input | false |
64-
| `vscode-erb-beautify.keepBlankLines` | Set number of consecutive blank lines | 0 |
65-
| `vscode-erb-beautify.customEnvVar` | Custom environment variables to pass to the htmlbeautifier | {} |
81+
### Fixing Encoding Issues
6682

67-
## References
83+
If you encounter the `Invalid byte sequence in US-ASCII` error, add the following setting to your `settings.json` to set the correct locale:
6884

69-
[Issue](https://github.com/threedaymonk/htmlbeautifier/issues/49)
85+
```json
86+
"vscode-erb-beautify.customEnvVar": {
87+
"LC_ALL": "en_US.UTF-8"
88+
}
89+
```
90+
91+
For more details, see the [related issue](https://github.com/aliariff/vscode-erb-beautify/issues/47).
7092

71-
[Issue](https://github.com/rubyide/vscode-ruby/issues/56)
93+
## Settings
94+
95+
Below is a list of settings you can configure in your `settings.json` file:
96+
97+
| Setting | Description | Default |
98+
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ---------------- |
99+
| `vscode-erb-beautify.executePath` | Path to the `htmlbeautifier` executable. Set this to an absolute path if `htmlbeautifier` is installed in a non-standard location. | `htmlbeautifier` |
100+
| `vscode-erb-beautify.useBundler` | Execute `htmlbeautifier` using Bundler (e.g., `bundle exec htmlbeautifier`). If true, `vscode-erb-beautify.executePath` is ignored. | `false` |
101+
| `vscode-erb-beautify.bundlerPath` | Path to the Bundler executable. Set this to an absolute path if Bundler is installed in a non-standard location. | `bundle` |
102+
| `vscode-erb-beautify.tabStops` | Number of spaces per indent. | `2` |
103+
| `vscode-erb-beautify.tab` | Indent using tabs instead of spaces. | `false` |
104+
| `vscode-erb-beautify.indentBy` | Indent the output by a specified number of steps. | `0` |
105+
| `vscode-erb-beautify.stopOnErrors` | Stop formatting when invalid nesting is encountered in the input. | `false` |
106+
| `vscode-erb-beautify.keepBlankLines` | Number of consecutive blank lines to keep in the formatted output. | `0` |
107+
| `vscode-erb-beautify.customEnvVar` | Custom environment variables to pass to `htmlbeautifier`. | `{}` |
108+
| `vscode-erb-beautify.ignoreFormatFilePatterns` | Glob patterns for files to ignore during formatting. | `[]` |
109+
110+
## References
72111

73-
[Ref](https://medium.com/@costa.alexoglou/enable-formatting-with-erb-files-in-vscode-d4b4ff537017)
112+
- [Issue on `htmlbeautifier`](https://github.com/threedaymonk/htmlbeautifier/issues/49)
113+
- [VS Code Ruby Extension Issue](https://github.com/rubyide/vscode-ruby/issues/56)
114+
- [Enable Formatting with ERB Files in VS Code](https://medium.com/@costa.alexoglou/enable-formatting-with-erb-files-in-vscode-d4b4ff537017)
74115

75116
## Credits
76117

package-lock.json

+20-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@
8282
"type": "object",
8383
"default": {},
8484
"description": "Custom environment variables to pass to the htmlbeautifier"
85+
},
86+
"vscode-erb-beautify.ignoreFormatFilePatterns": {
87+
"type": "array",
88+
"default": [],
89+
"items": {
90+
"type": "string"
91+
},
92+
"description": "Ignore formatting for files matching these patterns"
8593
}
8694
}
8795
}
@@ -97,6 +105,7 @@
97105
"devDependencies": {
98106
"@semantic-release/changelog": "^6.0.3",
99107
"@semantic-release/git": "^10.0.1",
108+
"@types/micromatch": "^4.0.9",
100109
"@types/mocha": "^10.0.7",
101110
"@types/node": "20.x",
102111
"@types/vscode": "^1.80.0",
@@ -110,6 +119,7 @@
110119
"typescript": "^5.4.5"
111120
},
112121
"dependencies": {
113-
"is-wsl": "^2.2.0"
122+
"is-wsl": "^2.2.0",
123+
"micromatch": "^4.0.8"
114124
}
115125
}

0 commit comments

Comments
 (0)