Top/Editor/emacs/lsp-mode

目次

lsp-mode

環境構築

  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
        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)
        

  4. 設定を記載する
    ;;; lsp-mode
    ;; https://qiita.com/false-git@github/items/3cb6ebd7f4d6f77acc54
     (setq lsp-clients-clangd-executable "/usr/bin/clangd-7")
    
    ;; 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)
    
    (with-eval-after-load 'lsp-mode
      (custom-set-variables
       '(lsp-prefer-flymake nil))              ; flycheckを優先する
    
      (define-key lsp-mode-map (kbd "C-c C-d") 'lsp-describe-thing-at-point)
      (define-key lsp-mode-map (kbd "C-c C-e") 'lsp-workspace-restart)
      (define-key lsp-mode-map (kbd "C-c C-i") 'lsp-format-buffer)
      (define-key lsp-mode-map (kbd "C-c C-n") 'lsp-rename)
      (define-key lsp-mode-map (kbd "C-c C-r") 'lsp-execute-code-action)
      )
    

参考 URI


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-08-05 (水) 20:37:13