サイトアイコン braveam

company補完でキー入力と同時に確定させる

VisualStudioのようににキー入力と同時に補完を確定させてたいと思い、やってみたので紹介です。

;; 補完確定と文字列挿入
(defun cc-selection-with-str (str)
  (company-complete-selection)
  (insert str)
  )

;; 特定のキーで確定。同時にそのキーの文字も挿入する
(define-key company-active-map (kbd ".") '(lambda () (interactive) (cc-selection-with-str ".")) )
(define-key company-active-map (kbd "(") '(lambda () (interactive) (cc-selection-with-str "(")) )
(define-key company-active-map (kbd ")") '(lambda () (interactive) (cc-selection-with-str ")")) )
(define-key company-active-map (kbd ":") '(lambda () (interactive) (cc-selection-with-str ":")) )

上記だとちょっと冗長なのと、company-search-mapにも対応させたいので、最終的に以下のようになりました。

;; 補完確定と文字列挿入
(defun cc-selection-with-str (str)
  (company-complete-selection)
  (insert str)
  )

;; company確定キー定義
(defun cc-define-selection-key (key)
  (let ((func `(lambda () (interactive) (cc-selection-with-str ,key))))
	(define-key company-search-map (kbd key) func ) ; サーチマップ用
	(define-key company-active-map (kbd key) func ) ; アクティブマップ用
	)
  )

(cc-define-selection-key ".")
(cc-define-selection-key "(")
(cc-define-selection-key ")")
(cc-define-selection-key ":")
(cc-define-selection-key "=")
(cc-define-selection-key ">")

完全にVisualStudioみたいに、とまではいきませんが、いろんなキーで確定して、同時にそのキーの文字も入力してくれます。

 

モバイルバージョンを終了