Emacs JS mode for npm style

Is there any JS mode for emacs that is pretty much compatible with npm style ?

So far I am working with a js2-mode modification with overridden source overlay functions and replaced with "tab key = 2 spaces", but it would be nice if my editor could handle the indentation as follows:

var o = { foo : 'bar' , baz : 'foo' } , p , q = new Squash( o , { start: 0 , limit: 50 } ) 

As in the case of js2-mode, it best tries to retreat correctly and cyclically moves between possible positions, but, for example, a comma built under r is not one of the options. Of course, writing decent indentation code in emacs is difficult, and my elisp doesn’t smell it.

Please note: if someone knows another editor that will work better for this, I might be open to switching.

+6
source share
3 answers

Emacs 23.2 includes js-mode, which is a renamed and touched espresso. I just did "indent-region" on your reddish code and got the following:

 var o = { foo : 'bar' , baz : 'foo' } , p , q = new Squash( o , { start: 0 , limit: 50 } ) 

I suppose that is not quite what you want. These commas are offset differently than you like.

(Gosh, that's ugly.)


EDIT
Ok, I looked in js-mode, and the indentation seems to be done using js--proper-indentation . He looks at the current line and then tries to make a decision on how to backtrack.

He got a bunch of conditions that he is testing for various syntax patterns. I just put the check on the line starting with a comma and got the following results:

 var o = { foo : 'bar' , baz : 'foo' } , p , q = new Squash( o , { start: 0 , limit: 50 } ) 

I think this is what you want, but it still looks completely broken for me. In any case, that's what I did.

Paste this cond at the top of the list for js--proper-indentation :

 (defun js--proper-indentation (parse-status) "Return the proper indentation for the current line." (save-excursion (back-to-indentation) (cond ((looking-at ",") (let ((spos (save-excursion (while (looking-back "}[\t\n ]*") (backward-sexp) (if (looking-back ",[\t\n ]*") (re-search-backward ",[\t\n ]*"))) (cond ((looking-back "\\(,\\|(\\)[ \t]*\\<\\([^:=\n\t ]+\\)[ \t\n]*") (re-search-backward "\\(,\\|(\\)[ \t]*\\<\\([^:=\n\t ]+\\)[ \t\n]*" (point-min) t) (+ (current-column) 2)) ((re-search-backward "\\<\\([^:=\n\t ]+\\)[ \t]*\\(:\\|=\\)" (point-min) t) (current-column)) (t nil))))) (if spos (- spos 2) (+ js-indent-level js-expr-indent-offset)))) .... 

Be sure to follow the rest of the conditions in this defun - I don't know what they are doing, but probably important.

I don’t know how safe it is, I have not tested it, except for one test case. But it should begin.

+1
source

Thank you very much for Cheeso's suggestion, here are some compiled mode combos that handle indentation for commas or commas in espresso using js2-mode's great parsing and error checking: https://github.com/thomblake/js-mode

If you encounter any problems, feel free to generate error reports - I intend to fix them.

EDIT: now located at https://github.com/thomblake/js3-mode and is called js3-mode

+5
source

You should take a look at the improved js2-mode widget ; it seems to be able to handle indentation a bit better than regular modes.

+2
source

Source: https://habr.com/ru/post/889150/