Skip to content

Commit 5fa2031

Browse files
committed
Add smart serch for dart sdk dir
1 parent 45186b3 commit 5fa2031

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lsp-dart.el

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
:group 'applications
3737
:link '(url-link :tag "GitHub" "https://github.com/emacs-lisp/lsp-java"))
3838

39-
(defcustom lsp-dart-sdk-dir "~/flutter/bin/cache/dart-sdk/"
40-
"Install directory for dart-sdk."
39+
(defcustom lsp-dart-sdk-dir nil
40+
"Install directory for dart-sdk.
41+
When nil, it will try to find the dart sdk from the dart or flutter executables
42+
in the PATH env."
4143
:group 'lsp-dart
4244
:risky t
4345
:type 'directory)
@@ -113,6 +115,16 @@ Defaults to side following treemacs default."
113115

114116
;;; Internal
115117

118+
(defun lsp-dart--find-sdk-dir ()
119+
"Find dart sdk by searching for dart executable or flutter cache dir."
120+
(-when-let (dart (or (executable-find "dart")
121+
(-when-let (flutter (executable-find "flutter"))
122+
(expand-file-name "cache/dart-sdk/bin/dart"
123+
(file-name-directory flutter)))))
124+
(-> dart
125+
(file-truename)
126+
(locate-dominating-file "bin"))))
127+
116128
(defun lsp-dart--outline-kind->icon (kind)
117129
"Maps an outline KIND to a treemacs icon symbol.
118130
Kinds from https://github.com/dart-lang/sdk/blob/master/pkg/analysis_server/tool/spec/generated/java/types/ElementKind.java"
@@ -252,9 +264,10 @@ It updates the Flutter outline view if it already exists."
252264
(defun lsp-dart--server-command ()
253265
"Generate LSP startup command."
254266
(or lsp-dart-server-command
255-
`(,(expand-file-name (f-join lsp-dart-sdk-dir "bin/dart"))
256-
,(expand-file-name (f-join lsp-dart-sdk-dir "bin/snapshots/analysis_server.dart.snapshot"))
257-
"--lsp")))
267+
(let ((sdk-dir (or lsp-dart-sdk-dir (lsp-dart--find-sdk-dir))))
268+
`(,(expand-file-name (f-join sdk-dir "bin/dart"))
269+
,(expand-file-name (f-join sdk-dir "bin/snapshots/analysis_server.dart.snapshot"))
270+
"--lsp"))))
258271

259272
(defun lsp-dart--handle-closing-labels (_workspace params)
260273
"Closing labels notification handling.

0 commit comments

Comments
 (0)