Skip to content

Commit 0b5dd33

Browse files
authored
feat: exclude modules by wildcard name (elixir-editors#363)
* feat: exclude modules by wildcard name With this PR I'd like to introduce the possibility for modules to be excluded without knowing their full name. Example: ``` { "type": "mix_task", "name": "mix phx.server", "request": "launch", "task": "phx.server", "projectDir": "${workspaceRoot}", "excludeModules": [ ":phoenix", "Phoenix", "Phoenix.*" ] } ``` This helped me to use the debugging feature of the vscode-elixir-ls extension. Without this feature I was unable to collect all the modules names that need to be excluded. * incorporate lukaszsamson's review comments * fix: remove IO.inspect
1 parent 1213efb commit 0b5dd33

File tree

1 file changed

+31
-14
lines changed
  • apps/elixir_ls_debugger/lib/debugger

1 file changed

+31
-14
lines changed

apps/elixir_ls_debugger/lib/debugger/server.ex

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,11 @@ defmodule ElixirLS.Debugger.Server do
530530
Mix.Task.run("app.start", [])
531531
end
532532

533-
exclude_modules =
533+
exclude_module_names =
534534
config
535535
|> Map.get("excludeModules", [])
536-
|> Enum.map(&string_to_module/1)
537536

538-
interpret_modules_in(Mix.Project.build_path(), exclude_modules)
537+
interpret_modules_in(Mix.Project.build_path(), exclude_module_names)
539538

540539
if required_files = config["requireFiles"], do: require_files(required_files)
541540

@@ -583,12 +582,16 @@ defmodule ElixirLS.Debugger.Server do
583582
}
584583
end
585584

586-
defp interpret_modules_in(path, exclude_modules) do
585+
defp interpret_modules_in(path, exclude_module_names) do
586+
exclude_module_pattern =
587+
exclude_module_names
588+
|> Enum.map(&wildcard_module_name_to_pattern/1)
589+
587590
path
588591
|> Path.join("**/*.beam")
589592
|> Path.wildcard()
590593
|> Enum.map(&(Path.basename(&1, ".beam") |> String.to_atom()))
591-
|> Enum.filter(&interpretable?(&1, exclude_modules))
594+
|> Enum.filter(&interpretable?(&1, exclude_module_pattern))
592595
|> Enum.map(fn mod ->
593596
try do
594597
{:module, _} = :int.ni(mod)
@@ -601,9 +604,30 @@ defmodule ElixirLS.Debugger.Server do
601604
end)
602605
end
603606

604-
defp interpretable?(module, exclude_modules) do
607+
defp wildcard_module_name_to_pattern(module_name) do
608+
module_name
609+
|> prefix_module_name()
610+
|> Regex.escape()
611+
|> String.replace("\\*", ~s(.+))
612+
|> Regex.compile!()
613+
end
614+
615+
defp interpretable?(module, exclude_module_pattern) do
605616
:int.interpretable(module) == true and !:code.is_sticky(module) and module != __MODULE__ and
606-
module not in exclude_modules
617+
not excluded_module?(module, exclude_module_pattern)
618+
end
619+
620+
defp excluded_module?(module, exclude_module_pattern) do
621+
module_name = module |> Atom.to_string()
622+
623+
Enum.any?(exclude_module_pattern, &Regex.match?(&1, module_name))
624+
end
625+
626+
defp prefix_module_name(module_name) when is_binary(module_name) do
627+
case module_name do
628+
":" <> name -> name
629+
_ -> "Elixir." <> module_name
630+
end
607631
end
608632

609633
defp change_env(env) do
@@ -697,11 +721,4 @@ defmodule ElixirLS.Debugger.Server do
697721
{:error, "Cannot interpret module #{inspect(module)}"}
698722
end
699723
end
700-
701-
defp string_to_module(str) when is_binary(str) do
702-
case str do
703-
":" <> name -> String.to_atom(name)
704-
name -> String.to_atom("Elixir." <> name)
705-
end
706-
end
707724
end

0 commit comments

Comments
 (0)