Skip to content

Commit 701bb3a

Browse files
committed
port formatter changes from upstream
add support for excludes option
1 parent 2232dab commit 701bb3a

File tree

1 file changed

+25
-5
lines changed
  • apps/language_server/lib/language_server/mix_tasks

1 file changed

+25
-5
lines changed

apps/language_server/lib/language_server/mix_tasks/format.ex

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file includes modified code extracted from the elixir project. Namely:
22
#
3-
# https://github.com/elixir-lang/elixir/blob/v1.18.0/lib/mix/lib/mix/tasks/format.ex
3+
# https://github.com/elixir-lang/elixir/blob/2e7bb9b47ffeba1950c6209fc8f3791bc8b8732b/lib/mix/lib/mix/tasks/format.ex
44
#
55
# The original code is licensed as follows:
66
#
@@ -53,6 +53,10 @@ defmodule Mix.Tasks.ElixirLSFormat do
5353
to be used by this task. For example, `["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]`.
5454
Patterns are expanded with `Path.wildcard/2`.
5555
56+
* `:excludes` (a list of paths and patterns) - specifies the files to exclude from the
57+
list of inputs to this task. For example, `["config/runtime.exs", "test/**/*.{ex,exs}"]`.
58+
Patterns are expanded with `Path.wildcard/2`.
59+
5660
* `:plugins` (a list of modules) (since v1.13.0) - specifies a list of
5761
modules to customize how the formatter works. See the "Plugins" section
5862
below for more information.
@@ -100,7 +104,7 @@ defmodule Mix.Tasks.ElixirLSFormat do
100104
* `--stdin-filename` - path to the file being formatted on stdin.
101105
This is useful if you are using plugins to support custom filetypes such
102106
as `.heex`. Without passing this flag, it is assumed that the code being
103-
passed via stdin is valid Elixir code. Defaults to "stdin.exs".
107+
passed via stdin is valid Elixir code. Defaults to `stdin.exs`.
104108
105109
* `--migrate` - enables the `:migrate` option, which should be able to
106110
automatically fix some deprecation warnings but changes the AST.
@@ -549,6 +553,12 @@ defmodule Mix.Tasks.ElixirLSFormat do
549553
defp eval_deps_opts(deps, opts) do
550554
deps_paths = opts[:deps_paths] || Mix.Project.deps_paths()
551555

556+
if not is_map(deps_paths) do
557+
Mix.raise(
558+
"Expected :deps_paths to return a map of dependency paths, got: #{inspect(deps_paths)}"
559+
)
560+
end
561+
552562
for dep <- deps,
553563
dep_path = assert_valid_dep_and_fetch_path(dep, deps_paths),
554564
dep_dot_formatter = Path.join(dep_path, ".formatter.exs"),
@@ -660,9 +670,16 @@ defmodule Mix.Tasks.ElixirLSFormat do
660670
Mix.raise("Expected :inputs or :subdirectories key in #{dot_formatter}")
661671
end
662672

673+
excluded_files =
674+
formatter_opts[:excludes]
675+
|> List.wrap()
676+
|> Enum.flat_map(&Path.wildcard(SourceFile.Path.expand(&1, cwd), match_dot: true))
677+
|> MapSet.new()
678+
663679
map =
664680
for input <- List.wrap(formatter_opts[:inputs]),
665681
file <- Path.wildcard(SourceFile.Path.expand(input, cwd), match_dot: true),
682+
file not in excluded_files,
666683
do: {file, {dot_formatter, formatter_opts}},
667684
into: %{}
668685

@@ -725,7 +742,7 @@ defmodule Mix.Tasks.ElixirLSFormat do
725742
size = byte_size(sub)
726743

727744
case file do
728-
<<prefix::binary-size(size), dir_separator, _::binary>>
745+
<<prefix::binary-size(^size), dir_separator, _::binary>>
729746
when prefix == sub and dir_separator in [?\\, ?/] ->
730747
recur_formatter_opts_for_file(file, sub, formatter_opts_and_subs)
731748

@@ -749,8 +766,11 @@ defmodule Mix.Tasks.ElixirLSFormat do
749766
|> Enum.filter(&File.regular?/1)
750767

751768
defp elixir_format(content, formatter_opts) do
752-
case Code.format_string!(content, formatter_opts) do
753-
[] -> ""
769+
content
770+
|> Code.format_string!(formatter_opts)
771+
|> IO.iodata_to_binary()
772+
|> case do
773+
"" -> ""
754774
formatted_content -> IO.iodata_to_binary([formatted_content, ?\n])
755775
end
756776
end

0 commit comments

Comments
 (0)