fix(chat): keep a visible gap between inline code and surrounding text
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -3,7 +3,7 @@ use std::ops::Range;
3
3
use teams_core::Span;
4
4
5
5
pub const IMAGE_PLACEHOLDER: &str = "[image]";
6
pub const CODE_PADDING: &str = "\u{200a}";
6
pub const CODE_PADDING: &str = "\u{2004}";
7
7
const URL_PREFIXES: [&str; 3] = ["https://", "http://", "www."];
8
8
const TRAILING_PUNCTUATION: &str = ".,;:!?'\"";
9
9
@@ -68,6 +68,10 @@ impl Inline {
68
68
});
69
69
}
70
70
71
pub fn plain_text(&self) -> String {
72
self.text.replace(CODE_PADDING, "")
73
}
74
71
75
pub fn with_trailing_room(&self, width_in_spaces: usize) -> Inline {
72
76
let mut padded = self.clone();
73
77
padded.push(
@@ -154,7 +158,7 @@ impl Block {
154
158
.join("\n")
155
159
};
156
160
match self {
157
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.text.clone()),
161
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),
158
162
Block::Code { code, .. } => Some(format!("```\n{code}\n```")),
159
163
Block::List {
160
164
ordered,
@@ -175,7 +179,7 @@ impl Block {
175
179
rows.iter()
176
180
.map(|row| {
177
181
row.iter()
178
.map(|cell| cell.text.as_str())
182
.map(Inline::plain_text)
179
183
.collect::<Vec<_>>()
180
184
.join(" | ")
181
185
})
@@ -189,10 +193,10 @@ impl Block {
189
193
190
194
pub fn first_line(&self) -> Option<String> {
191
195
match self {
192
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.text.clone()),
196
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),
193
197
Block::Code { code, .. } => Some(code.clone()),
194
198
Block::List { items, .. } => items.first()?.iter().find_map(Block::first_line),
195
Block::Table { rows, .. } => Some(rows.first()?.first()?.text.clone()),
199
Block::Table { rows, .. } => Some(rows.first()?.first()?.plain_text()),
196
200
Block::Quote(children) => children.iter().find_map(Block::first_line),
197
201
Block::Reply(_) | Block::Rule => None,
198
202
}
@@ -355,25 +359,17 @@ fn pad_code(inline: Inline) -> Inline {
355
359
let mut padded = Inline::default();
356
360
for (index, segment) in inline.segments.iter().enumerate() {
357
361
let text = &inline.text[segment.range.clone()];
358
let touches = |neighbour: Option<&Segment>, at_end: bool| {
359
neighbour.is_some_and(|neighbour| {
360
let neighbour_text = &inline.text[neighbour.range.clone()];
361
let edge = if at_end {
362
neighbour_text.chars().next()
363
} else {
364
neighbour_text.chars().next_back()
365
};
366
!neighbour.style.code && edge.is_some_and(|edge| !edge.is_whitespace())
367
})
362
let touches = |neighbour: Option<&Segment>| {
363
neighbour.is_some_and(|neighbour| !neighbour.style.code && !neighbour.range.is_empty())
368
364
};
369
365
let previous = index
370
366
.checked_sub(1)
371
367
.and_then(|previous| inline.segments.get(previous));
372
if segment.style.code && touches(previous, false) {
368
if segment.style.code && touches(previous) {
373
369
padded.push(CODE_PADDING, StyleFlags::default(), None);
374
370
}
375
371
padded.push(text, segment.style, segment.link.as_deref());
376
if segment.style.code && touches(inline.segments.get(index + 1), true) {
372
if segment.style.code && touches(inline.segments.get(index + 1)) {
377
373
padded.push(CODE_PADDING, StyleFlags::default(), None);
378
374
}
379
375
}
@@ -820,14 +816,15 @@ mod tests {
820
816
}
821
817
822
818
#[test]
823
fn inline_code_gets_room_only_next_to_text() {
819
fn inline_code_gets_room_next_to_text() {
824
820
let inline = paragraph_inline(&[
825
821
text("run "),
826
822
Span::Code("x".into()),
827
823
text(". "),
828
824
Span::Code("y".into()),
829
825
]);
830
assert_eq!(inline.text, "run x\u{200a}. y");
826
assert_eq!(inline.text, "run \u{2004}x\u{2004}. \u{2004}y");
827
assert_eq!(inline.plain_text(), "run x. y");
831
828
}
832
829
833
830
#[test]
@@ -3,7 +3,7 @@ use std::ops::Range;3
use teams_core::Span;3
use teams_core::Span;4
4
5
pub const IMAGE_PLACEHOLDER: &str = "[image]";5
pub const IMAGE_PLACEHOLDER: &str = "[image]";6
pub const CODE_PADDING: &str = "\u{200a}";6
pub const CODE_PADDING: &str = "\u{2004}";7
const URL_PREFIXES: [&str; 3] = ["https://", "http://", "www."];7
const URL_PREFIXES: [&str; 3] = ["https://", "http://", "www."];8
const TRAILING_PUNCTUATION: &str = ".,;:!?'\"";8
const TRAILING_PUNCTUATION: &str = ".,;:!?'\"";9
9
@@ -68,6 +68,10 @@ impl Inline {68
});68
});69
}69
}70
70
71
pub fn plain_text(&self) -> String {72
self.text.replace(CODE_PADDING, "")73
}74
71
pub fn with_trailing_room(&self, width_in_spaces: usize) -> Inline {75
pub fn with_trailing_room(&self, width_in_spaces: usize) -> Inline {72
let mut padded = self.clone();76
let mut padded = self.clone();73
padded.push(77
padded.push(@@ -154,7 +158,7 @@ impl Block {154
.join("\n")158
.join("\n")155
};159
};156
match self {160
match self {157
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.text.clone()),161
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),158
Block::Code { code, .. } => Some(format!("```\n{code}\n```")),162
Block::Code { code, .. } => Some(format!("```\n{code}\n```")),159
Block::List {163
Block::List {160
ordered,164
ordered,@@ -175,7 +179,7 @@ impl Block {175
rows.iter()179
rows.iter()176
.map(|row| {180
.map(|row| {177
row.iter()181
row.iter()178
.map(|cell| cell.text.as_str())182
.map(Inline::plain_text)179
.collect::<Vec<_>>()183
.collect::<Vec<_>>()180
.join(" | ")184
.join(" | ")181
})185
})@@ -189,10 +193,10 @@ impl Block {189
193
190
pub fn first_line(&self) -> Option<String> {194
pub fn first_line(&self) -> Option<String> {191
match self {195
match self {192
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.text.clone()),196
Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),193
Block::Code { code, .. } => Some(code.clone()),197
Block::Code { code, .. } => Some(code.clone()),194
Block::List { items, .. } => items.first()?.iter().find_map(Block::first_line),198
Block::List { items, .. } => items.first()?.iter().find_map(Block::first_line),195
Block::Table { rows, .. } => Some(rows.first()?.first()?.text.clone()),199
Block::Table { rows, .. } => Some(rows.first()?.first()?.plain_text()),196
Block::Quote(children) => children.iter().find_map(Block::first_line),200
Block::Quote(children) => children.iter().find_map(Block::first_line),197
Block::Reply(_) | Block::Rule => None,201
Block::Reply(_) | Block::Rule => None,198
}202
}@@ -355,25 +359,17 @@ fn pad_code(inline: Inline) -> Inline {355
let mut padded = Inline::default();359
let mut padded = Inline::default();356
for (index, segment) in inline.segments.iter().enumerate() {360
for (index, segment) in inline.segments.iter().enumerate() {357
let text = &inline.text[segment.range.clone()];361
let text = &inline.text[segment.range.clone()];358
let touches = |neighbour: Option<&Segment>, at_end: bool| {362
let touches = |neighbour: Option<&Segment>| {359
neighbour.is_some_and(|neighbour| {363
neighbour.is_some_and(|neighbour| !neighbour.style.code && !neighbour.range.is_empty())360
let neighbour_text = &inline.text[neighbour.range.clone()];361
let edge = if at_end {362
neighbour_text.chars().next()363
} else {364
neighbour_text.chars().next_back()365
};366
!neighbour.style.code && edge.is_some_and(|edge| !edge.is_whitespace())367
})368
};364
};369
let previous = index365
let previous = index370
.checked_sub(1)366
.checked_sub(1)371
.and_then(|previous| inline.segments.get(previous));367
.and_then(|previous| inline.segments.get(previous));372
if segment.style.code && touches(previous, false) {368
if segment.style.code && touches(previous) {373
padded.push(CODE_PADDING, StyleFlags::default(), None);369
padded.push(CODE_PADDING, StyleFlags::default(), None);374
}370
}375
padded.push(text, segment.style, segment.link.as_deref());371
padded.push(text, segment.style, segment.link.as_deref());376
if segment.style.code && touches(inline.segments.get(index + 1), true) {372
if segment.style.code && touches(inline.segments.get(index + 1)) {377
padded.push(CODE_PADDING, StyleFlags::default(), None);373
padded.push(CODE_PADDING, StyleFlags::default(), None);378
}374
}379
}375
}@@ -820,14 +816,15 @@ mod tests {820
}816
}821
817
822
#[test]818
#[test]823
fn inline_code_gets_room_only_next_to_text() {819
fn inline_code_gets_room_next_to_text() {824
let inline = paragraph_inline(&[820
let inline = paragraph_inline(&[825
text("run "),821
text("run "),826
Span::Code("x".into()),822
Span::Code("x".into()),827
text(". "),823
text(". "),828
Span::Code("y".into()),824
Span::Code("y".into()),829
]);825
]);830
assert_eq!(inline.text, "run x\u{200a}. y");826
assert_eq!(inline.text, "run \u{2004}x\u{2004}. \u{2004}y");827
assert_eq!(inline.plain_text(), "run x. y");831
}828
}832
829
833
#[test]830
#[test]