如何在 emacs 中将分配和哈希的智能对齐添加到缩进区域?

发布于 2024-10-27 08:02:27 字数 845 浏览 2 评论 0原文

当我有代码时:

a = 1
foo = 2

并且我想让它看起来像:

a   = 1
foo = 2

我可以使用此处定义的align-to-equals函数:

-align-equal-signs">Emacs 热键通过选择区域并运行函数 。我可以通过将函数定义中的“=”更改为“=>”来更改该函数以适用于哈希值并具有:

bar = { :a => 1,
 :foo => 2 }

转换为:

bar = { :a   => 1,
        :foo => 2 }

当我选择整个缓冲区并运行 indent-region 时,我希望对所有代码进行这种对齐。但我希望它能够智能地完成 - 不要将缓冲区中的每个“=”与最右边的“=”对齐,而是对赋值块和单个哈希文字进行对齐。

编辑:为了澄清最后一部分,假设我有以下缓冲区:

a = 1
foo = 2

some_other_code

def fn
  bar = { :a => 1,
   :foo => 2 }
end

我想做“Cx h”(选择整个缓冲区)、“MC-\”(缩进区域)并让它看起来像这样:

a   = 1
foo = 2

some_other_code

def fn
  bar = { :a   => 1,
          :foo => 2 }
end

When I have the code:

a = 1
foo = 2

and I want to make it look like:

a   = 1
foo = 2

I can use the align-to-equals function defined here:

Emacs hotkey to align equal signs

by selecting the area and running the function. I can alter the function to work for hashes as well by changing the '=' in the function definition to '=>' and have:

bar = { :a => 1,
 :foo => 2 }

be converted to:

bar = { :a   => 1,
        :foo => 2 }

I want this alignment to be done to all of my code when I select the whole buffer and run indent-region. But I want it to be done intelligently - not aligning every single '=' in the buffer to the rightmost '=', but instead doing it for assignment blocks and individual hash literals.

EDIT: To clarify this last part, say I have the following buffer:

a = 1
foo = 2

some_other_code

def fn
  bar = { :a => 1,
   :foo => 2 }
end

I want to do 'C-x h' (select whole buffer), 'M-C-\' (indent-region) and have it look like this:

a   = 1
foo = 2

some_other_code

def fn
  bar = { :a   => 1,
          :foo => 2 }
end

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

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

发布评论

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

评论(2

じее 2024-11-03 08:02:27

尝试Mxalign-regexp=。对于其他人也是如此。

Try M-x align-regexp =. And same for others as well.

几度春秋 2024-11-03 08:02:27

align.el 为此提供了对齐函数:

在 .emacs 中添加

     (push (ruby-hash-string
             (regexp . "\\(\\s-*\\)\\(\"[^\"]*\"\\|:[a-zA-Z]*\\)\\(\\s-*\\)=>\\(\\s-*\\)")
             (group . (1 3 4))
             (repeat . t)
             (modes '(ruby-mode)))
        align-rules-list)

然后 Mxalign 将在 ruby​​ 模式下对齐散列。您需要为您想要对齐的其他内容添加其他组。

align.el give the align function for this:

in .emacs add

     (push (ruby-hash-string
             (regexp . "\\(\\s-*\\)\\(\"[^\"]*\"\\|:[a-zA-Z]*\\)\\(\\s-*\\)=>\\(\\s-*\\)")
             (group . (1 3 4))
             (repeat . t)
             (modes '(ruby-mode)))
        align-rules-list)

then M-x align will align hashes in ruby-mode. You need to add other group for other thuings you want to align.

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