Skip to content

Commit

Permalink
track based on current kernelspec
Browse files Browse the repository at this point in the history
  • Loading branch information
rgbkrk committed Nov 6, 2024
1 parent 7eb3d27 commit d972d6a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 53 deletions.
11 changes: 5 additions & 6 deletions crates/quick_action_bar/src/repl_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ impl QuickActionBar {
let session = match session {
SessionSupport::ActiveSession(session) => session,
SessionSupport::Inactive(spec) => {
let spec = *spec;
return self.render_repl_launch_menu(spec, cx);
}
SessionSupport::RequiresSetup(language) => {
Expand Down Expand Up @@ -287,15 +286,14 @@ impl QuickActionBar {

let session = repl::session(editor.downgrade(), cx);

let current_kernel_name = match session {
SessionSupport::ActiveSession(view) => Some(view.read(cx).kernel_specification.name()),
SessionSupport::Inactive(kernel_specification) => Some(kernel_specification.name()),
let current_kernelspec = match session {
SessionSupport::ActiveSession(view) => Some(view.read(cx).kernel_specification.clone()),
SessionSupport::Inactive(kernel_specification) => Some(kernel_specification),
SessionSupport::RequiresSetup(_language_name) => None,
SessionSupport::Unsupported => None,
};

// let current_kernel_name = self.current_kernelspec.as_ref().map(|spec| spec.name());
let current_kernelspec: Option<KernelSpecification> = None;
let current_kernel_name = current_kernelspec.as_ref().map(|spec| spec.name());

let menu_handle: PopoverMenuHandle<Picker<KernelPickerDelegate>> =
PopoverMenuHandle::default();
Expand All @@ -305,6 +303,7 @@ impl QuickActionBar {
repl::assign_kernelspec(kernelspec, editor.downgrade(), cx).ok();
})
},
current_kernelspec.clone(),
ButtonLike::new("kernel-selector")
.style(ButtonStyle::Subtle)
.child(
Expand Down
88 changes: 43 additions & 45 deletions crates/repl/src/components/kernel_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ pub struct KernelSelector<T: PopoverTrigger> {
on_select: OnSelect,
trigger: T,
info_text: Option<SharedString>,
current_kernelspec: Option<KernelSpecification>,
}

pub struct KernelPickerDelegate {
all_kernels: Vec<KernelSpecification>,
filtered_kernels: Vec<KernelSpecification>,
selected_index: usize,
selected_kernelspec: Option<KernelSpecification>,
on_select: OnSelect,
}

impl<T: PopoverTrigger> KernelSelector<T> {
pub fn new(on_select: OnSelect, trigger: T) -> Self {
pub fn new(
on_select: OnSelect,
current_kernelspec: Option<KernelSpecification>,
trigger: T,
) -> Self {
KernelSelector {
on_select,
handle: None,
trigger,
info_text: None,
current_kernelspec,
}
}

Expand All @@ -60,49 +66,47 @@ impl PickerDelegate for KernelPickerDelegate {
}

fn selected_index(&self) -> usize {
self.selected_index
if let Some(kernelspec) = self.selected_kernelspec.as_ref() {
self.filtered_kernels
.iter()
.position(|k| k == kernelspec)
.unwrap_or(2)
} else {
1
}
}

fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>) {
self.selected_index = ix.min(self.filtered_kernels.len().saturating_sub(1));
self.selected_kernelspec = self.filtered_kernels.get(ix).cloned();
cx.notify();
}

fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
"Select a kernel...".into()
}

fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
fn update_matches(&mut self, query: String, _cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
let all_kernels = self.all_kernels.clone();
cx.spawn(|this, mut cx| async move {
let filtered_kernels = cx
.background_executor()
.spawn(async move {
if query.is_empty() {
all_kernels
} else {
all_kernels
.into_iter()
.filter(|kernel| {
// TODO: there is probably an off the shelf way to do this fuzzy
kernel.name().to_lowercase().contains(&query.to_lowercase())
})
.collect()
}
})
.await;

this.update(&mut cx, |this, cx| {
this.delegate.filtered_kernels = filtered_kernels;
this.delegate.set_selected_index(0, cx);
cx.notify();
})
.ok();
})

if query.is_empty() {
self.filtered_kernels = all_kernels;
return Task::Ready(Some(()));
}

self.filtered_kernels = if query.is_empty() {
all_kernels
} else {
all_kernels
.into_iter()
.filter(|kernel| kernel.name().to_lowercase().contains(&query.to_lowercase()))
.collect()
};

return Task::Ready(Some(()));
}

fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
if let Some(kernelspec) = self.filtered_kernels.get(self.selected_index) {
if let Some(kernelspec) = &self.selected_kernelspec {
(self.on_select)(kernelspec.clone(), cx.window_context());
cx.emit(DismissEvent);
}
Expand All @@ -116,29 +120,22 @@ impl PickerDelegate for KernelPickerDelegate {
selected: bool,
_cx: &mut ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> {
let kernel = self.filtered_kernels.get(ix)?;
let kernel_name = kernel.name();
let is_selected = self.selected_index == ix;
let kernelspec = self.filtered_kernels.get(ix)?;

let is_selected = self.selected_kernelspec.as_ref() == Some(kernelspec);

Some(
ListItem::new(ix)
.inset(true)
.spacing(ListItemSpacing::Sparse)
.selected(selected)
// .start_slot(
// div().pr_0p5().child(
// Icon::new(kernel_info.icon)
// .color(Color::Muted)
// .size(IconSize::Medium),
// ),
// )
.child(
h_flex().w_full().justify_between().min_w(px(200.)).child(
h_flex()
.gap_1p5()
.child(Label::new(kernel_name)) // TODO: Replace with actual kernel name
.child(Label::new(kernelspec.name()))
.child(
Label::new(kernel.type_name()) // TODO: Replace with actual kernel type
Label::new(kernelspec.type_name())
.size(LabelSize::XSmall)
.color(Color::Muted),
),
Expand Down Expand Up @@ -177,16 +174,17 @@ impl PickerDelegate for KernelPickerDelegate {

impl<T: PopoverTrigger> RenderOnce for KernelSelector<T> {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
// TODO: Implement kernel selection logic
let store = ReplStore::global(cx).read(cx);
let all_kernels: Vec<KernelSpecification> =
store.kernel_specifications().cloned().collect();

let selected_kernelspec = self.current_kernelspec;

let delegate = KernelPickerDelegate {
on_select: self.on_select,
all_kernels: all_kernels.clone(),
filtered_kernels: all_kernels,
selected_index: 0,
selected_kernelspec,
};

let picker_view = cx.new_view(|cx| {
Expand Down
4 changes: 2 additions & 2 deletions crates/repl/src/repl_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub fn run(editor: WeakView<Editor>, move_down: bool, cx: &mut WindowContext) ->

pub enum SessionSupport {
ActiveSession(View<Session>),
Inactive(Box<KernelSpecification>),
Inactive(KernelSpecification),
RequiresSetup(LanguageName),
Unsupported,
}
Expand All @@ -169,7 +169,7 @@ pub fn session(editor: WeakView<Editor>, cx: &mut WindowContext) -> SessionSuppo
});

match kernelspec {
Some(kernelspec) => SessionSupport::Inactive(Box::new(kernelspec)),
Some(kernelspec) => SessionSupport::Inactive(kernelspec),
None => {
if language_supported(&language) {
SessionSupport::RequiresSetup(language.name())
Expand Down

0 comments on commit d972d6a

Please sign in to comment.