-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcmake-integration-language-servers.el
More file actions
241 lines (179 loc) · 9.14 KB
/
cmake-integration-language-servers.el
File metadata and controls
241 lines (179 loc) · 9.14 KB
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
;;; cmake-integration-language-servers.el --- Some utility functions for language servers -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Darlan Cavalcante Moreira
;; Author: Darlan Cavalcante Moreira <darcamo@gmail.com>
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is not part of GNU Emacs
;; This file is part of cmake-integration.
;; cmake-integration is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; cmake-integration is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with cmake-integration. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file defines some utility functions to help with configuring language
;; server.
;; Note: All functions in this file have "-lsp-" as part of their name to
;; indicate that they are related to language servers.
;;; Code:
(require 'cmake-integration-configure)
(defgroup cmake-integration-lsp nil
"Better integration with language servers."
:group 'cmake-integration
:prefix "cmake-integration-")
(defvar ci--lsp-previous-build-folder nil
"Build folder used in the last update of language server configuration files.")
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxx clangd xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(defcustom ci-setup-clangd 'link
"How to update configuration for clangd language server.
The clangd language server can utilize the `compile_commands.json` file
generated by CMake to provide better code analysis. One way to make
clangd find this file is to create a symbolic link to it in the
project's root directory.
Another possible way is to create or update a `.clangd` file in the
project's root directory with the path to the `compile_commands.json`
file.
An example yaml content for the `.clangd` file is:
CompileFlags:
CompilationDatabase: /path/to/your/build/directory"
:type
'(choice
(const :tag "Do nothing" nil)
(const :tag "Create a link for compile_commands.json" link)
(const :tag "Update an existing \".clangd\" file" update)
(const :tag "Create and update \".clangd\" file" create))
:group 'cmake-integration-lsp)
(defun ci--lsp-get-clangd-config-file-path ()
"Get the path to the clangd configuration file."
(if-let* ((project-root (ci--get-project-root-folder)))
(expand-file-name ".clangd" project-root)))
(defun ci--lsp-create-clangd-config-file (config-file-path build-folder)
"Create the clangd configuration file and set the current BUILD-FOLDER.
The configuration file is created at CONFIG-FILE-PATH."
(with-temp-file config-file-path
(insert "CompileFlags:\n")
(insert (format " CompilationDatabase: \"%s\"\n" build-folder))))
(defun ci--lsp-update-clangd-config-file (config-file-path build-folder)
"Update the clangd configuration file at CONFIG-FILE-PATH.
The build folder in `buildDir` is set to BUILD-FOLDER."
(with-temp-buffer
(insert-file-contents config-file-path)
(goto-char (point-min))
(if (re-search-forward "^\\( +CompilationDatabase:\\) *\\(.*\\)" nil t)
(replace-match build-folder nil nil nil 2)
(error
"The CompilationDatabase field was not found in the \".clangd\" file"))
(write-region (point-min) (point-max) config-file-path)))
(defun ci-lsp-update-clangd (&optional create-if-missing)
"Update the clangd Language Server configuration file.
If the configuration file does not exist and CREATE-IF-MISSING is
non-nil, create a new configuration file."
(interactive)
(if-let* ((config-file-path (ci--lsp-get-clangd-config-file-path))
(build-folder (file-truename (ci-get-build-folder)))
(relative-build-folder
(file-relative-name build-folder (ci--get-project-root-folder))))
(if (f-exists? config-file-path)
;; Update existing file
(ci--lsp-update-clangd-config-file
config-file-path relative-build-folder)
;; Create a new file
(when create-if-missing
(ci--lsp-create-clangd-config-file
config-file-path relative-build-folder)))))
(defun ci--lsp-create-link-to-compile-commands ()
"Create a symbolic link to the compile_commands file in the project root."
(let* ((compile-commands-file
(file-name-concat (ci-get-build-folder) "compile_commands.json"))
(destination-directory (ci--get-project-root-folder))
(target
(file-relative-name compile-commands-file destination-directory)))
(make-symbolic-link target destination-directory t)))
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxx QML Language Server xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(defcustom ci-setup-qmlls 'update
"How to update configuration for the QML langage server.
The QML language server (qmlls) utilizes a configuration file named
\".qmlls.ini\" situated in the project's root directory. The
\"buildDir\" field within this file can be updated to reflect the build
directory corresponding to the selected configuration preset."
:type
'(choice
(const :tag "Do nothing" nil)
(const :tag "Update an existing \".qmlls.ini\" file" update)
(const :tag "Update \".qmlls.ini\" and create if it does not exist" create))
:group 'cmake-integration-lsp)
(defun ci--lsp-get-qmlls-config-file-path ()
"Get the path to the qmlls configuration file."
(if-let* ((project-root (ci--get-project-root-folder)))
(expand-file-name ".qmlls.ini" project-root)))
(defun ci--lsp-create-qmlls-config-file (config-file-path build-folder)
"Create the qmlls configuration file and set the current BUILD-FOLDER.
The configuration file is created at CONFIG-FILE-PATH."
(with-temp-file config-file-path
(insert "[General]\n")
(insert (format "buildDir=\"%s\"\n" build-folder))))
(defun ci--lsp-update-qmlls-config-file (config-file-path build-folder)
"Update the qmlls configuration file at CONFIG-FILE-PATH.
The build folder in `buildDir` is set to BUILD-FOLDER."
(with-temp-buffer
(insert-file-contents config-file-path)
(goto-char (point-min))
(if (re-search-forward "^buildDir=.*$" nil t)
(replace-match (concat "buildDir=\"" build-folder "\""))
(error "The buildDir field was not found in the INI file"))
(write-region (point-min) (point-max) config-file-path)))
(defun ci-lsp-update-qmlls (&optional create-if-missing)
"Update the QML Language Server configuration file.
If the configuration file does not exist and CREATE-IF-MISSING is
non-nil, create a new configuration file."
(interactive)
(if-let* ((config-file-path (ci--lsp-get-qmlls-config-file-path))
(build-folder (file-truename (ci-get-build-folder)))
(tramp-file-local-build-folder
(tramp-file-local-name build-folder)))
(if (f-exists? config-file-path)
;; Update existing file
(ci--lsp-update-qmlls-config-file
config-file-path tramp-file-local-build-folder)
;; Create a new file
(when create-if-missing
(ci--lsp-create-qmlls-config-file
config-file-path tramp-file-local-build-folder)))))
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(defun ci--lsp-update-language-servers ()
"Update the configuration files of all supported language servers."
(let ((current-build-folder (ci-get-build-folder)))
(unless (string-equal ci--lsp-previous-build-folder current-build-folder)
(setq ci--lsp-previous-build-folder current-build-folder)
;; Update QML configuration
(when ci-setup-qmlls
(let ((create-if-missing (eq ci-setup-qmlls 'create)))
(ci-lsp-update-qmlls create-if-missing)))
;; Create a link for the compile_commands.json file
(when (eq ci-setup-clangd 'link)
(ci--lsp-create-link-to-compile-commands))
;; Update clangd configuration
(when (eq ci-setup-clangd 'update)
(ci-lsp-update-clangd))
;; Create clangd configuration file if missing
(when (eq ci-setup-clangd 'create)
(ci-lsp-update-clangd t)))))
(add-hook 'ci-after-configure-hook 'ci--lsp-update-language-servers)
(provide 'cmake-integration-language-servers)
;;; cmake-integration-language-servers.el ends here
;; Local Variables:
;; read-symbol-shorthands: (("ci-" . "cmake-integration-"))
;; End: