repl: Do some cleanup (#14982)

This PR cleans up the REPL implementation a bit.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers
2024-07-22 16:39:32 -04:00
committed by GitHub
parent d8a42bbf63
commit 2a69420c42
3 changed files with 42 additions and 52 deletions
+4 -4
View File
@@ -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,
+27 -25
View File
@@ -19,9 +19,8 @@ pub fn run(editor: WeakView<Editor>, 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<Editor>, 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<Editor>, cx: &mut AppContext) -> SessionSupport
pub fn clear_outputs(editor: WeakView<Editor>, 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<Editor>, 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<Editor>, 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(
+11 -23
View File
@@ -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>,
editor: WeakView<Editor>,
pub kernel: Kernel,
blocks: HashMap<String, EditorBlock>,
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<Editor>, 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<Editor>,
fs: Arc<dyn Fs>,
@@ -218,11 +207,14 @@ impl Session {
cx: &mut ViewContext<Self>,
) -> 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<Anchor>, cx: &mut ViewContext<Self>) {
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;
};