Top/Editor/emacs/clang-format

目次

clang-format

環境構築

  1. パッケージをインストールする
    apt-get install clang-tools-7 clang-format
  2. clang-tools のパスを通す
    export PATH=/usr/lib/llvm-7/bin:${PATH}
  3. emacs 向けパッケージの設定
    • package-install を使う場合
      M-x package-install lsp-mode lsp-ui company-lsp dash-el elpa-f s-el elpa-markdown-mode dash-el
      • これとは別途、パッケージが存在しない ht.el を、下記と同様に取得する。
    • 個別に lisp を取ってくる場合
      1. 以下のリポジトリを取得する:
        package namepackage repository URIremark
        lsp-modehttp://github.com/emacs-lsp/lsp-mode.git各種LSPの言語モードを実装している。 C/C++ の設定もここに含まれる。
        lsp-uihttps://github.com/emacs-lsp/lsp-ui.git補完を担うcompany-modeのLSP向け設定
        company-lsphttps://github.com/tigersoldier/company-lsp.gitドキュメントの表示など各種アクションを実装している。
        elpahttps://git.savannah.gnu.org/git/emacs/elpa.gitelpa/packages/cl-generic 等を使用する為に取得する。サイズが大きいので注意。
        hthttps://github.com/Wilfred/ht.el.gitdebian9のパッケージには存在しない。lsp-mode の動作に必要
        dashhttps://github.com/magnars/dash.el
        fhttps://github.com/rejeep/f.el
        shttps://github.com/magnars/s.el
        markdown-modehttps://github.com/jrblevin/markdown-mode.git
        clang-format https://github.com/sonatard/clang-format.git このリポジトリのものではなく、clang-format-<version> package に含まれる物を使うこと
        flycheck-modehttps://github.com/flycheck/flycheck
        projectilehttps://github.com/bbatsov/projectile

      2. 上記各リポジトリを tag で checkout する
        package nametagremark
        lsp-moderefs/tags/7.0
        lsp-uirefs/tags/7.0
        company-lsprefs/tags/2.1.0
        elpa(HEAD)tagは試行中。動かなかったら検討する。

      3. 上記各リポジトリのファイルが emacs から見える場所に配置する。
        ${HOME}/.emacs.d/init.el への記述例
        ;;; ~/.emacs/init.el
        ;;; ~/.emacs.d/site-lisp 配下に各リポジトリを配置した場合の設定例
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/dash") load-path))                     ; dash
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/cl-generic") load-path)) ; cl-generic
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/ht") load-path))                       ; ht.el
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/hydra") load-path))      ; lv.el
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/spinner") load-path))    ; spinner.el
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/elpa/packages/company") load-path))    ; company.el
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/markdown-mode") load-path))            ; markdown-mode
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/flycheck") load-path))                 ; flycheck
        
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/projectile") load-path))               ; projectile
        (require 'projectile)
        
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/lsp-mode") load-path))
        (require 'lsp-mode)
        
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/lsp-ui") load-path))
        (require 'lsp-ui)
        
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/company-lsp") load-path))
        (require 'company-lsp)
        
        (setq load-path (cons (expand-file-name "~/.emacs.d/site-lisp/clang-format") load-path))
        (require 'clang-format)

  4. 設定を記載する
    ;;; .clang-firmat を読み込ませる関数を定義
    ;; https://eklitzke.org/smarter-emacs-clang-format
    (defun clang-format-buffer-smart ()
      "Reformat buffer if .clang-format exists in the projectile root."
      (interactive)
      (when (f-exists? (expand-file-name ".clang-format" (projectile-project-root)))
        (clang-format-buffer)))
    
    ;; https://eklitzke.org/smarter-emacs-clang-format
    ;(defun clang-format-buffer-smart-on-save ()
    ;  "Add auto-save hook for clang-format-buffer-smart."
    ;  (add-hook 'before-save-hook 'clang-format-buffer-smart nil t))
    
    
    ;;; hooks
    (add-hook 'c++-mode-hook 'company-mode)  ; 補完用
    (add-hook 'c-mode-hook   'company-mode)  ; 補完用
    (add-hook 'c++-mode-hook 'flycheck-mode) ; チェック用
    (add-hook 'c-mode-hook   'flycheck-mode) ; チェック用
    (add-hook 'c++-mode-hook #'lsp)
    (add-hook 'c-mode-hook   #'lsp)
    
    
    ;;; key bindings
    ;;; 以下は clang-format の README.md の記載より転記した。
    (global-set-key (kbd "C-c i") 'clang-format-region)
    (global-set-key (kbd "C-c u") 'clang-format-buffer-smart)
    ; (global-set-key (kbd "C-c i") 'clang-format-region)
    ; (global-set-key (kbd "C-c u") 'clang-format-buffer)
    ; (global-unset-key "\C-\M-q")
    ; (global-set-key (kbd "C-M q") 'clang-format-region)

    • clangd を呼び出せるようにする
      • clangd は、PATH が通ってない(または、PATH が通っている場所に、バージョン番号を含むファイル名で配置されている)場合がある。
        debian9 の場合は、以下にある:
        $ locate clangd
        /usr/bin/clangd-7   # 下記の SymLink
        /usr/lib/llvm-7/bin/clangd
        この場合、以下のように設定する:
        ;;; lsp-mode
        ;; https://qiita.com/false-git@github/items/3cb6ebd7f4d6f77acc54
        (setq lsp-clients-clangd-executable "clangd-7")
        ; または
        ; (setq lsp-clients-clangd-executable "/usr/bin/clangd-7")

フォーマットルールを設定する

用意されたフォーマットルールを利用する

;;; ~/.emacs/init.el
;;; format-style
(setq clang-format-style-option "llvm")  ; llvm スタイルのフォーマットルールを利用する場合

個別に設定を行う

参考 URI

clang-format-plus


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS