extensions: Add support for snippets provided by extensions (#14020)
For now extensions can only register global snippets, but there'll be follow-up work to support scope attribute in snippets.json. Release Notes: - Extensions can now provide snippets by including `snippets.json` file next to the extension manifest. --------- Co-authored-by: Marshall <marshall@zed.dev> Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
parent
2f2047ab22
commit
6f99399224
12 changed files with 129 additions and 16 deletions
|
@ -14,6 +14,7 @@ collections.workspace = true
|
|||
fs.workspace = true
|
||||
futures.workspace = true
|
||||
gpui.workspace = true
|
||||
parking_lot.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
snippet.workspace = true
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
mod format;
|
||||
mod registry;
|
||||
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
|
@ -12,8 +13,13 @@ use format::VSSnippetsFile;
|
|||
use fs::Fs;
|
||||
use futures::stream::StreamExt;
|
||||
use gpui::{AppContext, AsyncAppContext, Context, Model, ModelContext, Task, WeakModel};
|
||||
pub use registry::*;
|
||||
use util::ResultExt;
|
||||
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
SnippetRegistry::init_global(cx);
|
||||
}
|
||||
|
||||
// Is `None` if the snippet file is global.
|
||||
type SnippetKind = Option<String>;
|
||||
fn file_stem_to_key(stem: &str) -> SnippetKind {
|
||||
|
@ -168,28 +174,37 @@ impl SnippetProvider {
|
|||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn lookup_snippets<'a>(
|
||||
&'a self,
|
||||
language: &'a SnippetKind,
|
||||
) -> Option<impl Iterator<Item = Arc<Snippet>> + 'a> {
|
||||
Some(
|
||||
self.snippets
|
||||
.get(&language)?
|
||||
.iter()
|
||||
.flat_map(|(_, snippets)| snippets.iter().cloned()),
|
||||
)
|
||||
cx: &AppContext,
|
||||
) -> Vec<Arc<Snippet>> {
|
||||
let mut user_snippets: Vec<_> = self
|
||||
.snippets
|
||||
.get(&language)
|
||||
.cloned()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.flat_map(|(_, snippets)| snippets.into_iter())
|
||||
.collect();
|
||||
|
||||
let Some(registry) = SnippetRegistry::try_global(cx) else {
|
||||
return user_snippets;
|
||||
};
|
||||
|
||||
let registry_snippets = registry.get_snippets(language);
|
||||
user_snippets.extend(registry_snippets);
|
||||
|
||||
user_snippets
|
||||
}
|
||||
|
||||
pub fn snippets_for(&self, language: SnippetKind) -> Vec<Arc<Snippet>> {
|
||||
let mut requested_snippets: Vec<_> = self
|
||||
.lookup_snippets(&language)
|
||||
.map(|snippets| snippets.collect())
|
||||
.unwrap_or_default();
|
||||
pub fn snippets_for(&self, language: SnippetKind, cx: &AppContext) -> Vec<Arc<Snippet>> {
|
||||
let mut requested_snippets = self.lookup_snippets(&language, cx);
|
||||
|
||||
if language.is_some() {
|
||||
// Look up global snippets as well.
|
||||
if let Some(global_snippets) = self.lookup_snippets(&None) {
|
||||
requested_snippets.extend(global_snippets);
|
||||
}
|
||||
requested_snippets.extend(self.lookup_snippets(&None, cx));
|
||||
}
|
||||
requested_snippets
|
||||
}
|
||||
|
|
53
crates/snippet_provider/src/registry.rs
Normal file
53
crates/snippet_provider/src/registry.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use std::{path::Path, sync::Arc};
|
||||
|
||||
use anyhow::Result;
|
||||
use collections::HashMap;
|
||||
use gpui::{AppContext, Global, ReadGlobal, UpdateGlobal};
|
||||
use parking_lot::RwLock;
|
||||
|
||||
use crate::{file_stem_to_key, Snippet, SnippetKind};
|
||||
|
||||
struct GlobalSnippetRegistry(Arc<SnippetRegistry>);
|
||||
|
||||
impl Global for GlobalSnippetRegistry {}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SnippetRegistry {
|
||||
snippets: RwLock<HashMap<SnippetKind, Vec<Arc<Snippet>>>>,
|
||||
}
|
||||
|
||||
impl SnippetRegistry {
|
||||
pub fn global(cx: &AppContext) -> Arc<Self> {
|
||||
GlobalSnippetRegistry::global(cx).0.clone()
|
||||
}
|
||||
|
||||
pub fn try_global(cx: &AppContext) -> Option<Arc<Self>> {
|
||||
cx.try_global::<GlobalSnippetRegistry>()
|
||||
.map(|registry| registry.0.clone())
|
||||
}
|
||||
|
||||
pub fn init_global(cx: &mut AppContext) {
|
||||
GlobalSnippetRegistry::set_global(cx, GlobalSnippetRegistry(Arc::new(Self::new())))
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
snippets: RwLock::new(HashMap::default()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_snippets(&self, file_path: &Path, contents: &str) -> Result<()> {
|
||||
let snippets_in_file: crate::format::VSSnippetsFile = serde_json::from_str(contents)?;
|
||||
let kind = file_path
|
||||
.file_stem()
|
||||
.and_then(|stem| stem.to_str().and_then(file_stem_to_key));
|
||||
let snippets = crate::file_to_snippets(snippets_in_file);
|
||||
self.snippets.write().insert(kind, snippets);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_snippets(&self, kind: &SnippetKind) -> Vec<Arc<Snippet>> {
|
||||
self.snippets.read().get(kind).cloned().unwrap_or_default()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue