Add Astro Support (#6896)

Attempt to add `@astrojs/language-server` and
[virchau13/tree-sitter-astro](https://github.com/virchau13/tree-sitter-astro).

---------

Co-authored-by: Nate Butler <iamnbutler@gmail.com>
This commit is contained in:
Alvaro Gaona 2024-02-12 15:10:40 -03:00 committed by GitHub
parent 1c2081c10c
commit 2e7db57e16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 236 additions and 0 deletions

10
Cargo.lock generated
View File

@ -9112,6 +9112,15 @@ dependencies = [
"wasmtime-c-api-impl",
]
[[package]]
name = "tree-sitter-astro"
version = "0.0.1"
source = "git+https://github.com/virchau13/tree-sitter-astro.git?rev=e924787e12e8a03194f36a113290ac11d6dc10f3#e924787e12e8a03194f36a113290ac11d6dc10f3"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-bash"
version = "0.20.4"
@ -10846,6 +10855,7 @@ dependencies = [
"tiny_http",
"toml",
"tree-sitter",
"tree-sitter-astro",
"tree-sitter-bash",
"tree-sitter-beancount",
"tree-sitter-c",

View File

@ -223,6 +223,7 @@ tiktoken-rs = "0.5.7"
time = { version = "0.3", features = ["serde", "serde-well-known"] }
toml = "0.5"
tree-sitter = { version = "0.20", features = ["wasm"] }
tree-sitter-astro = { git = "https://github.com/virchau13/tree-sitter-astro.git", rev = "e924787e12e8a03194f36a113290ac11d6dc10f3" }
tree-sitter-bash = { git = "https://github.com/tree-sitter/tree-sitter-bash", rev = "7331995b19b8f8aba2d5e26deb51d2195c18bc94" }
tree-sitter-beancount = { git = "https://github.com/polarmutex/tree-sitter-beancount", rev = "da1bf8c6eb0ae7a97588affde7227630bcd678b6" }
tree-sitter-c = "0.20.1"

View File

@ -0,0 +1,4 @@
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M5.32668 11.5331C4.78215 10.9852 4.62318 9.834 4.85006 9C5.24343 9.52582 5.78848 9.69239 6.35304 9.78642C7.2246 9.93151 8.08055 9.87724 8.89019 9.43877C8.98281 9.38857 9.06841 9.32182 9.16962 9.25421C9.24559 9.49681 9.26536 9.74172 9.23882 9.99099C9.1743 10.5981 8.89982 11.067 8.46326 11.4225C8.28869 11.5647 8.10397 11.6918 7.92367 11.8259C7.36978 12.2379 7.21992 12.7211 7.42805 13.4239C7.433 13.4411 7.43742 13.4582 7.44861 13.5C7.1658 13.3606 6.95923 13.1578 6.80182 12.8911C6.63558 12.6097 6.55649 12.2983 6.55233 11.9614C6.55025 11.7974 6.55025 11.632 6.53022 11.4704C6.4813 11.0763 6.31323 10.8999 5.99661 10.8897C5.67166 10.8793 5.41462 11.1004 5.34646 11.4486C5.34125 11.4753 5.33371 11.5017 5.32616 11.5328L5.32668 11.5331Z" fill="#17191E" fill-opacity="0.6"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M5.30826 8.62679L6.73279 4.5906C6.82176 4.33854 7.17823 4.33854 7.26719 4.5906L8.69173 8.62679C8.69652 8.64035 8.70626 8.65153 8.7189 8.65814C8.80645 8.6531 8.89466 8.65055 8.98347 8.65055C10.1418 8.65055 11.1986 9.08493 12 9.79967L8.83801 1.36772C8.75507 1.14653 8.54362 1 8.30739 1H5.6926C5.45637 1 5.24492 1.14653 5.16198 1.36772L1.99999 9.79968C2.80137 9.08493 3.85822 8.65055 5.01652 8.65055C5.10533 8.65055 5.19355 8.6531 5.28109 8.65814C5.29373 8.65153 5.30347 8.64035 5.30826 8.62679Z" fill="black"/>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -1,5 +1,6 @@
{
"suffixes": {
"astro": "astro",
"Emakefile": "erlang",
"aac": "audio",
"accdb": "storage",
@ -148,6 +149,9 @@
"zshrc": "terminal"
},
"types": {
"astro": {
"icon": "icons/file_icons/astro.svg"
},
"audio": {
"icon": "icons/file_icons/audio.svg"
},

View File

@ -108,6 +108,7 @@ theme_selector.workspace = true
thiserror.workspace = true
tiny_http = "0.8"
toml.workspace = true
tree-sitter-astro.workspace = true
tree-sitter-bash.workspace = true
tree-sitter-beancount.workspace = true
tree-sitter-c-sharp.workspace = true

View File

@ -9,6 +9,7 @@ use util::asset_str;
use self::{deno::DenoSettings, elixir::ElixirSettings};
mod astro;
mod c;
mod clojure;
mod csharp;
@ -65,6 +66,7 @@ pub fn init(
DenoSettings::register(cx);
languages.register_native_grammars([
("astro", tree_sitter_astro::language()),
("bash", tree_sitter_bash::language()),
("beancount", tree_sitter_beancount::language()),
("c", tree_sitter_c::language()),
@ -130,6 +132,13 @@ pub fn init(
)
};
language(
"astro",
vec![
Arc::new(astro::AstroLspAdapter::new(node_runtime.clone())),
Arc::new(tailwind::TailwindLspAdapter::new(node_runtime.clone())),
],
);
language("bash", vec![]);
language("beancount", vec![]);
language("c", vec![Arc::new(c::CLspAdapter) as Arc<dyn LspAdapter>]);

View File

@ -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()
}

View File

@ -0,0 +1,3 @@
("{" @open "}" @close)
("<" @open ">" @close)
("\"" @open "\"" @close)

View File

@ -0,0 +1,22 @@
name = "Astro"
grammar = "astro"
path_suffixes = ["astro"]
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 = ["#", "$", "-"]
scope_opt_in_language_servers = ["tailwindcss-language-server"]
prettier_parser_name = "astro"
[overrides.string]
word_characters = ["-"]
opt_into_language_servers = ["tailwindcss-language-server"]

View File

@ -0,0 +1,25 @@
(tag_name) @tag
(erroneous_end_tag_name) @keyword
(doctype) @constant
(attribute_name) @property
(attribute_value) @string
(comment) @comment
[
(attribute_value)
(quoted_attribute_value)
] @string
"=" @operator
[
"{"
"}"
] @punctuation.bracket
[
"<"
">"
"</"
"/>"
] @tag.delimiter

View File

@ -0,0 +1,16 @@
; inherits: html_tags
(frontmatter
(raw_text) @content
(#set! "language" "typescript"))
(interpolation
(raw_text) @content
(#set! "language" "tsx"))
(script_element
(raw_text) @content
(#set! "language" "typescript"))
(style_element
(raw_text) @content
(#set! "language" "css"))

View File

@ -114,6 +114,7 @@ impl LspAdapter for TailwindLspAdapter {
fn language_ids(&self) -> HashMap<String, String> {
HashMap::from_iter([
("Astro".to_string(), "astro".to_string()),
("HTML".to_string(), "html".to_string()),
("CSS".to_string(), "css".to_string()),
("JavaScript".to_string(), "javascript".to_string()),

View File

@ -0,0 +1,4 @@
# Astro
- Tree Sitter: [tree-sitter-astro](https://github.com/virchau13/tree-sitter-astro)
- Language Server: [astro](https://github.com/withastro/language-tools/tree/main/packages/language-server)