context_store.rs

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