hhstore / blog

My Tech Blog: about Mojo / Rust / Golang / Python / Kotlin / Flutter / VueJS / Blockchain etc.
https://github.com/hhstore/blog/issues
276 stars 22 forks source link

Zig: setting up Zig + LSP(ZLS) with Vscode/Sublime Text 4/Clion #376

Open hhstore opened 1 year ago

hhstore commented 1 year ago

related:

sublime text 4 配置:

hhstore commented 1 year ago

Zig + Vscode/Sublime Text 4/Clion 搭建开发环境:

步骤说明:

✅ 1. 安装 zig. ✅ 2. 安装 zls, 用于 vscode 解析/索引 zig 代码. ✅ 3. 安装 vscode/clion 相关 zig 插件 ✅ 4. 更改 vscode 插件设置, 配置 zls 参数. ✅ 5. 配置结束, 愉快写代码.

安装 Zig:

setup zig development environment.

install:


  # for macos + m1:
  install:zig:dev:
    cmds:
      - mkdir -p $HOME/.local/bin/;
      - cd $HOME/.local/bin/; curl -L https://ziglang.org/builds/zig-macos-aarch64-0.10.0-dev.2617+47c4d4450.tar.xz | tar -xJ --strip-components=1 -C .;
      - chmod +x $HOME/.local/bin/zig;
      - zig version

  install:zig:
    cmds:
      - brew install zig
      - zig version
      - brew install xz # for zig zls
      - mkdir $HOME/zls && cd $HOME/zls && curl -L https://github.com/zigtools/zls/releases/download/0.9.0/x86_64-macos.tar.xz | tar -xJ --strip-components=1 -C .
      - chmod +x $HOME/zls/zls
      - sudo $HOME/zls/zls configure
    ignore_error: true

➤ which zig
/Users/dev/.local/bin/zig

➤ zig version
0.10.0-dev.2617+47c4d4450

➤ zig env
{
 "zig_exe": "/Users/dev/.local/bin/zig",
 "lib_dir": "/Users/dev/.local/bin/lib/zig",
 "std_dir": "/Users/dev/.local/bin/lib/zig/std",
 "global_cache_dir": "/Users/dev/.cache/zig",
 "version": "0.10.0-dev.2617+47c4d4450"
}

安装 ZLS: (代码解析)


  install:zls:
    cmds:
      - brew install xz # for zig zls
      - mkdir $HOME/zls && cd $HOME/zls && curl -L https://github.com/zigtools/zls/releases/download/0.9.0/x86_64-macos.tar.xz | tar -xJ --strip-components=1 -C .
      - chmod +x $HOME/zls/zls
      - sudo $HOME/zls/zls configure
    ignore_error: true

Welcome to the ZLS configuration wizard!
      *
       |\
      /* \
      |  *\
    _/_*___|_    x
      | @ @     /
     @     \   /
      \__-/   /

? Should this configuration be system-wide? (y/n) > y
Found zig executable '/Users/dev/.local/bin/zig' in PATH.
? Which code editor do you use? (select one)

  - VSCode
  - Sublime
  - Kate
  - Neovim
  - Vim8
  - Emacs
  - Doom
  - Other

> vscode
? Do you want to enable snippets? (y/n) > y
? Do you want to enable style warnings? (y/n) > y
? Do you want to enable semantic highlighting? (y/n) > y
? Do you want to enable .* and .? completions? (y/n) > y
Writing config to /Library/Preferences/zls.json ... successful.

To use ZLS in Visual Studio Code, install the 'ZLS for VSCode' extension from 
'https://github.com/zigtools/zls-vscode/releases' or via the extensions menu.
Then, open VSCode's 'settings.json' file, and add:

"zigLanguageClient.path": "[command_or_path_to_zls]"

Thank you for choosing ZLS!

vscode 配置:

vscode 下载:

vscode 插件安装:

参考:

clion / IntelliJ IDEA 配置:

IDE: clion

插件: intellij-zig

hhstore commented 1 year ago

vscode 配置:


# vscode 设置, 搜索 zls, 添加对应的 zls 安装路径

/Users/dev/zls/zls

vscode 设置:

image image image image

之后, 就可以愉快的写 zig 了.

hhstore commented 1 year ago

clion / IntelliJ IDEA 配置:

hhstore commented 1 year ago

1

hhstore commented 1 year ago

Sublime Text 4 + ZLS 配置 Zig 开发环境:

zls 配置:

步骤:

  1. 下载安装 Sublime Text 4.
  2. 安装 LSP 插件: https://github.com/sublimelsp/LSP
  3. 设置中, 添加 LSP 配置段:
    • 注意 zls 的路径, 填写自己机器对应的路径.
  4. 结束. (如果不生效, 重启一下编辑器)

// Settings in here override those in "LSP/LSP.sublime-settings"
{
    "clients": {
        "zig": {
            "command": ["/Users/dev/zls/zls"],
            "enabled": true,
            "selector": "source.zig"
        }
    }
}
image

配置成功效果:

image image

插件安装:

theme 插件:

必装插件:

image
hhstore commented 1 year ago

zls-vscode 插件炸机:

CreatorMetaSky commented 1 year ago

如果用最新版本的 zls-vscode, 安装方式:zig + zls-vscode + zls,最好看下 zls-vscode 提示的编译 zls 的 zig 版本,下载一个一样的版本 zig 就可以了。