FreeStyleWiki

Emacsの設定(lsp-mode)

このエントリーをはてなブックマークに追加

[Emacs,C++,D]

Emacsの設定(lsp-mode)

  • かつてC++でタグジャンプをする場合、etags/ctags/gtagsを使ったりした
  • それができないときは"M-x find-grep"で気合で関数を探していた

一方でRedditでIs etags still the way to go, or are there better tools now?なるスレッドが立っていて

Have a look at lsp-mode + clangd for Cpp.

なんと!

Qiitaで検索するとC++でも language server protocol(LSP)? を導入している記事をちらほら見かけるようになったので、導入を試みる。

  Emacsでlspを使う選択肢

どうやらC++に関しては

  • lspのクライアント実装
  • lspのサーバ実装
    • ccls, clangd, cquery らしい

  Emacs eglot + ccls

cclsとeglotの導入が簡単そうなので、試す

debian(bullseye)にcclsインストール

$ sudo apt-get install ccls

eglotをインストール

Emacs上で以下のようにする

M-x package-install RET eglot RET

C++

  • C/C++/ObjCのソースコードをEmacsで開く
  • 「M-x eglot-ensure」する

と、画面右下にeglotの文字が出て起動する。うまくいっていればbufferに「 %* *EGLOT (プロジェクト名/c++-mode) events* 220433 Fundamental 」みたいのが出る

あとは通常のTAGでのジャンプのように「M-.」すれば使える

D

D言語ではlsp-serverにserve-dを使う

dubでGoのinstallのような機能があればいいのだが、なさそうなのでバイナリをDLしてパスの通ったところに置いておく

$ cd ~/Downloads
$ wget https://github.com/Pure-D/serve-d/releases/download/v0.7.4/serve-d_0.7.4-linux-x86_64.tar.xz

$ tar xvf serve-d_0.7.4-linux-x86_64.tar.xz
$ chmod 755 serve-d
$ sudo mv serve-d /usr/local/bin/serve-d

❯ which serve-d
/usr/local/bin/serve-d

.emacsに追記

(add-hook 'd-mode-hook 'eglot-ensure)
(add-to-list 'eglot-server-programs '(d-mode . ("/usr/local/bin/serve-d")))