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

我正在使用这个极好的js2-mode fork,以及autopairs在Emacs中进行Javascript编辑功能.然而,我发现由于js2-mode是一个完整的解析器,每当我在函数调用上下文时,应该可以自动插入分号.

在我深入挖掘之前,我想我会问是否有人对此进行了调查.

解决方法:

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

LE-JS2模式 – 设置 – 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获取此代码.

上一篇:Linux emacs / nano:如何选择较大文件中的所有文本行并进行复制


下一篇:将emacs缓冲区发送到任意Python进程