diff --git a/app/lib/features/chat/ui/transcript/chat_link_text.dart b/app/lib/features/chat/ui/transcript/chat_link_text.dart index d3fcb2e2..6a56a242 100644 --- a/app/lib/features/chat/ui/transcript/chat_link_text.dart +++ b/app/lib/features/chat/ui/transcript/chat_link_text.dart @@ -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}); diff --git a/app/lib/features/chat/ui/transcript/chat_links.dart b/app/lib/features/chat/ui/transcript/chat_links.dart index 00deaf47..3f7bd639 100644 --- a/app/lib/features/chat/ui/transcript/chat_links.dart +++ b/app/lib/features/chat/ui/transcript/chat_links.dart @@ -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 findTextLinks(String text) { final links = []; 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)); diff --git a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart index 144455c0..7454cf14 100644 --- a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart @@ -265,8 +265,14 @@ class _ChatMarkdownViewState extends State { 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 { 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 _linked( + BuildContext context, + String text, + TextSpan Function(String part) piece, + ) { + final spans = []; + 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 { 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 { 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; diff --git a/app/test/features/chat/ui/transcript/chat_link_widgets_test.dart b/app/test/features/chat/ui/transcript/chat_link_widgets_test.dart index 1897a2fe..f5f46da1 100644 --- a/app/test/features/chat/ui/transcript/chat_link_widgets_test.dart +++ b/app/test/features/chat/ui/transcript/chat_link_widgets_test.dart @@ -46,7 +46,7 @@ Future _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()); } 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(), ); + expect( + _leaf(tester, 'https://example.com/fenced').recognizer, + isA(), + ); + 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(), ); await _tapText(tester, 'https://example.com/bare'); expect(opened.map((uri) => '$uri'), ['https://example.com/bare']); diff --git a/app/test/features/chat/ui/transcript/chat_links_test.dart b/app/test/features/chat/ui/transcript/chat_links_test.dart index 22bc29ce..61ad9d7e 100644 --- a/app/test/features/chat/ui/transcript/chat_links_test.dart +++ b/app/test/features/chat/ui/transcript/chat_links_test.dart @@ -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', () { diff --git a/app/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart b/app/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart index 60503e5b..db5f2d61 100644 --- a/app/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart +++ b/app/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart @@ -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); diff --git a/app/test/features/chat/ui/transcript/chat_markdown_view_test.dart b/app/test/features/chat/ui/transcript/chat_markdown_view_test.dart index 9f2bab77..dce22370 100644 --- a/app/test/features/chat/ui/transcript/chat_markdown_view_test.dart +++ b/app/test/features/chat/ui/transcript/chat_markdown_view_test.dart @@ -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( diff --git a/app/tool/perf/chat_scroll_fixture.dart b/app/tool/perf/chat_scroll_fixture.dart index f09946f9..46013643 100644 --- a/app/tool/perf/chat_scroll_fixture.dart +++ b/app/tool/perf/chat_scroll_fixture.dart @@ -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';