How to Add Laravel Pint to VS Code Formatter
Tags: laravel, laravelpint, vscode, formatter
Published: 2022-06-24 19:20:40

## What is laravel pint

as in the defination by the official repository,

> Laravel Pint is an opinionated PHP code style fixer for minimalists. Pint is built on top of PHP-CS-Fixer and makes it simple to ensure that your code style stays clean and consistent.
> -- laravel/pint

and as per me, Pint is the so far most sophisticated code formatter i have ever user in the reference of PHP. You can find more details about [laravel/pint here.](https://github.com/laravel/pint)

## Installation
```php
composer require laravel/pint --dev
```

## General Usage

to use the pint binary all you have to do is run the following command in the root of you laravel project.

```php
./vendor/bin/pint
```

## Configuration

The default setup of laravel pint does not require any configuration and as per the official docs, it for PSR-12 Style Guide. Still if you are unhappy with some default rules you can change then by creating a pint.json in root of your project, and adding the preset to it.

Currently the following preset are supported

1. psr12
2. laravel
3. symfony

Since Pint is based on PHP-CS-Fixer the configuration rules can be found [here](https://mlocati.github.io/php-cs-fixer-configurator/#version:3.8).

### Sample pint.json
```json
{
    "preset": "symfony",
    "rules": {
        "concat_space": {
            "spacing": "one"
        }
    }
}
```

## Binding laravel pint with VS Code

So far we are good to use laravel/pint, but opening terminal and running a binary everytime you need to format id one hell of a job. To reduce that effort we will bind laravel/pint with our VS Code with the help of VS Code Task

## Creating VS Code task for laravel pint

1. To create a task create a `.vscode directory in root `of you project, if you don't have one.
2. Add a `new file named as tasks.json` to .vscode directory
3. add the following content to the new created file
```json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Pint Formatter",
            "type": "shell",
            "command": "./vendor/bin/pint",
            "problemMatcher": [],
            "presentation": {
                "reveal": "silent"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
```

You can run this task directly from the `Tremial -> Run Tasks` from the navigation, but will do it a bit more simpler.

## Adding KeyBinding (Keyboard Shortcut) to Run laravel pint

1. Open Keyboard shortcut panel, either from `file -> preferences -> keyboard Shortcuts` or `Ctrl+k Ctrl+s`
2. Ones open click on the file icon on the top to open the json file of the keyboard shortcuts.
3. Ones open add the following lines to the file.

```json
[
  {
    "key": "ctrl+shift+l",
    "command": "workbench.action.tasks.runTask",
    "args": "Pint Formatter"
  }
]
```

Now all you have to do is hit `Ctrl + Shift + l` and your laravel project will formatted with laravel pint.

## The Extension

If you are more driven toward the extension approach you can get the above integration by using the following extension.

[Vscode Laravel Pint Extension
](https://marketplace.visualstudio.com/items?itemName=msamgan.laravel-pint-vscode&ssr=false#overview)

[show some support bby staring the repo](https://github.com/msamgan/vscode-larapint)

## Conclusion

I have been using the unsatisfactory formatters from a long time. All of them have one thing but lack other. Laravel Pint is one who has it all, combining it with vscode is like proving you laravel project a super power.

Feel free to pick your brain in comments.