You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A [Prettier plugin](https://prettier.io/docs/en/plugins.html) for automatically formatting your [Solidity](https://github.com/ethereum/solidity) code.
11
+
> ℹ️ This README is for the new major version of Prettier Plugin Solidity.
12
+
>
13
+
> The differences from v1 are minimal, but there are some breaking changes.
14
+
>
15
+
> To migrate from v1, follow the [migration guide](#migrating-from-v1).
16
+
>
17
+
> If you're looking for the previous version, check out the [v1 branch](https://github.com/prettier-solidity/prettier-plugin-solidity/tree/v1).
12
18
13
-
## Nomic Foundation's Slang
14
-
15
-
Nomic Foundation has put a lot of effort in providing a set of compiler APIs that helped us rethink our approach to parsing and rely on their flexibility, detail oriented solution and continuos support of new and old Solidity syntaxes.
16
-
17
-
Since v2.0.0 this package will ship with the Slang parser and this change must be implemented in existing configurations by replacing `parser: 'antlr'` with `parser: 'slang'`.
18
-
19
-
The `antlr4` parser (`antlr`) is still supported for the time being and will trigger a deprecation warning, since Slang gives us a much more powerful tool, is faster, allowed us to fully transition into typescript (minimizing the introduction of mismatching type bugs), and allows prettier to format the code in a much more decoupled way and position comments with a greater precision.
19
+
A [Prettier](https://prettier.io/) plugin for automatically formatting your [Solidity](https://docs.soliditylang.org/en/latest/) code.
20
20
21
21
## Installation and usage
22
22
@@ -28,15 +28,13 @@ Install both `prettier` and `prettier-plugin-solidity`:
@@ -52,13 +50,11 @@ Or you can use it as part of your linting to check that all your code is prettif
52
50
53
51
### Using in the Browser
54
52
55
-
_Added in v1.1.0_
56
-
57
53
To use this package in the browser, you need to load Prettier's standalone bundle before loading the build provided in this package.
58
54
59
55
Prettier's unpkg field points to `https://unpkg.com/prettier/standalone.js`, in a similar way this plugin points to `https://unpkg.com/prettier-plugin-solidity/dist/standalone.js`.
60
56
61
-
Once the scripts are loaded you will have access the globals `prettier` and `prettierPlugins`.
57
+
Once the scripts are loaded you will have access to the globals `prettier` and `prettierPlugins`.
62
58
63
59
We follow Prettier's strategy for populating their plugins in the object `prettierPlugins`, you can load other plugins like `https://unpkg.com/[email protected]/parser-markdown.js` and Prettier will have access to multiple parsers.
64
60
@@ -83,17 +79,15 @@ We follow Prettier's strategy for populating their plugins in the object `pretti
83
79
</script>
84
80
```
85
81
86
-
For more details and please have a look at [Prettier's documentation](https://prettier.io/docs/en/browser.html).
82
+
For more details, have a look at [Prettier's documentation](https://prettier.io/docs/en/browser.html).
87
83
88
84
### Creating a package for the Browser
89
85
90
-
_Added in v1.2.0_
91
-
92
86
If you are creating your own package to be run in a browser, you might want to import the standalone files directly.
@@ -138,7 +132,7 @@ Most options are described in Prettier's [documentation](https://prettier.io/doc
138
132
139
133
### Compiler
140
134
141
-
Many versions of the Solidity compiler have changes that affect how the code should be formatted. This plugin, by default, tries to format the code in the most compatible way that it's possible, but you can use the `compiler` option to nudge it in the right direction.
135
+
Many versions of the Solidity compiler have changes that affect how the code should be formatted. This plugin, by default, tries to format the code in the most compatible way that is possible, but you can use the `compiler` option to nudge it in the right direction.
142
136
143
137
One example of this is import directives. Before `0.7.4`, the compiler didn't accept multi-line import statements, so we always format them in a single line. But if you use the `compiler` option to indicate that you are using a version greater or equal than `0.7.4`, the plugin will use multi-line imports when it makes sense.
144
138
@@ -218,9 +212,18 @@ You might have a multi-version project, where different files are compiled with
You can configure the parser used by Prettier Solidity. Two Solidity parsers are supported:
222
218
223
-
_Added in v1.3.0_
219
+
-[Slang](https://nomicfoundation.github.io/slang) (the default), a more powerful and correct parser that results in better formatting for some edge cases, especially when comments are involved.
220
+
-[Solidity Parser for JavaScript](https://github.com/solidity-parser/parser/), an [ANTLR](https://www.antlr.org/)-based parser. This is the version that Prettier Solidity v1 used by default. This parser is still supported in v2, but it's deprecated and will be removed in the next major version. You can use this parser by setting `parser: "antlr"` in your Prettier configuration.
We modified the `do` instruction to also install this plugin. Then you'll have to configure the plugin to always use the
266
-
version installed in the vim plugin's directory. The vim-plug directory depends on value you use in `call plug#begin('~/.vim/<dir>')`:
268
+
We modified the `do` instruction to also install this plugin. Then you'll have to configure the plugin to always use the version installed in the vim plugin's directory. The vim-plug directory depends on the value you use in `call plug#begin('~/.vim/<dir>')`:
267
269
268
270
```vim
269
271
let g:prettier#exec_cmd_path = '~/.vim/plugged/vim-prettier/node_modules/.bin/prettier'
@@ -282,34 +284,29 @@ Now Prettier will be run every time the file is saved.
282
284
283
285
### VSCode
284
286
285
-
VSCode is not familiar with the Solidity language.
286
-
There are 2 extensions that you can install to provide support for Solidity:
287
+
VSCode is not familiar with the Solidity language. There are two extensions that you can install to provide support for Solidity:
> :warning:**These 2 extensions offer similar functionality and will clash with each other**: Please choose which one matches your projects better.
298
-
299
298
These extensions provide basic integration with Prettier; in most cases, no further action is needed.
300
299
301
-
Make sure your editor has format on save set to true.
302
-
When you save VSCode will ask you what formatter would you like to use for the Solidity language, you can choose `JuanBlanco.solidity` or `NomicFoundation.hardhat-solidity`.
300
+
Make sure your editor has format-on-save set to true. When you save, VSCode will ask you what formatter would you like to use for the Solidity language. We recommend choosing `NomicFoundation.hardhat-solidity`.
303
301
304
302
At this point VSCode's `settings.json` should have a configuration similar to this:
305
303
306
-
```JSON
304
+
```JSON5
307
305
{
308
306
"editor.formatOnSave":true,
309
307
"solidity.formatter":"prettier", // This is the default so it might be missing.
@@ -320,15 +317,15 @@ If you want more control over other details, you should proceed to install [`pre
320
317
code --install-extension esbenp.prettier-vscode
321
318
```
322
319
323
-
To interact with 3rd party plugins, `prettier-vscode` will look in the project's npm modules, so you'll need to have `prettier` and `prettier-plugin-solidity` in your `package.json`
320
+
To interact with third-party plugins, `prettier-vscode` will look in the project's npm modules, so you'll need to have `prettier` and `prettier-plugin-solidity` in your `package.json`
This will allow you to specify the version of the plugin in case you want to use the latest version of the plugin or need to freeze the formatting since new versions of this plugin will implement tweaks on the possible formats.
330
327
331
-
You'll have to let VSCode what formatter you prefer.
328
+
You'll have to let VSCode know what formatter you prefer.
332
329
This can be done by opening the command palette and executing:
333
330
334
331
```
@@ -351,7 +348,7 @@ Now VSCode's `settings.json` should have this:
351
348
352
349
Note: By design, Prettier prioritizes a local over a global configuration. If you have a `.prettierrc` file in your project, your VSCode's default settings or rules in `settings.json` are ignored ([prettier/prettier-vscode#1079](https://github.com/prettier/prettier-vscode/issues/1079)).
353
350
354
-
### Pnpm
351
+
### pnpm
355
352
356
353
To make Prettier Solidity work in your project, you have to add a `.prettierrc` file as shown [here](#configuration-file).
357
354
@@ -391,6 +388,26 @@ contract Foo is Bar {
391
388
392
389
Notice that the unnecessary parentheses in `modifier2` were removed in the function but not in the constructor.
393
390
391
+
## Migrating from v1
392
+
393
+
In most cases, upgrading to v2 of Prettier Solidity should be as easy as installing the latest version:
394
+
395
+
```bash
396
+
npm install prettier-plugin-solidity@latest
397
+
```
398
+
399
+
If you had the parser explicitly set in your config, you'll get this error:
400
+
401
+
```
402
+
[error] Couldn't resolve parser "solidity-parse".
403
+
```
404
+
405
+
To fix it, simply update the `parser` option in your `.prettierrc`:
0 commit comments