Extract Vue extension (#10486)
This PR extracts Vue support into an extension and removes the built-in C# support from Zed. Release Notes: - Removed built-in support for Vue, in favor of making it available as an extension. The Vue extension will be suggested for download when you open a `.vue` file. --------- Co-authored-by: Max <max@zed.dev>
This commit is contained in:
parent
8bca9cea26
commit
f3a78f613a
18 changed files with 196 additions and 275 deletions
135
extensions/vue/src/vue.rs
Normal file
135
extensions/vue/src/vue.rs
Normal file
|
@ -0,0 +1,135 @@
|
|||
use std::{env, fs};
|
||||
use zed::lsp::{Completion, CompletionKind};
|
||||
use zed::CodeLabelSpan;
|
||||
use zed_extension_api::{self as zed, serde_json, Result};
|
||||
|
||||
struct VueExtension {
|
||||
did_find_server: bool,
|
||||
}
|
||||
|
||||
const SERVER_PATH: &str = "node_modules/@vue/language-server/bin/vue-language-server.js";
|
||||
const PACKAGE_NAME: &str = "@vue/language-server";
|
||||
|
||||
impl VueExtension {
|
||||
fn server_exists(&self) -> bool {
|
||||
fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
|
||||
}
|
||||
|
||||
fn server_script_path(&mut self, 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(
|
||||
id,
|
||||
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
|
||||
);
|
||||
// We hardcode the version to 1.8 since we do not support @vue/language-server 2.0 yet.
|
||||
let version = "1.8".to_string();
|
||||
|
||||
if !server_exists
|
||||
|| zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
|
||||
{
|
||||
zed::set_language_server_installation_status(
|
||||
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 VueExtension {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
did_find_server: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn language_server_command(
|
||||
&mut self,
|
||||
id: &zed::LanguageServerId,
|
||||
_: &zed::Worktree,
|
||||
) -> Result<zed::Command> {
|
||||
let server_path = self.server_script_path(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_initialization_options(
|
||||
&mut self,
|
||||
_: &zed::LanguageServerId,
|
||||
_: &zed::Worktree,
|
||||
) -> Result<Option<serde_json::Value>> {
|
||||
Ok(Some(serde_json::json!({
|
||||
"typescript": {
|
||||
"tsdk": "node_modules/typescript/lib"
|
||||
}
|
||||
})))
|
||||
}
|
||||
|
||||
fn label_for_completion(
|
||||
&self,
|
||||
_language_server_id: &zed::LanguageServerId,
|
||||
completion: Completion,
|
||||
) -> Option<zed::CodeLabel> {
|
||||
let highlight_name = match completion.kind? {
|
||||
CompletionKind::Class | CompletionKind::Interface => "type",
|
||||
CompletionKind::Constructor => "type",
|
||||
CompletionKind::Constant => "constant",
|
||||
CompletionKind::Function | CompletionKind::Method => "function",
|
||||
CompletionKind::Property | CompletionKind::Field => "tag",
|
||||
CompletionKind::Variable => "type",
|
||||
CompletionKind::Keyword => "keyword",
|
||||
CompletionKind::Value => "tag",
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let len = completion.label.len();
|
||||
let name_span = CodeLabelSpan::literal(completion.label, Some(highlight_name.to_string()));
|
||||
|
||||
Some(zed::CodeLabel {
|
||||
code: Default::default(),
|
||||
spans: if let Some(detail) = completion.detail {
|
||||
vec![
|
||||
name_span,
|
||||
CodeLabelSpan::literal(" ", None),
|
||||
CodeLabelSpan::literal(detail, None),
|
||||
]
|
||||
} else {
|
||||
vec![name_span]
|
||||
},
|
||||
filter_range: (0..len).into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
zed::register_extension!(VueExtension);
|
Loading…
Add table
Add a link
Reference in a new issue