Skip to content

Commit

Permalink
增加 zls 调优配置
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Sep 6, 2024
1 parent 0993570 commit 5769046
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions course/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export default [
text: "环境配置",
items: [
{
text: "安装 Zig 环境",
text: "环境部署",
link: "/environment/install-environment",
},
{
text: "编辑器选择",
text: "编辑器",
link: "/environment/editor",
},
{
Expand Down
36 changes: 36 additions & 0 deletions course/environment/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,39 @@ windows 上最棒的开发 IDE,存在第三方插件:[ZigVS](https://marketp
::: danger ⛔ 危险
值得注意的是,该插件已经有两年无人维护!
:::

## zls 体验优化

当前的 zls 已经支持保存时自动检查代码,但默认关闭。

仅仅需要在 zls 的配置文件(可以通过 `zls --show-config-path`)中加入以下内容即可:

```json
{
"enable_build_on_save": true,
"build_on_save_step": "check"
}
```

同时对应的项目的 `build.zig` 也需要进行部分调整:

```zig
const exe_check = b.addExecutable(.{
.name = "foo",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const check = b.step("check", "Check if foo compiles");
check.dependOn(&exe_check.step);
```

这意味着要求编译器在编译时会添加 `-fno-emit-bin`,然后 Zig 将仅仅分析代码,但它不会调用 LLVM,所以并不会生成实际的文件

但需要注意的是,我们对 `zls` 的设置是全局的,也就意味着我们需要给所有项目添加上述 `build.zig` 的内容,否则诊断功能将会失效。

更多资料:

- [Improving Your Zig Language Server Experience](https://kristoff.it/blog/improving-your-zls-experience/)
- [Local ZLS config per project](https://github.com/zigtools/zls/issues/1687) (针对本地项目的 zls 配置)

0 comments on commit 5769046

Please sign in to comment.