Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Astro Support #6896

Merged
merged 18 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ tree-sitter-nu = { git = "https://github.com/nushell/tree-sitter-nu", rev = "26b
tree-sitter-vue = {git = "https://github.com/zed-industries/tree-sitter-vue", rev = "6608d9d60c386f19d80af7d8132322fa11199c42"}
tree-sitter-uiua = {git = "https://github.com/shnarazk/tree-sitter-uiua", rev = "9260f11be5900beda4ee6d1a24ab8ddfaf5a19b2"}
tree-sitter-zig = { git = "https://github.com/maxxnino/tree-sitter-zig", rev = "0d08703e4c3f426ec61695d7617415fff97029bd" }
tree-sitter-astro = { git = "https://github.com/virchau13/tree-sitter-astro.git", rev = "e924787e12e8a03194f36a113290ac11d6dc10f3" }

[patch.crates-io]
tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "31c40449749c4263a91a43593831b82229049a4c" }
Expand Down
1 change: 1 addition & 0 deletions crates/zed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ tree-sitter-nu.workspace = true
tree-sitter-vue.workspace = true
tree-sitter-uiua.workspace = true
tree-sitter-zig.workspace = true
tree-sitter-astro.workspace = true

url = "2.2"
urlencoding = "2.1.2"
Expand Down
9 changes: 9 additions & 0 deletions crates/zed/src/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod uiua;
mod vue;
mod yaml;
mod zig;
mod astro;

// 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
Expand Down Expand Up @@ -249,6 +250,14 @@ pub fn init(
Arc::new(tailwind::TailwindLspAdapter::new(node_runtime.clone())),
],
);
language(
"astro",
tree_sitter_astro::language(),
vec![
Arc::new(astro::AstroLspAdapter::new(node_runtime.clone())),
Arc::new(tailwind::TailwindLspAdapter::new(node_runtime.clone())),
]
);
language(
"php",
tree_sitter_php::language(),
Expand Down
136 changes: 136 additions & 0 deletions crates/zed/src/languages/astro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use futures::StreamExt;
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
use lsp::LanguageServerBinary;
use node_runtime::NodeRuntime;
use serde_json::json;
use smol::fs;
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use util::ResultExt;

const SERVER_PATH: &'static str = "node_modules/@astrojs/language-server/bin/nodeServer.js";

fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
}

pub struct AstroLspAdapter {
node: Arc<dyn NodeRuntime>,
}

impl AstroLspAdapter {
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
AstroLspAdapter { node }
}
}

#[async_trait]
impl LspAdapter for AstroLspAdapter {
fn name(&self) -> LanguageServerName {
LanguageServerName("astro-language-server".into())
}

fn short_name(&self) -> &'static str {
"astro"
}

async fn fetch_latest_server_version(
&self,
_: &dyn LspAdapterDelegate,
) -> Result<Box<dyn 'static + Any + Send>> {
Ok(Box::new(
self.node
.npm_package_latest_version("@astrojs/language-server")
.await?,
) as Box<_>)
}

async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Result<LanguageServerBinary> {
let version = version.downcast::<String>().unwrap();
let server_path = container_dir.join(SERVER_PATH);

if fs::metadata(&server_path).await.is_err() {
self.node
.npm_install_packages(
&container_dir,
&[("@astrojs/language-server", version.as_str())],
)
.await?;
}

Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
}

async fn cached_server_binary(
&self,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir, &*self.node).await
}

async fn installation_test_binary(
&self,
container_dir: PathBuf,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir, &*self.node).await
}

fn initialization_options(&self) -> Option<serde_json::Value> {
Some(json!({
"provideFormatter": true,
"typescript": {
"tsdk": "node_modules/typescript/lib",
}
}))
}

fn prettier_plugins(&self) -> &[&'static str] {
&["prettier-plugin-astro"]
}
}

async fn get_cached_server_binary(
container_dir: PathBuf,
node: &dyn NodeRuntime,
) -> Option<LanguageServerBinary> {
(|| async move {
let mut last_version_dir = 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_dir() {
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
let server_path = last_version_dir.join(SERVER_PATH);
if server_path.exists() {
Ok(LanguageServerBinary {
path: node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
} else {
Err(anyhow!(
"missing executable in directory {:?}",
last_version_dir
))
}
})()
.await
.log_err()
}
17 changes: 17 additions & 0 deletions crates/zed/src/languages/astro/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name = "Astro"
path_suffixes = ["astro"]
line_comments = ["// "]
block_comment = ["<!-- ", " -->"]
autoclose_before = ";:.,=}])>"
brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "[", end = "]", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = true },
{ start = "<", end = ">", close = false, newline = true, not_in = ["string", "comment"] },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string", "comment"] },
{ start = "'", end = "'", close = true, newline = false, not_in = ["string", "comment"] },
{ start = "`", end = "`", close = true, newline = false, not_in = ["string"] },
{ start = "/*", end = " */", close = true, newline = false, not_in = ["string", "comment"] },
]
word_characters = ["#", "$", '-']
prettier_parser_name = "astro"
12 changes: 12 additions & 0 deletions crates/zed/src/languages/astro/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(tag_name) @tag
(erroneous_end_tag_name) @tag.error
(doctype) @constant
(attribute_name) @attribute
(attribute_value) @string
(comment) @comment

[
"<"
">"
"</"
] @punctuation.bracket
15 changes: 15 additions & 0 deletions crates/zed/src/languages/astro/injections.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
((script_element
(raw_text) @injection.content)
(#set! injection.language "javascript"))

((style_element
(raw_text) @injection.content)
(#set! injection.language "css"))

((frontmatter
(raw_text) @injection.content)
(#set! injection.language "typescript"))

((interpolation
(raw_text) @injection.content)
(#set! injection.language "tsx"))