spacemacs c++ 智能提示2

Posted csfreebird

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spacemacs c++ 智能提示2相关的知识,希望对你有一定的参考价值。

ubuntu 16.10上搞ycmd没弄好,正好看到irony是一个更好的配置。据说主要是安装简单。就折腾了一下。


在spacemacs中配置irony layer

用spacemacs创建irony layer的默认配置文件,然后修改packages.el文件:

;;; packages.el --- irony layer packages file for Spacemacs.
;;
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
;;
;; Author: Dean Chen <chenshu@ysten.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3

;;; Commentary:

;; See the Spacemacs documentation and FAQs for instructions on how to implement
;; a new layer:
;;
;;   SPC h SPC layers RET
;;
;;
;; Briefly, each package to be installed or configured by this layer should be
;; added to `irony-packages'. Then, for each package PACKAGE:
;;
;; - If PACKAGE is not referenced by any other Spacemacs layer, define a
;;   function `irony/init-PACKAGE' to load and initialize the package.

;; - Otherwise, PACKAGE is already referenced by another Spacemacs layer, so
;;   define the functions `irony/pre-init-PACKAGE' and/or
;;   `irony/post-init-PACKAGE' to customize the package as it is loaded.

;;; Code:

(defconst irony-packages
  '(
    company-irony
    company-irony-c-headers
    irony
    irony-eldoc
    flycheck-irony)
  "The list of Lisp packages required by the irony layer.

Each entry is either:

1. A symbol, which is interpreted as a package to be installed, or

2. A list of the form (PACKAGE KEYS...), where PACKAGE is the
    name of the package to be installed or loaded, and KEYS are
    any number of keyword-value-pairs.

    The following keys are accepted:

    - :excluded (t or nil): Prevent the package from being loaded
      if value is non-nil

    - :location: Specify a custom installation location.
      The following values are legal:

      - The symbol `elpa' (default) means PACKAGE will be
        installed using the Emacs package manager.

      - The symbol `local' directs Spacemacs to load the file at
        `./local/PACKAGE/PACKAGE.el'

      - A list beginning with the symbol `recipe' is a melpa
        recipe.  See: https://github.com/milkypostman/melpa#recipe-format")


(defun irony/init-irony ()
  (use-package irony
    :defer t
    :commands (irony-mode irony-install-server)
    :init
    (progn
      (add-hook 'c-mode-hook 'irony-mode)
      (add-hook 'c++-mode-hook 'irony-mode))
    :config
    (progn
      (setq irony-user-dir (f-slash (f-join user-home-directory "bin" "irony")))
      (setq irony-server-install-prefix irony-user-dir)
      (setq irony-cdb-search-directory-list "/home/dean/work/gitlab/gitlab.com/mystudy/mongodb/code/simple/")
      (add-hook 'c++-mode-hook (lambda () (setq irony-additional-clang-options '("-std=c++11"))))
      (defun irony/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 'irony/irony-mode-hook)
      (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options))))

;;?
(when (configuration-layer/layer-usedp 'auto-completion)
  (defun irony/init-company-irony ()
    (use-package company-irony
      :if (configuration-layer/package-usedp 'company)
      :defer t
      :commands company-irony
      :init
      (progn
        (push 'company-irony company-backends-c-mode-common)
        (add-hook 'irony-mode-hook 'company-irony-setup-begin-commands))
      )))

;;

(when (configuration-layer/layer-usedp 'auto-completion)
  (defun irony/init-company-irony-c-headers ()
    (use-package company-irony-c-headers
      :if (configuration-layer/package-usedp 'company)
      :defer t
      :commands company-irony-c-headers
      :init
      (push 'company-irony-c-headers company-backends-c-mode-common))))

(when (configuration-layer/layer-usedp 'syntax-checking)
  (defun irony/init-flycheck-irony ()
    (use-package flycheck-irony
      :if (configuration-layer/package-usedp 'flycheck)
      :defer t
      :init (add-hook 'irony-mode-hook 'flycheck-irony-setup))))

(defun irony/init-irony-eldoc ()
  (use-package irony-eldoc
    :commands (irony-eldoc)
    :init
    (add-hook 'irony-mode-hook 'irony-eldoc)))


;;; packages.el ends here


注意这行配置,可以不断的增加项目目录:

(setq irony-cdb-search-directory-list "/home/dean/work/gitlab/gitlab.com/mystudy/mongodb/code/simple/")

重新启动emacs后,会自动安装所需package.


配置编译库(compilation database)

在项目根目录下创建.clang_complete文件,做一些配置:

-DDEBUG
-I./include
-I/usr/include/c++/6.2.0/

编译库有多种,irony支持不少。这是使用.clang_complete文件的方式。

具体参考:

https://github.com/Rip-Rip/clang_complete/blob/c8673142759b87316265eb0edd1f620196ec1fba/doc/clang_complete.txt#L55

https://sarcasm.github.io/notes/dev/compilation-database.html


测试

现在打开项目,第一次使用需要输入M-x company-complete命令(以后就直接自动提示了) . 效果图如下:



跳到错误位置

C-c ! n 或者 M-x flycheck-next-error



以上是关于spacemacs c++ 智能提示2的主要内容,如果未能解决你的问题,请参考以下文章

用spacemacs打造c++ IDE

spacemacs c++代码跳转最简单的方法:rgrep

spacemacs 使用gdb

vs2008 c++没有智能提示

vs2008中c++为啥代码没有智能提示

Spacemace 禁用ensime启动通知buffer