自定义 GNU/Emacs nxml 模式缩进

发布于 2024-12-21 01:22:13 字数 2663 浏览 2 评论 0原文

作为使用 GNU/Emacs 进行 Android 开发的人,您肯定知道最新的 Android Tools 引入了 新的 xml 格式化程序。我使用优秀的 nxml-mode 来编辑 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 Tools 兼容的缩进,是否可以改变该行为?我只是在文档中找不到任何内容,并且谷歌搜索失败了...

更新:

该评论帮助我意识到我不清楚。因此,这里是 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-mode 缩进行为):

  • manifest< 的 package 属性/code> 元素与 xmlns:android decl 对齐
  • Bar Activity 元素的 android:label 属性与 对齐android:name 元素。

第二种情况(预期结果):

  • manifest 元素的 package 属性与父 manifest 元素以及
  • manifest 元素 中可配置数量的空格对齐Bar Activity 元素的 code>android:label 属性与父元素以及可配置数量的空格对齐

我已经浏览了 nxml 模式源代码,缩进行为以 <代码>nxml-indent-line但是由于我缺乏 lisp 知识,我未能遵循许多子调用来了解应该定制什么 defun

您可以看到 manifest 第二个属性与第一个属性不一致,

干杯,

Renaud(很难管理符合 Android 编码和格式规则的巨大头痛)

As some of you who use GNU/Emacs to develop for Android, you are surely aware that the latest Android Tools introduces a new xml formatter. I use the excellent nxml-mode to edit xml since… I edit xml files ;) and I'm pretty happy with it BUT… as I can customize the Nxml Attribute Indent variable, the documentation says:

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.

What matters there is the fallback for which a standalone attribute is aligned on the first attribute when this one is on the same line that the element.

Is this possible to change that behavior, in order to obtain a Android Tools compatible indentation? I just found nothing in the documentation and googling failed…

Update:

The comment helps me to realize that I'm not clear. Thus, here is an example of what nxml-mode does by default:

<?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>

What I would like to get:

<?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>

First case (default nxml-mode indentation behavior):

  • the package attribute of the manifest element is aligned with the xmlns:android decl
  • the android:label attribute of the Bar activity element is aligned with the android:name element.

Second case (intended result):

  • the package attribute of the manifest element is aligned with the parent manifest element plus a configurable number of spaces
  • the android:label attribute of the Bar activity element is aligned with the parent element plus a configurable number of spaces

I have browsed the nxml-mode source code and the indentation behavior begins with nxml-indent-line but I failed to follow the many subcalls to see what defun should be customized… due to my lack of lisp knowledge.

You could see that the manifest second attribute is not aligned with the first

Cheers,

Renaud (hardly managing the massive headache to conform with the Android coding and formatting rules)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-12-28 01:22:13

该行为看起来并不容易修改,因为它似乎被硬编码到 nxml-compute-indent-in-start-tag 函数中。相关的代码块似乎是这样的:

              (let* ((att (car atts))
                     (start (xmltok-attribute-name-start att)))
                (when (< start pos)
                  (goto-char start)
                  (setq off 0))))

您始终可以将该方法复制到您自己的初始化文件中,注释掉这些行,并在 nxml 模式加载后加载您的函数定义(这将覆盖原始实现)。

请注意,您可能还想向 gnu emacs 维护人员提交增强请求,以便将来可以轻松自定义此行为。

doesn't look like that behavior is easily modifiable, as it appears to be hard coded into the nxml-compute-indent-in-start-tag function. the relevant chunk of code appears to be this:

              (let* ((att (car atts))
                     (start (xmltok-attribute-name-start att)))
                (when (< start pos)
                  (goto-char start)
                  (setq off 0))))

you could always copy that method into your own init file, comment those lines out, and load your function definition after nxml mode loads (which will override the original implementation).

Note, you might also want to submit an enhancement request to the gnu emacs maintainers to make this behavior easily customizable in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文