Extract Zig support into an extension (#9893)

This PR extracts Zig support into an extension and removes the built-in
Zig support from Zed.

There's a small workaround necessary in order for us to set the file
permissions on the `zls` binary so that it can be run. Eventually we'll
want to build this into the extension API, but for now we're just
hard-coding it on the host side.

Release Notes:

- Removed built-in support for Zig, in favor of making it available as
an extension. The Zig extension will be suggested for download when you
open a `.zig` file.
This commit is contained in:
Marshall Bowers 2024-03-27 20:56:30 -04:00 committed by GitHub
parent 9bce5e8b82
commit ff685b299d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 170 additions and 158 deletions

16
extensions/zig/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "zed_zig"
version = "0.0.1"
edition = "2021"
publish = false
license = "Apache-2.0"
[lints]
workspace = true
[lib]
path = "src/zig.rs"
crate-type = ["cdylib"]
[dependencies]
zed_extension_api = "0.0.4"

View file

@ -0,0 +1 @@
../../LICENSE-APACHE

View file

@ -0,0 +1,15 @@
id = "zig"
name = "Zig"
description = "Zig support."
version = "0.0.1"
schema_version = 1
authors = ["Allan Calix <contact@acx.dev>"]
repository = "https://github.com/zed-industries/zed"
[language_servers.zls]
name = "zls"
language = "Zig"
[grammars.zig]
repository = "https://github.com/maxxnino/tree-sitter-zig"
commit = "0d08703e4c3f426ec61695d7617415fff97029bd"

View file

@ -0,0 +1,3 @@
("(" @open ")" @close)
("[" @open "]" @close)
("{" @open "}" @close)

View file

@ -0,0 +1,11 @@
name = "Zig"
grammar = "zig"
path_suffixes = ["zig"]
line_comments = ["// "]
autoclose_before = ";:.,=}])>"
brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "[", end = "]", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = true },
{ start = "<", end = ">", close = true, newline = true },
]

View file

@ -0,0 +1,16 @@
[
(Block)
(ContainerDecl)
(SwitchExpr)
(InitList)
(AsmExpr)
(ErrorSetDecl)
(LINESTRING)
(
[
(IfPrefix)
(WhilePrefix)
(ForPrefix)
]
)
] @fold

View file

