Skip to content

Commit

Permalink
[formatter] Next batch of improvements (#20414)
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka authored Nov 26, 2024
1 parent 6255b64 commit 53387ff
Show file tree
Hide file tree
Showing 17 changed files with 3,390 additions and 2,852 deletions.
9 changes: 9 additions & 0 deletions .changeset/nervous-baboons-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@mysten/prettier-plugin-move': minor
---

- parser rename "move-parser" -> "move"
- adds `prettier-move` bin when installed globally
- better comments handling in empty blocks
- sorts abilities alphabetically, but `key` always first
- no longer inserts block in `if_expression` if expression breaks
4 changes: 2 additions & 2 deletions external-crates/move/tooling/prettier-move/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

### Patch Changes

- e1a85c2: fixes publishing issue, compiles prepublish
- e1a85c2: fixes publishing issue, compiles prepublish

## 0.1.0

### Minor Changes

- 9521492: Initial version of the prettier-plugin-move
- 9521492: Initial version of the prettier-plugin-move
84 changes: 54 additions & 30 deletions external-crates/move/tooling/prettier-move/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,82 @@ certain changes to the parser may break the plugin (e.g., if parse tree node typ

## Prerequisites

In order to use the plugin, you need to install `npm` command (`brew install npm` on a
Mac). You can use the plugin to format Move files (`.move` extension) both on the command line and
using Prettier's VSCode
[extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode). When the
plugin is complete, we will make it available directly from Move's VSCode extension.
Requires [nodejs 18+](https://nodejs.org/en) installed.

## Installation
## Usage (Global, CLI)

The plugin can be installed via npm:
For CLI usage, you can install the plugin globally by running the following command:

```bash
npm i -g prettier @mysten/prettier-plugin-move
```
npm i @mysten/prettier-plugin-move
```

## Usage

Go to the root directory of the Move package whose files you'd like to format (i.e., the directory
containing the Move.toml manifest file for this package) and run the following command:
Then there will be a registered executable `prettier-move` which works exactly like a regular `prettier` one, except that it automatically inserts the path to the plugin as an argument.

```bash
npm install [email protected] "$SUI"/external-crates/move/crates/move-analyzer/prettier-plugin
prettier-move -c sources/example.move # to check
prettier-move -w sources/example.move # to write
```

This will install both the prettier formatter and the plugin in the `./node_modules` directory.
This command is identical to the following:

```bash
prettier --plugin /path/to/local/npm/node_modules/@mysten/prettier-plugin-move/out/index.js -c sources/example.move # to check
prettier --plugin /path/to/local/npm/node_modules/@mysten/prettier-plugin-move/out/index.js -w sources/example.move # to write
```

# Command-line Usage
## Installation (Per-Project)

You can format Move files in the package where you completed the installation [step](#installation) by running the
following command:
If you decide to use the plugin per-project, you can install it in the project's directory. This way,
the plugin will be available via `prettier` call in the project's directory.

```bash
./node_modules/.bin/prettier --plugin=prettier-plugin-move "$PATH_TO_MOVE_FILE"
# install as a dev-dependency
npm i -D prettier @mysten/prettier-plugin-move
```

# VSCode integration
Add the `.prettierrc` or a similar configuration file (see [all supported formats](https://prettier.io/docs/en/configuration.html)):

In order to use the plugin in VSCode you first need to install Prettier's VSCode
[extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode).
```json
{
"printWidth": 100,
"tabWidth": 4,
"useModuleLabel": true,
"autoGroupImports": "module",
"plugins": ["@mysten/prettier-plugin-move"]
}
```

Then, in the root directory of the package where you completed the installation [step](#installation) you need to place the `.prettierrc` file containing the following configuration:
Then you can run prettier either via adding a script to `package.json`:

```
```json
{
"plugins": [
"prettier-plugin-move"
]
"scripts": {
"prettier": "prettier --write ."
}
}
```

After completing these steps, if you open the root directory of the package where you completed the
installation [step](#installation) and choose any of the Move source files in this package, you will
be able to format them by choosing `Format Code` command from VSCode's command palette.
```bash
npm run prettier -w sources/example.move
```

Or, if you have prettier installed globally, you can run it directly:

```bash
prettier --write sources/example.move
```

## VSCode integration

There is a bundled [Move Formatter](https://marketplace.visualstudio.com/items?itemName=mysten.prettier-move) extension for VSCode. It will detect prettier configuration for the workspace and use
the plugin automatically.

Alternatively, if you follow the per-project installation, [regular Pretter extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) should work as well.

## Known Integrations

- Neovim: see [this commit](https://github.com/amnn/nvim/commit/26236dc08162b61f95f689e232a5df2418708339) for configuration

## Contribute

Expand Down
29 changes: 29 additions & 0 deletions external-crates/move/tooling/prettier-move/bin/prettier-move.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env node

// use the
const path = require('path');
const plugin_path = path.resolve(__dirname, '..', 'out', 'index.js');
const child_process = require('child_process');

// command is prettier + plugin path + args passed to the script
const args = process.argv.slice(2);

// check that prettier is installed
try {
child_process.execFileSync('prettier', ['--version']);
} catch (e) {
console.error('Prettier is not installed. Please install it by running `npm install -g prettier`.');
process.exit(1);
}

// run prettier, print the output and exit with correct code
const prettier = child_process.execFile(
'prettier',
args.length ? ['--plugin', plugin_path, ...args] : ['--help'],
);

// additionally, exchange stdin/stdout/stderr with the prettier process
process.stdin.pipe(prettier.stdin);
prettier.stdout.pipe(process.stdout);
prettier.stderr.pipe(process.stderr);
prettier.on('exit', (code) => process.exit(code));
Loading

0 comments on commit 53387ff

Please sign in to comment.