代码补全

 

最近看emacs-devel里面关于IDE的讨论,有人提到semantic和irony-mode,分别尝试了一下,配置起来都很复杂,其中semantic无法补全LLVM里面的SmallVector,发信到邮件列表求帮助,暂时没有反馈。于是又试了irony-mode,折腾了好几天,才从log里面发现libclang couldn’t parse这样的错误信息,试了一下,原来是我们的项目用gcc编译没问题,用clang编译报错。改正之后就能正常工作了,补全信息非常准确,唯一的不足就是慢,尤其是第一次补全,要等十几秒,可能是LLVM的文件都比较大吧。

上图的效果是配合company-mode使用(单独使用irony-mode的话,使用C-M-i补全),需要分别下载company-mode, irony-mode, 和company-irony。我的配置如下:

(require 'company)
(add-hook 'c++-mode-hook 'company-mode)
(add-hook 'c-mode-hook 'company-mode)

;;; irony-mode

(add-to-list 'load-path "~/elisp/3rd-party-lib/irony-mode/")
(require 'irony)
(require 'irony-cdb)
(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'c-mode-hook 'irony-mode)

(add-to-list 'load-path "~/elisp/3rd-party-lib/company-irony/")
(require 'company-irony)
(eval-after-load 'company
  '(progn
     (setq company-idle-delay 3)
     (add-to-list 'company-backends 'company-irony)))

;; replace the `completion-at-point' and `complete-symbol' bindings in
;; irony-mode's buffers by irony-mode's function
(defun my-irony-mode-hook ()
  (define-key irony-mode-map [remap completion-at-point]
    'irony-completion-at-point-async)
  (define-key irony-mode-map [remap complete-symbol]
    'irony-completion-at-point-async))
(add-hook 'irony-mode-hook 'my-irony-mode-hook)
(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)

(require 'irony-cdb-json)
(irony-cdb-json-add-compile-commands-path
 "~/src/llvm/trunk"
 "~/src/llvm/trunk/build/compile_commands.json")

需要最后一句手工配置LLVM项目是因为irony找不到compile_commands.json文件。产生这个文件的方法是添加一个cmake宏:-DCMAKE_EXPORT_COMPILE_COMMANDS=ON

Share on Google+Share on FacebookTweet about this on TwitterShare on LinkedInEmail this to someonePrint this page

4 thoughts on “代码补全

  1. Ben Ning says:

    博主在Windows平台上使用Emacs+irony方案么?我一用irony-server就报错退出了,直接在windows上弹出错误崩溃,不是在emacs里面出的。

发表评论

电子邮件地址不会被公开。 必填项已用*标注