入门 Zed Industries 2026-09-14 17:42:19 · 0 阅读

第77章 Zed 编辑器中的 Go 语言支持

Zed 原生支持 Go。

安装配置

建议通过 go 的包管理器安装 gopls,而不是通过 Homebrew 或 Linux 发行版的包管理器。

  1. 先卸载通过包管理器安装的 gopls:
# MacOS homebrew
brew remove gopls
# Ubuntu
sudo apt-get remove gopls
sudo snap remove gopls
# Arch
sudo pacman -R gopls
  1. 使用 go module 工具安装或更新 gopls 到最新版本:
go install golang.org/x/tools/gopls@latest
  1. 确认 gopls 已在系统路径中:
which gopls
gopls version

如果找不到 gopls,可能需要在 .zshrc / .bash_profile 中添加 export PATH="$PATH:$HOME/go/bin"

Inlay Hints

当设置中启用了 inlay hints 时,Zed 会通过以下初始化选项让语言服务器返回 inlay hints:

"hints": {
    "assignVariableTypes": true,
    "compositeLiteralFields": true,
    "compositeLiteralTypes": true,
    "constantValues": true,
    "functionTypeParameters": true,
    "parameterNames": true,
    "rangeVariableTypes": true
}

如需覆盖这些设置,可以使用:

"lsp": {
    "gopls": {
        "initialization_options": {
            "hints": {
                // ....
            }
        }
    }
}

更多信息请参阅 gopls inlayHints 文档

Code Lens

Zed 默认启用 goplstest 代码透镜,它会在 *_test.go 文件中的 TestBenchmark 函数上方显示“运行测试”和“运行基准测试”链接。若要使用这些功能,请启用 code_lens 设置:

{
  "code_lens": "on"
}

你可以自定义默认设置,在 settings.json 中覆盖代码透镜配置:

{
  "lsp": {
    "gopls": {
      "initialization_options": {
        "codelenses": {
          "test": true,
          "generate": true,
          "regenerate_cgo": true,
          "tidy": true,
          "upgrade_dependency": true,
          "vendor": true
        }
      }
    }
  }
}

更多信息请参阅 gopls 代码透镜文档

调试

Zed 支持使用 Delve 对 Go 测试和入口点(func main)进行零配置调试。运行 {#action debugger::Start}({#kb debugger::Start})可查看这些预配置调试任务的上下文列表。

若需更精细控制,可在 .zed/debug.json 中添加调试配置。示例如下。

调试 Go 包

要调试特定包,可将 Delve 模式设为 “debug”,此时 “program” 字段需设置为包名。

[
  {
    "label": "Go (Delve)",
    "adapter": "Delve",
    "program": "$ZED_FILE",
    "request": "launch",
    "mode": "debug"
  },
  {
    "label": "运行服务器",
    "adapter": "Delve",
    "request": "launch",
    "mode": "debug",
    // 对于 Delve,program 可以是包名
    "program": "./cmd/server"
    // "args": [],
    // "buildFlags": [],
  }
]

调试 Go 测试

若要调试某个包的测试,将 Delve 模式设为 "test"。"program" 仍然指定包名;可用 "buildFlags" 设置编译标签等选项,用 "args" 给测试二进制传入参数(详见 go help testflags)。

[
  {
    "label": "Run integration tests",
    "adapter": "Delve",
    "request": "launch",
    "mode": "test",
    "program": ".",
    "buildFlags": ["-tags", "integration"]
    // 过滤到当前光标所在的测试:
    // "args": ["-test.run", "$ZED_SYMBOL"]
  }
]

构建与调试分离

如果需要用特定命令构建应用,可使用 Delve 的 "exec" 模式。此时 "program" 应指向可执行文件,"build" 命令负责生成该可执行文件。

[
  {
    "label": "Debug Prebuilt Unit Tests",
    "adapter": "Delve",
    "request": "launch",
    "mode": "exec",
    "program": "${ZED_WORKTREE_ROOT}/__debug_unit",
    "args": ["-test.v", "-test.run=${ZED_SYMBOL}"],
    "build": {
      "command": "go",
      "args": [
        "test",
        "-c",
        "-tags",
        "unit",
        "-gcflags\"all=-N -l\"",
        "-o",
        "__debug_unit",
        "./pkg/..."
      ]
    }
  }
]

连接到已运行的 Delve 实例

有时需要连接一个并非运行在本机上的已有 Delve 实例。这种情况可以通过 tcp_arguments 来控制 Zed 与 Delve 之间的连接。

[
  {
    "adapter": "Delve",
    "label": "Connect to a running Delve instance",
    "program": "/Users/zed/Projects/language_repositories/golang/hello/hello",
    "cwd": "/Users/zed/Projects/language_repositories/golang/hello",
    "args": [],
    "env": {},
    "request": "launch",
    "mode": "exec",
    "stopOnEntry": false,
    "tcp_connection": { "host": "127.0.0.1", "port": 53412 }
  }
]

这种情况下 Zed 不会启动新的 Delve 实例,而是复用已有的实例。代价是 Zed 中不会有终端,你需要直接与 Delve 实例交互,因为它接管了被调试程序的 stdin/stdout。

Go Mod

Go Sum

Go Work

在 Templ 中使用 Tailwind CSS 语言服务器

要想在 Templ 文件中获得 Tailwind CSS 语言服务器的全部功能(自动补全、代码检查等),需要为 Templ 启用该语言服务器,并配置 CSS 类的查找位置,即在 settings.json 中添加以下内容:

{
  "languages": {
    "Templ": {
      "language_servers": ["tailwindcss-language-server", "..."]
    }
  },
  "lsp": {
    "tailwindcss-language-server": {
      "settings": {
        "includeLanguages": {
          "templ": "html"
        },
        "experimental": {
          "classRegex": ["class=\"([^\"]*)\""]
        }
      }
    }
  }
}

注意:与其他语言不同,你需要明确告知 Tailwind 将 .templ 文件视为 HTML。

这样你就能在 .templ 文件的 class="..." 属性中获得 Tailwind CSS 的代码补全功能。

评论 (0)