[cmake] Add lsp server support

This commit is contained in:
Maximilian Wolff 2020-08-13 23:22:11 +02:00
parent 3216aedb27
commit 76301564c5
No known key found for this signature in database
GPG key ID: 2DD07025BFDBD89A
5 changed files with 128 additions and 20 deletions

View file

@ -8,28 +8,79 @@
- [[#description][Description]]
- [[#features][Features:]]
- [[#install][Install]]
- [[#layer][Layer]]
- [[#cmake-ide-configuration][CMake-ide configuration]]
- [[#configuration][Configuration]]
- [[#choosing-a-backend][Choosing a backend]]
- [[#company-cmake][Company-cmake]]
- [[#lsp][LSP]]
- [[#cmake-ide][CMake-ide]]
- [[#key-bindings][Key bindings]]
* Description
This layer adds support [[https://cmake.org/][CMake]] scripts.
** Features:
- Support for CMake configure/build (with limited support for other build systems),
automatic generation of =compile_commands.json= (compile flags), on-the-fly configuration
of flycheck, company-clang and RTags (if installed) with [[https://github.com/atilaneves/cmake-ide][cmake-ide]].
- Syntax highlighting
- Auto-completion
- Support for CMake configure/build (with limited support for other build systems) and
automatic generation of =compile_commands.json= (compile flags).
- Run selected test using =Helm= interface via =helm-ctest=.
- Extraction of compile parameters from =compile_commands.json= with [[https://github.com/atilaneves/cmake-ide][cmake-ide]]
* Install
** Layer
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =cmake= to the existing =dotspacemacs-configuration-layers= list in this
file.
** CMake-ide configuration
* Configuration
All layer configurations can be done by setting layer variables in your dotfile.
No custom user config lines are necessary
** Choosing a backend
This layer provides two alternative backends to choose from.
*** Company-cmake
This is the default choice if nothing is set and no lsp layer
is loaded in your dotfile. This mode only provides very
limited IDE capabilities. Used best if only small scripts
are edited. To set explicitly set the following in your
dotfile:
#+BEGIN_SRC emacs-lisp
(cmake :variables cmake-backend 'company-cmake)
#+END_SRC
*** LSP
For proper IDE support this backend should be used. It is
based on an external server which will be started automatically
by emacs, once a cmake file is opened. The key bindings are
the same for all lsp modes so if you are already familiar with
one you should be able to work the same in all modes.
To set explicitly do the following in your dotfile:
#+BEGIN_SRC emacs-lisp
(cmake :variables
cmake-backend 'lsp)
#+END_SRC
For this to work you will also need to obtain
the latest version of the lsp server from [[https://github.com/regen100/cmake-language-server][here]].
You can also simply install it with pip:
#+BEGIN_SRC sh
pip install cmake-language-server
#+END_SRC
NOTE: Key bindings for LSP are defined in the
LSP layer. Also it is advisable to have a look
at the autocomplete layer for an optimal
intellisense config for LSP.
** CMake-ide
To enable CMake projects support set the layer variable =cmake-enable-cmake-ide-support=
to =t= in the dotfile:
to =t= in your dotfile:
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
@ -37,8 +88,11 @@ to =t= in the dotfile:
#+END_SRC
=cmake-ide= plugin has several useful configuration options.
Normally all should just work well as long as there is a standard
cmake compilation database available. If not some manual configuration
may be necessary. Details can be found below.
To configure project you need to create =.dir-locals.el= file. In case of using
To configure a project you need to create =.dir-locals.el= file. In case of using
make as CMake backend you can use =helm-make= to select required build target.
Here is a sample configuration. This configuration forces =cmake-ide= to use the

View file

@ -1,6 +1,6 @@
;;; config.el --- CMake layer config file for Spacemacs.
;;
;; Copyright (c) 2018 Sylvain Benner & Contributors
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Alexander Dalshov <dalshov@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
@ -16,3 +16,8 @@
(defvar cmake-enable-cmake-ide-support nil
"If non nil CMake related packages and configuration are enabled.")
(defvar cmake-backend nil
"The backend to use for IDE features.
Possible values are `lsp' and `company-cmake'.
If `nil' then 'company-cmake` is the default backend unless `lsp' layer is used")

View file

@ -0,0 +1,35 @@
;;; funcs.el --- CMake Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Maximilian Wolff <smile13241324@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs//cmake-backend ()
"Returns selected backend."
(if cmake-backend
cmake-backend
(cond
((configuration-layer/layer-used-p 'lsp) 'lsp)
(t 'company-cmake))))
(defun spacemacs//cmake-setup-company ()
"Conditionally setup company based on backend."
(pcase (spacemacs//cmake-backend)
;; Activate lsp company explicitly to activate
;; standard backends as well
(`lsp (spacemacs|add-company-backends
:backends company-capf
:modes cmake-mode))
(`company-cmake (spacemacs|add-company-backends
:backends company-cmake
:modes cmake-mode))))
(defun spacemacs//cmake-setup-backend ()
"Conditionally setup cmake backend."
(pcase (spacemacs//cmake-backend)
(`lsp (lsp))))

View file

@ -0,0 +1,14 @@
;;; layers.el --- cmake Layer layers File for Spacemacs
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Maximilian Wolff <smile13241324@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(when (and (boundp 'cmake-backend)
(eq cmake-backend 'lsp))
(configuration-layer/declare-layer-dependencies '(lsp)))

View file

@ -1,6 +1,6 @@
;;; packages.el --- CMake layer packages file for Spacemacs.
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Alexander Dalshov <dalshov@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
@ -8,13 +8,12 @@
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(setq cmake-packages
'(
cmake-ide
cmake-mode
company
(helm-ctest :requires helm)
))
(defconst cmake-packages
'(
(cmake-ide :toggle cmake-enable-cmake-ide-support)
cmake-mode
company
(helm-ctest :requires helm)))
(defun cmake/init-cmake-ide ()
(use-package cmake-ide
@ -38,10 +37,11 @@
(defun cmake/init-cmake-mode ()
(use-package cmake-mode
:defer t
:mode (("CMakeLists\\.txt\\'" . cmake-mode) ("\\.cmake\\'" . cmake-mode))))
:mode (("CMakeLists\\.txt\\'" . cmake-mode) ("\\.cmake\\'" . cmake-mode))
:init (add-hook 'cmake-mode-hook #'spacemacs//cmake-setup-backend)))
(defun cmake/post-init-company ()
(spacemacs|add-company-backends :backends company-cmake :modes cmake-mode))
(spacemacs//cmake-setup-company))
(defun cmake/init-helm-ctest ()
(use-package helm-ctest