context_store.rs

 1use gpui::SharedString;
 2use project::ProjectEntryId;
 3
 4use crate::{
 5    context::{Context, ContextId, ContextKind},
 6    thread::ThreadId,
 7};
 8
 9pub struct ContextStore {
10    context: Vec<Context>,
11    next_context_id: ContextId,
12}
13
14impl ContextStore {
15    pub fn new() -> Self {
16        Self {
17            context: Vec::new(),
18            next_context_id: ContextId(0),
19        }
20    }
21
22    pub fn context(&self) -> &Vec<Context> {
23        &self.context
24    }
25
26    pub fn drain(&mut self) -> Vec<Context> {
27        self.context.drain(..).collect()
28    }
29
30    pub fn clear(&mut self) {
31        self.context.clear();
32    }
33
34    pub fn insert_context(
35        &mut self,
36        kind: ContextKind,
37        name: impl Into<SharedString>,
38        text: impl Into<SharedString>,
39    ) {
40        self.context.push(Context {
41            id: self.next_context_id.post_inc(),
42            name: name.into(),
43            kind,
44            text: text.into(),
45        });
46    }
47
48    pub fn remove_context(&mut self, id: &ContextId) {
49        self.context.retain(|context| context.id != *id);
50    }
51
52    pub fn contains_project_entry(&self, entry_id: ProjectEntryId) -> bool {
53        self.context.iter().any(|probe| match probe.kind {
54            ContextKind::File(probe_entry_id) => probe_entry_id == entry_id,
55            ContextKind::Directory | ContextKind::FetchedUrl | ContextKind::Thread(_) => false,
56        })
57    }
58
59    pub fn contains_thread(&self, thread_id: &ThreadId) -> bool {
60        self.context.iter().any(|probe| match probe.kind {
61            ContextKind::Thread(ref probe_thread_id) => probe_thread_id == thread_id,
62            ContextKind::File(_) | ContextKind::Directory | ContextKind::FetchedUrl => false,
63        })
64    }
65}