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:
@@ -8,8 +8,8 @@ import 'chat_links.dart';
|
|||||||
import 'chat_transcript_selection.dart';
|
import 'chat_transcript_selection.dart';
|
||||||
|
|
||||||
/// Plain selectable text with its bare links made tappable: the operator's
|
/// Plain selectable text with its bare links made tappable: the operator's
|
||||||
/// own message keeps every character exactly as typed (no markdown), and only
|
/// own message keeps every character exactly as typed (no markdown), and its
|
||||||
/// `http(s)://`/`www.` runs outside `` `code` `` become links. Text without a
|
/// `http(s)://`/`www.` runs, in `` `code` `` too, become links. Text without a
|
||||||
/// link stays a plain [SelectableText] outside a shared transcript document.
|
/// link stays a plain [SelectableText] outside a shared transcript document.
|
||||||
class ChatLinkText extends StatefulWidget {
|
class ChatLinkText extends StatefulWidget {
|
||||||
const ChatLinkText(this.text, {super.key});
|
const ChatLinkText(this.text, {super.key});
|
||||||
|
|||||||
@@ -118,25 +118,13 @@ String? autolinkTarget(String inner) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bare links in plain (non-markdown) text, outside `` `code` `` spans and
|
/// Bare links anywhere in [text]: plain (non-markdown) text, or the body of a
|
||||||
/// ```` ``` ```` fences, so an operator's pasted command is never linkified.
|
/// `` `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) {
|
List<ChatTextLink> findTextLinks(String text) {
|
||||||
final links = <ChatTextLink>[];
|
final links = <ChatTextLink>[];
|
||||||
var i = 0;
|
var i = 0;
|
||||||
while (i < text.length) {
|
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);
|
final url = bareLinkAt(text, i);
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
links.add(ChatTextLink(i, i + url.length, url));
|
links.add(ChatTextLink(i, i + url.length, url));
|
||||||
|
|||||||
@@ -265,8 +265,14 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
|||||||
padding: const EdgeInsets.all(12),
|
padding: const EdgeInsets.all(12),
|
||||||
child: ChatTranscriptHorizontalScrollView(
|
child: ChatTranscriptHorizontalScrollView(
|
||||||
label: 'code block',
|
label: 'code block',
|
||||||
child: Text(
|
child: Text.rich(
|
||||||
code,
|
TextSpan(
|
||||||
|
children: _linked(
|
||||||
|
context,
|
||||||
|
code,
|
||||||
|
(part) => TextSpan(text: part),
|
||||||
|
),
|
||||||
|
),
|
||||||
style: const TextStyle(fontFamily: appMonospaceFamily),
|
style: const TextStyle(fontFamily: appMonospaceFamily),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -378,23 +384,60 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
|||||||
MarkdownSourceCursor source,
|
MarkdownSourceCursor source,
|
||||||
) {
|
) {
|
||||||
final token = text;
|
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(
|
return TextSpan(
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontFamily: appMonospaceFamily,
|
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: _linked(context, text, piece),
|
||||||
children: highlighted.children,
|
|
||||||
onEnter: (event) {
|
|
||||||
if (_route?.isCurrent != false) {
|
|
||||||
_codeHover.show(context, token, text, event.position);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onExit: (_) => _codeHover.scheduleHide(token),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// [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(
|
InlineSpan _link(
|
||||||
BuildContext context,
|
BuildContext context,
|
||||||
String url,
|
String url,
|
||||||
@@ -404,11 +447,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
|||||||
context,
|
context,
|
||||||
url,
|
url,
|
||||||
_spans(context, spans, source),
|
_spans(context, spans, source),
|
||||||
onOpen: (uri) {
|
onOpen: (uri) => _open(context, uri),
|
||||||
final onOpenLink = widget.onOpenLink;
|
|
||||||
if (onOpenLink != null) return onOpenLink(uri.toString());
|
|
||||||
unawaited(openChatLink(context, uri));
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/// A leaf's spans, recording where it renders so a spoken word in it can
|
/// 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;
|
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) =>
|
Color _codeFill(BuildContext context) =>
|
||||||
Theme.of(context).colorScheme.surfaceContainerHighest;
|
Theme.of(context).colorScheme.surfaceContainerHighest;
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ Future<void> _tapText(WidgetTester tester, String text) async {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('assistant links open through the launcher; code stays plain', (
|
testWidgets('assistant links, in code too, open through the launcher', (
|
||||||
tester,
|
tester,
|
||||||
) async {
|
) async {
|
||||||
await phoneViewport(tester);
|
await phoneViewport(tester);
|
||||||
@@ -75,16 +75,20 @@ void main() {
|
|||||||
expect(_leaf(tester, text).recognizer, isA<TapGestureRecognizer>());
|
expect(_leaf(tester, text).recognizer, isA<TapGestureRecognizer>());
|
||||||
}
|
}
|
||||||
expect(_leaf(tester, 'this').recognizer, isNull);
|
expect(_leaf(tester, 'this').recognizer, isNull);
|
||||||
expect(_leaf(tester, 'https://example.com/inline').recognizer, isNull);
|
|
||||||
expect(
|
expect(
|
||||||
find.byWidgetPredicate(
|
_leaf(tester, 'https://example.com/inline').recognizer,
|
||||||
(widget) =>
|
isA<TapGestureRecognizer>(),
|
||||||
widget is Text &&
|
|
||||||
widget.data == 'curl https://example.com/fenced' &&
|
|
||||||
widget.textSpan == null,
|
|
||||||
),
|
|
||||||
findsOneWidget,
|
|
||||||
);
|
);
|
||||||
|
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, 'the docs');
|
||||||
await _tapText(tester, 'www.example.org');
|
await _tapText(tester, 'www.example.org');
|
||||||
@@ -93,6 +97,7 @@ void main() {
|
|||||||
expect(opened.map((uri) => '$uri'), [
|
expect(opened.map((uri) => '$uri'), [
|
||||||
'https://example.com/docs',
|
'https://example.com/docs',
|
||||||
'https://www.example.org',
|
'https://www.example.org',
|
||||||
|
'https://example.com/inline',
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -301,11 +306,8 @@ void main() {
|
|||||||
);
|
);
|
||||||
expect(_leaf(tester, 'https://example.com/bare').recognizer, isNotNull);
|
expect(_leaf(tester, 'https://example.com/bare').recognizer, isNotNull);
|
||||||
expect(
|
expect(
|
||||||
spansOf(tester, 'https://code.com')
|
_leaf(tester, 'https://code.com').recognizer,
|
||||||
.where((span) => span.text?.contains('https://code.com') ?? false)
|
isA<TapGestureRecognizer>(),
|
||||||
.single
|
|
||||||
.recognizer,
|
|
||||||
isNull,
|
|
||||||
);
|
);
|
||||||
await _tapText(tester, 'https://example.com/bare');
|
await _tapText(tester, 'https://example.com/bare');
|
||||||
expect(opened.map((uri) => '$uri'), ['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);
|
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'), [
|
expect(_targets('run `curl https://x.com/a` then https://y.com/b'), [
|
||||||
|
'https://x.com/a',
|
||||||
'https://y.com/b',
|
'https://y.com/b',
|
||||||
]);
|
]);
|
||||||
expect(_targets('```\ncurl https://x.com/a\n```\nhttps://y.com/b'), [
|
expect(_targets('```\ncurl https://x.com/a\n```\nhttps://y.com/b'), [
|
||||||
|
'https://x.com/a',
|
||||||
'https://y.com/b',
|
'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', () {
|
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 context = tester.element(find.byType(ChatMarkdownView));
|
||||||
final fill = Theme.of(context).colorScheme.surfaceContainerHighest;
|
final fill = Theme.of(context).colorScheme.surfaceContainerHighest;
|
||||||
final spans = spansOf(tester, 'Use');
|
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(
|
final plain = spans.firstWhere(
|
||||||
(span) => span.text == ' in the middle of this sentence.',
|
(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(plain.style?.backgroundColor, isNull);
|
||||||
expect(code.style?.fontSize, isNull);
|
expect(code.style?.fontSize, isNull);
|
||||||
expect(code.style?.fontFamily, appMonospaceFamily);
|
expect(code.style?.fontFamily, appMonospaceFamily);
|
||||||
|
|||||||
@@ -57,9 +57,10 @@ void main() {
|
|||||||
final inlineCode = paragraph.firstWhere(
|
final inlineCode = paragraph.firstWhere(
|
||||||
(span) => span.toPlainText() == 'inline',
|
(span) => span.toPlainText() == 'inline',
|
||||||
);
|
);
|
||||||
|
// Half the code fill, so a selection behind it stays visible.
|
||||||
expect(
|
expect(
|
||||||
inlineCode.style?.backgroundColor,
|
inlineCode.style?.backgroundColor,
|
||||||
theme.colorScheme.surfaceContainerHighest,
|
theme.colorScheme.surfaceContainerHighest.withValues(alpha: 0.5),
|
||||||
);
|
);
|
||||||
final plain = paragraph.firstWhere(
|
final plain = paragraph.firstWhere(
|
||||||
(span) => span.toPlainText() == ' and snake_case_name.',
|
(span) => span.toPlainText() == ' and snake_case_name.',
|
||||||
@@ -79,7 +80,8 @@ void main() {
|
|||||||
final code = find.byWidgetPredicate(
|
final code = find.byWidgetPredicate(
|
||||||
(widget) =>
|
(widget) =>
|
||||||
widget is Text &&
|
widget is Text &&
|
||||||
widget.data == 'const answer = 42;\n keep spaces',
|
widget.textSpan?.toPlainText() ==
|
||||||
|
'const answer = 42;\n keep spaces',
|
||||||
);
|
);
|
||||||
expect(code, findsOneWidget);
|
expect(code, findsOneWidget);
|
||||||
final scroll = find.ancestor(
|
final scroll = find.ancestor(
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
// answers: tool-only turns ("Used 9 tools", "Ran 1 command",
|
// answers: tool-only turns ("Used 9 tools", "Ran 1 command",
|
||||||
// one with a failed command) split by background-task notices;
|
// one with a failed command) split by background-task notices;
|
||||||
// `?links=1` answers and operator turns carry a markdown link, an
|
// `?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.
|
// `?timestamps=1` messages carry recorded times over two calendar days.
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user