ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Js​Prettier

by jonlabelle ST2/ST3

JsPrettier is a Sublime Text Plug-in for Prettier, the opinionated code formatter.

Details

Installs

  • Total 11K
  • Win 3K
  • OS X 6K
  • Linux 1K
Aug 20 Aug 19 Aug 18 Aug 17 Aug 16 Aug 15 Aug 14 Aug 13 Aug 12 Aug 11 Aug 10 Aug 9 Aug 8 Aug 7 Aug 6 Aug 5 Aug 4 Aug 3 Aug 2 Aug 1 Jul 31 Jul 30 Jul 29 Jul 28 Jul 27 Jul 26 Jul 25 Jul 24 Jul 23 Jul 22 Jul 21 Jul 20 Jul 19 Jul 18 Jul 17 Jul 16 Jul 15 Jul 14 Jul 13 Jul 12 Jul 11 Jul 10 Jul 9 Jul 8 Jul 7 Jul 6
Windows 4 16 24 29 20 23 23 8 13 23 23 38 32 22 24 18 24 34 25 24 25 13 6 16 23 33 26 20 11 6 19 24 25 37 34 10 17 35 28 28 24 33 13 15 16 23
OS X 11 26 57 38 61 51 49 24 32 77 77 124 55 53 16 28 42 56 75 55 47 24 33 48 49 64 57 48 14 25 47 48 55 52 45 25 30 55 42 65 51 55 23 21 55 60
Linux 5 4 17 20 13 6 7 5 9 13 20 25 11 11 9 8 9 13 15 11 14 14 9 12 8 8 8 7 3 4 12 12 15 17 13 10 5 18 11 15 12 6 4 7 13 9

Readme

Source
raw.​githubusercontent.​com

JsPrettier

Build Status Build status Downloads

JsPrettier is a Sublime Text Plug-in for Prettier, the opinionated code formatter.

Watch a Quick Demo


Table of Contents

Installation

JsPrettier is compatible with both Sublime Text 2 and 3, and all supported Operating Systems.

Requirements

JsPrettier requires the following programs to be installed prior to use:

Install Prettier

If you installed Prettier globally (using the yarn or npm command below), there is nothing else you need to do.

# using yarn:
yarn global add prettier

# using npm:
npm install -g prettier

Install JsPrettier via Package Control

The easiest and recommended way to install Js​Prettier is using Package Control.

From the main application menu, navigate to:

  • Tools -> Command Palette... -> Package Control: Install Package, type the word JsPrettier, then select it to complete the installation.

Install JsPrettier Manually

  1. Download and extract Js​Prettier zip file to your Sublime Text Packages directory.
  2. Rename the extracted directory from SublimeJsPrettier-master to JsPrettier.

Default Sublime Text Packages Paths:

  • OS X: ~/Library/Application Support/Sublime Text [2|3]/Packages
  • Linux: ~/.Sublime Text [2|3]/Packages
  • Windows: %APPDATA%/Sublime Text [2|3]/Packages

NOTE Replace the [2|3] part with the appropriate Sublime Text version for your installation.

Install JsPrettier Using Git

Alternatively, if you're a Git user, you can install JsPrettier and keep it up-to-date by cloning the repository directly into your Sublime Text Packages directory.

You can locate your Sublime Text Packages directory by using the menu item Preferences -> Browse Packages...

git clone https://github.com/jonlabelle/SublimeJsPrettier.git "JsPrettier"

Usage

To run the JsPrettier command… open the Sublime Text Command Palette (super + shift + p) and type JsPrettier: Format Code.

You can also right-click anywhere in the file to bring up the Context Menu and select JsPrettier Format Code.

Command Scope

JsPrettier will attempt to format selections of code first, otherwise the entire file will be formatted.

NOTE: When the auto_format_on_save setting is set to true, the entire file will always be formatted.

Custom Key Binding

To add a custom key binding to JsPrettier, please reference the following example which binds js_prettier to ctrl/cmd + b.

{ "keys": ["super+b"], "command": "js_prettier" }

Settings

All Prettier options are configurable from the JsPrettier.sublime-settings file, accessible from the Preferences > Package Settings > JsPrettier menu shortcut.

