From 67839a967b7bf0e19d49df2570e0475204afa7f2 Mon Sep 17 00:00:00 2001 From: Matthew Gramigna Date: Sat, 10 Feb 2024 13:26:39 -0500 Subject: [PATCH] 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 --- Cargo.lock | 10 ++ Cargo.toml | 1 + crates/zed/Cargo.toml | 1 + crates/zed/src/languages.rs | 13 +- crates/zed/src/languages/prisma.rs | 126 ++++++++++++++++++ crates/zed/src/languages/prisma/config.toml | 9 ++ .../zed/src/languages/prisma/highlights.scm | 26 ++++ docs/src/languages/prisma.md | 4 + 8 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 crates/zed/src/languages/prisma.rs create mode 100644 crates/zed/src/languages/prisma/config.toml create mode 100644 crates/zed/src/languages/prisma/highlights.scm create mode 100644 docs/src/languages/prisma.md diff --git a/Cargo.lock b/Cargo.lock index 5dbdcd78d7..d05fc4ae93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 4633b0c122..84627885e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index 1e9a5ba31e..05b74515d5 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -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 diff --git a/crates/zed/src/languages.rs b/crates/zed/src/languages.rs index e032cd7521..3b78b23225 100644 --- a/crates/zed/src/languages.rs +++ b/crates/zed/src/languages.rs @@ -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"))] diff --git a/crates/zed/src/languages/prisma.rs b/crates/zed/src/languages/prisma.rs new file mode 100644 index 0000000000..e84cf8d3e9 --- /dev/null +++ b/crates/zed/src/languages/prisma.rs @@ -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 { + vec![server_path.into(), "--stdio".into()] +} + +pub struct PrismaLspAdapter { + node: Arc, +} + +impl PrismaLspAdapter { + pub fn new(node: Arc) -> 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> { + Ok(Box::new( + self.node + .npm_package_latest_version("@prisma/language-server") + .await?, + ) as Box<_>) + } + + async fn fetch_server_binary( + &self, + version: Box, + container_dir: PathBuf, + _: &dyn LspAdapterDelegate, + ) -> Result { + let version = version.downcast::().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 { + get_cached_server_binary(container_dir, &*self.node).await + } + + async fn installation_test_binary( + &self, + container_dir: PathBuf, + ) -> Option { + get_cached_server_binary(container_dir, &*self.node).await + } + + fn initialization_options(&self) -> Option { + None + } +} + +async fn get_cached_server_binary( + container_dir: PathBuf, + node: &dyn NodeRuntime, +) -> Option { + 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() +} diff --git a/crates/zed/src/languages/prisma/config.toml b/crates/zed/src/languages/prisma/config.toml new file mode 100644 index 0000000000..c7f2d1ef6f --- /dev/null +++ b/crates/zed/src/languages/prisma/config.toml @@ -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 } +] diff --git a/crates/zed/src/languages/prisma/highlights.scm b/crates/zed/src/languages/prisma/highlights.scm new file mode 100644 index 0000000000..5a7d7a8124 --- /dev/null +++ b/crates/zed/src/languages/prisma/highlights.scm @@ -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 diff --git a/docs/src/languages/prisma.md b/docs/src/languages/prisma.md new file mode 100644 index 0000000000..935a8d3efe --- /dev/null +++ b/docs/src/languages/prisma.md @@ -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)