Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions crates/session-view/src/timeline/parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ pub fn user_bubble<S: TimelineHost>(s: &S, p: &Pal, session: &str, id: &str, bod
);
}
let ink = if pending { p.muted } else { p.text };
// Mentions read as badges; a badge copies as its text.
let (shown, badges, links, paths) = crate::user_parts::bubble_badges(s, p, id, body);
// Mentions read as badges (copying as their text); plain http(s) URLs become clickable links
// (`packages/session-ui/src/message/message-content.tsx` `CurrentHighlightedText`,
// `packages/session-ui/src/components/message-part.css:195-213`).
let (shown, badges, spans, links, paths) = crate::user_parts::bubble_badges(s, p, id, body, ink);
let highlights = if !pending { s.find_text_highlights(p, &format!("u-{id}")) } else { Vec::new() };
// File and folder badges, and (when the prompt contains RTL text) the paths, calls, flags
// and file names typed around them, each keep their own left-to-right order in the line.
Expand All @@ -156,26 +158,40 @@ pub fn user_bubble<S: TimelineHost>(s: &S, p: &Pal, session: &str, id: &str, bod
Vec::new()
};
let mut words = text(shown.clone()).key(format!("text-{id}")).font(crate::style::bubble_font(p.dark)).color(ink).wrap_anywhere().pills(badges).text_highlights(highlights);
if !spans.is_empty() {
words = words.spans(spans);
}
for r in paths.into_iter().chain(units) {
words = words.isolate(r, rune::Direction::Ltr);
}
if !links.is_empty() {
for link in links {
words = words.link(link);
}
let msg_session = (!session.is_empty()).then(|| session.to_owned());
let msg_id = id.to_owned();
words = words
.on_link(|s: &mut S, link: &str, tx: &mut Tx| {
// A chip, not an address: a middle click on it does nothing.
if tx.button() == rune::PointerButton::Middle {
return;
}
if let Some(sid) = crate::user_parts::session_of_link(link) {
s.open_session(sid, tx);
// A chip, not an address: a middle click on it does nothing.
if tx.button() != rune::PointerButton::Middle {
s.open_session(sid, tx);
}
} else {
tx.instant();
s.open_link(link, tx);
}
})
.on_link_secondary(|s: &mut S, link: &str, press, tx: &mut Tx| {
.on_link_secondary(move |s: &mut S, link: &str, press, tx: &mut Tx| {
if let Some(sid) = crate::user_parts::session_of_link(link) {
s.session_chip_menu(sid, press, tx);
} else if !s.reply_link_menu(link, press, tx) {
let at = crate::images::menu::MenuAt {
session: msg_session.clone().or_else(|| s.active_session_id().map(str::to_owned)),
message: Some(msg_id.clone()),
image: None,
};
s.context_menu(at, press, tx);
}
});
}
Expand Down
67 changes: 63 additions & 4 deletions crates/session-view/src/tools/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,66 @@ pub fn one_line_owned(s: String) -> String {
one_line(&s)
}

/// Characters that terminate a bare URL in prose: whitespace, control characters, ASCII
/// delimiters, CJK / fullwidth sentence punctuation and brackets, typographic quotes, guillemets,
/// horizontal ellipsis, and em dash. Stops before CJK punctuation (e.g. `https://example.com。下一句`)
/// while preserving non-ASCII IRI path characters (e.g. `https://zh.wikipedia.org/wiki/中文`).
fn url_stop(c: char) -> bool {
c.is_whitespace()
|| c.is_control()
|| matches!(
c,
'<' | '>'
| '"'
| '\''
| '`'
| '\u{00AB}'
| '\u{00BB}'
| '\u{2014}'
| '\u{2018}'..='\u{201F}'
| '\u{2026}'
| '\u{2039}'
| '\u{203A}'
| '\u{3001}'
| '\u{3002}'
| '\u{3008}'..='\u{3011}'
| '\u{3014}'..='\u{301F}'
| '\u{FE50}'..='\u{FE6B}'
| '\u{FF01}'..='\u{FF0F}'
| '\u{FF1A}'..='\u{FF20}'
| '\u{FF3B}'..='\u{FF40}'
| '\u{FF5B}'..='\u{FF65}'
)
}

/// Strips trailing ASCII sentence punctuation and unmatched closing brackets from a scanned URL.
fn trim_url_end(s: &str, scheme: usize) -> &str {
let mut url = s;
while url.len() > scheme {
let Some(last) = url.chars().next_back() else { break };
let drop = match last {
',' | '.' | ';' | ':' | '!' | '?' => true,
')' => {
let body = &url[scheme..];
body.bytes().filter(|&b| b == b')').count() > body.bytes().filter(|&b| b == b'(').count()
}
']' => {
let body = &url[scheme..];
body.bytes().filter(|&b| b == b']').count() > body.bytes().filter(|&b| b == b'[').count()
}
_ => false,
};
if !drop {
break;
}
url = &url[..url.len() - last.len_utf8()];
}
url
}

/// The byte ranges of the http(s) URLs in `text`, without the sentence punctuation or closing
/// brackets after them (Desktop's `linkSegments`).
/// brackets after them (Desktop's `packages/session-ui/src/components/link-segments.ts`, extended
/// to stop at CJK and fullwidth sentence punctuation).
pub fn urls(text: &str) -> Vec<Range<usize>> {
let mut out = Vec::new();
let mut from = 0;
Expand All @@ -125,9 +183,10 @@ pub fn urls(text: &str) -> Vec<Range<usize>> {
from = at + 4;
continue;
}
let end = tail.find(|c: char| c.is_whitespace() || "<>\"'`".contains(c)).unwrap_or(tail.len());
let url = tail[..end].trim_end_matches(|c: char| "),.;:!?]".contains(c));
if url.len() > scheme {
let end = tail.find(url_stop).unwrap_or(tail.len());
let url = trim_url_end(&tail[..end], scheme);
let rest = &url[scheme..];
if !rest.is_empty() && !matches!(rest.as_bytes()[0], b'/' | b'?' | b'#' | b':') {
out.push(at..at + url.len());
}
from = at + end.max(scheme);
Expand Down
44 changes: 34 additions & 10 deletions crates/session-view/src/user_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use opencode_kit::mention::{
session_of_link, session_of_raw_link, session_raw_link, short_session_id,
};
use opencode_kit::model::{FileComment, PromptComment, Quote};
use rune::{Color, El, Icon, PillStyle, TextStyle, Tx, col, el, icon, row, text};
use rune::{Color, El, Icon, LinkPaint, PillStyle, Span, TextStyle, Tx, col, el, icon, row, text};

use crate::TimelineHost;
use crate::focus::FocusExt as _;
Expand Down Expand Up @@ -89,24 +89,48 @@ pub fn session_tip(id: &str, title: Option<&str>) -> String {
}
}

pub type BubbleBadges = (String, Vec<(Range<usize>, PillStyle)>, Vec<rune::Link>, Vec<Range<usize>>);
pub type BubbleBadges = (
String,
Vec<(Range<usize>, PillStyle)>,
Vec<Span>,
Vec<rune::Link>,
Vec<Range<usize>>,
);

/// Message `id`'s bubble text and its mention badges: pills styled by kind, a session's with
/// its icon, and a link per session badge (opening it when the host can). A badge keeps its own
/// order in the line around it: a file's or folder's reads left to right as a path does, any
/// other's the way its own name does.
pub fn bubble_badges<S: TimelineHost>(s: &S, p: &Pal, id: &str, body: &str) -> BubbleBadges {
/// Message `id`'s bubble text, mention badges, and linkified plain URLs: pills styled by kind (a
/// session's with its icon), 40%-opacity underline spans for plain http(s) URLs outside mention
/// badges, pressable links in text order (session badges when the host can open them, plus plain
/// URLs matching `packages/session-ui/src/message/message-content.tsx` `CurrentHighlightedText`
/// and `packages/session-ui/src/components/message-part.css:195-213`), and LTR path ranges.
pub fn bubble_badges<S: TimelineHost>(s: &S, p: &Pal, id: &str, body: &str, ink: Color) -> BubbleBadges {
let title = |sid: &str| s.session_ref(sid).map(|r| r.title).filter(|t| !t.trim().is_empty()).or_else(|| s.session_title(sid));
let (text, marks) = bubble_marks(body, &mention::user_mentions(id), title);
let links = marks
let mut links: Vec<(usize, rune::Link)> = marks
.iter()
.filter(|_| s.can_open_session())
.filter_map(|m| {
let sid = m.session.as_deref()?;
let link = rune::Link::new(m.range.clone(), session_link(sid)).role("button").help(session_tip(sid, m.title.as_deref()));
Some(link)
Some((m.range.start, link))
})
.collect();
let mut spans = Vec::new();
for r in crate::tools::plain::urls(&text) {
if marks.iter().any(|m| m.range.start < r.end && r.start < m.range.end) {
continue;
}
let url = &text[r.clone()];
let tip = S::reply_link_help(url).unwrap_or_else(|| url.to_owned());
spans.push(Span::new(r.start, r.end).underline(ink.alpha(0.4)));
links.push((
r.start,
rune::Link::new(r.clone(), url)
.hover(LinkPaint::new().underline(ink))
.help(tip),
));
}
links.sort_by_key(|(start, _)| *start);
let links = links.into_iter().map(|(_, link)| link).collect();
let paths = marks.iter().filter(|m| matches!(m.kind, MentionKind::File | MentionKind::Directory)).map(|m| m.range.clone()).collect();
let pills = marks
.into_iter()
Expand All @@ -115,7 +139,7 @@ pub fn bubble_badges<S: TimelineHost>(s: &S, p: &Pal, id: &str, body: &str) -> B
(m.range, if m.session.is_some() { style.icon(&SESSION) } else { style })
})
.collect();
(text, pills, links, paths)
(text, pills, spans, links, paths)
}

// ---------------------------------------------------------------------------------------------
Expand Down
Loading