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

View file

@ -36,7 +36,6 @@ mod toml;
mod typescript;
mod vue;
mod yaml;
mod zig;
// 1. Add tree-sitter-{language} parser to zed crate
// 2. Create a language directory in zed/crates/zed/src/languages and add the language to init function below
@ -105,7 +104,6 @@ pub fn init(
("typescript", tree_sitter_typescript::language_typescript()),
("vue", tree_sitter_vue::language()),
("yaml", tree_sitter_yaml::language()),
("zig", tree_sitter_zig::language()),
("dart", tree_sitter_dart::language()),
]);
@ -212,7 +210,6 @@ pub fn init(
language!("go", vec![Arc::new(go::GoLspAdapter)]);
language!("gomod");
language!("gowork");
language!("zig", vec![Arc::new(zig::ZlsAdapter)]);
language!(
"heex",
vec![

View file

@ -1,143 +0,0 @@
use anyhow::{anyhow, Context, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use async_trait::async_trait;
use futures::{io::BufReader, StreamExt};
use gpui::AsyncAppContext;
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
use lsp::LanguageServerBinary;
use smol::fs;
use std::env::consts::{ARCH, OS};
use std::{any::Any, path::PathBuf};
use util::github::latest_github_release;
use util::maybe;
use util::{github::GitHubLspBinaryVersion, ResultExt};
pub struct ZlsAdapter;
#[async_trait(?Send)]
impl LspAdapter for ZlsAdapter {
fn name(&self) -> LanguageServerName {
LanguageServerName("zls".into())
}
async fn fetch_latest_server_version(
&self,
delegate: &dyn LspAdapterDelegate,
) -> Result<Box<dyn 'static + Send + Any>> {
let release =
latest_github_release("zigtools/zls", true, false, delegate.http_client()).await?;
let asset_name = format!("zls-{ARCH}-{OS}.tar.gz");
let asset = release
.assets
.iter()
.find(|asset| asset.name == asset_name)
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
let version = GitHubLspBinaryVersion {
name: release.tag_name,
url: asset.browser_download_url.clone(),
};
Ok(Box::new(version) as Box<_>)
}
async fn check_if_user_installed(
&self,
delegate: &dyn LspAdapterDelegate,
_cx: &AsyncAppContext,
) -> Option<LanguageServerBinary> {
let env = delegate.shell_env().await;
let path = delegate.which("zls".as_ref()).await?;
Some(LanguageServerBinary {
path,
arguments: vec![],
env: Some(env),
})
}
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
container_dir: PathBuf,
delegate: &dyn LspAdapterDelegate,
) -> Result<LanguageServerBinary> {
let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
let binary_path = container_dir.join("bin/zls");
if fs::metadata(&binary_path).await.is_err() {
let mut response = delegate
.http_client()
.get(&version.url, Default::default(), true)
.await
.context("error downloading release")?;
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
let archive = Archive::new(decompressed_bytes);
archive.unpack(container_dir).await?;
}
// todo("windows")
#[cfg(not(windows))]
{
fs::set_permissions(
&binary_path,
<fs::Permissions as fs::unix::PermissionsExt>::from_mode(0o755),
)
.await?;
}
Ok(LanguageServerBinary {
path: binary_path,
env: None,
arguments: vec![],
})
}
async fn cached_server_binary(
&self,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir).await
}
async fn installation_test_binary(
&self,
container_dir: PathBuf,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir)
.await
.map(|mut binary| {
binary.arguments = vec!["--help".into()];
binary
})
}
}
async fn get_cached_server_binary(container_dir: PathBuf) -> Option<LanguageServerBinary> {
maybe!(async {
let mut last_binary_path = None;
let mut entries = fs::read_dir(&container_dir).await?;
while let Some(entry) = entries.next().await {
let entry = entry?;
if entry.file_type().await?.is_file()
&& entry
.file_name()
.to_str()
.map_or(false, |name| name == "zls")
{
last_binary_path = Some(entry.path());
}
}
if let Some(path) = last_binary_path {
Ok(LanguageServerBinary {
path,
env: None,
arguments: Vec::new(),
})
} else {
Err(anyhow!("no cached binary"))
}
})
.await
.log_err()
}

View file

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

View file

@ -1,11 +0,0 @@
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

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

View file

@ -1,240 +0,0 @@
[
(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

@ -1,24 +0,0 @@
[
(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

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

View file

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