forked from doomemacs/doomemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.el
290 lines (248 loc) · 11.2 KB
/
config.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
;;; lang/cc/config.el --- c, c++, and obj-c -*- lexical-binding: t; -*-
(defvar +cc-default-include-paths
(list "include"
"includes")
"A list of default relative paths which will be searched for up from the
current file, to be passed to irony as extra header search paths. Paths can be
absolute. This is ignored if your project has a compilation database.
This is ignored by ccls.")
(defvar +cc-default-header-file-mode 'c-mode
"Fallback major mode for .h files if all other heuristics fail (in
`+cc-c-c++-objc-mode').")
(defvar +cc-default-compiler-options
`((c-mode . nil)
(c++-mode
. ,(list "-std=c++1z" ; use C++17 draft by default
(when IS-MAC
;; NOTE beware: you'll get abi-inconsistencies when passing
;; std-objects to libraries linked with libstdc++ (e.g. if you
;; use boost which wasn't compiled with libc++)
"-stdlib=libc++")))
(objc-mode . nil))
"A list of default compiler options for the C family. These are ignored if a
compilation database is present in the project.
This is ignored by ccls.")
;;
;;; Packages
(use-package! cc-mode
:mode ("\\.mm\\'" . objc-mode)
;; Use `c-mode'/`c++-mode'/`objc-mode' depending on heuristics
:mode ("\\.h\\'" . +cc-c-c++-objc-mode)
;; Ensure find-file-at-point recognize system libraries in C modes. It must be
;; set up before the likes of irony/lsp are initialized. Also, we use
;; local-vars hooks to ensure these only run in their respective major modes,
;; and not their derived modes.
:hook ((c-mode-local-vars c++-mode-local-vars objc-mode-local-vars) . +cc-init-ffap-integration-h)
;;; Improve fontification in C/C++ (also see `modern-cpp-font-lock')
:hook (c-mode-common . rainbow-delimiters-mode)
:hook ((c-mode c++-mode) . +cc-fontify-constants-h)
:config
(set-docsets! 'c-mode "C")
(set-docsets! 'c++-mode "C++" "Boost")
(set-electric! '(c-mode c++-mode objc-mode java-mode) :chars '(?\n ?\} ?\{))
(set-rotate-patterns! 'c++-mode
:symbols '(("public" "protected" "private")
("class" "struct")))
(set-pretty-symbols! '(c-mode c++-mode)
;; Functional
;; :def "void "
;; Types
:null "nullptr"
:true "true" :false "false"
:int "int" :float "float"
:str "std::string"
:bool "bool"
;; Flow
:not "!"
:and "&&" :or "||"
:for "for"
:return "return"
:yield "#require")
;; HACK Suppress 'Args out of range' error in when multiple modifications are
;; performed at once in a `c++-mode' buffer, e.g. with `iedit' or
;; multiple cursors.
(undefadvice! +cc--suppress-silly-errors-a (orig-fn &rest args)
:around #'c-after-change-mark-abnormal-strings
(ignore-errors (apply orig-fn args)))
;; Custom style, based off of linux
(setq c-basic-offset tab-width
c-backspace-function #'delete-backward-char)
(c-add-style
"doom" '((c-comment-only-line-offset . 0)
(c-hanging-braces-alist (brace-list-open)
(brace-entry-open)
(substatement-open after)
(block-close . c-snug-do-while)
(arglist-cont-nonempty))
(c-cleanup-list brace-else-brace)
(c-offsets-alist
(knr-argdecl-intro . 0)
(substatement-open . 0)
(substatement-label . 0)
(statement-cont . +)
(case-label . +)
;; align args with open brace OR don't indent at all (if open
;; brace is at eolp and close brace is after arg with no trailing
;; comma)
(brace-list-intro . 0)
(brace-list-close . -)
(arglist-intro . +)
(arglist-close +cc-lineup-arglist-close 0)
;; don't over-indent lambda blocks
(inline-open . 0)
(inlambda . 0)
;; indent access keywords +1 level, and properties beneath them
;; another level
(access-label . -)
(inclass +cc-c++-lineup-inclass +)
(label . 0))))
(when (listp c-default-style)
(setf (alist-get 'other c-default-style) "doom"))
(after! ffap
(add-to-list 'ffap-alist '(c-mode . ffap-c-mode))))
(use-package! modern-cpp-font-lock
:hook (c++-mode . modern-c++-font-lock-mode))
(use-package! irony
:unless (featurep! +lsp)
:commands irony-install-server
;; Initialize compilation database, if present. Otherwise, fall back on
;; `+cc-default-compiler-options'.
:hook (irony-mode . +cc-init-irony-compile-options-h)
;; Only initialize `irony-mode' if the server is available. Otherwise fail
;; quietly and gracefully.
:hook ((c-mode-local-vars c++-mode-local-vars objc-mode-local-vars) . +cc-init-irony-mode-maybe-h)
:preface (setq irony-server-install-prefix (concat doom-etc-dir "irony-server/"))
:config
(defun +cc-init-irony-mode-maybe-h ()
(if (file-directory-p irony-server-install-prefix)
(irony-mode +1)
(message "Irony server isn't installed")))
(setq irony-cdb-search-directory-list '("." "build" "build-conda"))
(use-package! irony-eldoc
:hook (irony-mode . irony-eldoc))
(use-package! flycheck-irony
:when (featurep! :checkers syntax)
:config (flycheck-irony-setup))
(use-package! company-irony
:when (featurep! :completion company)
:init (set-company-backend! 'irony-mode '(:separate company-irony-c-headers company-irony))
:config (require 'company-irony-c-headers)))
;;
;; Major modes
(after! cmake-mode
(set-docsets! 'cmake-mode "CMake"))
(use-package! company-cmake ; for `cmake-mode'
:when (featurep! :completion company)
:after cmake-mode
:config (set-company-backend! 'cmake-mode 'company-cmake))
(use-package! demangle-mode
:hook llvm-mode)
(use-package! company-glsl ; for `glsl-mode'
:when (featurep! :completion company)
:after glsl-mode
:config (set-company-backend! 'glsl-mode 'company-glsl))
;;
;; Rtags Support
(use-package! rtags
:unless (featurep! +lsp)
;; Only initialize rtags-mode if rtags and rdm are available.
:hook ((c-mode-local-vars c++-mode-local-vars objc-mode-local-vars) . +cc-init-rtags-maybe-h)
:preface (setq rtags-install-path (concat doom-etc-dir "rtags/"))
:config
(defun +cc-init-rtags-maybe-h ()
"Start an rtags server in c-mode and c++-mode buffers.
If rtags or rdm aren't available, fail silently instead of throwing a breaking error."
(and (require 'rtags nil t)
(rtags-executable-find rtags-rdm-binary-name)
(rtags-start-process-unless-running)))
(setq rtags-autostart-diagnostics t
rtags-use-bookmarks nil
rtags-completions-enabled nil
rtags-display-result-backend
(cond ((featurep! :completion ivy) 'ivy)
((featurep! :completion helm) 'helm)
('default))
;; These executables are named rtags-* on debian
rtags-rc-binary-name
(or (cl-find-if #'executable-find (list rtags-rc-binary-name "rtags-rc"))
rtags-rc-binary-name)
rtags-rdm-binary-name
(or (cl-find-if #'executable-find (list rtags-rdm-binary-name "rtags-rdm"))
rtags-rdm-binary-name)
;; If not using ivy or helm to view results, use a pop-up window rather
;; than displaying it in the current window...
rtags-results-buffer-other-window t
;; ...and don't auto-jump to first match before making a selection.
rtags-jump-to-first-match nil)
(set-lookup-handlers! '(c-mode c++-mode)
:definition #'rtags-find-symbol-at-point
:references #'rtags-find-references-at-point)
;; Use rtags-imenu instead of imenu/counsel-imenu
(define-key! (c-mode-map c++-mode-map) [remap imenu] #'+cc/imenu)
;; Ensure rtags cleans up after itself properly when exiting Emacs, rather
;; than display a jarring confirmation prompt for killing it.
(add-hook! 'kill-emacs-hook (ignore-errors (rtags-cancel-process)))
(add-hook 'rtags-jump-hook #'better-jumper-set-jump)
(add-hook 'rtags-after-find-file-hook #'recenter))
;;
;; LSP
(when (featurep! +lsp)
(add-hook! '(c-mode-local-vars-hook
c++-mode-local-vars-hook
objc-mode-local-vars-hook)
(defun +cc-init-lsp-h ()
(setq-local company-transformers nil)
(setq-local company-lsp-async t)
(setq-local company-lsp-cache-candidates nil)
(lsp!))))
(when (and (featurep! +lsp) (featurep! :tools lsp +eglot))
;; TODO : test this value
;; IS-MAC custom configuration
(when IS-MAC
(add-to-list 'eglot-workspace-configuration
((:ccls . ((:clang . ,(list :extraArgs ["-isystem/Library/Developer/CommandLineTools/usr/include/c++/v1"
"-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
"-isystem/usr/local/include"]
:resourceDir (string-trim (shell-command-to-string "clang -print-resource-dir")))))))))
;; Eglot specific helper, courtesy of MaskRay
(defun eglot-ccls-inheritance-hierarchy (&optional derived)
"Show inheritance hierarchy for the thing at point.
If DERIVED is non-nil (interactively, with prefix argument), show
the children of class at point."
(interactive "P")
(if-let* ((res (jsonrpc-request
(eglot--current-server-or-lose)
:$ccls/inheritance
(append (eglot--TextDocumentPositionParams)
`(:derived ,(if derived t :json-false))
'(:levels 100) '(:hierarchy t))))
(tree (list (cons 0 res))))
(with-help-window "*ccls inheritance*"
(with-current-buffer standard-output
(while tree
(pcase-let ((`(,depth . ,node) (pop tree)))
(cl-destructuring-bind (&key uri range) (plist-get node :location)
(insert (make-string depth ?\ ) (plist-get node :name) "\n")
(make-text-button (+ (point-at-bol 0) depth) (point-at-eol 0)
'action (lambda (_arg)
(interactive)
(find-file (eglot--uri-to-path uri))
(goto-char (car (eglot--range-region range)))))
(cl-loop for child across (plist-get node :children)
do (push (cons (1+ depth) child) tree)))))))
(eglot--error "Hierarchy unavailable"))))
(use-package! ccls
:when (and (featurep! +lsp) (not (featurep! :tools lsp +eglot)))
:after lsp
:init
(after! projectile
(add-to-list 'projectile-globally-ignored-directories ".ccls-cache")
(add-to-list 'projectile-project-root-files-bottom-up ".ccls-root")
(add-to-list 'projectile-project-root-files-top-down-recurring "compile_commands.json"))
:config
(when IS-MAC
(setq ccls-initialization-options
`(:clang ,(list :extraArgs ["-isystem/Library/Developer/CommandLineTools/usr/include/c++/v1"
"-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
"-isystem/usr/local/include"]
:resourceDir (string-trim (shell-command-to-string "clang -print-resource-dir")))))))