Skip to content

Commit aafa987

Browse files
authored
Ask xcrun(1) for the sourcekit-lsp server path (#16)
Recent Xcode ships with the `sourcekit-lsp` server, but it is a "developer tool" and so it might not be in PATH. This updates the logic for discovering the executable to: 1. Use a `lsp-dependency` to specify the executable, and 2. Try `xcrun --find` if the executable is not in PATH. Additionally, the server is registered with `lsp-mode` automatically, through an autoload, so the package will just work™ after installation.
1 parent ff204ed commit aafa987

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

lsp-sourcekit.el

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,49 @@
4040
;; ---------------------------------------------------------------------
4141
;; Customization
4242
;; ---------------------------------------------------------------------
43+
(defgroup lsp-sourcekit nil
44+
"LSP support for swift, using sourcekit-lsp."
45+
:group 'lsp-mode
46+
:prefix "lsp-sourcekit-"
47+
:link '(url-link "https://github.com/apple/sourcekit-lsp"))
4348

44-
(defcustom lsp-sourcekit-executable
45-
"sourcekit"
46-
"Path of the lsp-sourcekit executable."
47-
:type 'file
48-
:group 'sourcekit)
49+
(defcustom lsp-sourcekit-executable "sourcekit-lsp"
50+
"Path of the sourcekit-lsp executable."
51+
:group 'lsp-sourcekit
52+
:type 'file)
4953

50-
(defcustom lsp-sourcekit-extra-args
51-
nil
54+
(defcustom lsp-sourcekit-extra-args nil
5255
"Additional command line options passed to the lsp-sourcekit executable."
5356
:type '(repeat string)
54-
:group 'sourcekit)
57+
:group 'lsp-sourcekit)
58+
59+
60+
;;;###autoload
61+
(defun lsp-sourcekit--find-executable-with-xcrun ()
62+
"sourcekit-lsp may be installed behind xcrun; if we can't find
63+
the `lsp-sourcekit-executable' on PATH, try it with xcrun."
64+
(and (not (file-name-absolute-p lsp-sourcekit-executable))
65+
(executable-find "xcrun")
66+
(with-demoted-errors "lsp-sourcekit: find server with xcrun(1): %S"
67+
(car-safe (process-lines "xcrun" "--find" lsp-sourcekit-executable)))))
68+
5569

5670
;; ---------------------------------------------------------------------
5771
;; Register lsp client
5872
;; ---------------------------------------------------------------------
73+
;;;###autoload
74+
(with-eval-after-load 'lsp-mode
75+
(lsp-dependency
76+
'sourcekit-lsp
77+
(list :system 'lsp-sourcekit-executable)
78+
(list :system #'lsp-sourcekit--find-executable-with-xcrun))
5979

60-
(defun lsp-sourcekit--lsp-command ()
61-
"Generate the language server startup command."
62-
`(,lsp-sourcekit-executable
63-
,@lsp-sourcekit-extra-args))
6480

65-
(lsp-register-client
66-
(make-lsp-client :new-connection (lsp-stdio-connection 'lsp-sourcekit--lsp-command)
67-
:major-modes '(swift-mode)
68-
:server-id 'sourcekit-ls))
81+
(lsp-register-client
82+
(make-lsp-client :new-connection (lsp-stdio-connection
83+
(apply-partially #'lsp-package-path 'sourcekit-lsp))
84+
:major-modes '(swift-mode)
85+
:server-id 'sourcekit-ls)))
6986

7087
(provide 'lsp-sourcekit)
7188
;;; lsp-sourcekit.el ends here

0 commit comments

Comments
 (0)