项目作者: futurist

项目描述 :
Emacs package to format or transform code style using different javascript formatter (standard, jsbeautify, esformatter, etc.)
高级语言: Emacs Lisp
项目地址: git://github.com/futurist/js-format.el.git
创建时间: 2016-12-05T05:59:35Z
项目社区:https://github.com/futurist/js-format.el

开源协议:GNU General Public License v3.0

下载


js-format.el

Emacs package for format or transform code style using different javascript formatter (standard, jsbeautify, esformatter, etc.)

Send region or buffer to a format server (will setup localhost:58000 by default), with below formatters:

Install

  1. You need NodeJS >= 6 in your system path

  2. js-format.el is available via MELPA and can be installed via

    1. ;; confirm below package config exists
    2. (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
    3. ;; then run below
    4. M-x package-install js-format
    5. ;; then require the package in your config
    6. (require 'js-format)

    Or if you don’t use MELPA, download this repo, add below lines to your init.el

    1. (add-to-list 'load-path "folder-of-js-format.el")
    2. (require 'js-format)
  3. It should auto setup for the first time of use, according to different style package’s setup command.

Usage

  • js-format-setup to switch and setup style (default value: "standard").

To make different mode using different format style, you can add below:

  1. ;; using "standard" as js formatter
  2. (after-load 'js2-mode
  3. (add-hook 'js2-mode-hook
  4. (lambda()
  5. (js-format-setup "standard"))))
  6. ;; using "jsbeautify-css" as css formatter
  7. (after-load 'css-mode
  8. (add-hook 'css-mode-hook
  9. (lambda()
  10. (js-format-setup "jsb-css"))))

The style name is from styles.json key, you can change the key to any name.

  • js-format-mark-statement to mark current statement under point (only in js2-mode).

  • js-format-region to try mark current statement, pass it to js-format-server, then get
    back the result code to replace the statement.

  • js-format-buffer to format the whole buffer.

You may also want to bind above func to keys:

  1. (global-set-key (kbd "M-,") 'js-format-mark-statement)
  2. (global-set-key (kbd "C-x j j") 'js-format-region)
  3. (global-set-key (kbd "C-x j b") 'js-format-buffer)

Add new format style guide

  1. Create a folder, with name say “my-style”
  2. Add package.json file, to specify an entry file in “main”, or will use “index.js” if not specified.
  3. Entry file should have function format(code, cb){} exported as a node module.
  4. Add a style name and the folder into “styles.json” file to register the new style.

Why use NodeJS Server instead of `call-process’ etc.?

At first I’m using `call-process’ to run a JS code, but every time
there’s a lag, since starting a new node is a heavy operation, and
the output/input pipe not easily controlled if run as deamon, with
need of formatting region constantly, or even, auto formatting when
press RETURN, that lag is fatal.

Using server instead, giving a fast response from socket, and you
can format remotely (setup a format server in your workplace).

NodeJS is a good choise for using NPM, with rich module to import,
and easy to write a new style with javascript.