Skip to content

Commit

Permalink
Fix find definition of external files on Flutter, not asking for proj…
Browse files Browse the repository at this point in the history
…ect root anymore
  • Loading branch information
ericdallo committed Oct 29, 2022
1 parent 33455a4 commit a8939d6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Fix `lsp-dart-run` when default-directory is not the project root. #173
* Add `lsp-dart-project-root-discovery-strategies` variable to search project root with different startegies and orders.
* Fix find definition of external files on Flutter, not asking for project root anymore.

## 1.22.2

Expand Down
20 changes: 11 additions & 9 deletions lsp-dart-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
;;; Code:

(require 'dash)
(require 'f)
(require 'lsp-mode)

(defcustom lsp-dart-sdk-dir nil
Expand Down Expand Up @@ -87,11 +88,12 @@ Order is important."

(defun lsp-dart--flutter-repo-p ()
"Return non-nil if buffer is the flutter repository."
(if-let (bin-path (locate-dominating-file default-directory lsp-dart-flutter-executable))
(and (file-regular-p (expand-file-name lsp-dart-flutter-executable bin-path))
(->> bin-path
(expand-file-name "cache/dart-sdk")
file-directory-p))))
(let ((bin-command (f-join "bin" lsp-dart-flutter-executable)))
(when-let (root-path (locate-dominating-file default-directory bin-command))
(and (file-regular-p (expand-file-name bin-command root-path))
(->> root-path
(expand-file-name (f-join "bin" "cache" "dart-sdk"))
file-directory-p)))))

(defun lsp-dart-flutter-project-p ()
"Return non-nil if buffer is a flutter project."
Expand Down Expand Up @@ -119,12 +121,12 @@ Order is important."
list))

(defun lsp-dart-flutter-snap-install-p ()
;; Detecting whether this is a Linux system with a Snap style install
"Detecting whether this is a Linux system with a Snap style install."
(and (string= system-type "gnu/linux")
(let ((first-dir (-some-> (executable-find lsp-dart-flutter-executable) f-split cdr car)))
(and first-dir
(string= first-dir "snap")
(file-exists-p "~/snap/flutter/common/flutter/bin/flutter")))))
(and first-dir
(string= first-dir "snap")
(file-exists-p "~/snap/flutter/common/flutter/bin/flutter")))))


;; SDKs
Expand Down
3 changes: 2 additions & 1 deletion lsp-dart.el
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ If unspecified, diagnostics will not be generated."
(let ((sdk-root (if (lsp-dart-flutter-project-p)
(lsp-dart-get-flutter-sdk-dir)
(lsp-dart-get-sdk-dir))))
(if (string-prefix-p sdk-root (buffer-file-name))
(if (or (string-prefix-p sdk-root (buffer-file-name))
(lsp-dart--flutter-repo-p))
(append (list (file-name-directory (buffer-file-name))) lsp-dart-extra-library-directories)
lsp-dart-extra-library-directories)))

Expand Down
12 changes: 6 additions & 6 deletions test/lsp-dart-utils-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@
(require 'lsp-dart-utils)
(require 'el-mock)

(ert-deftest lsp-dart--flutter-repo-p--true-test ()
(ert-deftest lsp-dart--flutter-repo-p--on-flutter-project-root-test ()
(with-mock
(mock (locate-dominating-file * "flutter") => "/sdk/bin")
(mock (locate-dominating-file * (f-join "bin" "flutter")) => (f-join (f-root) "sdk"))
(mock (file-regular-p (f-join (f-root) "sdk/bin/flutter")) => t)
(mock (file-directory-p (f-join (f-root) "sdk/bin/cache/dart-sdk")) => t)
(should (lsp-dart--flutter-repo-p))))

(ert-deftest lsp-dart--flutter-repo-p--not-flutter-executable-test ()
(ert-deftest lsp-dart--flutter-repo-p--not-flutter-executable-1-test ()
(with-mock
(mock (locate-dominating-file * "flutter") => "/not-sdk/bin")
(mock (locate-dominating-file * (f-join "bin" "flutter")) => "/not-sdk")
(stub file-regular-p => nil)
(should-not (lsp-dart--flutter-repo-p))))

(ert-deftest lsp-dart--flutter-repo-p--not-flutter-executable-test ()
(ert-deftest lsp-dart--flutter-repo-p--not-flutter-executable-2-test ()
(with-mock
(mock (locate-dominating-file * "flutter") => "/not-sdk/bin")
(mock (locate-dominating-file * (f-join "bin" "flutter")) => "/not-sdk")
(mock (file-regular-p (f-join (f-root) "not-sdk/bin/flutter")) => t)
(mock (file-directory-p (f-join (f-root) "not-sdk/bin/cache/dart-sdk")) => nil)
(should-not (lsp-dart--flutter-repo-p))))
Expand Down

0 comments on commit a8939d6

Please sign in to comment.