如何阻止emacs缩进javascript逗号分隔列表(例如array或json)的第二行? [英] How can I stop emacs from indenting the 2nd line of a javascript comma-separated list (e.g. array or json)?

查看:132
本文介绍了如何阻止emacs缩进javascript逗号分隔列表(例如array或json)的第二行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var myArray = [
1,
2,
3,
4,
5,
];

而不是这样:

  var myArray = [
1,
2,
3,
4,
5,
];

如何防止这种情况?



Mx版本给了我:2010-03-26的GNU Emacs 23.1.1(x86_64-pc-linux-gnu,GTK + Version 2.18.3),由Debian修改



我不能更新我的emacs版本,不幸的是。

解决方案

看起来您正在使用 java-mode 来编辑JavaScript码。虽然Java和JavaScript分享足够的语法,这可能会正常,最好使用专用的JavaScript模式¹。



Emacs js-mode ,这是一个相当不错的基本模式JavaScript编辑。如果您可以升级Emacs,这可能是您最简单的选择。您也可以在Emacs 23.1上安装 js-mode (或其前身 espresso-mode )。以下是最新版本



或者,您可以使用第三方模式,如 js2-mode ,其中包括一个完整的JavaScript解释器。还有 js3-mode ,其中声称为


js2-mode和js-mode的嵌合叉


我的Emacs内置了 js2-mode js-mode 的版本24.4将您的示例代码完全按照您想要的方式缩进:

  var myArray = [
1 ,
2,
3,
4,
5,
];

¹不幸的是,仍然有足够的混乱,值得一提的是Java和JavaScript是完全不同的语言。你可能已经知道了我认为Emacs实际上默认为 java-mode JavaScript代码一段时间。


Emacs indents my code like this:

var myArray = [
    1,
        2,
        3,
        4,
        5,
    ];

Instead of like this:

var myArray = [
    1,
    2,
    3,
    4,
    5,
];

How can I prevent this?

M-x version gives me: GNU Emacs 23.1.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.3) of 2010-03-26 on crested, modified by Debian

I'm not able to update my emacs version, unfortunately.

解决方案

It looks like you are using java-mode to edit JavaScript code. While Java and JavaScript share enough syntax that this will probably work okay, it is better to use a dedicated JavaScript mode¹.

Emacs includes js-mode from version 23.2, which is a reasonably good mode for basic JavaScript editing. If you can upgrade Emacs this is likely your easiest option. You may also be able to install js-mode (or its predecessor espresso-mode) on Emacs 23.1. Here is the latest version.

Alternatively, you can use a third-party mode like js2-mode, which actually includes a full JavaScript interpreter. There is also js3-mode, which claims to be

A chimeric fork of js2-mode and js-mode

Both js2-mode and the version of js-mode built into my Emacs 24.4 indent your sample code exactly as you want:

var myArray = [
    1,
    2,
    3,
    4,
    5,
];

¹Unfortunately, there is still enough confusion out there that it's worth stating that Java and JavaScript are completely different languages. You probably already know this; I think Emacs actually defaulted to java-mode for JavaScript code for a while.

这篇关于如何阻止emacs缩进javascript逗号分隔列表(例如array或json)的第二行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