用于npm样式的Emacs JS模式

Tam*_*ake 6 javascript emacs npm

是否有针对emacs的JS模式,它与npm样式非常兼容?

到目前为止,我正在修改js2-mode,其中覆盖了原生意图,并替换为'tab key = 2 spaces'.但是让我的编辑器能够像这样处理缩进会很好:

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

因为它是,JS2模式会尽可能正确地缩进和可能的位置之间的周期,但例如"逗号下ř一字排开"不是一个选项.当然,在emacs中编写体面的缩进代码很难,而且我的elisp不适合在那里使用.

请注意,如果有人知道另一个编辑器可以更好地工作,我可以开放切换.

Tam*_*ake 5

非常感谢Cheeso的建议,这里有一个有点混乱的模式组合,在使用js2-mode的优秀解析和错误检查时处理浓缩咖啡方式的逗号优先或逗号最后样式的缩进:https:/ /github.com/thomblake/js-mode

如果遇到任何问题,请随时提交错误报告 - 我打算修复它们.

编辑:现在在https://github.com/thomblake/js3-mode找到并调用js3-mode


Che*_*eso 1

Emacs 23.2 包含 js-mode,它是经过重命名和修饰的 Espresso。我刚刚对你的冗余代码做了“缩进区域”并得到了这个:

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

我想这并不完全是你想要的。逗号的偏移方式与您可能喜欢的不同。

(天哪,太丑了。)


编辑
好吧,我研究了 js-mode ,缩进似乎是由js--proper-indentation. 它查看当前行,然后尝试做出有关如何缩进的决定。

它针对各种语法模式测试了一堆条件。我只是检查了以逗号开头的行,并得到了以下结果:

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

我认为这就是你想要的,但在我看来它仍然完全破碎。无论如何,我就是这样做的。

将此条件插入到列表顶部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))))
         ....
Run Code Online (Sandbox Code Playgroud)

请务必保留该 defun 中的其余条件 - 我不知道它们的作用,但它们可能很重要。

不知道这是否可靠,除了您的一个测试用例之外,我还没有测试过它。但这应该让你开始。