2

currently i'm typing

leptosfmt .

each time i want to format the html within the view!{} macro. I'd like to automate this on save or whenever rustfmt runs.

I tried adding a rustfmt section to my Zed settings.json, to support leptos, but it seemed to break formatting of my code. Anyone know the correct way to do this?

{
  "telemetry": {
    "diagnostics": false,
    "metrics": false
  },
  "ui_font_size": 16,
  "buffer_font_size": 16,
  "autosave": "on_focus_change",
  "lsp": {
    "rust-analyzer": {
      "procMacro": {
        "ignored": [
          // "component",
          "server"
        ]
      },
      "rustfmt": {
        "overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"]
      }
    }
  }
}

1 Answer 1

1

I'm not sure what part of your formatting is breaking but I've managed to get it to work successfully by setting up a command in format_on_save. I've also successfully configured Zed to format Rust code using external tools like rustfmt, leptosfmt, and rustywind. Here's a breakdown of the setup:

  1. .zed/settings.json configuration

    {
      "languages": {
        "Rust": {
          "format_on_save": {
            "external": {
              "command": "bash",
              "arguments": [".zed/format.sh"]
            }
          }
        }
      },
      "lsp": {
        "rust-analyzer": {
          "procMacro": {
            "ignored": ["component", "server"]
          }
        }
      }
    }
    
  2. .zed/format.sh script

    #!/usr/bin/env bash
    rustfmt | leptosfmt --stdin| rustywind --output-css-file "$(pwd)/style/main.scss" --stdin
    
    

I'm using Zed 0.145.1

Notes:

  • Make sure you have rustfmt, leptosfmt, rustywind installed in your terminal.
  • You may also need to setup a rustfmt.toml with a correct Rust edition:
    edition = "2021"
    
  • You should set this up in your project folder (not in your main config). Instructions can be found here.
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. is it possible to toggle this by workspace? e.g. leptos specific projects
Yes, it's possible. You'll just need to create a folder called .zed within your project. Instructions found here. Let me know if you need more guidance
I've added a note in the instructions. Thanks for pointing this out @RustyRob.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.