Skip to content

Commit

Permalink
assistant2: Make ContextStore::insert_* methods private (#22989)
Browse files Browse the repository at this point in the history
This PR makes the `insert_*` methods on the `ContextStore` private, to
reduce confusion with the public `add_*` methods.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Jan 10, 2025
1 parent 1fcc9b3 commit 40ecc38
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
4 changes: 1 addition & 3 deletions crates/assistant2/src/context_picker/fetch_context_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ impl PickerDelegate for FetchContextPickerDelegate {
this.delegate
.context_store
.update(cx, |context_store, _cx| {
if context_store.includes_url(&url).is_none() {
context_store.insert_fetched_url(url, text);
}
context_store.add_fetched_url(url, text);
})?;

match confirm_behavior {
Expand Down
39 changes: 35 additions & 4 deletions crates/assistant2/src/context_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::context::{
Context, ContextBuffer, ContextId, ContextSnapshot, DirectoryContext, FetchedUrlContext,
FileContext, ThreadContext,
};
use crate::context_strip::SuggestedContext;
use crate::thread::{Thread, ThreadId};

pub struct ContextStore {
Expand Down Expand Up @@ -148,7 +149,7 @@ impl ContextStore {
})
}

pub fn insert_file(&mut self, context_buffer: ContextBuffer) {
fn insert_file(&mut self, context_buffer: ContextBuffer) {
let id = self.next_context_id.post_inc();
self.files.insert(context_buffer.id, id);
self.context
Expand Down Expand Up @@ -239,7 +240,7 @@ impl ContextStore {
})
}

pub fn insert_directory(&mut self, path: &Path, context_buffers: Vec<ContextBuffer>) {
fn insert_directory(&mut self, path: &Path, context_buffers: Vec<ContextBuffer>) {
let id = self.next_context_id.post_inc();
self.directories.insert(path.to_path_buf(), id);

Expand All @@ -258,7 +259,7 @@ impl ContextStore {
}
}

pub fn insert_thread(&mut self, thread: Model<Thread>, cx: &AppContext) {
fn insert_thread(&mut self, thread: Model<Thread>, cx: &AppContext) {
let id = self.next_context_id.post_inc();
let text = thread.read(cx).text().into();

Expand All @@ -267,7 +268,13 @@ impl ContextStore {
.push(Context::Thread(ThreadContext { id, thread, text }));
}

pub fn insert_fetched_url(&mut self, url: String, text: impl Into<SharedString>) {
pub fn add_fetched_url(&mut self, url: String, text: impl Into<SharedString>) {
if self.includes_url(&url).is_none() {
self.insert_fetched_url(url, text);
}
}

fn insert_fetched_url(&mut self, url: String, text: impl Into<SharedString>) {
let id = self.next_context_id.post_inc();

self.fetched_urls.insert(url.clone(), id);
Expand All @@ -278,6 +285,30 @@ impl ContextStore {
}));
}

pub fn accept_suggested_context(
&mut self,
suggested: &SuggestedContext,
cx: &mut ModelContext<ContextStore>,
) -> Task<Result<()>> {
match suggested {
SuggestedContext::File {
buffer,
icon_path: _,
name: _,
} => {
if let Some(buffer) = buffer.upgrade() {
return self.add_file_from_buffer(buffer, cx);
};
}
SuggestedContext::Thread { thread, name: _ } => {
if let Some(thread) = thread.upgrade() {
self.insert_thread(thread, cx);
};
}
}
Task::ready(Ok(()))
}

pub fn remove_context(&mut self, id: ContextId) {
let Some(ix) = self.context.iter().position(|context| context.id() == id) else {
return;
Expand Down
30 changes: 2 additions & 28 deletions crates/assistant2/src/context_strip.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::rc::Rc;

use anyhow::Result;
use collections::HashSet;
use editor::Editor;
use file_icons::FileIcons;
use gpui::{
DismissEvent, EventEmitter, FocusHandle, Model, ModelContext, Subscription, Task, View,
WeakModel, WeakView,
DismissEvent, EventEmitter, FocusHandle, Model, Subscription, View, WeakModel, WeakView,
};
use itertools::Itertools;
use language::Buffer;
Expand Down Expand Up @@ -244,7 +242,7 @@ impl Render for ContextStrip {
let context_store = self.context_store.clone();
Rc::new(cx.listener(move |this, _event, cx| {
let task = context_store.update(cx, |context_store, cx| {
suggested.accept(context_store, cx)
context_store.accept_suggested_context(&suggested, cx)
});

let workspace = this.workspace.clone();
Expand Down Expand Up @@ -339,30 +337,6 @@ impl SuggestedContext {
}
}

pub fn accept(
&self,
context_store: &mut ContextStore,
cx: &mut ModelContext<ContextStore>,
) -> Task<Result<()>> {
match self {
Self::File {
buffer,
icon_path: _,
name: _,
} => {
if let Some(buffer) = buffer.upgrade() {
return context_store.add_file_from_buffer(buffer, cx);
};
}
Self::Thread { thread, name: _ } => {
if let Some(thread) = thread.upgrade() {
context_store.insert_thread(thread, cx);
};
}
}
Task::ready(Ok(()))
}

pub fn kind(&self) -> ContextKind {
match self {
Self::File { .. } => ContextKind::File,
Expand Down

0 comments on commit 40ecc38

Please sign in to comment.