Show selection over inline code; link URLs inside code

An opaque inline-code fill painted over the selection highlight, so a
selected code span looked unselected; it now keeps half the fill. Bare
URLs inside inline code, fenced blocks and operator messages' backticks
are now links like any other.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 19:23:47 +00:00
parent fc3bb5a004
commit 54be08994b
8 changed files with 98 additions and 55 deletions
@@ -8,8 +8,8 @@ import 'chat_links.dart';
import 'chat_transcript_selection.dart';
/// Plain selectable text with its bare links made tappable: the operator's
/// own message keeps every character exactly as typed (no markdown), and only
/// `http(s)://`/`www.` runs outside `` `code` `` become links. Text without a
/// own message keeps every character exactly as typed (no markdown), and its
/// `http(s)://`/`www.` runs, in `` `code` `` too, become links. Text without a
/// link stays a plain [SelectableText] outside a shared transcript document.
class ChatLinkText extends StatefulWidget {
const ChatLinkText(this.text, {super.key});
@@ -118,25 +118,13 @@ String? autolinkTarget(String inner) {
return null;
}
/// Bare links in plain (non-markdown) text, outside `` `code` `` spans and
/// ```` ``` ```` fences, so an operator's pasted command is never linkified.
/// Bare links anywhere in [text]: plain (non-markdown) text, or the body of a
/// `` `code` `` span or fenced block, so a URL an agent quotes in code opens
/// like any other. A backtick ends a link, so a quoted URL excludes it.
List<ChatTextLink> findTextLinks(String text) {
final links = <ChatTextLink>[];
var i = 0;
while (i < text.length) {
if (text.startsWith('```', i)) {
final close = text.indexOf('```', i + 3);
if (close < 0) break;
i = close + 3;
continue;
}
if (text[i] == '`') {
final close = text.indexOf('`', i + 1);
if (close > i) {
i = close + 1;
continue;
}
}
final url = bareLinkAt(text, i);
if (url != null) {
links.add(ChatTextLink(i, i + url.length, url));
@@ -265,8 +265,14 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
padding: const EdgeInsets.all(12),
child: ChatTranscriptHorizontalScrollView(
label: 'code block',
child: Text(
code,
child: Text.rich(
TextSpan(
children: _linked(
context,
code,
(part) => TextSpan(text: part),
),
),
style: const TextStyle(fontFamily: appMonospaceFamily),
),
),
@@ -378,23 +384,60 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
MarkdownSourceCursor source,
) {
final token = text;
final highlighted = _highlightedText(context, text, source);
TextSpan piece(String part) {
final highlighted = _highlightedText(context, part, source);
return TextSpan(
text: highlighted.text,
children: highlighted.children,
onEnter: (event) {
if (_route?.isCurrent != false) {
_codeHover.show(context, token, text, event.position);
}
},
onExit: (_) => _codeHover.scheduleHide(token),
);
}
return TextSpan(
style: TextStyle(
fontFamily: appMonospaceFamily,
backgroundColor: _codeFill(context),
// A span's background is painted over the selection highlight, so an
// opaque fill hid a selected code span; a partial fill keeps code
// distinct and lets the selection show through.
backgroundColor: _codeFill(context).withValues(alpha: _inlineCodeAlpha),
),
text: highlighted.text,
children: highlighted.children,
onEnter: (event) {
if (_route?.isCurrent != false) {
_codeHover.show(context, token, text, event.position);
}
},
onExit: (_) => _codeHover.scheduleHide(token),
children: _linked(context, text, piece),
);
}
/// [text]'s bare URLs as links and the runs between them as [piece] spans,
/// so a URL quoted in code is highlighted and opens like one in prose.
List<InlineSpan> _linked(
BuildContext context,
String text,
TextSpan Function(String part) piece,
) {
final spans = <InlineSpan>[];
var at = 0;
for (final link in findTextLinks(text)) {
if (link.start > at) spans.add(piece(text.substring(at, link.start)));
spans.add(
_links.link(context, link.target, [
piece(text.substring(link.start, link.end)),
], onOpen: (uri) => _open(context, uri)),
);
at = link.end;
}
if (at < text.length || spans.isEmpty) spans.add(piece(text.substring(at)));
return spans;
}
void _open(BuildContext context, Uri uri) {
final onOpenLink = widget.onOpenLink;
if (onOpenLink != null) return onOpenLink(uri.toString());
unawaited(openChatLink(context, uri));
}
InlineSpan _link(
BuildContext context,
String url,
@@ -404,11 +447,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
context,
url,
_spans(context, spans, source),
onOpen: (uri) {
final onOpenLink = widget.onOpenLink;
if (onOpenLink != null) return onOpenLink(uri.toString());
unawaited(openChatLink(context, uri));
},
onOpen: (uri) => _open(context, uri),
);
/// A leaf's spans, recording where it renders so a spoken word in it can
@@ -473,6 +512,10 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
return theme.dividerTheme.color ?? theme.dividerColor;
}
/// How much of the code fill an inline code span keeps, so a selection
/// behind it stays visible.
static const double _inlineCodeAlpha = 0.5;
Color _codeFill(BuildContext context) =>
Theme.of(context).colorScheme.surfaceContainerHighest;