【问题标题】:In web-mode - how to pull closing braces and parenthesis to indentation level of opening line在 web 模式下 - 如何将右大括号和括号拉到开头行的缩进级别
【发布时间】:2017-01-01 18:58:59
【问题描述】:

在大多数 emacs 模式中,当在块的末尾键入右大括号时,大括号会自动拉回到其相应开头行的缩进级别。例如,输入以下内容后:

int main(int argc, char* argv[]) {
    // stuff
    }

右大括号被拉回左侧:

int main(int argc, char* argv[]) {
    // stuff
}   // <---- pulled back with no additional input

但是,当使用 web 模式时,情况并非如此,至少在默认设置下是这样。直到有某些额外的输入,例如当光标位于右大括号的行上时,按 Tab 或 Enter。这不适用于在添加任何块内容之前添加右大括号的编码风格,这在 emacs 中用 C 编码时效果很好(具体来说,我的意思是输入“{ enter enter } C-p”)。在这个序列中,没有什么可以触发右大括号拉回一个缩进级别,所以我的代码如下所示:

class Header extends React.Component {
    render() {
        return (
            <div></div>
            );
        }
    }

除非我在该序列中添加一个制表键(“{ enter enter } tab C-p”)。

我知道这只是一个键,我可以改变我的习惯,但是有没有办法改变 emacs 的行为呢?我在 web 模式的 documentation 中没有看到任何相关变量需要更改,但我是否遗漏了什么?

编辑:在学习了超出我想象的更多关于 emacs 的知识后,我意识到我正在寻找的实际上是 cc-mode 的一个特定功能。在 cc-mode 中,} 绑定到 c-electric-brace。来源:

(defun c-electric-brace (arg)
  "Insert a brace.

If `c-electric-flag' is non-nil, the brace is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:

\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
the mode line) newlines are inserted before and after the brace as
directed by the settings in `c-hanging-braces-alist'.

\(b) Any auto-newlines are indented.  The original line is also
reindented unless `c-syntactic-indentation' is nil.

\(c) If auto-newline is turned on, various newline cleanups based on the
settings of `c-cleanup-list' are done."

所以看来要在 web 模式下获得这个功能,我需要学习一些 lisp 并提交 PR。这就是我要做的,如果/当我完成时,我会在这里提交答案。与此同时,我仍然希望了解更多相关信息的任何人提供意见。

【问题讨论】:

    标签: emacs indentation


    【解决方案1】:

    使用 web-mode.el 中的 defun 尝试:

    (local-set-key (kbd "RET") 'newline-and-indent)
    

    【讨论】:

    • 不幸的是,这不起作用,因为 web 模式已经更正了 RET 上的缩进,所以更改 RET 上的绑定不会改变任何东西。它必须是绑定到 } 的东西。
    • 但是,您的回答让我陷入了学习 emacs 键绑定到函数的困境(两个小时前,我对 lisp 一无所知 - 变化很快),所以我想了解更多关于我想要什么。因此,尽管我目前的代表不允许,但我会赞成,因为它为我指明了正确的方向。我会更新我的问题。
    【解决方案2】:

    所以我最终确实提交了具有此功能的 PR,但维护者显然有相同的想法并将其合并到自己身上。它从 v14.0.36 开始生效(提交 3e74b74)。

    正如我在问题编辑中所述,c-electric-brace 是一个键绑定,因此效果立竿见影。这在可能很容易启用/禁用的情况下更难以实现,因此在 web 模式下,它被添加为命令后挂钩。因此,如果启用了show-paren-mode,它会在闪烁匹配的括号/大括号/括号之后发生。因此会有短暂的延迟。

    此效果在web-mode-enable-auto-indentation 设置为t 时启用,默认情况下。以下是相关代码(合并添加,不是我的):

    (when (and web-mode-enable-auto-indentation
               (member this-command '(self-insert-command))
               (member (get-text-property (point) 'part-side) '(javascript jsx))
               (looking-back "^[ \t]+[]})]"))
      (indent-according-to-mode)
      ;;(message "%S" (point))
      (when (and web-mode-change-end (> web-mode-change-end (point-max)))
        (message "post-command: enlarge web-mode-change-end")
        (setq web-mode-change-end (point-max))
      )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2017-08-28
      相关资源
      最近更新 更多