admin管理员组文章数量:1384349
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 badges3 Answers
Reset to default 5With 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.
本文标签: javascriptEmacs JS mode for npm styleStack Overflow
版权声明:本文标题:javascript - Emacs JS mode for npm style - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744529220a2610928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论