mirror of
https://github.com/zed-industries/zed.git
synced 2024-10-23 06:56:33 +00:00
Add language-agnostic snippets (#13253)
Note that right now we can't attach a language server to arbitrary buffer, which is why I've listed a bunch of languages verbatim. See https://github.com/zed-industries/simple-completion-language-server/tree/main for docs on how to define your snippets. They should be placed in ~/.config/zed/snippets ; `snippets.(toml|json)` file can be used to define language-agnostic snippets, and any other name (e.g. `python.toml`) will apply only to buffers of that particular type. There's https://github.com/rafamadriz/friendly-snippets you can use as a repository of snippets, for your convenience. Fixes https://github.com/zed-industries/zed/issues/4611 Release Notes: - Added support for snippets via simple-completion-language-server
This commit is contained in:
parent
8c4fb34f6e
commit
d665f28671
6 changed files with 177 additions and 0 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -13584,6 +13584,14 @@ dependencies = [
|
|||
"zed_extension_api 0.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zed_snippets"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
"zed_extension_api 0.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zed_svelte"
|
||||
version = "0.0.1"
|
||||
|
|
|
@ -136,6 +136,7 @@ members = [
|
|||
"extensions/prisma",
|
||||
"extensions/purescript",
|
||||
"extensions/ruby",
|
||||
"extensions/snippets",
|
||||
"extensions/svelte",
|
||||
"extensions/terraform",
|
||||
"extensions/toml",
|
||||
|
|
17
extensions/snippets/Cargo.toml
Normal file
17
extensions/snippets/Cargo.toml
Normal file
|
@ -0,0 +1,17 @@
|
|||
[package]
|
||||
name = "zed_snippets"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
license = "Apache-2.0"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/snippets.rs"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
zed_extension_api = "0.0.6"
|
||||
serde_json = "1.0"
|
1
extensions/snippets/LICENSE-APACHE
Symbolic link
1
extensions/snippets/LICENSE-APACHE
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../LICENSE-APACHE
|
12
extensions/snippets/extension.toml
Normal file
12
extensions/snippets/extension.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
id = "snippets"
|
||||
name = "Snippets"
|
||||
description = "Support for language-agnostic snippets, provided by simple-completion-language-server"
|
||||
version = "0.0.1"
|
||||
schema_version = 1
|
||||
authors = []
|
||||
repository = "https://github.com/zed-extensions/svelte"
|
||||
|
||||
[language_servers.snippet-completion-server]
|
||||
name = "Snippet Completion Server"
|
||||
languages = ["TypeScript", "TSX", "JavaScript", "JSDoc", "Go", "Markdown", "Rust", "C", "C++", "PHP", "Python", "Ruby", "Shell"]
|
||||
language_ids = { "TypeScript" = "typescript", "TSX" = "typescriptreact", "JavaScript" = "javascript" }
|
138
extensions/snippets/src/snippets.rs
Normal file
138
extensions/snippets/src/snippets.rs
Normal file
|
@ -0,0 +1,138 @@
|
|||
use serde_json::json;
|
||||
use std::fs;
|
||||
use zed::LanguageServerId;
|
||||
use zed_extension_api::{self as zed, Result};
|
||||
|
||||
struct SnippetExtension {
|
||||
cached_binary_path: Option<String>,
|
||||
}
|
||||
|
||||
impl SnippetExtension {
|
||||
fn language_server_binary_path(
|
||||
&mut self,
|
||||
language_server_id: &LanguageServerId,
|
||||
worktree: &zed::Worktree,
|
||||
) -> Result<String> {
|
||||
if let Some(path) = worktree.which("simple-completion-language-server") {
|
||||
return Ok(path);
|
||||
}
|
||||
|
||||
if let Some(path) = &self.cached_binary_path {
|
||||
if fs::metadata(path).map_or(false, |stat| stat.is_file()) {
|
||||
return Ok(path.clone());
|
||||
}
|
||||
}
|
||||
|
||||
zed::set_language_server_installation_status(
|
||||
&language_server_id,
|
||||
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
|
||||
);
|
||||
let release = zed::latest_github_release(
|
||||
"zed-industries/simple-completion-language-server",
|
||||
zed::GithubReleaseOptions {
|
||||
require_assets: true,
|
||||
pre_release: false,
|
||||
},
|
||||
)?;
|
||||
|
||||
let (platform, arch) = zed::current_platform();
|
||||
let asset_name = format!(
|
||||
"simple-completion-language-server-{arch}-{os}.tar.gz",
|
||||
arch = match arch {
|
||||
zed::Architecture::Aarch64 => "aarch64",
|
||||
zed::Architecture::X86 => "x86",
|
||||
zed::Architecture::X8664 => "x86_64",
|
||||
},
|
||||
os = match platform {
|
||||
zed::Os::Mac => "apple-darwin",
|
||||
zed::Os::Linux => "unknown-linux-musl",
|
||||
zed::Os::Windows => "pc-windows-msvc",
|
||||
},
|
||||
);
|
||||
|
||||
let asset = release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
|
||||
|
||||
let version_dir = format!("simple-completion-language-server-{}", release.version);
|
||||
let binary_path = format!("{version_dir}/simple-completion-language-server");
|
||||
|
||||
if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
|
||||
zed::set_language_server_installation_status(
|
||||
&language_server_id,
|
||||
&zed::LanguageServerInstallationStatus::Downloading,
|
||||
);
|
||||
|
||||
zed::download_file(
|
||||
&asset.download_url,
|
||||
&version_dir,
|
||||
zed::DownloadedFileType::GzipTar,
|
||||
)
|
||||
.map_err(|e| format!("failed to download file: {e}"))?;
|
||||
|
||||
let entries =
|
||||
fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
|
||||
for entry in entries {
|
||||
let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
|
||||
if entry.file_name().to_str() != Some(&version_dir) {
|
||||
fs::remove_dir_all(&entry.path()).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.cached_binary_path = Some(binary_path.clone());
|
||||
Ok(binary_path)
|
||||
}
|
||||
}
|
||||
|
||||
impl zed::Extension for SnippetExtension {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
cached_binary_path: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn language_server_command(
|
||||
&mut self,
|
||||
language_server_id: &LanguageServerId,
|
||||
worktree: &zed::Worktree,
|
||||
) -> Result<zed::Command> {
|
||||
Ok(zed::Command {
|
||||
command: self.language_server_binary_path(language_server_id, worktree)?,
|
||||
args: vec![],
|
||||
env: vec![("SCLS_CONFIG_SUBDIRECTORY".to_owned(), "zed".to_owned())],
|
||||
})
|
||||
}
|
||||
|
||||
fn language_server_initialization_options(
|
||||
&mut self,
|
||||
_language_server_id: &LanguageServerId,
|
||||
_worktree: &zed_extension_api::Worktree,
|
||||
) -> Result<Option<zed_extension_api::serde_json::Value>> {
|
||||
Ok(Some(json!({
|
||||
"max_completion_items": 20,
|
||||
"snippets_first": true,
|
||||
"feature_words": false,
|
||||
"feature_snippets": true,
|
||||
"feature_paths": true
|
||||
})))
|
||||
}
|
||||
|
||||
fn language_server_workspace_configuration(
|
||||
&mut self,
|
||||
_language_server_id: &LanguageServerId,
|
||||
_worktree: &zed_extension_api::Worktree,
|
||||
) -> Result<Option<zed_extension_api::serde_json::Value>> {
|
||||
Ok(Some(json!({
|
||||
"max_completion_items": 20,
|
||||
"snippets_first": true,
|
||||
"feature_words": false,
|
||||
"feature_snippets": true,
|
||||
"feature_paths": true
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
zed::register_extension!(SnippetExtension);
|
Loading…
Reference in a new issue