1mod extension_snippet;
2pub mod format;
3mod registry;
4
5use std::{
6 path::{Path, PathBuf},
7 sync::Arc,
8 time::Duration,
9};
10
11use anyhow::Result;
12use collections::{BTreeMap, BTreeSet, HashMap};
13use format::VSSnippetsFile;
14use fs::Fs;
15use futures::stream::StreamExt;
16use gpui::{App, AppContext as _, AsyncApp, Context, Entity, Task, WeakEntity};
17pub use registry::*;
18use util::ResultExt;
19
20pub fn init(cx: &mut App) {
21 SnippetRegistry::init_global(cx);
22 extension_snippet::init(cx);
23}
24
25// Is `None` if the snippet file is global.
26type SnippetKind = Option<String>;
27fn file_stem_to_key(stem: &str) -> SnippetKind {
28 if stem == "snippets" {
29 None
30 } else {
31 Some(stem.to_owned())
32 }
33}
34
35fn file_to_snippets(file_contents: VSSnippetsFile) -> Vec<Arc<Snippet>> {
36 let mut snippets = vec![];
37 for (prefix, snippet) in file_contents.snippets {
38 let prefixes = snippet
39 .prefix
40 .map_or_else(move || vec![prefix], |prefixes| prefixes.into());
41 let description = snippet
42 .description
43 .map(|description| description.to_string());
44 let body = snippet.body.to_string();
45 if snippet::Snippet::parse(&body).log_err().is_none() {
46 continue;
47 };
48 snippets.push(Arc::new(Snippet {
49 body,
50 prefix: prefixes,
51 description,
52 }));
53 }
54 snippets
55}
56// Snippet with all of the metadata
57#[derive(Debug)]
58pub struct Snippet {
59 pub prefix: Vec<String>,
60 pub body: String,
61 pub description: Option<String>,
62}
63
64async fn process_updates(
65 this: WeakEntity<SnippetProvider>,
66 entries: Vec<PathBuf>,
67 mut cx: AsyncApp,
68) -> Result<()> {
69 let fs = this.update(&mut cx, |this, _| this.fs.clone())?;
70 for entry_path in entries {
71 if !entry_path
72 .extension()
73 .map_or(false, |extension| extension == "json")
74 {
75 continue;
76 }
77 let entry_metadata = fs.metadata(&entry_path).await;
78 // Entry could have been removed, in which case we should no longer show completions for it.
79 let entry_exists = entry_metadata.is_ok();
80 if entry_metadata.map_or(false, |entry| entry.map_or(false, |e| e.is_dir)) {
81 // Don't process dirs.
82 continue;
83 }
84 let Some(stem) = entry_path.file_stem().and_then(|s| s.to_str()) else {
85 continue;
86 };
87 let key = file_stem_to_key(stem);
88
89 let contents = if entry_exists {
90 fs.load(&entry_path).await.ok()
91 } else {
92 None
93 };
94
95 this.update(&mut cx, move |this, _| {
96 let snippets_of_kind = this.snippets.entry(key).or_default();
97 if entry_exists {
98 let Some(file_contents) = contents else {
99 return;
100 };
101 let Ok(as_json) = serde_json::from_str::<VSSnippetsFile>(&file_contents) else {
102 return;
103 };
104 let snippets = file_to_snippets(as_json);
105 *snippets_of_kind.entry(entry_path).or_default() = snippets;
106 } else {
107 snippets_of_kind.remove(&entry_path);
108 }
109 })?;
110 }
111 Ok(())
112}
113
114async fn initial_scan(
115 this: WeakEntity<SnippetProvider>,
116 path: Arc<Path>,
117 mut cx: AsyncApp,
118) -> Result<()> {
119 let fs = this.update(&mut cx, |this, _| this.fs.clone())?;
120 let entries = fs.read_dir(&path).await;
121 if let Ok(entries) = entries {
122 let entries = entries
123 .collect::<Vec<_>>()
124 .await
125 .into_iter()
126 .collect::<Result<Vec<_>>>()?;
127 process_updates(this, entries, cx).await?;
128 }
129 Ok(())
130}
131
132pub struct SnippetProvider {
133 fs: Arc<dyn Fs>,
134 snippets: HashMap<SnippetKind, BTreeMap<PathBuf, Vec<Arc<Snippet>>>>,
135 watch_tasks: Vec<Task<Result<()>>>,
136}
137
138// Watches global snippet directory, is created just once and reused across multiple projects
139struct GlobalSnippetWatcher(Entity<SnippetProvider>);
140
141impl GlobalSnippetWatcher {
142 fn new(fs: Arc<dyn Fs>, cx: &mut App) -> Self {
143 let global_snippets_dir = paths::config_dir().join("snippets");
144 let provider = cx.new(|_cx| SnippetProvider {
145 fs,
146 snippets: Default::default(),
147 watch_tasks: vec![],
148 });
149 provider.update(cx, |this, cx| {
150 this.watch_directory(&global_snippets_dir, cx)
151 });
152 Self(provider)
153 }
154}
155
156impl gpui::Global for GlobalSnippetWatcher {}
157
158impl SnippetProvider {
159 pub fn new(fs: Arc<dyn Fs>, dirs_to_watch: BTreeSet<PathBuf>, cx: &mut App) -> Entity<Self> {
160 cx.new(move |cx| {
161 if !cx.has_global::<GlobalSnippetWatcher>() {
162 let global_watcher = GlobalSnippetWatcher::new(fs.clone(), cx);
163 cx.set_global(global_watcher);
164 }
165 let mut this = Self {
166 fs,
167 watch_tasks: Vec::new(),
168 snippets: Default::default(),
169 };
170
171 for dir in dirs_to_watch {
172 this.watch_directory(&dir, cx);
173 }
174
175 this
176 })
177 }
178
179 /// Add directory to be watched for content changes
180 fn watch_directory(&mut self, path: &Path, cx: &Context<Self>) {
181 let path: Arc<Path> = Arc::from(path);
182
183 self.watch_tasks.push(cx.spawn(|this, mut cx| async move {
184 let fs = this.update(&mut cx, |this, _| this.fs.clone())?;
185 let watched_path = path.clone();
186 let watcher = fs.watch(&watched_path, Duration::from_secs(1));
187 initial_scan(this.clone(), path, cx.clone()).await?;
188
189 let (mut entries, _) = watcher.await;
190 while let Some(entries) = entries.next().await {
191 process_updates(
192 this.clone(),
193 entries.into_iter().map(|event| event.path).collect(),
194 cx.clone(),
195 )
196 .await?;
197 }
198 Ok(())
199 }));
200 }
201
202 fn lookup_snippets<'a, const LOOKUP_GLOBALS: bool>(
203 &'a self,
204 language: &'a SnippetKind,
205 cx: &App,
206 ) -> Vec<Arc<Snippet>> {
207 let mut user_snippets: Vec<_> = self
208 .snippets
209 .get(language)
210 .cloned()
211 .unwrap_or_default()
212 .into_iter()
213 .flat_map(|(_, snippets)| snippets.into_iter())
214 .collect();
215 if LOOKUP_GLOBALS {
216 if let Some(global_watcher) = cx.try_global::<GlobalSnippetWatcher>() {
217 user_snippets.extend(
218 global_watcher
219 .0
220 .read(cx)
221 .lookup_snippets::<false>(language, cx),
222 );
223 }
224 }
225
226 let Some(registry) = SnippetRegistry::try_global(cx) else {
227 return user_snippets;
228 };
229
230 let registry_snippets = registry.get_snippets(language);
231 user_snippets.extend(registry_snippets);
232
233 user_snippets
234 }
235
236 pub fn snippets_for(&self, language: SnippetKind, cx: &App) -> Vec<Arc<Snippet>> {
237 let mut requested_snippets = self.lookup_snippets::<true>(&language, cx);
238
239 if language.is_some() {
240 // Look up global snippets as well.
241 requested_snippets.extend(self.lookup_snippets::<true>(&None, cx));
242 }
243 requested_snippets
244 }
245}