Skip to content

Instantly share code, notes, and snippets.

@adrianovcar
Created March 15, 2024 10:49
Show Gist options
  • Save adrianovcar/d8f6e3057c9d318286f6b6b2a2cea849 to your computer and use it in GitHub Desktop.
Save adrianovcar/d8f6e3057c9d318286f6b6b2a2cea849 to your computer and use it in GitHub Desktop.
Laravel Pint - VS Code Auto Formatter

I'm assuming you already have installed Laravel Pint on your project, if not, follow the official installation guide

To set up the integration with Laravel Pint, you have 2 options:

  1. VS Code Plugin: Just install Laravel Pint Formatter
  2. Creating VS Code task

Creating a VS Code task

  • Create a .vscode directory in the root of your project;
  • Create a file tasks.json to the .vscode directory
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Pint Auto Formatter",
            "type": "shell",
            "command": "./vendor/bin/pint --dirty",
            "problemMatcher": [],
            "presentation": {
                "reveal": "silent"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

The command above will work on the uncommitted files only. If you want to validate all your files, just remove the --dirty instruction.

Adding a keyboard shortcut (KeyBinding) for running Laravel Pint

  1. Access the Keyboard Shortcuts panel. You can do this by navigating through the menu via FilePreferencesKeyboard Shortcuts or by using the shortcut Ctrl+K Ctrl+S.
  2. Once the panel is open, click on the file icon located at the top to access the JSON file containing the keyboard shortcuts.
  3. With the JSON file open, you will need to insert specific lines of code to define the new keyboard shortcut for Laravel Pint. Unfortunately, the text you provided ends before specifying these lines. However, typically, you would add a JSON object that specifies the command to run Laravel Pint, along with your desired key combination. For example:
[
  {
    "key": "ctrl+shift+l",
    "command": "workbench.action.tasks.runTask",
    "args": "Pint Auto Formatter"
  }
]

Optional

You can define custom rules for your Pint validation. Just create a pint.json file on your project root, here is an example:

{
    "preset": "laravel",
    "rules": {
        "simplified_null_return": true,
        "braces": false,
        "new_with_braces": {
            "anonymous_class": false,
            "named_class": false
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment