Clean up inline assist editor rendering (#15536)

Release Notes:

- N/A

---------

Co-authored-by: Nathan <nathan@zed.dev>
Co-authored-by: Max <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2024-07-31 17:43:08 +02:00 committed by GitHub
parent 73d8370177
commit 5b1ea7eda0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 267 additions and 1777 deletions

View file

@ -19,7 +19,7 @@ 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.
pub trait LineHeight: Sized {
fn num_lines(&self, cx: &mut WindowContext) -> u8;
fn num_lines(&self, cx: &mut WindowContext) -> usize;
}
/// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance
@ -88,15 +88,9 @@ impl ImageView {
}
impl LineHeight for ImageView {
fn num_lines(&self, cx: &mut WindowContext) -> u8 {
fn num_lines(&self, cx: &mut WindowContext) -> usize {
let line_height = cx.line_height();
let lines = self.height as f32 / line_height.0;
if lines > u8::MAX as f32 {
return u8::MAX;
}
lines as u8
(self.height as f32 / line_height.0) as usize
}
}
@ -257,7 +251,7 @@ impl TableView {
}
impl LineHeight for TableView {
fn num_lines(&self, _cx: &mut WindowContext) -> u8 {
fn num_lines(&self, _cx: &mut WindowContext) -> usize {
let num_rows = match &self.table.data {
// Rows + header
Some(data) => data.len() + 1,
@ -267,7 +261,7 @@ impl LineHeight for TableView {
};
let num_lines = num_rows as f32 * (1.0 + TABLE_Y_PADDING_MULTIPLE) + 1.0;
num_lines.ceil() as u8
num_lines.ceil() as usize
}
}
@ -303,12 +297,9 @@ impl ErrorView {
}
impl LineHeight for ErrorView {
fn num_lines(&self, cx: &mut WindowContext) -> u8 {
let mut height: u8 = 1; // Start at 1 to account for the y padding
height = height.saturating_add(self.ename.lines().count() as u8);
height = height.saturating_add(self.evalue.lines().count() as u8);
height = height.saturating_add(self.traceback.num_lines(cx));
height
fn num_lines(&self, cx: &mut WindowContext) -> usize {
// Start at 1 to account for the y padding
1 + self.ename.lines().count() + self.evalue.lines().count() + self.traceback.num_lines(cx)
}
}
@ -357,12 +348,12 @@ impl OutputType {
impl LineHeight for OutputType {
/// Calculates the expected number of lines
fn num_lines(&self, cx: &mut WindowContext) -> u8 {
fn num_lines(&self, cx: &mut WindowContext) -> usize {
match self {
Self::Plain(stdio) => stdio.num_lines(cx),
Self::Stream(stdio) => stdio.num_lines(cx),
Self::Image(image) => image.num_lines(cx),
Self::Message(message) => message.lines().count() as u8,
Self::Message(message) => message.lines().count(),
Self::Table(table) => table.num_lines(cx),
Self::ErrorOutput(error_view) => error_view.num_lines(cx),
Self::ClearOutputWaitMarker => 0,
@ -572,7 +563,7 @@ impl Render for ExecutionView {
}
impl LineHeight for ExecutionView {
fn num_lines(&self, cx: &mut WindowContext) -> u8 {
fn num_lines(&self, cx: &mut WindowContext) -> usize {
if self.outputs.is_empty() {
return 1; // For the status message if outputs are not there
}
@ -581,9 +572,7 @@ impl LineHeight for ExecutionView {
.outputs
.iter()
.map(|output| output.num_lines(cx))
.fold(0_u8, |acc, additional_height| {
acc.saturating_add(additional_height)
})
.sum::<usize>()
.max(1);
let num_lines = match self.status {
@ -597,7 +586,7 @@ impl LineHeight for ExecutionView {
}
impl LineHeight for View<ExecutionView> {
fn num_lines(&self, cx: &mut WindowContext) -> u8 {
fn num_lines(&self, cx: &mut WindowContext) -> usize {
self.update(cx, |execution_view, cx| execution_view.num_lines(cx))
}
}

View file

@ -42,12 +42,10 @@ pub struct Session {
}
struct EditorBlock {
editor: WeakView<Editor>,
code_range: Range<Anchor>,
invalidation_anchor: Anchor,
block_id: CustomBlockId,
execution_view: View<ExecutionView>,
on_close: CloseBlockFn,
}
type CloseBlockFn =
@ -84,7 +82,7 @@ impl EditorBlock {
let invalidation_anchor = buffer.read(cx).read(cx).anchor_before(next_row_start);
let block = BlockProperties {
position: code_range.end,
height: execution_view.num_lines(cx).saturating_add(1),
height: (execution_view.num_lines(cx) + 1) as u32,
style: BlockStyle::Sticky,
render: Self::create_output_area_renderer(execution_view.clone(), on_close.clone()),
disposition: BlockDisposition::Below,
@ -95,12 +93,10 @@ impl EditorBlock {
})?;
anyhow::Ok(Self {
editor,
code_range,
invalidation_anchor,
block_id,
execution_view,
on_close,
})
}
@ -108,24 +104,6 @@ impl EditorBlock {
self.execution_view.update(cx, |execution_view, cx| {
execution_view.push_message(&message.content, cx);
});
self.editor
.update(cx, |editor, cx| {
let mut replacements = HashMap::default();
replacements.insert(
self.block_id,
(
Some(self.execution_view.num_lines(cx).saturating_add(1)),
Self::create_output_area_renderer(
self.execution_view.clone(),
self.on_close.clone(),
),
),
);
editor.replace_blocks(replacements, None, cx);
})
.ok();
}
fn create_output_area_renderer(

View file

@ -96,8 +96,8 @@ impl TerminalOutput {
}
impl LineHeight for TerminalOutput {
fn num_lines(&self, _cx: &mut WindowContext) -> u8 {
self.handler.buffer.lines().count().max(1) as u8
fn num_lines(&self, _cx: &mut WindowContext) -> usize {
self.handler.buffer.lines().count().max(1)
}
}