Sublime Text Settings

  • debug (default: false)
    When enabled (true), additional debugging information about the command and configured settings will be printed to the Sublime Text Console; useful for troubleshooting purposes.

  • prettier_cli_path (default: empty)
    It's recommended to leave this setting empty (the default). However, if Sublime Text has problems resolving the CLI path to the Prettier executable, you can explicitly set the appropriate path here.

    When the setting is left empty, the path is resolved by searching locations in the following order, returning the first matched path:

    • Locally installed Prettier, relative to the Sublime Text Project file root directory, e.g.: node_modules/.bin/prettier.
    • The user's home directory, e.g.: $HOME/node_modules/.bin/prettier.
    • Look in the JsPrettier Sublime Text plug-in directory for node_modules/.bin/prettier.
    • Finally, check if Prettier is installed globally.

    nvm users are required to set an appropriate absolute prettier_cli_path (and absolute node_path); according to the target runtime environment.

  • node_path (default: empty)
    It's recommended to leave this setting empty (the default). However, if Sublime Text has problems resolving the absolute path to the node executable, you can explicitly set the appropriate path here.

    nvm users are required to set an appropriate absolute node_path (and absolute prettier_cli_path); according to the target runtime environment.

  • auto_format_on_save (default: false)
    Whether or not to automatically format on every file save.

  • auto_format_on_save_excludes (default: [“*/node_modules/*”, “*/.git/*”])
    Ignore auto formatting when the target file, or its path resides in a particular location, and when auto_format_on_save is turned on.

    Example:

    [
        "*/node_modules/*",
        "*/file.js",
        "*.json"
    ]
    
  • allow_inline_formatting (default: false)
    Enables the ability to format selections of in-lined code. For example, to format a selection of JavaScript code within a PHP or HTML file. When true, the JsPrettier command is available for use across all Sublime Text syntaxes.

  • custom_file_extensions (default: [])
    There's built-in support already for js, jsx, json, graphql, ts, tsx, css, scss and less files. Any additional file extensions must be specified here (exclude the leading dot).

  • additional_cli_args (default: {})
    A key-value pair of additional arguments to append to the prettier command.

    Example:

    {
        "--no-color": "",
        "--single-quote=false": "",
        "--cursor-offset": -1,
        "--list-different": ""
    }
    

    NOTE: If choosing to specify additional cli args, it is assumed that each argument is supported by the prettier-cli. Otherwise, the command will fail to run, and errors will be dumped out to the Sublime Text Console. You can also enable the debug setting to inspect the generated command-line output passed to prettier; which is also useful for quickly troubleshooting issues.

  • max_file_size_limit (default: -1)
    The maximum allowed file size to format in bytes. For performance reasons, files with a greater file size than the specified max_file_size_limit will not be formatted. Setting the max_file_size_limit value to -1 will disable file size checking (default).

Prettier Options

  • useTabs (internally set by the translate_tabs_to_spaces setting)
    Indent lines with tabs.

  • printWidth (default: 80)
    Specifies that the formatted code should fit within this line limit.

  • tabWidth (internally set by the tab_size setting)
    The number of spaces to use per tab.

  • singleQuote (default: false)
    If true, code will be formatted using single-quotes, instead of double-quotes.

  • trailingComma (default: “none”)
    Controls the printing of trailing commas wherever possible. Valid options:

    • none” – No trailing commas
    • es5” – Trailing commas where valid in ES5 (objects, arrays, etc)
    • all” – Trailing commas wherever possible (function arguments)
  • bracketSpacing (default: true)
    Controls the printing of spaces inside object literals.

  • jsxBracketSameLine (default: false)
    When true, right-angle brackets (“>”) of multi-line jsx elements will be placed at the end of the last line, instead of being alone on the next line.

  • parser (default: “babylon”)
    Which parser to use. Valid options are “flow”, “babylon”, “typescript” and “postcss”.

    If CSS or TypeScript is detected in Sublime Text, the parser option will always be internally overridden and set to “postcss” or “typescript” respectively.

  • semi (default: true)
    true to add a semicolon at the end of every line, or false to add a semicolon only at the beginning of lines that may introduce ASI failures.

For further details and examples of setting Prettier's options, please see the Prettier API section on the Prettier homepage.

Project-level Settings

JsPrettier supports Project-level settings, specified in <project_name>.sublime-project files. In order for Project-level settings to override the Defaults and User configured settings, a new js_prettier section must be created under the project file's settings section.

Example Sublime Text Project File

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "js_prettier": {
            "debug": false,
            "prettier_cli_path": "",
            "node_path": "",
            "auto_format_on_save": false,
            "auto_format_on_save_excludes": ["*/node_modules/*", "*/.git/*"],
            "allow_inline_formatting": false,
            "custom_file_extensions": [],
            "additional_cli_args": {},
            "max_file_size_limit": -1,
            "prettier_options": {
                "printWidth": 80,
                "singleQuote": false,
                "trailingComma": "none",
                "bracketSpacing": true,
                "jsxBracketSameLine": false,
                "parser": "babylon",
                "semi": true
            }
        }
    }
}

Issues

To report an issue, please follow the steps outlined in the Issue Template.

Changes

Please visit the Changelog page for a complete list of changes.

Author

Jon LaBelle

License

MIT License