1mod extension_snippet;
2mod 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::{AppContext, AsyncAppContext, Context, Model, ModelContext, Task, WeakModel};
17pub use registry::*;
18use util::ResultExt;
19
20pub fn init(cx: &mut AppContext) {
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: WeakModel<SnippetProvider>,
66 entries: Vec<PathBuf>,
67 mut cx: AsyncAppContext,
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: WeakModel<SnippetProvider>,
116 path: Arc<Path>,
117 mut cx: AsyncAppContext,
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(Model<SnippetProvider>);
140
141impl GlobalSnippetWatcher {
142 fn new(fs: Arc<dyn Fs>, cx: &mut AppContext) -> Self {
143 let global_snippets_dir = paths::config_dir().join("snippets");
144 let provider = cx.new_model(|_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(
160 fs: Arc<dyn Fs>,
161 dirs_to_watch: BTreeSet<PathBuf>,
162 cx: &mut AppContext,
163 ) -> Model<Self> {
164 cx.new_model(move |cx| {
165 if !cx.has_global::<GlobalSnippetWatcher>() {
166 let global_watcher = GlobalSnippetWatcher::new(fs.clone(), cx);
167 cx.set_global(global_watcher);
168 }
169 let mut this = Self {
170 fs,
171 watch_tasks: Vec::new(),
172 snippets: Default::default(),
173 };
174
175 for dir in dirs_to_watch {
176 this.watch_directory(&dir, cx);
177 }
178
179 this
180 })
181 }
182
183 /// Add directory to be watched for content changes
184 fn watch_directory(&mut self, path: &Path, cx: &ModelContext<Self>) {
185 let path: Arc<Path> = Arc::from(path);
186
187 self.watch_tasks.push(cx.spawn(|this, mut cx| async move {
188 let fs = this.update(&mut cx, |this, _| this.fs.clone())?;
189 let watched_path = path.clone();
190 let watcher = fs.watch(&watched_path, Duration::from_secs(1));
191 initial_scan(this.clone(), path, cx.clone()).await?;
192
193 let (mut entries, _) = watcher.await;
194 while let Some(entries) = entries.next().await {
195 process_updates(
196 this.clone(),
197 entries.into_iter().map(|event| event.path).collect(),
198 cx.clone(),
199 )
200 .await?;
201 }
202 Ok(())
203 }));
204 }
205
206 fn lookup_snippets<'a, const LOOKUP_GLOBALS: bool>(
207 &'a self,
208 language: &'a SnippetKind,
209 cx: &AppContext,
210 ) -> Vec<Arc<Snippet>> {
211 let mut user_snippets: Vec<_> = self
212 .snippets
213 .get(language)
214 .cloned()
215 .unwrap_or_default()
216 .into_iter()
217 .flat_map(|(_, snippets)| snippets.into_iter())
218 .collect();
219 if LOOKUP_GLOBALS {
220 if let Some(global_watcher) = cx.try_global::<GlobalSnippetWatcher>() {
221 user_snippets.extend(
222 global_watcher
223 .0
224 .read(cx)
225 .lookup_snippets::<false>(language, cx),
226 );
227 }
228 }
229
230 let Some(registry) = SnippetRegistry::try_global(cx) else {
231 return user_snippets;
232 };
233
234 let registry_snippets = registry.get_snippets(language);
235 user_snippets.extend(registry_snippets);
236
237 user_snippets
238 }
239
240 pub fn snippets_for(&self, language: SnippetKind, cx: &AppContext) -> Vec<Arc<Snippet>> {
241 let mut requested_snippets = self.lookup_snippets::<true>(&language, cx);
242
243 if language.is_some() {
244 // Look up global snippets as well.
245 requested_snippets.extend(self.lookup_snippets::<true>(&None, cx));
246 }
247 requested_snippets
248 }
249}