100phlecs / tailwind_formatter

Sorts tailwind classes within elixir projects
https://hexdocs.pm/tailwind_formatter
MIT License
111 stars 11 forks source link

Multi Formatter #9

Closed ddengler closed 1 year ago

ddengler commented 2 years ago

Thanks for the formatter. Exactly what I was looking for.

I created an alternative workaround for the mix limitations prior to the unreleased 1.15 for my use case looking like this, so I can still use this properly with my vscode plugin formatting files on save:

defmodule MultiFormatter do
  if Version.match?(System.version(), ">= 1.13.0") do
    @behaviour Mix.Tasks.Format
  end

  def features(_opts) do
    [sigils: [:H], extensions: [".heex"]]
  end

  def format(contents, opts) do
    Phoenix.LiveView.HTMLFormatter.format(contents, opts) |> TailwindFormatter.format(opts)
  end
end

Maybe this is useful to you or could provide a basis for a more generic workaround in you project.

mjyoung commented 2 years ago

@ddengler Does this work for you to automatically format tailwind classes in heex files? I can't get vscode to automatically format these (need to manually run mix format). what do your settings look like for [phoenix-heex]?

ddengler commented 2 years ago
{
    "html": [
      "HTML (EEx)",
      "htm",
      "html"
    ]
  },
  "files.associations": {
    "*.js": "javascript",
    "*.ex": "elixir",
    "*.exs": "elixir",
    "*.html.eex": "html-eex",
    "*.html.leex": "html-eex",
    "*.eex": "html-eex",
    "*.leex": "html-eex",
    "*.heex": "phoenix-heex",
    "*.html.heex": "phoenix-heex",
  },
  "tailwindCSS.experimental.classRegex": [
    "class:\\s*\"([^\"]*)\""
  ],
  "tailwindCSS.includeLanguages": {
    "html-eex": "html",
    "phoenix-heex": "html",
    "html": "html",
    "elixir": "html",
    "eex": "html"
  },
  "editor.formatOnSave": true,
  "[phoenix-heex]": {
    "editor.defaultFormatter": "animus-coop.vscode-elixir-mix-formatter"
  },
}

These are all my possibly relevant settings. Also the animus-coop.vscode-elixir-mix-formatter is installed as a plugin to auto-run the mix format task. Hope this helps?

100phlecs commented 1 year ago

Sure! Cleaner solution than the bash script. If you're using this library you are definitely using phoenix/heex so may as well include it as a dependency.

Thanks for the provided file. I'll include it along with new instructions.

edit: fully implemented with correct namespace on commit https://github.com/100phlecs/tailwind_formatter/commit/685f8a55e3190fb8ed28265842e8ee9e897e9409