Add Prisma language support (#7267)

Fixes #4832

Adds tree-sitter grammar and LSP adapter for Prisma



https://github.com/zed-industries/zed/assets/16297930/0f288ab1-ce5c-4e31-ad7f-6bb9655863c1
This commit is contained in:
Matthew Gramigna 2024-02-10 13:26:39 -05:00 committed by GitHub
parent 2f3ad9da4c
commit 67839a967b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 189 additions and 1 deletions

10
Cargo.lock generated
View File

@ -9371,6 +9371,15 @@ dependencies = [
"tree-sitter",
]
[[package]]
name = "tree-sitter-prisma-io"
version = "1.4.0"
source = "git+https://github.com/victorhqc/tree-sitter-prisma#eca2596a355b1a9952b4f80f8f9caed300a272b5"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-proto"
version = "0.0.2"
@ -10855,6 +10864,7 @@ dependencies = [
"tree-sitter-nu",
"tree-sitter-ocaml",
"tree-sitter-php",
"tree-sitter-prisma-io",
"tree-sitter-proto",
"tree-sitter-purescript",
"tree-sitter-python",

View File

@ -250,6 +250,7 @@ tree-sitter-nix = { git = "https://github.com/nix-community/tree-sitter-nix", re
tree-sitter-nu = { git = "https://github.com/nushell/tree-sitter-nu", rev = "26bbaecda0039df4067861ab38ea8ea169f7f5aa" }
tree-sitter-ocaml = { git = "https://github.com/tree-sitter/tree-sitter-ocaml", rev = "4abfdc1c7af2c6c77a370aee974627be1c285b3b" }
tree-sitter-php = "0.21.1"
tree-sitter-prisma-io = { git = "https://github.com/victorhqc/tree-sitter-prisma" }
tree-sitter-proto = { git = "https://github.com/rewinfrey/tree-sitter-proto", rev = "36d54f288aee112f13a67b550ad32634d0c2cb52" }
tree-sitter-purescript = { git = "https://github.com/ivanmoreau/tree-sitter-purescript", rev = "a37140f0c7034977b90faa73c94fcb8a5e45ed08" }
tree-sitter-python = "0.20.2"

View File

@ -135,6 +135,7 @@ tree-sitter-nix.workspace = true
tree-sitter-nu.workspace = true
tree-sitter-ocaml.workspace = true
tree-sitter-php.workspace = true
tree-sitter-prisma-io.workspace = true
tree-sitter-proto.workspace = true
tree-sitter-purescript.workspace = true
tree-sitter-python.workspace = true

View File

@ -27,6 +27,7 @@ mod lua;
mod nu;
mod ocaml;
mod php;
mod prisma;
mod purescript;
mod python;
mod ruby;
@ -97,6 +98,7 @@ pub fn init(
tree_sitter_ocaml::language_ocaml_interface(),
),
("php", tree_sitter_php::language_php()),
("prisma", tree_sitter_prisma_io::language()),
("proto", tree_sitter_proto::language()),
#[cfg(not(target_os = "linux"))]
("purescript", tree_sitter_purescript::language()),
@ -290,12 +292,21 @@ pub fn init(
language("nu", vec![Arc::new(nu::NuLanguageServer {})]);
language("ocaml", vec![Arc::new(ocaml::OCamlLspAdapter)]);
language("ocaml-interface", vec![Arc::new(ocaml::OCamlLspAdapter)]);
language("vue", vec![Arc::new(vue::VueLspAdapter::new(node_runtime))]);
language(
"vue",
vec![Arc::new(vue::VueLspAdapter::new(node_runtime.clone()))],
);
language("uiua", vec![Arc::new(uiua::UiuaLanguageServer {})]);
language("proto", vec![]);
language("terraform", vec![]);
language("terraform-vars", vec![]);
language("hcl", vec![]);
language(
"prisma",
vec![Arc::new(prisma::PrismaLspAdapter::new(
node_runtime.clone(),
))],
);
}
#[cfg(any(test, feature = "test-support"))]

View File

@ -0,0 +1,126 @@
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 smol::fs;
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use util::{async_maybe, ResultExt};
const SERVER_PATH: &'static str = "node_modules/.bin/prisma-language-server";
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
}
pub struct PrismaLspAdapter {
node: Arc<dyn NodeRuntime>,
}
impl PrismaLspAdapter {
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
PrismaLspAdapter { node }
}
}
#[async_trait]
impl LspAdapter for PrismaLspAdapter {
fn name(&self) -> LanguageServerName {
LanguageServerName("prisma-language-server".into())
}
fn short_name(&self) -> &'static str {
"prisma-language-server"
}
async fn fetch_latest_server_version(
&self,
_: &dyn LspAdapterDelegate,
) -> Result<Box<dyn 'static + Any + Send>> {
Ok(Box::new(
self.node
.npm_package_latest_version("@prisma/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,
&[("@prisma/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> {
None
}
}
async fn get_cached_server_binary(
container_dir: PathBuf,
node: &dyn NodeRuntime,
) -> Option<LanguageServerBinary> {
async_maybe!({
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,9 @@
name = "Prisma"
grammar = "prisma"
path_suffixes = ["prisma"]
line_comments = ["// "]
brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "[", end = "]", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = true }
]

View File

@ -0,0 +1,26 @@
[
"datasource"
"enum"
"generator"
"model"
] @keyword
(comment) @comment
(developer_comment) @comment
(arguments) @property
(attribute) @function
(call_expression) @function
(column_type) @type
(enumeral) @constant
(identifier) @variable
(string) @string
"(" @punctuation.bracket
")" @punctuation.bracket
"[" @punctuation.bracket
"]" @punctuation.bracket
"{" @punctuation.bracket
"}" @punctuation.bracket
"=" @operator
"@" @operator

View File

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