【发布时间】:2011-12-13 18:26:15
【问题描述】:
作为一些使用 GNU/Emacs 为 Android 开发的人,你肯定知道最新的 Android 工具引入了new xml formatter。我使用出色的 nxml 模式来编辑 xml,因为……我编辑 xml 文件;)我对它非常满意,但是……因为我可以自定义 Nxml 属性缩进变量,文档说:
Indentation for the attributes of an element relative to the start-tag. Hide
This only applies when the first attribute of a tag starts a line.
In other cases, the first attribute on one line is indented the same
as the first attribute on the previous line.
重要的是当独立属性与元素在同一行时,独立属性与第一个属性对齐的后备。
是否可以更改该行为,以获得与 Android 工具兼容的缩进?我只是在文档中一无所获,谷歌搜索失败了……
更新:
评论帮助我意识到我并不清楚。因此,这是一个 nxml-mode 默认执行的示例:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.foo.bar"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<activity
android:name="Foo"
android:label="@string/foo" />
<activity android:name="Bar"
android:label="@string/bar" />
</application>
</manifest>
我想得到什么:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.foo.bar"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<activity
android:name="Foo"
android:label="@string/foo" />
<activity android:name="Bar"
android:label="@string/bar" />
</application>
</manifest>
第一种情况(默认的 nxml 模式缩进行为):
-
manifest元素的package属性与xmlns:android decl对齐 -
Bar activity元素的android:label属性与android:name元素对齐。
第二种情况(预期结果):
-
manifest元素的package属性与父manifest元素以及可配置数量的空格对齐 -
Bar activity元素的android:label属性与父元素以及可配置数量的空格对齐
我浏览了 nxml-mode 源代码,缩进行为以 nxml-indent-line 开头,但由于我缺乏 lisp 知识,我未能遵循许多子调用来查看应该自定义 defun 的内容。
您可以看到manifest 第二个属性与第一个不对齐
干杯,
Renaud(为了符合 Android 编码和格式规则而感到头疼)
【问题讨论】:
-
您能展示一些 XML 示例吗?我不明白您所说的“当独立属性与元素位于同一行时,独立属性与第一个属性对齐的后备”是什么意思。
-
用 XML sn-ps 更新的问题 ;) 对不起,我应该把它们放在第一位……