CLI 与编辑器
cargo-rsx CLI 与编辑器语言服务(cargo rsx lsp)和过程宏共用同一套 Rust 解析器、class 目录与 rsx.toml 配置。安装 CLI 需要 Rust 1.98+(gpui-rsx 宏与核心库本身支持 Rust 1.88+)。
常用命令速查
Section titled “常用命令速查”| 命令 | 用途 |
|---|---|
cargo rsx doctor |
检查 GPUI 依赖图、别名一致性与热更新就绪状态 |
cargo rsx dev |
执行热更新预检并启动 dx serve --hot-patch |
cargo rsx lint |
扫描工作区内的 RSX 宏(支持 --deny-warnings、--json) |
cargo rsx fmt |
仅格式化 RSX 宏体,不改动周围 Rust 代码(--check、--write) |
cargo rsx theme check/list/explain |
校验 rsx.toml 令牌并追踪单个 class 的展开结果 |
cargo rsx expand |
打印独立 RSX 宏体的完整 Rust 展开代码 |
cargo rsx convert |
将 JSX / TSX 转换为 RSX,并附带人工审阅提示 |
cargo rsx preview |
在 http://127.0.0.1:9230 启动本地静态布局实时预览 |
cargo rsx migrate |
迁移至 Tailwind 4px 刻度时将数值类批量除以 4 |
cargo rsx lsp |
驱动 VS Code 与 Zed 扩展的语言服务器 |
1. 安装与环境诊断
Section titled “1. 安装与环境诊断”cargo install cargo-rsx --version 0.13.0 --lockedcargo rsx doctor --manifest-path demo/Cargo.tomldoctor 会检查解析后的 Cargo 依赖图(别名、Git 依赖、已发布快照),也可以用于热补丁就绪预检:
cargo rsx dev --check --manifest-path tests/fixtures/hot-reload/Cargo.toml --features hotreload,kitcargo rsx dev my-bincargo rsx dev 在省略 --features 时会自动检测包的 hotreload / hot-reload feature,检查 dx serve --hot-patch 支持(可通过环境变量 RSX_DX 指定二进制路径),并提示可能被 Dioxus CLI 拦截的 rsx 宏。详见热更新排查。
2. Lint 检查与格式化
Section titled “2. Lint 检查与格式化”cargo rsx lint --deny-warningscargo rsx lint --min-severity warning --jsoncargo rsx fmt --checkcargo rsx fmt src/main.rs --write- 工作区感知:
lint自动遍历 workspace 成员并读取各自包的rsx.toml与tailwind-scalefeature(可用--tailwind强制覆盖)。--quiet/-q仅显示错误,但--deny-warnings仍会对被过滤的警告返回失败状态。 - 无侵入格式化:
fmt只重写括号平衡的 RSX 宏体,不会修改外围 Rust 代码,包含注释的宏体也会原样保留。 - 导入别名与容错恢复: 支持直接宏别名(如
use gpui_rsx::{rsx as gpui_view, rsx_tw as tw};),并保留原宏的严格模式与间距刻度(同时尊重同作用域内的macro_rules!遮蔽)。当 Rust 文件尚未写完时,括号平衡的 RSX 宏仍可提供补全和诊断,并在破损处报告rsx/rust-syntax。 - 结构化诊断码: JSON 与 LSP 诊断提供稳定错误码(如
rsx/missing-loop-key、rsx/missing-required-attribute、rsx/missing-component-prop)。
3. 检查主题与展开代码
Section titled “3. 检查主题与展开代码”cargo rsx theme check --manifest-path demo/Cargo.tomlcargo rsx theme list --manifest-path demo/Cargo.toml --jsoncargo rsx theme explain bg-primary --manifest-path demo/Cargo.tomlprintf '<div class="flex gap-4" />' | cargo rsx expand -expand 输出完整展开代码(含未启用共享运行时时的内联动态匹配器),支持 --strict 和 --tailwind 覆盖。多宏文件可使用 preview --index N 或在编辑器中悬停查看展开结果。
4. JSX 转换、本地预览与间距迁移
Section titled “4. JSX 转换、本地预览与间距迁移”cargo rsx convert component.tsx --tailwindcargo rsx preview src/main.rs --watchcargo rsx migrate src/main.rs # 先预览变更cargo rsx migrate src/main.rs --write # 应用一次convert: 基于 SWC 解析,保留简单标识符与成员访问,并对需要手动改写为 Rust 的 JS 表达式、事件回调和不支持的 class 输出提示;传--tailwind会生成rsx_tw! { ... }。preview: 在http://127.0.0.1:9230提供近似静态布局预览(--watch同时监听.rs与rsx.toml变更)。与在线体验一样,预览不执行 Rust 表达式也不做类型检查,最终效果请在原生 GPUI 中验证。migrate: 一次性将数值类除以 4 以保持原有像素尺寸,随后即可启用tailwind-scale或[options] spacing-scale = "tailwind";不会修改rsx_tw!、带单位长度或分数。
5. 连接编辑器(VS Code / Zed)
Section titled “5. 连接编辑器(VS Code / Zed)”| 编辑器 | 配置方式 |
|---|---|
| VS Code | 从 0.13.0 Release 下载 .vsix,执行 Extensions: Install from VSIX 安装。 |
| Zed | 将本仓库的 editors/zed 安装为开发扩展;如自定义了 Rust 语言服务器,请将 "gpui-rsx" 与 "rust-analyzer" 并列配置。 |
两款扩展均通过 cargo rsx lsp 与 rust-analyzer 协同工作,提供:
- 静态 class、标签名、GPUI 方法及当前文件内
#[component]属性补全。 - 悬停查看展开代码、语义高亮与
rsx.toml颜色色块预览。 - 诊断与快速修复(裸文本、闭合标签不匹配、缺失必填 prop、循环缺失 key)。
- 自动监听
rsx.toml与Cargo.toml变更并重新分析。
如需了解本地原生开发重载循环,请参阅测量开发重载。