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;
@@ -46,7 +46,7 @@ Future<void> _tapText(WidgetTester tester, String text) async {
}
void main() {
testWidgets('assistant links open through the launcher; code stays plain', (
testWidgets('assistant links, in code too, open through the launcher', (
tester,
) async {
await phoneViewport(tester);
@@ -75,16 +75,20 @@ void main() {
expect(_leaf(tester, text).recognizer, isA<TapGestureRecognizer>());
}
expect(_leaf(tester, 'this').recognizer, isNull);
expect(_leaf(tester, 'https://example.com/inline').recognizer, isNull);
expect(
find.byWidgetPredicate(
(widget) =>
widget is Text &&
widget.data == 'curl https://example.com/fenced' &&
widget.textSpan == null,
),
findsOneWidget,
_leaf(tester, 'https://example.com/inline').recognizer,
isA<TapGestureRecognizer>(),
);
expect(
_leaf(tester, 'https://example.com/fenced').recognizer,
isA<TapGestureRecognizer>(),
);
expect(_leaf(tester, 'curl ').recognizer, isNull);
final inline = spansOf(
tester,
'https://example.com/inline',
).firstWhere((span) => span.style?.fontFamily != null);
expect(inline.style?.backgroundColor, isNotNull);
await _tapText(tester, 'the docs');
await _tapText(tester, 'www.example.org');
@@ -93,6 +97,7 @@ void main() {
expect(opened.map((uri) => '$uri'), [
'https://example.com/docs',
'https://www.example.org',
'https://example.com/inline',
]);
});
@@ -301,11 +306,8 @@ void main() {
);
expect(_leaf(tester, 'https://example.com/bare').recognizer, isNotNull);
expect(
spansOf(tester, 'https://code.com')
.where((span) => span.text?.contains('https://code.com') ?? false)
.single
.recognizer,
isNull,
_leaf(tester, 'https://code.com').recognizer,
isA<TapGestureRecognizer>(),
);
await _tapText(tester, 'https://example.com/bare');
expect(opened.map((uri) => '$uri'), ['https://example.com/bare']);
@@ -52,14 +52,18 @@ void main() {
expect(_targets('www. and www.example'), isEmpty);
});
test('skips inline code and fenced code', () {
test('finds links in inline code and fenced code too', () {
expect(_targets('run `curl https://x.com/a` then https://y.com/b'), [
'https://x.com/a',
'https://y.com/b',
]);
expect(_targets('```\ncurl https://x.com/a\n```\nhttps://y.com/b'), [
'https://x.com/a',
'https://y.com/b',
]);
expect(_targets('```\nunclosed https://x.com'), isEmpty);
expect(findTextLinks('`https://x.com`'), [
const ChatTextLink(1, 14, 'https://x.com'),
]);
});
test('a markdown link in plain text links its target in place', () {
@@ -69,11 +69,15 @@ void main() {
final context = tester.element(find.byType(ChatMarkdownView));
final fill = Theme.of(context).colorScheme.surfaceContainerHighest;
final spans = spansOf(tester, 'Use');
final code = spans.firstWhere((span) => span.text == 'inline_code()');
// The code's style sits on the span grouping its text (and any links).
final code = spans.firstWhere(
(span) => span.style != null && span.toPlainText() == 'inline_code()',
);
final plain = spans.firstWhere(
(span) => span.text == ' in the middle of this sentence.',
);
expect(code.style?.backgroundColor, fill);
// Half the fill, so a selection behind the code stays visible.
expect(code.style?.backgroundColor, fill.withValues(alpha: 0.5));
expect(plain.style?.backgroundColor, isNull);
expect(code.style?.fontSize, isNull);
expect(code.style?.fontFamily, appMonospaceFamily);
@@ -57,9 +57,10 @@ void main() {
final inlineCode = paragraph.firstWhere(
(span) => span.toPlainText() == 'inline',
);
// Half the code fill, so a selection behind it stays visible.
expect(
inlineCode.style?.backgroundColor,
theme.colorScheme.surfaceContainerHighest,
theme.colorScheme.surfaceContainerHighest.withValues(alpha: 0.5),
);
final plain = paragraph.firstWhere(
(span) => span.toPlainText() == ' and snake_case_name.',
@@ -79,7 +80,8 @@ void main() {
final code = find.byWidgetPredicate(
(widget) =>
widget is Text &&
widget.data == 'const answer = 42;\n keep spaces',
widget.textSpan?.toPlainText() ==
'const answer = 42;\n keep spaces',
);
expect(code, findsOneWidget);
final scroll = find.ancestor(
+1 -1
View File
@@ -16,7 +16,7 @@
// answers: tool-only turns ("Used 9 tools", "Ran 1 command",
// one with a failed command) split by background-task notices;
// `?links=1` answers and operator turns carry a markdown link, an
// autolink, a bare URL and URLs inside code (never linkified);
// autolink, a bare URL and URLs inside code (linked too);
// `?timestamps=1` messages carry recorded times over two calendar days.
import 'dart:async';