使用 cquery: C++ language server

2017-12-09 13:00:30 +08:00
 MaskRay

原文 https://maskray.me/blog/2017-12-03-c++-language-server-cquery

C++代码索引工具现状

Tag system 流派

clang 流派

IDE(Any sufficiently complicated IDE contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of C++.)

cquery 安装、配置

https://github.com/jacobdufault/cquery。Arch Linux 可用aur/cquery-git

Emacs

安装lsp-mode,参照https://github.com/jacobdufault/cquery/wiki/Emacs配置。lsp-mode 会设置xref-backend-functions,把以下函数

定向到 lsp-mode 中的处理函数,发送textDocument/definition等请求并渲染。

如果使用helm,可以考虑安装helm-xrefxref-show-xrefs-function会使用helm-xref-show-xrefs。但目前有两个 issues 影响了我的使用体验:

注意协议中定义返回结果为Location[] | null,只含位置信息,不包含代码行内容,如果要显示行内容。若文件在某个 buffer 内,则显示该 buffer 相应行;若未打开则需要打开该文件。

Emacs Lisp dynamic scoping 使得我们可以很容易复用已有代码。可以基于 evil-jumps 做一个用于 xref 的 jump list。我喜欢 xref jump list 和正常 jump list 分离。因为查找定义 /引用后会进行一些局部跳转,喜欢有快捷键回到定义 /引用跳转前的位置。并且需要能双向移动,不能只是 jump stack,xref.el用 ring buffer 实现的是 stack。

(defmacro my-xref//with-evil-jumps (&rest body)
  "Make `evil-jumps.el' commands work on `my-xref--jumps'."
  (declare (indent 1))
  `(let ((evil--jumps-window-jumps ,my-xref--jumps))
     ,@body))

(with-eval-after-load 'evil-jumps
  (evil-define-motion my-xref/evil-jump-backward (count)
    (my-xref//with-evil-jumps
        (evil--jump-backward count)
      (run-hooks 'xref-after-return-hook)))

  (evil-define-motion my-xref/evil-jump-forward (count)
    (my-xref//with-evil-jumps
        (evil--jump-forward count)
      (run-hooks 'xref-after-return-hook))))

最近的新包https://github.com/emacs-lsp/lsp-ui包含 code lens、flycheck 等功能,以及一个基于quick-peekfind-{definitions,references,apropos}

https://github.com/MaskRay/Config/blob/master/home/.emacs.d/layers/%2Bmy/my-code/funcs.el

Neovim

参照https://github.com/autozimu/LanguageClient-neovim/wiki/cquery

nn <leader>ji :Denite documentSymbol<cr>
nn <leader>jI :Denite workspaceSymbol<cr>
" 终端限制,<C-,>不可用。ord(`,`) & 64 为 0 无法表示
nn <M-,> :Denite references<cr>
nn <silent> <C-j> :MarkPush<cr>:call LanguageClient_textDocument_definition()<cr>

不清楚怎么把定义 /引用改造成使用可双向移动的 jump list。

生成compile_commands.json

CMake

% mkdir build
% (cd build; cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=YES ..)
% ln -s build/compile_commands.json

Build EAR

Bear is a tool that generates a compilation database for clang tooling. It can be used for any project based on Makefile.

bear make
# generates compile_commands.json

Ninja

ninja -t compdb rule_names... > compile_commands.json

问题

其他

索引 Linux kernel

wget 'https://git.archlinux.org/svntogit/packages.git/plain/trunk/config?h=packages/linux' -O .config
yes '' | make config
bear make -j bzImage modules

生成 3GiB 文件。

索引 llvm,du -sh => 1.1GB,索引完内存占用 2G。

查看 LSP requests/responses

sudo sysdig -As999 --unbuffered -p '%evt.type %evt.buffer' "proc.pid=$(pgrep -fn build/app) and fd.type=pipe" | egrep -v '^Content|^$'

生成compile_commands.json,参考https://github.com/jacobdufault/cquery/wiki

希望有朝一日 Debug Protocol 也能获得重视,https://github.com/Microsoft/vscode-debugadapter-node/blob/master/protocol/src/debugProtocol.ts,让realgud轻松一点。

和 YouCompleteMe 等项目一样,cquery 默认下载prebuilt clang+llvm,即.h .so .a。用户不需要编译完整的 llvm,开发门槛比 clangd 低。

感谢 ngkaho1234。

5580 次点击
所在节点    Vim
18 条回复
glacier2002
2017-12-09 16:30:09 +08:00
完全没看懂。。。。
htfy96
2017-12-09 16:40:07 +08:00
cquery 感觉的确是现代的方案…时间不太够只能维护下 cquery-git 了
forestyuan
2017-12-09 21:46:23 +08:00
这是来推广的吗?
MaskRay
2017-12-10 05:41:07 +08:00
@glacier2002 原文裏有幾張圖,內容改了點……這裏編輯困難
MaskRay
2017-12-10 05:42:15 +08:00
@forestyuan 是的。希望能找到更多有 C/C++代碼閱讀需求的 geek 用戶貢獻這個項目。很多 code assistant 功能靠 clangd 這種大教堂模式是搞不好的
YeT9
2017-12-10 10:23:58 +08:00
Ray 教授啊!火钳流明~资瓷一个~
congeec
2017-12-10 11:18:29 +08:00
cquery 目前 hover definition 不能显示函数签名,跟 YouCompleteMe 比还差好多。不过能重构,也是屌屌的
ivechan
2017-12-10 15:48:00 +08:00
之前试用了一下 clangd, 实在太难用了,有时候性能极差,还不如直接用 fzf ack 这类工具。
skt041959
2017-12-10 16:54:48 +08:00
LanguageClient-neovim 是 rust 写的,没法编译。试了一下 vim-lsp 好像不会用。有其他支持的 vim/neovim LSP plugin 么?
bookit
2017-12-10 20:03:44 +08:00
善,哪一天能赶上 vax 就好了。
autozimu
2017-12-11 10:49:49 +08:00
@skt041959 什么是没法编译?

大多数情况应该是不需要编译的, 已经有编译好的 binaries 了呀
congeec
2017-12-11 12:46:26 +08:00
@autozimu 谢谢你的作品
ivechan
2017-12-22 15:27:20 +08:00
之前看了但是还没尝试, 今天去尝试了一下, 在 vim 下体验比其他的好。谢谢。
5thcat
2018-01-06 01:52:11 +08:00
非常感谢! 终于找到了失散多年的阅读代码工具!
skt041959
2018-01-08 13:23:44 +08:00
@autozimu #11 之前理解错了,以为必须编译 rust 才能用。其实不用编译,直接用 python 的就可以了
bef0rewind
2018-01-26 09:40:55 +08:00
很有意思的项目啊
f2ed
2018-04-18 17:38:07 +08:00
我用的 vscode-cquery,表示 C++符号经常找不到啊,c 语言还可以
MaskRay
2018-05-14 14:55:10 +08:00
原來都已經半年了……我現在用自己的 fork https://github.com/MaskRay/ccls

移除了不必要的第三方依賴
import_pipeline.cc 比原來簡單的多,效果更好(index merge, id map 都是 cquery 中不妥當的設計,我上週移除了)
mem index 有少量提升

https://github.com/autozimu/LanguageClient-neovim/issues/293 自定義 cross reference 能支持就最好了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/413308

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX