T21a round 1 fixes
- Enter over a selection drops it and splits at its start, one undo step - undo and redo keep where the caret was and put it back - a format unwraps only its own markers, so italic inside bold keeps the bold - keys into a merged table cell land on the cell that shows - undo rebuilds a block whose kind needs other fields, so styling comes back - tests activate their window, as focus events need - undo bookkeeping moves to blocks/edit.rs
This commit is contained in:
1 parent
6a9ce8a2c3
commit
f3e2c6c27f
10 files changed
+330
-81
No files matched your search
@@ -114,6 +114,7 @@ Ely GPUI Component. A component library for GPUI, in light and dark.
|
|||||||
- A component that fills its container says `w_full` on its root. As a window's root, or in a flex row, it would shrink to its text's narrowest width.
|
- A component that fills its container says `w_full` on its root. As a window's root, or in a flex row, it would shrink to its text's narrowest width.
|
||||||
- A field's `Highlight` sets color, wash, weight, slant and strike per span; measuring and painting share one set of runs, so wrapped heights match.
|
- A field's `Highlight` sets color, wash, weight, slant and strike per span; measuring and painting share one set of runs, so wrapped heights match.
|
||||||
- A block editor keeps one undo for the document: text and structure together, as snapshots through `forms::History`; it takes Undo and Redo before its fields do.
|
- A block editor keeps one undo for the document: text and structure together, as snapshots through `forms::History`; it takes Undo and Redo before its fields do.
|
||||||
|
- gpui calls focus listeners only while the window is active. A test that needs focus events activates its window with `window.activate_window()`.
|
||||||
- A terminal takes Tab and Shift-Tab through bindings under `ElyTerminal`, ahead of focus moves; other keys go through `terminal::keys` in a key listener, and typed text through its input handler.
|
- A terminal takes Tab and Shift-Tab through bindings under `ElyTerminal`, ahead of focus moves; other keys go through `terminal::keys` in a key listener, and typed text through its input handler.
|
||||||
- A shell in the gallery starts without startup files and with a fixed prompt, so captures show no user or host.
|
- A shell in the gallery starts without startup files and with a fixed prompt, so captures show no user or host.
|
||||||
- gpui 0.2.2 has no `(ElementId, usize)` id. A child id names its index: `(self.id.clone(), format!("row-{ix}"))`.
|
- gpui 0.2.2 has no `(ElementId, usize)` id. A child id names its index: `(self.id.clone(), format!("row-{ix}"))`.
|
||||||
|
|||||||
@@ -247,4 +247,5 @@ Per-component lines live in `tasks/`. Tags there:
|
|||||||
| T19 | 3 | PASS | |
|
| T19 | 3 | PASS | |
|
||||||
| T20 | 1 | FAIL | the demo picked past memory's end; an empty gutter had no height to press; a breakpoint's box also opened it; zoomed ticks lost their places; docs promised bound keys and batch actions |
|
| T20 | 1 | FAIL | the demo picked past memory's end; an empty gutter had no height to press; a breakpoint's box also opened it; zoomed ticks lost their places; docs promised bound keys and batch actions |
|
||||||
| T20 | 2 | PASS | |
|
| T20 | 2 | PASS | |
|
||||||
| T21a | 1 | — | pending review |
|
| T21a | 1 | FAIL | Enter kept a selection; undo of a split left focus on a gone field; italic inside bold ate the bold; keys went into merged cells; undo kept a changed kind's highlighter |
|
||||||
|
| T21a | 2 | — | pending review |
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use gpui::{Context, Window};
|
use gpui::{App, Context, Focusable, Window};
|
||||||
|
|
||||||
use super::{Block, BlockData, BlockEditor, BlockEvent, BlockKind, Snapshot};
|
use super::{Block, BlockData, BlockEditor, BlockEvent, BlockKind, Snapshot};
|
||||||
|
|
||||||
@@ -17,13 +17,13 @@ impl BlockEditor {
|
|||||||
self.turn_into(key, BlockKind::Paragraph, window, cx);
|
self.turn_into(key, BlockKind::Paragraph, window, cx);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let caret = field.read(cx).cursor();
|
let selection = field.read(cx).selection();
|
||||||
let tail = field.read(cx).text()[caret..].to_string();
|
let tail = field.read(cx).text()[selection.end..].to_string();
|
||||||
log::info!("block editor: block {key} splits at {caret}");
|
log::info!("block editor: block {key} splits over {selection:?}");
|
||||||
self.before_change(cx);
|
self.before_change(cx);
|
||||||
field.update(cx, |field, cx| {
|
field.update(cx, |field, cx| {
|
||||||
let end = field.text().len();
|
let end = field.text().len();
|
||||||
field.select(caret..end, cx);
|
field.select(selection.start..end, cx);
|
||||||
field.insert("", cx);
|
field.insert("", cx);
|
||||||
});
|
});
|
||||||
let key = self.fresh_key();
|
let key = self.fresh_key();
|
||||||
@@ -73,29 +73,39 @@ impl BlockEditor {
|
|||||||
|
|
||||||
pub(crate) fn undo(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
pub(crate) fn undo(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
self.settle(true, cx);
|
self.settle(true, cx);
|
||||||
if let Some(previous) = self.history.undo(self.current.clone()) {
|
if let Some(previous) = self.history.undo(self.now(cx)) {
|
||||||
log::info!("block editor: undo");
|
log::info!("block editor: undo");
|
||||||
self.restore(previous, window, cx);
|
self.restore(previous, window, cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn redo(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
pub(crate) fn redo(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
if let Some(next) = self.history.redo(self.current.clone()) {
|
if let Some(next) = self.history.redo(self.now(cx)) {
|
||||||
log::info!("block editor: redo");
|
log::info!("block editor: redo");
|
||||||
self.restore(next, window, cx);
|
self.restore(next, window, cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Puts the document back as `snapshot` holds it, keeping fields whose blocks stay.
|
/// The current document with the caret where it is now.
|
||||||
|
fn now(&self, cx: &Context<Self>) -> Snapshot {
|
||||||
|
let mut now = self.current.clone();
|
||||||
|
now.caret = self.snapshot(cx).caret;
|
||||||
|
now
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Puts the document back as `snapshot` holds it, keeping fields whose blocks stay, and the caret where it was.
|
||||||
fn restore(&mut self, snapshot: Snapshot, window: &mut Window, cx: &mut Context<Self>) {
|
fn restore(&mut self, snapshot: Snapshot, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
let mut kept: HashMap<u64, Block> = self
|
let mut kept: HashMap<u64, Block> = self
|
||||||
.blocks
|
.blocks
|
||||||
.drain(..)
|
.drain(..)
|
||||||
.map(|block| (block.key, block))
|
.map(|block| (block.key, block))
|
||||||
.collect();
|
.collect();
|
||||||
for (key, data) in &snapshot.0 {
|
for (key, data) in &snapshot.blocks {
|
||||||
let block = match kept.remove(key) {
|
let block = match kept.remove(key) {
|
||||||
Some(mut block) if block.fields.len() == data.texts.len() => {
|
Some(mut block)
|
||||||
|
if block.fields.len() == data.texts.len()
|
||||||
|
&& block.kind.same_fields(&data.kind) =>
|
||||||
|
{
|
||||||
block.kind = data.kind.clone();
|
block.kind = data.kind.clone();
|
||||||
for (field, text) in block.fields.iter().zip(&data.texts) {
|
for (field, text) in block.fields.iter().zip(&data.texts) {
|
||||||
if field.read(cx).text() != text {
|
if field.read(cx).text() != text {
|
||||||
@@ -108,8 +118,70 @@ impl BlockEditor {
|
|||||||
};
|
};
|
||||||
self.blocks.push(block);
|
self.blocks.push(block);
|
||||||
}
|
}
|
||||||
|
if let Some((key, field, selection)) = snapshot.caret.clone()
|
||||||
|
&& let Some(block) = self.blocks.iter().find(|block| block.key == key)
|
||||||
|
&& let Some(field) = block.fields.get(field)
|
||||||
|
{
|
||||||
|
field.update(cx, |field, cx| field.select(selection, cx));
|
||||||
|
window.focus(&field.focus_handle(cx));
|
||||||
|
}
|
||||||
self.current = snapshot;
|
self.current = snapshot;
|
||||||
cx.emit(BlockEvent::Changed);
|
cx.emit(BlockEvent::Changed);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn snapshot(&self, cx: &App) -> Snapshot {
|
||||||
|
let caret = self.caret.and_then(|(key, field)| {
|
||||||
|
let block = self.blocks.iter().find(|block| block.key == key)?;
|
||||||
|
let selection = block.fields.get(field)?.read(cx).selection();
|
||||||
|
Some((key, field, selection))
|
||||||
|
});
|
||||||
|
Snapshot {
|
||||||
|
caret,
|
||||||
|
blocks: self
|
||||||
|
.blocks
|
||||||
|
.iter()
|
||||||
|
.map(|block| {
|
||||||
|
let texts = block
|
||||||
|
.fields
|
||||||
|
.iter()
|
||||||
|
.map(|field| field.read(cx).text().to_string())
|
||||||
|
.collect();
|
||||||
|
(
|
||||||
|
block.key,
|
||||||
|
BlockData {
|
||||||
|
kind: block.kind.clone(),
|
||||||
|
texts,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Records what changed as one undo step, a burst of typing as one, and tells the owner.
|
||||||
|
pub(super) fn settle(&mut self, typing: bool, cx: &mut Context<Self>) {
|
||||||
|
let now = self.snapshot(cx);
|
||||||
|
if now.blocks != self.current.blocks {
|
||||||
|
let before = std::mem::replace(&mut self.current, now);
|
||||||
|
self.history.record(before, typing);
|
||||||
|
cx.emit(BlockEvent::Changed);
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Records the document before a change to its blocks.
|
||||||
|
pub(crate) fn before_change(&mut self, cx: &mut Context<Self>) {
|
||||||
|
self.settle(true, cx);
|
||||||
|
let mut before = self.current.clone();
|
||||||
|
before.caret = self.snapshot(cx).caret;
|
||||||
|
self.history.record(before, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// After a change to the blocks: the new state is current.
|
||||||
|
pub(crate) fn after_change(&mut self, cx: &mut Context<Self>) {
|
||||||
|
self.current = self.snapshot(cx);
|
||||||
|
cx.emit(BlockEvent::Changed);
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
use gpui::{Context, Window};
|
use gpui::{Context, Window};
|
||||||
|
|
||||||
use super::{BlockData, BlockEditor, BlockKind};
|
use super::{BlockData, BlockEditor, BlockKind, table::shown_cell};
|
||||||
use crate::documents::modes::at_edge;
|
use crate::documents::modes::at_edge;
|
||||||
|
|
||||||
/// A new empty paragraph.
|
/// A new empty paragraph.
|
||||||
@@ -43,12 +43,13 @@ impl BlockEditor {
|
|||||||
BlockKind::Image(_) | BlockKind::Video(_) | BlockKind::Embed(_) => {
|
BlockKind::Image(_) | BlockKind::Video(_) | BlockKind::Embed(_) => {
|
||||||
self.insert(Some(key), paragraph(), window, cx);
|
self.insert(Some(key), paragraph(), window, cx);
|
||||||
}
|
}
|
||||||
BlockKind::Table { columns, .. } => {
|
BlockKind::Table { columns, merged } => {
|
||||||
let below = field + columns;
|
let below = field + columns;
|
||||||
if below >= self.blocks[self.index(key)].fields.len() {
|
if below >= self.blocks[self.index(key)].fields.len() {
|
||||||
self.add_row(key, window, cx);
|
self.add_row(key, window, cx);
|
||||||
}
|
}
|
||||||
self.focus_field(key, below, None, window, cx);
|
let shown = shown_cell(&merged, columns, below);
|
||||||
|
self.focus_field(key, shown, None, window, cx);
|
||||||
}
|
}
|
||||||
_ => return false,
|
_ => return false,
|
||||||
}
|
}
|
||||||
@@ -95,14 +96,15 @@ impl BlockEditor {
|
|||||||
if !at_edge(block.fields[field].read(cx), down) {
|
if !at_edge(block.fields[field].read(cx), down) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if let BlockKind::Table { columns, .. } = block.kind {
|
if let BlockKind::Table { columns, merged } = &block.kind {
|
||||||
let row = if down {
|
let row = if down {
|
||||||
Some(field + columns)
|
Some(field + columns)
|
||||||
} else {
|
} else {
|
||||||
field.checked_sub(columns)
|
field.checked_sub(*columns)
|
||||||
};
|
};
|
||||||
if let Some(row) = row.filter(|row| *row < block.fields.len()) {
|
if let Some(row) = row.filter(|row| *row < block.fields.len()) {
|
||||||
self.focus_field(key, row, None, window, cx);
|
let shown = shown_cell(merged, *columns, row);
|
||||||
|
self.focus_field(key, shown, None, window, cx);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,6 +150,16 @@ impl BlockKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether a field made for this kind serves `other` unchanged: the same highlighter and hint.
|
||||||
|
pub(crate) fn same_fields(&self, other: &Self) -> bool {
|
||||||
|
std::mem::discriminant(self) == std::mem::discriminant(other)
|
||||||
|
&& match (self, other) {
|
||||||
|
(Self::Heading(one), Self::Heading(two)) => one == two,
|
||||||
|
(Self::Synced(one), Self::Synced(two)) => one == two,
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Prose: Enter splits it, Backspace at its start turns it back into text or joins it up.
|
/// Prose: Enter splits it, Backspace at its start turns it back into text or joins it up.
|
||||||
pub fn is_prose(&self) -> bool {
|
pub fn is_prose(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
|
|||||||
+25
-55
@@ -61,9 +61,12 @@ pub(crate) struct Block {
|
|||||||
_changes: Vec<Subscription>,
|
_changes: Vec<Subscription>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The whole document, as undo keeps it.
|
/// The whole document as undo keeps it, and where the caret was: block, field and selection.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug)]
|
||||||
struct Snapshot(Vec<(u64, BlockData)>);
|
struct Snapshot {
|
||||||
|
blocks: Vec<(u64, BlockData)>,
|
||||||
|
caret: Option<(u64, usize, std::ops::Range<usize>)>,
|
||||||
|
}
|
||||||
|
|
||||||
/// A document of blocks, Notion-like: prose, lists and to-dos, quotes and callouts, toggles, code, math, diagrams, dividers, tables, media, columns and synced blocks. Markdown at a paragraph's start changes its kind; `/` offers every kind; `@` and `:` suggest people and emoji; blocks drag by their handle. Undo covers text and structure alike.
|
/// A document of blocks, Notion-like: prose, lists and to-dos, quotes and callouts, toggles, code, math, diagrams, dividers, tables, media, columns and synced blocks. Markdown at a paragraph's start changes its kind; `/` offers every kind; `@` and `:` suggest people and emoji; blocks drag by their handle. Undo covers text and structure alike.
|
||||||
pub struct BlockEditor {
|
pub struct BlockEditor {
|
||||||
@@ -71,6 +74,8 @@ pub struct BlockEditor {
|
|||||||
next: u64,
|
next: u64,
|
||||||
history: History<Snapshot>,
|
history: History<Snapshot>,
|
||||||
current: Snapshot,
|
current: Snapshot,
|
||||||
|
/// The field that last held focus, as block and field.
|
||||||
|
caret: Option<(u64, usize)>,
|
||||||
pub(crate) people: Vec<SharedString>,
|
pub(crate) people: Vec<SharedString>,
|
||||||
pub(crate) diagram: Option<RenderDiagram>,
|
pub(crate) diagram: Option<RenderDiagram>,
|
||||||
_synced: Subscription,
|
_synced: Subscription,
|
||||||
@@ -85,7 +90,11 @@ impl BlockEditor {
|
|||||||
blocks: Vec::new(),
|
blocks: Vec::new(),
|
||||||
next: 0,
|
next: 0,
|
||||||
history: History::default(),
|
history: History::default(),
|
||||||
current: Snapshot(Vec::new()),
|
current: Snapshot {
|
||||||
|
blocks: Vec::new(),
|
||||||
|
caret: None,
|
||||||
|
},
|
||||||
|
caret: None,
|
||||||
people: Vec::new(),
|
people: Vec::new(),
|
||||||
diagram: None,
|
diagram: None,
|
||||||
_synced: synced,
|
_synced: synced,
|
||||||
@@ -114,34 +123,12 @@ impl BlockEditor {
|
|||||||
/// The document as it stands.
|
/// The document as it stands.
|
||||||
pub fn blocks(&self, cx: &App) -> Vec<BlockData> {
|
pub fn blocks(&self, cx: &App) -> Vec<BlockData> {
|
||||||
self.snapshot(cx)
|
self.snapshot(cx)
|
||||||
.0
|
.blocks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(_, data)| data)
|
.map(|(_, data)| data)
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn snapshot(&self, cx: &App) -> Snapshot {
|
|
||||||
Snapshot(
|
|
||||||
self.blocks
|
|
||||||
.iter()
|
|
||||||
.map(|block| {
|
|
||||||
let texts = block
|
|
||||||
.fields
|
|
||||||
.iter()
|
|
||||||
.map(|field| field.read(cx).text().to_string())
|
|
||||||
.collect();
|
|
||||||
(
|
|
||||||
block.key,
|
|
||||||
BlockData {
|
|
||||||
kind: block.kind.clone(),
|
|
||||||
texts,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A field for `kind`: inline markdown styled as typed, or code colored as code.
|
/// A field for `kind`: inline markdown styled as typed, or code colored as code.
|
||||||
fn field(
|
fn field(
|
||||||
kind: &BlockKind,
|
kind: &BlockKind,
|
||||||
@@ -300,9 +287,16 @@ impl BlockEditor {
|
|||||||
}
|
}
|
||||||
self.settle(true, cx);
|
self.settle(true, cx);
|
||||||
}
|
}
|
||||||
InputEvent::Focus if field.read(cx).is_empty() => {
|
InputEvent::Focus => {
|
||||||
let hint = self.blocks[ix].kind.placeholder();
|
self.caret = self.blocks[ix]
|
||||||
field.update(cx, |field, cx| field.set_placeholder(hint, cx));
|
.fields
|
||||||
|
.iter()
|
||||||
|
.position(|each| each == field)
|
||||||
|
.map(|at| (key, at));
|
||||||
|
if field.read(cx).is_empty() {
|
||||||
|
let hint = self.blocks[ix].kind.placeholder();
|
||||||
|
field.update(cx, |field, cx| field.set_placeholder(hint, cx));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
InputEvent::Blur if self.blocks[ix].kind == BlockKind::Paragraph => {
|
InputEvent::Blur if self.blocks[ix].kind == BlockKind::Paragraph => {
|
||||||
field.update(cx, |field, cx| field.set_placeholder("", cx));
|
field.update(cx, |field, cx| field.set_placeholder("", cx));
|
||||||
@@ -332,30 +326,6 @@ impl BlockEditor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Records what changed as one undo step, a burst of typing as one, and tells the owner.
|
|
||||||
fn settle(&mut self, typing: bool, cx: &mut Context<Self>) {
|
|
||||||
let now = self.snapshot(cx);
|
|
||||||
if now != self.current {
|
|
||||||
let before = std::mem::replace(&mut self.current, now);
|
|
||||||
self.history.record(before, typing);
|
|
||||||
cx.emit(BlockEvent::Changed);
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Records the document before a change to its blocks.
|
|
||||||
pub(crate) fn before_change(&mut self, cx: &mut Context<Self>) {
|
|
||||||
self.settle(true, cx);
|
|
||||||
self.history.record(self.current.clone(), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// After a change to the blocks: the new state is current.
|
|
||||||
pub(crate) fn after_change(&mut self, cx: &mut Context<Self>) {
|
|
||||||
self.current = self.snapshot(cx);
|
|
||||||
cx.emit(BlockEvent::Changed);
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn focus_field(
|
pub(crate) fn focus_field(
|
||||||
&self,
|
&self,
|
||||||
key: u64,
|
key: u64,
|
||||||
@@ -453,7 +423,7 @@ impl BlockEditor {
|
|||||||
|
|
||||||
pub fn duplicate(&mut self, key: u64, window: &mut Window, cx: &mut Context<Self>) {
|
pub fn duplicate(&mut self, key: u64, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
let ix = self.index(key);
|
let ix = self.index(key);
|
||||||
let data = self.snapshot(cx).0[ix].1.clone();
|
let data = self.snapshot(cx).blocks[ix].1.clone();
|
||||||
self.insert(Some(key), data, window, cx);
|
self.insert(Some(key), data, window, cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,15 @@ pub(crate) fn span(merged: &[(usize, usize)], columns: usize, row: usize, column
|
|||||||
.count()
|
.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The cell that shows for `cell`: itself, or the one a merge folds it into.
|
||||||
|
pub(crate) fn shown_cell(merged: &[(usize, usize)], columns: usize, cell: usize) -> usize {
|
||||||
|
let (row, mut column) = (cell / columns, cell % columns);
|
||||||
|
while column > 0 && merged.contains(&(row, column)) {
|
||||||
|
column -= 1;
|
||||||
|
}
|
||||||
|
row * columns + column
|
||||||
|
}
|
||||||
|
|
||||||
/// Merges after `column` is removed: its own go, the ones right of it move left.
|
/// Merges after `column` is removed: its own go, the ones right of it move left.
|
||||||
pub(crate) fn without_column(merged: &[(usize, usize)], column: usize) -> Vec<(usize, usize)> {
|
pub(crate) fn without_column(merged: &[(usize, usize)], column: usize) -> Vec<(usize, usize)> {
|
||||||
merged
|
merged
|
||||||
|
|||||||
+58
-2
@@ -2,7 +2,7 @@ use std::ops::Range;
|
|||||||
|
|
||||||
use gpui::{App, Entity};
|
use gpui::{App, Entity};
|
||||||
|
|
||||||
use super::markdown::{Mark, marks};
|
use super::markdown::{Mark, edge, marks};
|
||||||
use crate::forms::TextInput;
|
use crate::forms::TextInput;
|
||||||
|
|
||||||
/// A format the toolbars apply to markdown.
|
/// A format the toolbars apply to markdown.
|
||||||
@@ -32,6 +32,17 @@ impl Format {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The mark an inline format writes.
|
||||||
|
fn mark(self) -> Option<Mark> {
|
||||||
|
match self {
|
||||||
|
Self::Bold => Some(Mark::Strong),
|
||||||
|
Self::Italic => Some(Mark::Emphasis),
|
||||||
|
Self::Strike => Some(Mark::Strike),
|
||||||
|
Self::Code => Some(Mark::Code),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The prefix a line format starts its lines with; numbered lines count up from one.
|
/// The prefix a line format starts its lines with; numbered lines count up from one.
|
||||||
fn prefix(self, nth: usize) -> String {
|
fn prefix(self, nth: usize) -> String {
|
||||||
match self {
|
match self {
|
||||||
@@ -81,6 +92,28 @@ fn line_prefix(line: &str) -> usize {
|
|||||||
number_prefix(line)
|
number_prefix(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The span of `format` that `selection` fills, but for other styles' markers: where it is written, and where its text sits.
|
||||||
|
fn own(
|
||||||
|
text: &str,
|
||||||
|
selection: &Range<usize>,
|
||||||
|
format: Format,
|
||||||
|
) -> Option<(Range<usize>, Range<usize>)> {
|
||||||
|
let wanted = format.mark().expect("an inline format has a mark");
|
||||||
|
let only_markers = |part: &str| part.chars().all(|ch| matches!(ch, '*' | '_' | '~' | '`'));
|
||||||
|
marks(text).into_iter().find_map(|(range, mark)| {
|
||||||
|
if mark != wanted || selection.start < range.start || range.end < selection.end {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let edge = edge(&text[range.clone()], mark);
|
||||||
|
let inner = range.start + edge..range.end - edge;
|
||||||
|
let fills = inner.start <= selection.start
|
||||||
|
&& selection.end <= inner.end
|
||||||
|
&& only_markers(&text[inner.start..selection.start])
|
||||||
|
&& only_markers(&text[selection.end..inner.end]);
|
||||||
|
fills.then_some((range, inner))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// `text` with `format` applied over `selection`, and where the selection lands. An inline format wraps the selection, or unwraps it when already wrapped; an empty one leaves the caret between its markers. A line format prefixes each touched line, or takes the prefix off when every line has it.
|
/// `text` with `format` applied over `selection`, and where the selection lands. An inline format wraps the selection, or unwraps it when already wrapped; an empty one leaves the caret between its markers. A line format prefixes each touched line, or takes the prefix off when every line has it.
|
||||||
pub fn apply(text: &str, selection: Range<usize>, format: Format) -> (String, Range<usize>) {
|
pub fn apply(text: &str, selection: Range<usize>, format: Format) -> (String, Range<usize>) {
|
||||||
assert!(
|
assert!(
|
||||||
@@ -97,11 +130,21 @@ pub fn apply(text: &str, selection: Range<usize>, format: Format) -> (String, Ra
|
|||||||
if let Some(marker) = format.marker() {
|
if let Some(marker) = format.marker() {
|
||||||
let (before, after) = (&text[..selection.start], &text[selection.end..]);
|
let (before, after) = (&text[..selection.start], &text[selection.end..]);
|
||||||
let inner = &text[selection.clone()];
|
let inner = &text[selection.clone()];
|
||||||
if before.ends_with(marker) && after.starts_with(marker) {
|
if selection.is_empty() && before.ends_with(marker) && after.starts_with(marker) {
|
||||||
let start = selection.start - marker.len();
|
let start = selection.start - marker.len();
|
||||||
let unwrapped = format!("{}{inner}{}", &before[..start], &after[marker.len()..]);
|
let unwrapped = format!("{}{inner}{}", &before[..start], &after[marker.len()..]);
|
||||||
return (unwrapped, start..selection.end - marker.len());
|
return (unwrapped, start..selection.end - marker.len());
|
||||||
}
|
}
|
||||||
|
if let Some((range, text_range)) = own(text, &selection, format) {
|
||||||
|
let shift = text_range.start - range.start;
|
||||||
|
let unwrapped = format!(
|
||||||
|
"{}{}{}",
|
||||||
|
&text[..range.start],
|
||||||
|
&text[text_range],
|
||||||
|
&text[range.end..]
|
||||||
|
);
|
||||||
|
return (unwrapped, selection.start - shift..selection.end - shift);
|
||||||
|
}
|
||||||
let shift = marker.len();
|
let shift = marker.len();
|
||||||
let wrapped = format!("{before}{marker}{inner}{marker}{after}");
|
let wrapped = format!("{before}{marker}{inner}{marker}{after}");
|
||||||
return (wrapped, selection.start + shift..selection.end + shift);
|
return (wrapped, selection.start + shift..selection.end + shift);
|
||||||
@@ -204,6 +247,19 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_format_unwraps_only_its_own_markers() {
|
||||||
|
assert_eq!(
|
||||||
|
apply("**bold**", 2..6, Format::Italic),
|
||||||
|
("***bold***".into(), 3..7)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
apply("a *it* b", 3..5, Format::Italic),
|
||||||
|
("a it b".into(), 2..4)
|
||||||
|
);
|
||||||
|
assert_eq!(apply("**bold**", 2..6, Format::Bold), ("bold".into(), 0..4));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn line_formats_prefix_every_touched_line_or_take_it_off() {
|
fn line_formats_prefix_every_touched_line_or_take_it_off() {
|
||||||
let text = "one\ntwo\nthree";
|
let text = "one\ntwo\nthree";
|
||||||
|
|||||||
@@ -37,6 +37,16 @@ fn leading(text: &str, ch: char) -> usize {
|
|||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// How many bytes at each end of `span` write `mark`: its asterisks, tildes or backticks.
|
||||||
|
pub(crate) fn edge(span: &str, mark: Mark) -> usize {
|
||||||
|
let first = span.chars().next().expect("a styled span has its marker");
|
||||||
|
let run = leading(span, first).min(span.len() / 2);
|
||||||
|
match mark {
|
||||||
|
Mark::Emphasis => run.min(first.len_utf8()),
|
||||||
|
_ => run,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The marks over `text`'s bytes: styles, code, links, headings and quotes, and the syntax that writes each.
|
/// The marks over `text`'s bytes: styles, code, links, headings and quotes, and the syntax that writes each.
|
||||||
pub fn marks(text: &str) -> Vec<(Range<usize>, Mark)> {
|
pub fn marks(text: &str) -> Vec<(Range<usize>, Mark)> {
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
@@ -50,18 +60,13 @@ pub fn marks(text: &str) -> Vec<(Range<usize>, Mark)> {
|
|||||||
Event::Start(Tag::Emphasis) => Mark::Emphasis,
|
Event::Start(Tag::Emphasis) => Mark::Emphasis,
|
||||||
_ => Mark::Strike,
|
_ => Mark::Strike,
|
||||||
};
|
};
|
||||||
let first = span.chars().next().expect("a styled span has its marker");
|
let edge = edge(span, mark);
|
||||||
let edge = leading(span, first).min(span.len() / 2);
|
|
||||||
let edge = match mark {
|
|
||||||
Mark::Emphasis => edge.min(first.len_utf8()),
|
|
||||||
_ => edge,
|
|
||||||
};
|
|
||||||
out.push((range.clone(), mark));
|
out.push((range.clone(), mark));
|
||||||
out.push((range.start..range.start + edge, Mark::Syntax));
|
out.push((range.start..range.start + edge, Mark::Syntax));
|
||||||
out.push((range.end - edge..range.end, Mark::Syntax));
|
out.push((range.end - edge..range.end, Mark::Syntax));
|
||||||
}
|
}
|
||||||
Event::Code(_) => {
|
Event::Code(_) => {
|
||||||
let ticks = leading(span, '`');
|
let ticks = edge(span, Mark::Code);
|
||||||
out.push((range.clone(), Mark::Code));
|
out.push((range.clone(), Mark::Code));
|
||||||
out.push((range.start..range.start + ticks, Mark::Syntax));
|
out.push((range.start..range.start + ticks, Mark::Syntax));
|
||||||
out.push((range.end - ticks..range.end, Mark::Syntax));
|
out.push((range.end - ticks..range.end, Mark::Syntax));
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ fn open(
|
|||||||
super::bind_keys(cx);
|
super::bind_keys(cx);
|
||||||
});
|
});
|
||||||
let (editor, cx) = cx.add_window_view(|window, cx| BlockEditor::new(blocks, window, cx));
|
let (editor, cx) = cx.add_window_view(|window, cx| BlockEditor::new(blocks, window, cx));
|
||||||
|
cx.update(|window, _| window.activate_window());
|
||||||
settle(cx);
|
settle(cx);
|
||||||
(editor, cx)
|
(editor, cx)
|
||||||
}
|
}
|
||||||
@@ -182,3 +183,125 @@ fn a_press_beside_a_blocks_text_puts_the_caret_at_its_end(cx: &mut TestAppContex
|
|||||||
Some((editor.read_with(cx, |editor, _| editor.blocks[0].key), 0))
|
Some((editor.read_with(cx, |editor, _| editor.blocks[0].key), 0))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Puts block `ix`'s field `field` in focus with `range` selected.
|
||||||
|
fn select(
|
||||||
|
editor: &Entity<BlockEditor>,
|
||||||
|
ix: usize,
|
||||||
|
field: usize,
|
||||||
|
range: std::ops::Range<usize>,
|
||||||
|
cx: &mut VisualTestContext,
|
||||||
|
) {
|
||||||
|
cx.update(|window, cx| {
|
||||||
|
let field = editor.read(cx).blocks[ix].fields[field].clone();
|
||||||
|
field.update(cx, |field, cx| field.select(range, cx));
|
||||||
|
window.focus(&field.focus_handle(cx));
|
||||||
|
});
|
||||||
|
settle(cx);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
fn enter_over_a_selection_drops_it_and_splits_there(cx: &mut TestAppContext) {
|
||||||
|
let (editor, cx) = open(vec![BlockData::new(BlockKind::Paragraph, ["abcdef"])], cx);
|
||||||
|
select(&editor, 0, 0, 2..4, cx);
|
||||||
|
cx.simulate_keystrokes("enter");
|
||||||
|
settle(cx);
|
||||||
|
assert_eq!(
|
||||||
|
blocks(&editor, cx),
|
||||||
|
[
|
||||||
|
text(BlockKind::Paragraph, "ab"),
|
||||||
|
text(BlockKind::Paragraph, "ef")
|
||||||
|
]
|
||||||
|
);
|
||||||
|
cx.simulate_keystrokes("cmd-z");
|
||||||
|
settle(cx);
|
||||||
|
assert_eq!(
|
||||||
|
blocks(&editor, cx),
|
||||||
|
[text(BlockKind::Paragraph, "abcdef")],
|
||||||
|
"one step undoes it"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
fn undo_puts_the_caret_back_where_the_change_began(cx: &mut TestAppContext) {
|
||||||
|
let (editor, cx) = open(vec![BlockData::new(BlockKind::Paragraph, ["abcdef"])], cx);
|
||||||
|
caret(&editor, 0, 3, cx);
|
||||||
|
cx.simulate_keystrokes("enter");
|
||||||
|
settle(cx);
|
||||||
|
cx.simulate_keystrokes("cmd-z");
|
||||||
|
settle(cx);
|
||||||
|
let first = cx.update(|_, cx| editor.read(cx).blocks[0].key);
|
||||||
|
let focused = cx.update(|window, cx| editor.read(cx).focused(window, cx));
|
||||||
|
assert_eq!(focused, Some((first, 0)));
|
||||||
|
cx.simulate_keystrokes("cmd-shift-z");
|
||||||
|
settle(cx);
|
||||||
|
assert_eq!(
|
||||||
|
blocks(&editor, cx),
|
||||||
|
[
|
||||||
|
text(BlockKind::Paragraph, "abc"),
|
||||||
|
text(BlockKind::Paragraph, "def")
|
||||||
|
],
|
||||||
|
"redo splits again"
|
||||||
|
);
|
||||||
|
cx.simulate_keystrokes("cmd-z");
|
||||||
|
settle(cx);
|
||||||
|
cx.simulate_input("x");
|
||||||
|
settle(cx);
|
||||||
|
assert_eq!(
|
||||||
|
blocks(&editor, cx),
|
||||||
|
[text(BlockKind::Paragraph, "abcxdef")],
|
||||||
|
"typing lands at the caret undo put back"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
fn keys_into_a_merged_cell_land_on_the_cell_that_shows(cx: &mut TestAppContext) {
|
||||||
|
let table = BlockKind::Table {
|
||||||
|
columns: 2,
|
||||||
|
merged: vec![(1, 1)],
|
||||||
|
};
|
||||||
|
let (editor, cx) = open(vec![BlockData::new(table, ["A", "B", "c", ""])], cx);
|
||||||
|
select(&editor, 0, 0, 1..1, cx);
|
||||||
|
cx.update(|window, cx| {
|
||||||
|
let field = editor.read(cx).blocks[0].fields[1].clone();
|
||||||
|
window.focus(&field.focus_handle(cx));
|
||||||
|
});
|
||||||
|
settle(cx);
|
||||||
|
cx.simulate_keystrokes("down");
|
||||||
|
settle(cx);
|
||||||
|
let key = cx.update(|_, cx| editor.read(cx).blocks[0].key);
|
||||||
|
let focused = cx.update(|window, cx| editor.read(cx).focused(window, cx));
|
||||||
|
assert_eq!(
|
||||||
|
focused,
|
||||||
|
Some((key, 2)),
|
||||||
|
"down from B lands on the cell c spans"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
fn undoing_a_kind_change_brings_back_its_styling(cx: &mut TestAppContext) {
|
||||||
|
let (editor, cx) = open(vec![BlockData::new(BlockKind::Paragraph, ["**lift**"])], cx);
|
||||||
|
cx.update(|window, cx| {
|
||||||
|
let key = editor.read(cx).blocks[0].key;
|
||||||
|
editor.update(cx, |editor, cx| {
|
||||||
|
editor.turn_into(key, BlockKind::Code, window, cx)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
settle(cx);
|
||||||
|
caret(&editor, 0, 0, cx);
|
||||||
|
cx.simulate_keystrokes("cmd-z");
|
||||||
|
settle(cx);
|
||||||
|
let bold = cx.update(|_, cx| {
|
||||||
|
let field = editor.read(cx).blocks[0].fields[0].clone();
|
||||||
|
field
|
||||||
|
.read(cx)
|
||||||
|
.highlights(cx)
|
||||||
|
.iter()
|
||||||
|
.any(|(_, highlight)| highlight.weight.is_some())
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
blocks(&editor, cx),
|
||||||
|
[text(BlockKind::Paragraph, "**lift**")]
|
||||||
|
);
|
||||||
|
assert!(bold, "the paragraph styles its markdown again");
|
||||||
|
}
|
||||||
Reference in new issue
Block a user