|
5 | 5 | import 'package:flutter/widgets.dart'; |
6 | 6 | import '_utils_desktop.dart' if (dart.library.js_interop) '_utils_web.dart'; |
7 | 7 |
|
8 | | -/// Converts a [dartDocText] String into a [RichText] widget. |
9 | | -/// |
10 | | -/// Removes any brackets and backticks and displays the text inside them as |
11 | | -/// fixed font. |
12 | | -RichText convertDartDocToRichText( |
13 | | - String dartDocText, { |
14 | | - required TextStyle regularFontStyle, |
15 | | - required TextStyle fixedFontStyle, |
16 | | -}) { |
17 | | - final children = <TextSpan>[]; |
18 | | - int currentIndex = 0; |
| 8 | +/// Converts a [dartDocText] String into a [Text] widget. |
| 9 | +class DartDocConverter { |
| 10 | + DartDocConverter(this.dartDocText); |
19 | 11 |
|
20 | | - while (currentIndex < dartDocText.length) { |
21 | | - final openBracketIndex = dartDocText.indexOf('[', currentIndex); |
22 | | - final openBacktickIndex = dartDocText.indexOf('`', currentIndex); |
| 12 | + final String dartDocText; |
23 | 13 |
|
24 | | - int nextSpecialCharIndex = -1; |
25 | | - bool isLink = false; |
| 14 | + /// Converts the [dartDocText] String into a [Text] widget. |
| 15 | + /// |
| 16 | + /// Removes any brackets and backticks and displays the text inside them with |
| 17 | + /// [fixedFontStyle]. All other text uses [regularFontStyle]. |
| 18 | + Text toText({ |
| 19 | + required TextStyle regularFontStyle, |
| 20 | + required TextStyle fixedFontStyle, |
| 21 | + }) { |
| 22 | + final children = toTextSpans( |
| 23 | + regularFontStyle: regularFontStyle, |
| 24 | + fixedFontStyle: fixedFontStyle, |
| 25 | + ); |
| 26 | + return Text.rich(TextSpan(children: children)); |
| 27 | + } |
26 | 28 |
|
27 | | - if (openBracketIndex != -1 && |
28 | | - (openBacktickIndex == -1 || openBracketIndex < openBacktickIndex)) { |
29 | | - nextSpecialCharIndex = openBracketIndex; |
30 | | - isLink = true; |
31 | | - } else if (openBacktickIndex != -1 && |
32 | | - (openBracketIndex == -1 || openBacktickIndex < openBracketIndex)) { |
33 | | - nextSpecialCharIndex = openBacktickIndex; |
34 | | - } |
| 29 | + @visibleForTesting |
| 30 | + List<TextSpan> toTextSpans({ |
| 31 | + required TextStyle regularFontStyle, |
| 32 | + required TextStyle fixedFontStyle, |
| 33 | + }) { |
| 34 | + final children = <TextSpan>[]; |
| 35 | + int currentIndex = 0; |
35 | 36 |
|
36 | | - if (nextSpecialCharIndex == -1) { |
37 | | - // No more special characters, add the remaining text. |
38 | | - children.add( |
39 | | - TextSpan( |
40 | | - text: dartDocText.substring(currentIndex), |
41 | | - style: regularFontStyle, |
42 | | - ), |
43 | | - ); |
44 | | - break; |
45 | | - } |
| 37 | + while (currentIndex < dartDocText.length) { |
| 38 | + final openBracketIndex = dartDocText.indexOf('[', currentIndex); |
| 39 | + final openBacktickIndex = dartDocText.indexOf('`', currentIndex); |
46 | 40 |
|
47 | | - // Add text before the special character. |
48 | | - children.add( |
49 | | - TextSpan( |
50 | | - text: dartDocText.substring(currentIndex, nextSpecialCharIndex), |
51 | | - style: regularFontStyle, |
52 | | - ), |
53 | | - ); |
| 41 | + int nextSpecialCharIndex = -1; |
| 42 | + bool isLink = false; |
54 | 43 |
|
55 | | - final closeIndex = dartDocText.indexOf( |
56 | | - isLink ? ']' : '`', |
57 | | - isLink ? nextSpecialCharIndex : nextSpecialCharIndex + 1, |
58 | | - ); |
59 | | - if (closeIndex == -1) { |
60 | | - // Treat unmatched brackets/backticks as regular text. |
| 44 | + if (openBracketIndex != -1 && |
| 45 | + (openBacktickIndex == -1 || openBracketIndex < openBacktickIndex)) { |
| 46 | + nextSpecialCharIndex = openBracketIndex; |
| 47 | + isLink = true; |
| 48 | + } else if (openBacktickIndex != -1 && |
| 49 | + (openBracketIndex == -1 || openBacktickIndex < openBracketIndex)) { |
| 50 | + nextSpecialCharIndex = openBacktickIndex; |
| 51 | + } |
| 52 | + |
| 53 | + if (nextSpecialCharIndex == -1) { |
| 54 | + // No more special characters, add the remaining text. |
| 55 | + children.add( |
| 56 | + TextSpan( |
| 57 | + text: dartDocText.substring(currentIndex), |
| 58 | + style: regularFontStyle, |
| 59 | + ), |
| 60 | + ); |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + // Add text before the special character. |
61 | 65 | children.add( |
62 | 66 | TextSpan( |
63 | | - text: dartDocText.substring(nextSpecialCharIndex), |
| 67 | + text: dartDocText.substring(currentIndex, nextSpecialCharIndex), |
64 | 68 | style: regularFontStyle, |
65 | 69 | ), |
66 | 70 | ); |
67 | | - currentIndex = dartDocText.length; // Effectively break the loop. |
68 | | - } else { |
69 | | - final content = dartDocText.substring( |
70 | | - nextSpecialCharIndex + 1, |
71 | | - closeIndex, |
| 71 | + |
| 72 | + final closeIndex = dartDocText.indexOf( |
| 73 | + isLink ? ']' : '`', |
| 74 | + isLink ? nextSpecialCharIndex : nextSpecialCharIndex + 1, |
72 | 75 | ); |
73 | | - children.add(TextSpan(text: content, style: fixedFontStyle)); |
74 | | - currentIndex = closeIndex + 1; |
| 76 | + if (closeIndex == -1) { |
| 77 | + // Treat unmatched brackets/backticks as regular text. |
| 78 | + children.add( |
| 79 | + TextSpan( |
| 80 | + text: dartDocText.substring(nextSpecialCharIndex), |
| 81 | + style: regularFontStyle, |
| 82 | + ), |
| 83 | + ); |
| 84 | + currentIndex = dartDocText.length; // Effectively break the loop. |
| 85 | + } else { |
| 86 | + final content = dartDocText.substring( |
| 87 | + nextSpecialCharIndex + 1, |
| 88 | + closeIndex, |
| 89 | + ); |
| 90 | + children.add(TextSpan(text: content, style: fixedFontStyle)); |
| 91 | + currentIndex = closeIndex + 1; |
| 92 | + } |
75 | 93 | } |
| 94 | + return children; |
76 | 95 | } |
77 | | - |
78 | | - return RichText(text: TextSpan(children: children)); |
79 | 96 | } |
80 | 97 |
|
81 | 98 | /// Workaround to force reload the Property Editor when it disconnects. |
|
0 commit comments