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
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