@ -0,0 +1,240 @@
[
(container_doc_comment)
(doc_comment)
] @comment.doc
[
(line_comment)
] @comment
[
variable: (IDENTIFIER)
variable_type_function: (IDENTIFIER)
] @variable
;; func parameter
parameter: (IDENTIFIER) @property
[
field_member: (IDENTIFIER)
field_access: (IDENTIFIER)
] @property
;; assume TitleCase is a type
(
[
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
parameter: (IDENTIFIER)
] @type
(#match? @type "^[A-Z]([a-z]+[A-Za-z0-9]*)+$")
)
;; assume camelCase is a function
(
[
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
parameter: (IDENTIFIER)
] @function
(#match? @function "^[a-z]+([A-Z][a-z0-9]*)+$")
)
;; assume all CAPS_1 is a constant
(
[
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
] @constant
(#match? @constant "^[A-Z][A-Z_0-9]+$")
)
[
function_call: (IDENTIFIER)
function: (IDENTIFIER)
] @function
exception: "!" @keyword
(
(IDENTIFIER) @variable.special
(#eq? @variable.special "_")
)
(PtrTypeStart "c" @variable.special)
(
(ContainerDeclType
[
(ErrorUnionExpr)
"enum"
]
)
(ContainerField (IDENTIFIER) @constant)
)
field_constant: (IDENTIFIER) @constant
(BUILTINIDENTIFIER) @keyword
((BUILTINIDENTIFIER) @function
(#any-of? @function "@import" "@cImport"))
(INTEGER) @number
(FLOAT) @number
[
"true"
"false"
] @boolean
[
(LINESTRING)
(STRINGLITERALSINGLE)
] @string
(CHAR_LITERAL) @string.special.symbol
(EscapeSequence) @string.escape
(FormatSequence) @string.special
(BreakLabel (IDENTIFIER) @tag)
(BlockLabel (IDENTIFIER) @tag)
[
"asm"
"defer"
"errdefer"
"test"
"struct"
"union"
"enum"
"opaque"
"error"
] @keyword
[
"async"
"await"
"suspend"
"nosuspend"
"resume"
] @keyword.coroutine
[
"fn"
] @keyword
[
"and"
"or"
"orelse"
] @operator
[
"return"
] @keyword.return
[
"if"
"else"
"switch"
] @keyword.control
[
"for"
"while"
"break"
"continue"
] @keyword
[
"usingnamespace"
] @constant
[
"try"
"catch"
] @keyword
[
"anytype"
"anyframe"
(BuildinTypeExpr)
] @type
[
"const"
"var"
"volatile"
"allowzero"
"noalias"
] @keyword
[
"addrspace"
"align"
"callconv"
"linksection"
] @keyword.storage
[
"comptime"
"export"
"extern"
"inline"
"noinline"
"packed"
"pub"
"threadlocal"
] @keyword
[
"null"
"unreachable"
"undefined"
] @constant
[
(CompareOp)
(BitwiseOp)
(BitShiftOp)
(AdditionOp)
(AssignOp)
(MultiplyOp)
(PrefixOp)
"*"
"**"
"->"
".?"
".*"
"?"
] @operator
[
";"
"."
","
":"
] @punctuation.delimiter
[
".."
"..."
] @punctuation.special
[
"["
"]"
"("
")"
"{"
"}"
(Payload "|")
(PtrPayload "|")
(PtrIndexPayload "|")
] @punctuation.bracket
; Error
(ERROR) @error

View file

@ -0,0 +1,24 @@
[
(AsmExpr)
(AssignExpr)
(Block)
(BlockExpr)
(ContainerDecl)
(ErrorUnionExpr)
(InitList)
(SwitchExpr)
(TestDecl)
] @indent.begin
[
"}"
"]"
")"
] @indent.branch
[
(line_comment)
(container_doc_comment)
(doc_comment)
(LINESTRING)
] @indent.ignore

View file

@ -0,0 +1,5 @@
[
(container_doc_comment)
(doc_comment)
(line_comment)
] @comment

View file

@ -0,0 +1,24 @@
(Decl (
FnProto(
"fn" @context
function: (_) @name
)
)
) @item
(
Decl (
VarDecl (
"const"
variable_type_function: (_) @name
(ErrorUnionExpr) @context
)
)
) @item
(
TestDecl (
"test" @context
(STRINGLITERALSINGLE)? @name
)
) @item

116
extensions/zig/src/zig.rs Normal file
View file

@ -0,0 +1,116 @@
use std::fs;
use zed_extension_api::{self as zed, Result};
struct ZigExtension {
cached_binary_path: Option<String>,
}
impl ZigExtension {
fn language_server_binary_path(
&mut self,
config: zed::LanguageServerConfig,
worktree: &zed::Worktree,
) -> Result<String> {
if let Some(path) = &self.cached_binary_path {
if fs::metadata(path).map_or(false, |stat| stat.is_file()) {
return Ok(path.clone());
}
}
if let Some(path) = worktree.which("zls") {
self.cached_binary_path = Some(path.clone());
return Ok(path);
}
zed::set_language_server_installation_status(
&config.name,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);
let release = zed::latest_github_release(
"zigtools/zls",
zed::GithubReleaseOptions {
require_assets: true,
pre_release: false,
},
)?;
let (platform, arch) = zed::current_platform();
let asset_name = format!(
"zls-{arch}-{os}.{extension}",
arch = match arch {
zed::Architecture::Aarch64 => "aarch64",
zed::Architecture::X86 => "x86",
zed::Architecture::X8664 => "x86_64",
},
os = match platform {
zed::Os::Mac => "macos",
zed::Os::Linux => "linux",
zed::Os::Windows => "windows",
},
extension = match platform {
zed::Os::Mac | zed::Os::Linux => "tar.gz",
zed::Os::Windows => "zip",
}
);
let asset = release
.assets
.iter()
.find(|asset| asset.name == asset_name)
.ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
let version_dir = format!("zls-{}", release.version);
let binary_path = format!("{version_dir}/bin/zls");
if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
zed::set_language_server_installation_status(
&config.name,
&zed::LanguageServerInstallationStatus::Downloading,
);
zed::download_file(
&asset.download_url,
&version_dir,
match platform {
zed::Os::Mac | zed::Os::Linux => zed::DownloadedFileType::GzipTar,
zed::Os::Windows => zed::DownloadedFileType::Zip,
},
)
.map_err(|e| format!("failed to download file: {e}"))?;
let entries =
fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
for entry in entries {
let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
if entry.file_name().to_str() != Some(&version_dir) {
fs::remove_dir_all(&entry.path()).ok();
}
}
}
self.cached_binary_path = Some(binary_path.clone());
Ok(binary_path)
}
}
impl zed::Extension for ZigExtension {
fn new() -> Self {
Self {
cached_binary_path: None,
}
}
fn language_server_command(
&mut self,
config: zed::LanguageServerConfig,
worktree: &zed::Worktree,
) -> Result<zed::Command> {
Ok(zed::Command {
command: self.language_server_binary_path(config, worktree)?,
args: vec![],
env: Default::default(),
})
}
}
zed::register_extension!(ZigExtension);