Skip to content

Commit

Permalink
repl: Do some cleanup (zed-industries#14982)
Browse files Browse the repository at this point in the history
This PR cleans up the REPL implementation a bit.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Jul 22, 2024
1 parent d8a42bb commit 2a69420
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
8 changes: 4 additions & 4 deletions crates/repl/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
52 changes: 27 additions & 25 deletions crates/repl/src/repl_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));

Expand All @@ -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(
Expand Down
34 changes: 11 additions & 23 deletions crates/repl/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -200,29 +200,21 @@ 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>,
kernel_specification: KernelSpecification,
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,
);
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
};

Expand Down

0 comments on commit 2a69420

Please sign in to comment.