Giter VIP home page Giter VIP logo

prettier-emacs's Introduction

Prettier-js for Emacs

MELPA

prettier-js is a function that formats the current buffer using prettier. The package also exports a minor mode that applies (prettier-js) on save.

Configuration

Requirements

Ensure that the prettier program is installed:

which prettier

If prettier is not installed already, you can install prettier using npm install -g prettier or via your package manager.

Basic configuration

First require the package:

(require 'prettier-js)

Then you can hook to your favorite javascript mode:

(add-hook 'js2-mode-hook 'prettier-js-mode)
(add-hook 'web-mode-hook 'prettier-js-mode)
...

Prettier arguments

To adjust the CLI args used for the prettier command, you can customize the prettier-js-args variable:

(setq prettier-js-args '(
  "--trailing-comma" "all"
  "--bracket-spacing" "false"
))

Usage with web-mode

Web-mode is a popular mode for editing .js and .jsx files, but it is used to edit other template files too. If you want to hook prettier-js to web-mode for .js and .jsx files only, you can define a helper function like this:

(defun enable-minor-mode (my-pair)
  "Enable minor mode if filename match the regexp.  MY-PAIR is a cons cell (regexp . minor-mode)."
  (if (buffer-file-name)
      (if (string-match (car my-pair) buffer-file-name)
      (funcall (cdr my-pair)))))

And then hook to web-mode like this:

(add-hook 'web-mode-hook #'(lambda ()
                            (enable-minor-mode
                             '("\\.jsx?\\'" . prettier-js-mode))))

Installing on Windows

This package requires the diff tool which is already included on Unix platforms. The simplest way to install diff on Windows is to use Chocolatey. The steps are as follows:

  1. Follow the Chocolatey install instructions: https://chocolatey.org/install
  2. Open an Admin Powershell session
  3. Install the diff program: choco install diffutils

You should now be able to open Emacs and successfully use this package.

Customization

This package is customizable via custom.el:

M-x customize-group prettier-js
  • prettier-js-command is the prettier command
  • prettier-js-args are the args passed to the prettier command
  • prettier-js-show-errors customizes where to display the error output (buffer, echo or nil)
  • prettier-js-width-mode customizes the width when formatting buffer contents (window, fill or nil)

Using node_modules/.bin/prettier

If you want to use your project's prettier version you can rely on https://github.com/codesuki/add-node-modules-path

(eval-after-load 'web-mode
    '(progn
       (add-hook 'web-mode-hook #'add-node-modules-path)
       (add-hook 'web-mode-hook #'prettier-js-mode)))

prettier-emacs's People

Contributors

aaronjensen avatar anmonteiro avatar baerrach avatar xuchunyang avatar clayrisser avatar jkseppan avatar juergenhoetzel avatar jscheid avatar mikegolod avatar eqyiel avatar sunesimonsen avatar troglotit avatar modulitos avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.