Skip to content

Add support for .formatter.exs in subdirectories #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

wingyplus
Copy link
Contributor

When try to format Ecto migration script that generate project from
Phoenix, it will returns inputs "*.exs". But the glob list just detect
it only the root of project and apps directory which for umbrella
project. This cause make migration script won't match any glob pattern.

This changes fixes by add directory of file that want to format to the end
of glob list.

Fixes #361

@@ -56,7 +56,8 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
|> Stream.flat_map(fn glob ->
[
Path.join([project_dir, glob]),
Path.join([project_dir, "apps", "*", glob])
Path.join([project_dir, "apps", "*", glob]),
Path.join([Path.dirname(file), glob])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be added only when Path.dirname(file) differs from project_dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It should check file are in the project dir before add to the globs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, see comments below

When try to format Ecto migration script that generate project from
Phoenix, it will returns inputs "*.exs". But the glob list just detect
it only the root of project and apps directory which for umbrella
project. This cause make migration script won't match any glob pattern.

This changes fixes by add directory of file that want to format to the end
of glob list.

Fixes elixir-lsp#361
@lukaszsamson
Copy link
Collaborator

lukaszsamson commented Sep 2, 2020

Unfortunately it's still broken.

  1. When formatting a file from top level project dir e.g. mix.exs, we end up expanding the same dirs twice
[
 "/Users/bug_repro/phoenix_ecto_formatter/*.{ex,exs}", # this
 "/Users/bug_repro/phoenix_ecto_formatter/apps/*/*.{ex,exs}",
 "/Users/bug_repro/phoenix_ecto_formatter/*.{ex,exs}" # and this
]

I meant that we should add something to globs only if it's not already there.
Probably we shouldn't also try to expand apps/* if it's not an umbrella project.
2. It doesn't work if the .formatter.exs is in a parent dir of the formatted file. In your repro project consider following:
.formatter.exs

[
  import_deps: [:ecto, :phoenix],
  inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
  subdirectories: ["priv/*/migrations", "priv/*"]
]

priv/repo/.formatter.exs (note it's moved one level up)

[
  import_deps: [:ecto_sql],
  inputs: ["migrations/*.exs"]
]

mix format works fine in that case
To fix it we probably need to check all parent dirs up to a project dir (or app dir in case of an umbrella) for relevant .formatter.exs files

@wingyplus
Copy link
Contributor Author

@lukaszsamson thanks for your guide. I think it would be great to do this. Let me try. 🙏

@lukaszsamson lukaszsamson changed the title Fix textDocument/formatting returns [] when format Ecto migration script Add support for .formatter.exs in subdirectories Oct 29, 2020
@lukaszsamson
Copy link
Collaborator

@wingyplus ping

@wingyplus
Copy link
Contributor Author

wingyplus commented Nov 7, 2020

@lukaszsamson Hi, Sorry for disappear for a long time. I already see the WIP PR (but it's read in details yet), it's seems that it solve this issue?

@lukaszsamson
Copy link
Collaborator

@wingyplus is it still an issue?

@lukaszsamson
Copy link
Collaborator

Closing as this was superseded by #609

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

textDocument/formatting does not support subdirectories in .formatter.exs
3 participants