最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Emacs JS mode for npm style - Stack Overflow

programmeradmin3浏览0评论

Is there a JS mode for emacs which is pretty much patible with npm style?

So far I'm working with a modification of js2-mode with native intending features overridden and replaced with 'tab key = 2 spaces'. But it would be nice to have my editor be able to handle indenting like this:

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

As it is, js2-mode tries its best to indent correctly and cycles between possible positions, but for example 'ma lined up under r' isn't one of the options. Of course, writing decent indenting code in emacs is hard and my elisp isn't up to snuff there.

Note that if someone knows another editor that would work better for this, I could be open to switching.

Is there a JS mode for emacs which is pretty much patible with npm style?

So far I'm working with a modification of js2-mode with native intending features overridden and replaced with 'tab key = 2 spaces'. But it would be nice to have my editor be able to handle indenting like this:

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

As it is, js2-mode tries its best to indent correctly and cycles between possible positions, but for example 'ma lined up under r' isn't one of the options. Of course, writing decent indenting code in emacs is hard and my elisp isn't up to snuff there.

Note that if someone knows another editor that would work better for this, I could be open to switching.

Share Improve this question edited Nov 8, 2012 at 10:54 Jacob Oscarson 6,4031 gold badge38 silver badges46 bronze badges asked May 26, 2011 at 20:53 Tamzin BlakeTamzin Blake 2,59421 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

With much thanks to Cheeso's suggestion, here is a somewhat hacked-together mode-bo that handles indenting for ma-first or ma-last styles in the espresso way while using the great parsing and error-checking of js2-mode: https://github./thomblake/js-mode

If any problems are encountered, feel free to file bug reports - I intend to fix them.

EDIT: now found at https://github./thomblake/js3-mode and called js3-mode

You should have a look at the improved js2-mode fork, it seems to be able to handle indentation a bit better than the ordinary modes.

Emacs 23.2 includes js-mode, which is a renamed and touched-up Espresso. I just did "indent-region" on your redunkulous code and got this:

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

I guess that's not exactly what you want. The mas are offset differently than you probably like.

(Gosh that's ugly.)


EDIT
Ok I looked into js-mode and the indentation seems to be done by js--proper-indentation. It looks at the current line and then tries to make decisions about how to indent.

It's got a bunch of conditions it tests, for various syntax patterns. I just put in a check for a line starting with a ma, and got these results:

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

I think that is what you want, but it still looks totally broken to me. Anyway this is how I did it.

Inject this cond into 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 keep the rest of the conditions in that defun - I don't know what they do but the're probably important.

No idea if this is robust, I haven't tested it except for your one test case. But this oughta get you started.

发布评论

评论列表(0)

  1. 暂无评论