Skip to content

format sigils using applicable plugins#37

Open
superhawk610 wants to merge 2 commits intoremoteoss:mainfrom
superhawk610:fix/format-sigils
Open

format sigils using applicable plugins#37
superhawk610 wants to merge 2 commits intoremoteoss:mainfrom
superhawk610:fix/format-sigils

Conversation

@superhawk610
Copy link
Copy Markdown

Closes #36.

This PR modifies the .ex / .exs format process to include plugins that are capable of formatting sigil contents, e.g. Phoenix.LiveView.HTMLFormatter for the ~H sigil.

See https://github.com/elixir-lang/elixir/blob/d67cb9634baba80e61188da5a688c9b127f94038/lib/mix/lib/mix/tasks/format.ex#L337 for corresponding implementation in mix format.

Copy link
Copy Markdown
Collaborator

@JesseHerrick JesseHerrick left a comment

Choose a reason for hiding this comment

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

Hi @superhawk610, thank you very much for your contribution! I just have one note about unifying things here.

Comment thread internal/lsp/formatter_server.exs Outdated
Process.group_leader(self(), old_gl)
end
else
sigils =
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think that this logic could actually be built above where we accumulate applicable plugins. Unless I'm misunderstanding it, we just need to add the additional sigil config to the plugin.format calls. As-is, this code is doing very similar logic to the branch directly above. I think this can be unified. This is all the more important because our protocol breaks if we leak any data to IO during the failed formatting, hence the logic in the try block.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The sigils option is passed to Code.format_string! (took me a bit to figure that out when reading through the mix format source, since it's undocumented). That means both arms of the if statement will now potentially call plugin.format, so I moved the entire statement into the try block to make sure nothing leaks to stdout. I also moved the sigil collection into the same loop that checks extensions.

This should avoid polluting stdout with any exception messages.
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.

Formatter doesn't handle sigils like ~H in .ex files

2 participants