5

私はこの優れた js2-mode forkと autopairs を使用して、Emacs で Javascript 編集を素晴らしいものにしています。ただし、js2-mode は完全なパーサーであるため、関数呼び出しコンテキストにいるときは常にセミコロンを自動的に挿入できるはずです。

深く掘り下げる前に、誰かがこれを調べたかどうか尋ねようと思いました。

4

1 に答える 1

4

これを解決する私のコードは次のとおりです。

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)

このコードは、この GitHub Gistから取得することもできます。

于 2012-02-28T16:00:40.563 に答える