javascript - Emacs:是否可以在 js2 模式下自动插入分号?

标签 javascript emacs js2-mode

我正在使用这个精湛的 js2 模式 fork ,连同自动配对功能,使 Emacs 中的 Javascript 编辑变得很棒。但是我突然想到,由于 js2-mode 是一个完整的解析器,所以只要我在函数调用上下文中,就应该可以自动插入分号。

我想在我深入挖掘之前先问问是否有人调查过这个问题。

最佳答案

这是我解决这个问题的代码:

le-js2-mode-setup-partial.el
(defvar js2-semicolon-contexts (list js2-NAME js2-LP js2-SCRIPT js2-CALL js2-BLOCK))
(defun autopair-js2-maybe-insert-semi-colon (action pair pos-before)
  "handler for automatically inserting semi-colon at the end of function call.
"
  ;; (message "node before is %s" (js2-node-type (js2-node-at-point (- (point) 1))))
  ;; (message "action is %s" action)
  ;; (message "pair is %c" pair)
  ;; (message "context is %s" (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
  ;; (message "point is %s" (point))
  (cond ((and (eq action 'opening)
              (eq pair ?\))
             (save-excursion
               (goto-char pos-before)
               (skip-chars-backward " \t")
               ;; (message "node is %s." (js2-node-type (js2-node-at-point (point))))
               (memq (js2-node-type (js2-node-at-point (point))) js2-semicolon-contexts)
               ))
         (save-excursion
           (let ((forward-sexp-function nil))
             (goto-char pos-before)
             (forward-sexp))
           (if (looking-at-p "[^[:graph:]]*$")
             (insert ";"))))))

;;;###autoload
(defun le::js2-mode-setup ()
  (setq autopair-handle-action-fns
        (list #'autopair-default-handle-action
              #'autopair-js2-maybe-insert-semi-colon))
  (rebox-mode 1)
  (le::prog-modes-setup))
;;;###autoload(add-hook 'js2-mode-hook 'le::js2-mode-setup)

您也可以从 this GitHub Gist 获取此代码.

关于javascript - Emacs:是否可以在 js2 模式下自动插入分号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241523/

相关文章:

javascript - setTimeout(0) vs window.postMessage vs MessagePort.postMessage

javascript - 如何打破主干集合,以便每个子集合也注册到父集合将注册的所有事件

java - 为什么 try-with-resources 会破坏 Emacs 中的缩进?”

emacs - 你的 .emacs 里有什么?

emacs - 如何在js2-mode中将缩进设置为2个空格?

emacs - 如何在 js2-mode 中禁用换行符的自动缩进?

javascript - 如何在 Asp.Net 中加载脚本

javascript - Google 地球回调未触发

emacs - emacs 中的locate-library 与 find-library

JavaScript 错误 : missing = in destructuring declaration