Run any Jupyter kernel in Zed on any buffer (editor): <img width="1074" alt="image" src="https://github.com/zed-industries/zed/assets/836375/eac8ed69-d02b-4d46-b379-6186d8f59470"> ## TODO ### Lifecycle * [x] Launch kernels on demand * [x] Wait for kernel to be started * [x] Request Kernel info on start * [x] Show in progress indicator * [ ] Allow picking kernel (it defaults to first matching language name) * [ ] Menu for interrupting and shutting down the kernel * [ ] Drop running kernels once editor is dropped ### Media Outputs * [x] Render text and tracebacks with ANSI color handling * [x] Render markdown as text * [x] Render PNG and JPEG images using an explicit height based on line-height * ~~Render SVG~~ -- not happening for this PR due to lack of text in SVG support * [ ] Process `update_display_data` message and related `display_id` * [x] Process `page` data from payloads as outputs * [ ] Render markdown as, well, rendered markdown -- Note: unsure if we can get line heights here ### Document * [x] Select code and run * [x] Run current line * [x] Clear previous overlapping runs * [ ] Support running markdown code blocks * [ ] Action to export session as notebook or output files * [ ] Action to clear all outputs * [ ] Delete outputs when lines are deleted ## Other missing features The following is a list of missing functionality or expectations that are out of scope for this PR. ### Python Environments Detecting python environments should probably be done in a separate PR in tandem with how they're used with LSP. Users likely want to pick an environment for their project, whether a virtualenv, conda env, pyenv, poetry backed virtualenv, or the system. Related issues: * https://github.com/zed-industries/zed/issues/7646 * https://github.com/zed-industries/zed/issues/7808 * https://github.com/zed-industries/zed/issues/7296 ### LSP Integration * Submit `complete_request` messages for completions to interleave interactive variables with LSP * LSP for IPython semantics (`%%timeit`, `!ls`, `get_ipython`, etc.) ## Future release notes - Run code in any editor, whether it's a script or a markdown document Release Notes: - N/A
152 lines
4.7 KiB
Rust
152 lines
4.7 KiB
Rust
use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToOffsetUtf16, ToPoint};
|
|
use language::{OffsetUtf16, Point, TextDimension};
|
|
use std::{
|
|
cmp::Ordering,
|
|
ops::{Range, Sub},
|
|
};
|
|
use sum_tree::Bias;
|
|
use text::BufferId;
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
|
|
pub struct Anchor {
|
|
pub buffer_id: Option<BufferId>,
|
|
pub excerpt_id: ExcerptId,
|
|
pub text_anchor: text::Anchor,
|
|
}
|
|
|
|
impl Anchor {
|
|
pub fn min() -> Self {
|
|
Self {
|
|
buffer_id: None,
|
|
excerpt_id: ExcerptId::min(),
|
|
text_anchor: text::Anchor::MIN,
|
|
}
|
|
}
|
|
|
|
pub fn max() -> Self {
|
|
Self {
|
|
buffer_id: None,
|
|
excerpt_id: ExcerptId::max(),
|
|
text_anchor: text::Anchor::MAX,
|
|
}
|
|
}
|
|
|
|
pub fn cmp(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
|
|
let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id, snapshot);
|
|
if excerpt_id_cmp.is_eq() {
|
|
if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
|
|
Ordering::Equal
|
|
} else if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
|
|
self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer)
|
|
} else {
|
|
Ordering::Equal
|
|
}
|
|
} else {
|
|
excerpt_id_cmp
|
|
}
|
|
}
|
|
|
|
pub fn bias(&self) -> Bias {
|
|
self.text_anchor.bias
|
|
}
|
|
|
|
pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
|
|
if self.text_anchor.bias != Bias::Left {
|
|
if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
|
|
return Self {
|
|
buffer_id: self.buffer_id,
|
|
excerpt_id: self.excerpt_id,
|
|
text_anchor: self.text_anchor.bias_left(&excerpt.buffer),
|
|
};
|
|
}
|
|
}
|
|
*self
|
|
}
|
|
|
|
pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
|
|
if self.text_anchor.bias != Bias::Right {
|
|
if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
|
|
return Self {
|
|
buffer_id: self.buffer_id,
|
|
excerpt_id: self.excerpt_id,
|
|
text_anchor: self.text_anchor.bias_right(&excerpt.buffer),
|
|
};
|
|
}
|
|
}
|
|
*self
|
|
}
|
|
|
|
pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
|
|
where
|
|
D: TextDimension + Ord + Sub<D, Output = D>,
|
|
{
|
|
snapshot.summary_for_anchor(self)
|
|
}
|
|
|
|
pub fn is_valid(&self, snapshot: &MultiBufferSnapshot) -> bool {
|
|
if *self == Anchor::min() || *self == Anchor::max() {
|
|
true
|
|
} else if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
|
|
excerpt.contains(self)
|
|
&& (self.text_anchor == excerpt.range.context.start
|
|
|| self.text_anchor == excerpt.range.context.end
|
|
|| self.text_anchor.is_valid(&excerpt.buffer))
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ToOffset for Anchor {
|
|
fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> usize {
|
|
self.summary(snapshot)
|
|
}
|
|
}
|
|
|
|
impl ToOffsetUtf16 for Anchor {
|
|
fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> OffsetUtf16 {
|
|
self.summary(snapshot)
|
|
}
|
|
}
|
|
|
|
impl ToPoint for Anchor {
|
|
fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
|
|
self.summary(snapshot)
|
|
}
|
|
}
|
|
|
|
pub trait AnchorRangeExt {
|
|
fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
|
|
fn overlaps(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
|
|
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
|
|
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
|
|
}
|
|
|
|
impl AnchorRangeExt for Range<Anchor> {
|
|
fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering {
|
|
match self.start.cmp(&other.start, buffer) {
|
|
Ordering::Equal => other.end.cmp(&self.end, buffer),
|
|
ord => ord,
|
|
}
|
|
}
|
|
|
|
fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
|
|
let start_cmp = self.start.cmp(&other.start, buffer);
|
|
let end_cmp = self.end.cmp(&other.end, buffer);
|
|
|
|
(start_cmp == Ordering::Less || start_cmp == Ordering::Equal)
|
|
&& (end_cmp == Ordering::Greater || end_cmp == Ordering::Equal)
|
|
}
|
|
|
|
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {
|
|
self.start.to_offset(content)..self.end.to_offset(content)
|
|
}
|
|
|
|
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
|
|
self.start.to_point(content)..self.end.to_point(content)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Ord, PartialOrd)]
|
|
pub struct Offset(pub usize);
|