rust-analyzerleptos

add leptosfmt to Zed settings.json


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"]
      }
    }
  }
}

Solution

  • 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: