6

是否有与npm 风格非常兼容的 emacs JS 模式?

到目前为止,我正在对js2-mode进行修改,其中本机预期功能被覆盖并替换为“tab key = 2 个空格”。但是让我的编辑器能够像这样处理缩进会很好:

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

实际上,js2-mode 会尽力正确缩进并在可能的位置之间循环,但例如“逗号在 r 下排列”不是选项之一。当然,在 emacs 中编写体面的缩进代码是很困难的,而且我的 elisp 也不能满足要求。

请注意,如果有人知道另一个可以更好地为此工作的编辑器,我可以愿意切换。

4

3 回答 3

5

With much thanks to Cheeso's suggestion, here is a somewhat hacked-together mode-combo that handles indenting for comma-first or comma-last styles in the espresso way while using the great parsing and error-checking of js2-mode: https://github.com/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.com/thomblake/js3-mode and called js3-mode

于 2011-06-02T21:20:41.250 回答
2

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.

于 2011-05-28T23:18:04.283 回答
1

Emacs 23.2 包含 js-mode,这是一个重命名和修饰的 Espresso。我刚刚在你冗长的代码上做了“缩进区域”,得到了这个:

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

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

(天哪,太丑了。)


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

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

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

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

将此条件注入列表顶部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.

于 2011-05-27T04:13:38.490 回答