Extract Elm language into an extension (#10432)

Release Notes:

- Extracted Elm language support into an extension

Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-04-11 10:23:49 -07:00 committed by GitHub
parent 78d6beee80
commit bcd2ca6196
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 155 additions and 18 deletions

17
Cargo.lock generated
View File

@ -5429,7 +5429,6 @@ dependencies = [
"tree-sitter-cpp",
"tree-sitter-css",
"tree-sitter-elixir",
"tree-sitter-elm",
"tree-sitter-embedded-template",
"tree-sitter-glsl",
"tree-sitter-go",
@ -10343,15 +10342,6 @@ dependencies = [
"tree-sitter",
]
[[package]]
name = "tree-sitter-elm"
version = "5.6.4"
source = "git+https://github.com/elm-tooling/tree-sitter-elm?rev=692c50c0b961364c40299e73c1306aecb5d20f40#692c50c0b961364c40299e73c1306aecb5d20f40"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-embedded-template"
version = "0.20.0"
@ -12587,6 +12577,13 @@ dependencies = [
"zed_extension_api 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "zed_elm"
version = "0.0.1"
dependencies = [
"zed_extension_api 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "zed_emmet"
version = "0.0.2"

View File

@ -104,6 +104,7 @@ members = [
"extensions/clojure",
"extensions/csharp",
"extensions/dart",
"extensions/elm",
"extensions/emmet",
"extensions/erlang",
"extensions/gleam",
@ -312,7 +313,6 @@ tree-sitter-c = "0.20.1"
tree-sitter-cpp = { git = "https://github.com/tree-sitter/tree-sitter-cpp", rev = "f44509141e7e483323d2ec178f2d2e6c0fc041c1" }
tree-sitter-css = { git = "https://github.com/tree-sitter/tree-sitter-css", rev = "769203d0f9abe1a9a691ac2b9fe4bb4397a73c51" }
tree-sitter-elixir = { git = "https://github.com/elixir-lang/tree-sitter-elixir", rev = "a2861e88a730287a60c11ea9299c033c7d076e30" }
tree-sitter-elm = { git = "https://github.com/elm-tooling/tree-sitter-elm", rev = "692c50c0b961364c40299e73c1306aecb5d20f40" }
tree-sitter-embedded-template = "0.20.0"
tree-sitter-glsl = { git = "https://github.com/theHamsta/tree-sitter-glsl", rev = "2a56fb7bc8bb03a1892b4741279dd0a8758b7fb3" }
tree-sitter-go = { git = "https://github.com/tree-sitter/tree-sitter-go", rev = "aeb2f33b366fd78d5789ff104956ce23508b85db" }

View File

@ -26,6 +26,7 @@ fn suggested_extensions() -> &'static HashMap<&'static str, Arc<str>> {
("dart", "dart"),
("dockerfile", "Dockerfile"),
("elisp", "el"),
("elm", "elm"),
("erlang", "erl"),
("erlang", "hrl"),
("fish", "fish"),

View File

@ -41,7 +41,6 @@ tree-sitter-c.workspace = true
tree-sitter-cpp.workspace = true
tree-sitter-css.workspace = true
tree-sitter-elixir.workspace = true
tree-sitter-elm.workspace = true
tree-sitter-embedded-template.workspace = true
tree-sitter-glsl.workspace = true
tree-sitter-go.workspace = true

View File

@ -16,7 +16,6 @@ mod c;
mod css;
mod deno;
mod elixir;
mod elm;
mod go;
mod json;
mod lua;
@ -59,7 +58,6 @@ pub fn init(
("cpp", tree_sitter_cpp::language()),
("css", tree_sitter_css::language()),
("elixir", tree_sitter_elixir::language()),
("elm", tree_sitter_elm::language()),
(
"embedded_template",
tree_sitter_embedded_template::language(),
@ -288,10 +286,6 @@ pub fn init(
"yaml",
vec![Arc::new(yaml::YamlLspAdapter::new(node_runtime.clone()))]
);
language!(
"elm",
vec![Arc::new(elm::ElmLspAdapter::new(node_runtime.clone()))]
);
language!("glsl");
language!("nix");
language!("nu", vec![Arc::new(nu::NuLanguageServer {})]);

16
extensions/elm/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "zed_elm"
version = "0.0.1"
edition = "2021"
publish = false
license = "Apache-2.0"
[lints]
workspace = true
[lib]
path = "src/elm.rs"
crate-type = ["cdylib"]
[dependencies]
zed_extension_api = "0.0.6"

View File

@ -0,0 +1 @@
../../LICENSE-APACHE

View File

@ -0,0 +1,15 @@
id = "elm"
name = "Elm"
description = "Elm support."
version = "0.0.1"
schema_version = 1
authors = ["Quinn Wilton <quinn@quinnwilton.com>", "Andrey Kuzmin <hi@unsoundscapes.com>"]
repository = "https://github.com/zed-industries/zed"
[language_servers.elm-language-server]
name = "elm-language-server"
language = "Elm"
[grammars.elm]
repository = "https://github.com/elm-tooling/tree-sitter-elm"
commit = "09dbf221d7491dc8d8839616b27c21b9c025c457"

View File

@ -10,3 +10,4 @@ brackets = [
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
{ start = "'", end = "'", close = true, newline = false, not_in = ["string", "comment"] },
]
tab_size = 2

113
extensions/elm/src/elm.rs Normal file
View File

@ -0,0 +1,113 @@
use std::{env, fs};
use zed::{
serde_json::{self, Value},
settings::LspSettings,
};
use zed_extension_api::{self as zed, Result};
const SERVER_PATH: &str = "node_modules/@elm-tooling/elm-language-server/out/node/index.js";
const PACKAGE_NAME: &str = "@elm-tooling/elm-language-server";
struct ElmExtension {
did_find_server: bool,
}
impl ElmExtension {
fn server_exists(&self) -> bool {
fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
}
fn server_script_path(&mut self, server_id: &zed::LanguageServerId) -> Result<String> {
let server_exists = self.server_exists();
if self.did_find_server && server_exists {
return Ok(SERVER_PATH.to_string());
}
zed::set_language_server_installation_status(
&server_id,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);
let version = zed::npm_package_latest_version(PACKAGE_NAME)?;
if !server_exists
|| zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
{
zed::set_language_server_installation_status(
&server_id,
&zed::LanguageServerInstallationStatus::Downloading,
);
let result = zed::npm_install_package(PACKAGE_NAME, &version);
match result {
Ok(()) => {
if !self.server_exists() {
Err(format!(
"installed package '{PACKAGE_NAME}' did not contain expected path '{SERVER_PATH}'",
))?;
}
}
Err(error) => {
if !self.server_exists() {
Err(error)?;
}
}
}
}
self.did_find_server = true;
Ok(SERVER_PATH.to_string())
}
}
impl zed::Extension for ElmExtension {
fn new() -> Self {
Self {
did_find_server: false,
}
}
fn language_server_command(
&mut self,
server_id: &zed::LanguageServerId,
_worktree: &zed::Worktree,
) -> Result<zed::Command> {
let server_path = self.server_script_path(server_id)?;
Ok(zed::Command {
command: zed::node_binary_path()?,
args: vec![
env::current_dir()
.unwrap()
.join(&server_path)
.to_string_lossy()
.to_string(),
"--stdio".to_string(),
],
env: Default::default(),
})
}
fn language_server_workspace_configuration(
&mut self,
server_id: &zed::LanguageServerId,
worktree: &zed::Worktree,
) -> Result<Option<Value>> {
// elm-language-server expects workspace didChangeConfiguration notification
// params to be the same as lsp initialization_options
let initialization_options = LspSettings::for_worktree(server_id.as_ref(), worktree)?
.initialization_options
.clone()
.unwrap_or_default();
Ok(Some(match initialization_options.clone().as_object_mut() {
Some(op) => {
// elm-language-server requests workspace configuration
// for the `elmLS` section, so we have to nest
// another copy of initialization_options there
op.insert("elmLS".into(), initialization_options);
serde_json::to_value(op).unwrap_or_default()
}
None => initialization_options,
}))
}
}
zed::register_extension!(ElmExtension);