From 2a69420c425a444244bdb9086185a8f8cd6e6054 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Mon, 22 Jul 2024 16:39:32 -0400 Subject: [PATCH] repl: Do some cleanup (#14982) This PR cleans up the REPL implementation a bit. Release Notes: - N/A --- crates/repl/src/outputs.rs | 8 +++--- crates/repl/src/repl_editor.rs | 52 ++++++++++++++++++---------------- crates/repl/src/session.rs | 34 +++++++--------------- 3 files changed, 42 insertions(+), 52 deletions(-) diff --git a/crates/repl/src/outputs.rs b/crates/repl/src/outputs.rs index 5873ae57a8d3e098218e05140ce6b8b83777c6a2..b76359af8a7c86c6701a979df57143fc6188f115 100644 --- a/crates/repl/src/outputs.rs +++ b/crates/repl/src/outputs.rs @@ -11,13 +11,13 @@ use settings::Settings; use theme::ThemeSettings; use ui::{div, prelude::*, v_flex, IntoElement, Styled, ViewContext}; -// Given these outputs are destined for the editor with the block decorations API, all of them must report -// how many lines they will take up in the editor. +/// Given these outputs are destined for the editor with the block decorations API, all of them must report +/// how many lines they will take up in the editor. pub trait LineHeight: Sized { fn num_lines(&self, cx: &mut WindowContext) -> u8; } -// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance +/// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance fn rank_mime_type(mimetype: &MimeType) -> usize { match mimetype { MimeType::DataTable(_) => 6, @@ -264,7 +264,7 @@ impl LineHeight for TableView { } } -// Userspace error from the kernel +/// Userspace error from the kernel pub struct ErrorView { pub ename: String, pub evalue: String, diff --git a/crates/repl/src/repl_editor.rs b/crates/repl/src/repl_editor.rs index 3fa2284fac92c05cf10158616f804a584c443394..0dde4a3203c5716cfa76739ba0a409af85b7fd00 100644 --- a/crates/repl/src/repl_editor.rs +++ b/crates/repl/src/repl_editor.rs @@ -19,9 +19,8 @@ pub fn run(editor: WeakView, cx: &mut WindowContext) -> Result<()> { return Ok(()); } - let (selected_text, language, anchor_range) = match snippet(editor.clone(), cx) { - Some(snippet) => snippet, - None => return Ok(()), + let Some((selected_text, language, anchor_range)) = snippet(editor.clone(), cx) else { + return Ok(()); }; let entity_id = editor.entity_id(); @@ -83,10 +82,8 @@ pub fn session(editor: WeakView, cx: &mut AppContext) -> SessionSupport return SessionSupport::ActiveSession(session); }; - let language = get_language(editor, cx); - let language = match language { - Some(language) => language, - None => return SessionSupport::Unsupported, + let Some(language) = get_language(editor, cx) else { + return SessionSupport::Unsupported; }; let kernelspec = store.update(cx, |store, cx| store.kernelspec(&language, cx)); @@ -102,34 +99,39 @@ pub fn session(editor: WeakView, cx: &mut AppContext) -> SessionSupport pub fn clear_outputs(editor: WeakView, cx: &mut WindowContext) { let store = ReplStore::global(cx); let entity_id = editor.entity_id(); - if let Some(session) = store.read(cx).get_session(entity_id).cloned() { - session.update(cx, |session, cx| { - session.clear_outputs(cx); - cx.notify(); - }); - } + let Some(session) = store.read(cx).get_session(entity_id).cloned() else { + return; + }; + session.update(cx, |session, cx| { + session.clear_outputs(cx); + cx.notify(); + }); } pub fn interrupt(editor: WeakView, cx: &mut WindowContext) { let store = ReplStore::global(cx); let entity_id = editor.entity_id(); - if let Some(session) = store.read(cx).get_session(entity_id).cloned() { - session.update(cx, |session, cx| { - session.interrupt(cx); - cx.notify(); - }); - } + let Some(session) = store.read(cx).get_session(entity_id).cloned() else { + return; + }; + + session.update(cx, |session, cx| { + session.interrupt(cx); + cx.notify(); + }); } pub fn shutdown(editor: WeakView, cx: &mut WindowContext) { let store = ReplStore::global(cx); let entity_id = editor.entity_id(); - if let Some(session) = store.read(cx).get_session(entity_id).cloned() { - session.update(cx, |session, cx| { - session.shutdown(cx); - cx.notify(); - }); - } + let Some(session) = store.read(cx).get_session(entity_id).cloned() else { + return; + }; + + session.update(cx, |session, cx| { + session.shutdown(cx); + cx.notify(); + }); } fn snippet( diff --git a/crates/repl/src/session.rs b/crates/repl/src/session.rs index b5283fe96c5c7e5773e0554705f73335f2e84008..34501ecae129591cc0ba59de5927f4cdad4abdf8 100644 --- a/crates/repl/src/session.rs +++ b/crates/repl/src/session.rs @@ -22,15 +22,15 @@ use runtimelib::{ ExecuteRequest, InterruptRequest, JupyterMessage, JupyterMessageContent, ShutdownRequest, }; use settings::Settings as _; -use std::{env::temp_dir, ops::Range, path::PathBuf, sync::Arc, time::Duration}; +use std::{env::temp_dir, ops::Range, sync::Arc, time::Duration}; use theme::{ActiveTheme, ThemeSettings}; use ui::{h_flex, prelude::*, v_flex, ButtonLike, ButtonStyle, IconButtonShape, Label, Tooltip}; pub struct Session { - pub editor: WeakView, + editor: WeakView, pub kernel: Kernel, blocks: HashMap, - pub messaging_task: Task<()>, + messaging_task: Task<()>, pub kernel_specification: KernelSpecification, _buffer_subscription: Subscription, } @@ -200,17 +200,6 @@ impl EditorBlock { } impl Session { - pub fn working_directory(editor: WeakView, cx: &WindowContext) -> PathBuf { - if let Some(working_directory) = editor - .upgrade() - .and_then(|editor| editor.read(cx).working_directory(cx)) - { - working_directory - } else { - temp_dir() - } - } - pub fn new( editor: WeakView, fs: Arc, @@ -218,11 +207,14 @@ impl Session { cx: &mut ViewContext, ) -> Self { let entity_id = editor.entity_id(); - + let working_directory = editor + .upgrade() + .and_then(|editor| editor.read(cx).working_directory(cx)) + .unwrap_or_else(temp_dir); let kernel = RunningKernel::new( kernel_specification.clone(), entity_id, - Self::working_directory(editor.clone(), cx), + working_directory, fs.clone(), cx, ); @@ -384,9 +376,7 @@ impl Session { } pub fn execute(&mut self, code: &str, anchor_range: Range, cx: &mut ViewContext) { - let editor = if let Some(editor) = self.editor.upgrade() { - editor - } else { + let Some(editor) = self.editor.upgrade() else { return; }; @@ -450,11 +440,9 @@ impl Session { } }); - let editor_block = if let Ok(editor_block) = + let Ok(editor_block) = EditorBlock::new(self.editor.clone(), anchor_range, status, on_close, cx) - { - editor_block - } else { + else { return; };