From 3833c182a2e38589126a1fc50e75c5ff537bc22b Mon Sep 17 00:00:00 2001 From: Steve Cohen Date: Tue, 23 Apr 2024 08:49:14 -0700 Subject: [PATCH] Included non-deps symbols in fuzzy matcher (#704) * Included non-deps symbols in fuzzy matcher I noticed that the fuzzy matcher wouldn't match any unit tests. This is because their application is nil, as they're scripts. This change checks for nil applications, and if it finds one, only excludes the mapped entry if the path looks like it comes from a dependencies directory. Note, the deps directory name is hard coded. I originally queried the project for the deps directory name, but this would include dependencies in the projects directory, which was quite annoying, hence the hard-coding. * Changed how we detect dependencies Dependencies are now detected via seeing if the path is in our deps directory or one of our sub-project's dependencies. Co-authored-by: Zach Allaun --------- Co-authored-by: Zach Allaun --- .../lexical/remote_control/search/fuzzy.ex | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/apps/remote_control/lib/lexical/remote_control/search/fuzzy.ex b/apps/remote_control/lib/lexical/remote_control/search/fuzzy.ex index 7fa6fca18..1deb49e01 100644 --- a/apps/remote_control/lib/lexical/remote_control/search/fuzzy.ex +++ b/apps/remote_control/lib/lexical/remote_control/search/fuzzy.ex @@ -11,6 +11,7 @@ defmodule Lexical.RemoteControl.Search.Fuzzy do returned. """ + alias Lexical.Project alias Lexical.RemoteControl alias Lexical.RemoteControl.Search.Fuzzy.Scorer alias Lexical.RemoteControl.Search.Indexer.Entry @@ -336,31 +337,52 @@ defmodule Lexical.RemoteControl.Search.Fuzzy do end defp build_filter_fn do - mix_app_names = fn -> - applications = - if Mix.Project.umbrella?() do - Map.keys(Mix.Project.apps_paths()) - else - List.wrap(Mix.Project.config()[:app]) - end - - MapSet.new(applications) - end - - app_names = + deps_directories = if Mix.Project.get() do - mix_app_names.() + deps_roots() else - {:ok, names} = + {:ok, deps_roots} = RemoteControl.Mix.in_project(fn _ -> - mix_app_names.() + deps_roots() end) - names + deps_roots end - fn mapped(application: app, subtype: subtype) -> - subtype == :definition and MapSet.member?(app_names, app) + fn + mapped(subtype: :definition, grouping_key: path) -> + # if we don't have an app name, just make sure we're not + # in what looks like a deps directory + not Enum.any?(deps_directories, &String.starts_with?(path, &1)) + + _ -> + false end end + + defp deps_roots do + deps_roots(RemoteControl.get_project()) + end + + defp deps_roots(%Project{mix_project?: true} = project) do + # Note: This function assumes that the deps directories for all + # found projects is `deps`. Projects may override this directory + # and lexical won't understand this. This was done because loading + # each sub-project is expensive and changes our global directory. + + [Project.root_path(project), "**", "mix.exs"] + |> Path.join() + |> Path.wildcard() + |> Enum.map(fn relative_mix_path -> + relative_mix_path + |> Path.absname() + |> Path.dirname() + |> Path.join("deps") + end) + |> Enum.filter(&File.exists?/1) + end + + defp deps_roots(_) do + [] + end end