Fix compile errors, now lines are being laid out

This commit is contained in:
Antonio Scandurra 2023-11-04 14:37:57 +01:00
parent 436dc93441
commit 86d1defda2

View file

@ -1471,20 +1471,18 @@ impl EditorElement {
.get(&(ix as u32 + rows.start)) .get(&(ix as u32 + rows.start))
.unwrap_or(&default_number); .unwrap_or(&default_number);
write!(&mut line_number, "{}", number).unwrap(); write!(&mut line_number, "{}", number).unwrap();
let run = TextRun {
len: line_number.len(),
font: self.style.text.font(),
color,
underline: None,
};
let layout = cx let layout = cx
.text_system() .text_system()
.layout_text( .layout_text(&line_number, font_size, &[run], None)
&line_number, .unwrap()
font_size, .pop()
&[TextRun { .unwrap();
len: line_number.len(),
font: self.style.text.font(),
color,
underline: None,
}],
None,
)
.unwrap()[0];
line_number_layouts.push(Some(layout)); line_number_layouts.push(Some(layout));
fold_statuses.push( fold_statuses.push(
is_singleton is_singleton
@ -1518,6 +1516,7 @@ impl EditorElement {
// When the editor is empty and unfocused, then show the placeholder. // When the editor is empty and unfocused, then show the placeholder.
if snapshot.is_empty() { if snapshot.is_empty() {
let font_size = self.style.text.font_size * cx.rem_size();
let placeholder_color = cx.theme().styles.colors.text_placeholder; let placeholder_color = cx.theme().styles.colors.text_placeholder;
let placeholder_text = snapshot.placeholder_text(); let placeholder_text = snapshot.placeholder_text();
let placeholder_lines = placeholder_text let placeholder_lines = placeholder_text
@ -1529,19 +1528,17 @@ impl EditorElement {
.take(rows.len()); .take(rows.len());
placeholder_lines placeholder_lines
.map(|line| { .map(|line| {
let run = TextRun {
len: line.len(),
font: self.style.text.font(),
color: placeholder_color,
underline: Default::default(),
};
cx.text_system() cx.text_system()
.layout_text( .layout_text(line, font_size, &[run], None)
line, .unwrap()
self.style.text.font_size * cx.rem_size(), .pop()
&[TextRun { .unwrap()
len: line.len(),
font: self.style.text.font(),
color: placeholder_color,
underline: Default::default(),
}],
None,
)
.unwrap()[0]
}) })
.map(|line| LineWithInvisibles { .map(|line| LineWithInvisibles {
line, line,
@ -1559,7 +1556,7 @@ impl EditorElement {
rows.len() as usize, rows.len() as usize,
line_number_layouts, line_number_layouts,
snapshot.mode, snapshot.mode,
cx.window_context(), cx,
) )
} }
} }
@ -1800,7 +1797,7 @@ impl LineWithInvisibles {
max_line_count: usize, max_line_count: usize,
line_number_layouts: &[Option<Line>], line_number_layouts: &[Option<Line>],
editor_mode: EditorMode, editor_mode: EditorMode,
cx: &mut WindowContext, cx: &WindowContext,
) -> Vec<Self> { ) -> Vec<Self> {
let mut layouts = Vec::with_capacity(max_line_count); let mut layouts = Vec::with_capacity(max_line_count);
let mut line = String::new(); let mut line = String::new();
@ -1809,6 +1806,8 @@ impl LineWithInvisibles {
let mut non_whitespace_added = false; let mut non_whitespace_added = false;
let mut row = 0; let mut row = 0;
let mut line_exceeded_max_len = false; let mut line_exceeded_max_len = false;
let font_size = text_style.font_size * cx.rem_size();
for highlighted_chunk in chunks.chain([HighlightedChunk { for highlighted_chunk in chunks.chain([HighlightedChunk {
chunk: "\n", chunk: "\n",
style: None, style: None,
@ -1816,11 +1815,11 @@ impl LineWithInvisibles {
}]) { }]) {
for (ix, mut line_chunk) in highlighted_chunk.chunk.split('\n').enumerate() { for (ix, mut line_chunk) in highlighted_chunk.chunk.split('\n').enumerate() {
if ix > 0 { if ix > 0 {
let layout = cx
.text_system()
.layout_text(&line, font_size, &styles, None);
layouts.push(Self { layouts.push(Self {
line: cx line: layout.unwrap().pop().unwrap(),
.text_system()
.layout_text(&line, text_style.font_size * cx.rem_size(), &styles, None)
.unwrap()[0],
invisibles: invisibles.drain(..).collect(), invisibles: invisibles.drain(..).collect(),
}); });
@ -2060,11 +2059,11 @@ impl Element<Editor> for EditorElement {
}; };
let text_width = bounds.size.width - gutter_width; let text_width = bounds.size.width - gutter_width;
let overscroll = point(em_width, px(0.)); let overscroll = size(em_width, px(0.));
let snapshot = { let snapshot = {
editor.set_visible_line_count((bounds.size.height / line_height).into(), cx); editor.set_visible_line_count((bounds.size.height / line_height).into(), cx);
let editor_width = text_width - gutter_margin - overscroll.x - em_width; let editor_width = text_width - gutter_margin - overscroll.width - em_width;
let wrap_width = match editor.soft_wrap_mode(cx) { let wrap_width = match editor.soft_wrap_mode(cx) {
SoftWrap::None => (MAX_LINE_LEN / 2) as f32 * em_advance, SoftWrap::None => (MAX_LINE_LEN / 2) as f32 * em_advance,
SoftWrap::EditorWidth => editor_width, SoftWrap::EditorWidth => editor_width,
@ -2320,16 +2319,11 @@ impl Element<Editor> for EditorElement {
} }
} }
// let style = self.style.clone(); let longest_line_width = layout_line(snapshot.longest_row(), &snapshot, &style, cx)
// let longest_line_width = layout_line( .unwrap()
// snapshot.longest_row(), .width();
// &snapshot, let scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.width;
// &style, // todo!("blocks")
// cx.text_layout_cache(),
// )
// .width();
// let scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.x();
// let em_width = style.text.em_width(cx.font_cache());
// let (scroll_width, blocks) = self.layout_blocks( // let (scroll_width, blocks) = self.layout_blocks(
// start_row..end_row, // start_row..end_row,
// &snapshot, // &snapshot,
@ -2346,32 +2340,31 @@ impl Element<Editor> for EditorElement {
// cx, // cx,
// ); // );
// let scroll_max = vec2f( let scroll_max = point(
// ((scroll_width - text_size.x()) / em_width).max(0.0), f32::from((scroll_width - text_size.width) / em_width).max(0.0),
// max_row as f32, max_row as f32,
// ); );
// let clamped = editor.scroll_manager.clamp_scroll_left(scroll_max.x()); let clamped = editor.scroll_manager.clamp_scroll_left(scroll_max.x);
// let autoscrolled = if autoscroll_horizontally { let autoscrolled = if autoscroll_horizontally {
// editor.autoscroll_horizontally( editor.autoscroll_horizontally(
// start_row, start_row,
// text_size.x(), text_size.width,
// scroll_width, scroll_width,
// em_width, em_width,
// &line_layouts, &line_layouts,
// cx, cx,
// ) )
// } else { } else {
// false false
// }; };
// if clamped || autoscrolled { if clamped || autoscrolled {
// snapshot = editor.snapshot(cx); snapshot = editor.snapshot(cx);
// } }
// let style = editor.style(cx);
// todo!("context menu")
// let mut context_menu = None; // let mut context_menu = None;
// let mut code_actions_indicator = None; // let mut code_actions_indicator = None;
// if let Some(newest_selection_head) = newest_selection_head { // if let Some(newest_selection_head) = newest_selection_head {
@ -2392,7 +2385,8 @@ impl Element<Editor> for EditorElement {
// } // }
// } // }
// let visible_rows = start_row..start_row + line_layouts.len() as u32; let visible_rows = start_row..start_row + line_layouts.len() as u32;
// todo!("hover")
// let mut hover = editor.hover_state.render( // let mut hover = editor.hover_state.render(
// &snapshot, // &snapshot,
// &style, // &style,
@ -2402,6 +2396,7 @@ impl Element<Editor> for EditorElement {
// ); // );
// let mode = editor.mode; // let mode = editor.mode;
// todo!("fold_indicators")
// let mut fold_indicators = editor.render_fold_indicators( // let mut fold_indicators = editor.render_fold_indicators(
// fold_statuses, // fold_statuses,
// &style, // &style,
@ -2411,6 +2406,7 @@ impl Element<Editor> for EditorElement {
// cx, // cx,
// ); // );
// todo!("context_menu")
// if let Some((_, context_menu)) = context_menu.as_mut() { // if let Some((_, context_menu)) = context_menu.as_mut() {
// context_menu.layout( // context_menu.layout(
// SizeConstraint { // SizeConstraint {
@ -2425,6 +2421,7 @@ impl Element<Editor> for EditorElement {
// ); // );
// } // }
// todo!("code actions")
// if let Some((_, indicator)) = code_actions_indicator.as_mut() { // if let Some((_, indicator)) = code_actions_indicator.as_mut() {
// indicator.layout( // indicator.layout(
// SizeConstraint::strict_along( // SizeConstraint::strict_along(
@ -2436,6 +2433,7 @@ impl Element<Editor> for EditorElement {
// ); // );
// } // }
// todo!("fold indicators")
// for fold_indicator in fold_indicators.iter_mut() { // for fold_indicator in fold_indicators.iter_mut() {
// if let Some(indicator) = fold_indicator.as_mut() { // if let Some(indicator) = fold_indicator.as_mut() {
// indicator.layout( // indicator.layout(
@ -2449,6 +2447,7 @@ impl Element<Editor> for EditorElement {
// } // }
// } // }
// todo!("hover popovers")
// if let Some((_, hover_popovers)) = hover.as_mut() { // if let Some((_, hover_popovers)) = hover.as_mut() {
// for hover_popover in hover_popovers.iter_mut() { // for hover_popover in hover_popovers.iter_mut() {
// hover_popover.layout( // hover_popover.layout(
@ -2475,6 +2474,7 @@ impl Element<Editor> for EditorElement {
// font_id: self.style.text.font_id, // font_id: self.style.text.font_id,
// underline: Default::default(), // underline: Default::default(),
// }; // };
//
} }
} }
@ -3196,9 +3196,8 @@ fn layout_line(
row: u32, row: u32,
snapshot: &EditorSnapshot, snapshot: &EditorSnapshot,
style: &EditorStyle, style: &EditorStyle,
rem_size: Pixels, cx: &WindowContext,
text_system: &TextSystem, ) -> Result<Line> {
) -> Result<SmallVec<[Line; 1]>> {
let mut line = snapshot.line(row); let mut line = snapshot.line(row);
if line.len() > MAX_LINE_LEN { if line.len() > MAX_LINE_LEN {
@ -3210,17 +3209,21 @@ fn layout_line(
line.truncate(len); line.truncate(len);
} }
text_system.layout_text( Ok(cx
&line, .text_system()
style.text.font_size * rem_size, .layout_text(
&[TextRun { &line,
len: snapshot.line_len(row) as usize, style.text.font_size * cx.rem_size(),
font: style.text.font(), &[TextRun {
color: black(), len: snapshot.line_len(row) as usize,
underline: Default::default(), font: style.text.font(),
}], color: Hsla::default(),
None, underline: None,
) }],
None,
)?
.pop()
.unwrap())
} }
#[derive(Debug)] #[derive(Debug)]