How to Add Laravel Pint to VS Code Formatter
#code-quality #vscode #laravel-pint #laravel

How to Add Laravel Pint to VS Code Formatter

Table Of Content


  1. What is laravel pint
  2. Installation
  3. General Usage
  4. Configuration
  5. Binding laravel pint with VS Code
  6. Creating VS Code task for laravel pint
  7. Adding KeyBinding (Keyboard Shortcut) to Run laravel pint
  8. The Extension
  9. Conclusion

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.

Installation

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.

./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.

Sample pint.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
{
    "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.
[
  {
    "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

Show Some Support by staring the repo

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.

Most Viewed Posts


Must Read


January 28, 2022

#AppImage #linux

How to Add AppImage Application to Menu in Ubuntu (Linux)

AppImages are the most easiest to run compared.The challenging task is to make the AppImage accessible globally through he system like an installed application (in the menu)

Reading time: less than a minute
March 24, 2021

#express js #node #node js #sequelize #mysql

How to create model association in Sequelize - express JS / Node JS

In the very initial days of my work in Node JS if faced this issue. Though Sequelize provide a great ORM for MySQL but the association within the models is a bit tricky.

Reading time: 2 minutes
December 14, 2021

#database #javascript #sequelize #npx

Sequelize CLI Commands

list of Sequelize CLI commands which comes handy, while working on a project.

Reading time: 1 minute
How to create a progressive web app (PWA) in laravel 5.* and above
July 07, 2019

#functionality #pwa #javascript #laravel

How to create a progressive web app (PWA) in laravel 5.* and above

The progressive web app is the new trend as well as the need for the present time. I hope before getting in the “HOW” part you know what is exactly a PWA

Reading time: 4 minutes
November 02, 2021

#laravel #tricks #eloquent

Approach to Handle Complex queries in laravel

I am familiar with the love of Eloquent among the laravel community but sometimes, sometimes its better to go for raw queries. Its saves you a lot of time and trouble.

Reading time: 1 minute
How to create a progressive web app (PWA) in laravel 5.* and above
July 07, 2019

#functionality #pwa #javascript #laravel

How to create a progressive web app (PWA) in laravel 5.* and above

The progressive web app is the new trend as well as the need for the present time. I hope before getting in the “HOW” part you know what is exactly a PWA

Reading time: 4 minutes
November 02, 2021

#laravel #tricks #eloquent

Approach to Handle Complex queries in laravel

I am familiar with the love of Eloquent among the laravel community but sometimes, sometimes its better to go for raw queries. Its saves you a lot of time and trouble.

Reading time: 1 minute
September 06, 2019

#cli #authentication #laravel

PHP artisan make:auth in laravel 6.0

As you may have known by now that laravel 6.0 has been out. Though it's expected to have a lot of changes in the major release but in 6.0 there are not that many changes.

Reading time: less than a minute
March 02, 2020

#hosting #deployment #laravel

Checklist for Production Deployment laravel Project

below are mentioned things to be taken care of at the time of deploying a laravel project to production.

Reading time: less than a minute
Rare and Extra Ordinary must have VS Code Extensions
June 06, 2022

#extensions #vscode

Rare and Extra Ordinary must have VS Code Extensions

NO, this is not another list of same old same old list of extensions.

Reading time: less than a minute
mohammed-samgan-khan

Hi, I am Samgan, I know the adds are bit annoying but i hope you understand. Talking about me, I excel at solving complex problems involving logic and step by step breakdown of the problem. Besides, to develop complex algorithms, I specialize in problem framing, systems design, and product development strategy. Sometimes I also enjoy public speaking.