Skip to content

Commit 02c64e6

Browse files
authored
Auto detect macos clangd (#2161)
* Auto detect Xcode clangd * Fix lsp-clients-clangd-executable defcustom type error * Move executable-find util function to lsp-mode * fix quoting * Add return values to docstring * Fix typo and formatting
1 parent c2b480d commit 02c64e6

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

clients/lsp-clangd.el

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ available in your path to use. Otherwise the system will try to
165165
find a suitable one. Set this variable before loading lsp."
166166
:group 'lsp-clangd
167167
:risky t
168-
:type 'file)
168+
:type '(choice (file :tag "Path")
169+
(const :tag "Auto" nil)))
169170

170171
(defvar lsp-clients--clangd-default-executable nil
171172
"Clang default executable full path when found.
@@ -181,10 +182,12 @@ This must be set only once after loading the clang client.")
181182
"Generate the language server startup command."
182183
(unless lsp-clients--clangd-default-executable
183184
(setq lsp-clients--clangd-default-executable
184-
(-first #'executable-find
185-
(-map (lambda (version)
186-
(concat "clangd" version))
187-
'("" "-12" "-11" "-10" "-9" "-8" "-7" "-6")))))
185+
(or (-first #'executable-find
186+
(-map (lambda (version)
187+
(concat "clangd" version))
188+
'("" "-12" "-11" "-10" "-9" "-8" "-7" "-6")))
189+
(lsp-clients-executable-find "xcodebuild" "-find-executable" "clangd")
190+
(lsp-clients-executable-find "xcrun" "--find" "clangd"))))
188191

189192
`(,(or lsp-clients-clangd-executable lsp-clients--clangd-default-executable "clangd")
190193
,@lsp-clients-clangd-args))

lsp-mode.el

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,26 @@ BODY should never return `t' value."
15201520
download-in-progress?
15211521
buffers)
15221522

1523+
(defun lsp-clients-executable-find (find-command &rest args)
1524+
"Finds an executable by invoking a search command.
1525+
1526+
FIND-COMMAND is the executable finder that searches for the
1527+
actual language server executable. ARGS is a list of arguments to
1528+
give to FIND-COMMAND to find the language server. Returns the
1529+
output of FIND-COMMAND if it exits successfully, nil otherwise.
1530+
1531+
Typical uses include finding an executable by invoking 'find' in
1532+
a project, finding LLVM commands on macOS with 'xcrun', or
1533+
looking up project-specific language servers for projects written
1534+
in the various dynamic languages, e.g. 'nvm', 'pyenv' and 'rbenv'
1535+
etc."
1536+
(when-let* ((find-command-path (executable-find find-command))
1537+
(executable-path
1538+
(with-temp-buffer
1539+
(when (zerop (apply 'call-process find-command-path nil t nil args))
1540+
(buffer-substring-no-properties (point-min) (point-max))))))
1541+
(string-trim executable-path)))
1542+
15231543
(defvar lsp--already-widened nil)
15241544

15251545
(defmacro lsp-save-restriction-and-excursion (&rest form)

0 commit comments

Comments
 (0)