Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions lib/src/code_field/code_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class _CodeFieldState extends State<CodeField> {
FocusNode? _focusNode;
String? lines;
String longestLine = '';
// Track the available width of the code column in order to compute
// visual line wrapping for line-numbers.
double _codeColumnWidth = 0;
var _currentTextStyle = const TextStyle();

@override
void initState() {
Expand Down Expand Up @@ -186,8 +190,34 @@ class _CodeFieldState extends State<CodeField> {
final str = widget.controller.text.split('\n');
final buf = <String>[];

for (var k = 0; k < str.length; k++) {
buf.add((k + 1).toString());
if (widget.wrap && _codeColumnWidth > 0) {
// When wrapping is enabled we need to account for visual lines that
// are created by the soft wrap. We measure each logical line using a
// TextPainter and add blank placeholders so the scrolling offset of
// the linked controllers stays in sync.
for (var k = 0; k < str.length; k++) {
final logicalLine = str[k];

// Compute how many visual lines this logical line occupies.
final tp = TextPainter(
text: TextSpan(text: logicalLine, style: _currentTextStyle),
textDirection: TextDirection.ltr,
)..layout(maxWidth: _codeColumnWidth);
final visualLines = tp.computeLineMetrics().length;

// First visual line gets the real line number.
buf.add((k + 1).toString());

// Remaining visual lines get an empty placeholder so that the
// total number of lines matches the code field.
for (int v = 1; v < visualLines; v++) {
buf.add('');
}
}
} else {
for (var k = 0; k < str.length; k++) {
buf.add((k + 1).toString());
}
}

_numberController?.text = buf.join('\n');
Expand Down Expand Up @@ -370,7 +400,28 @@ class _CodeFieldState extends State<CodeField> {
textSelectionTheme: widget.textSelectionTheme,
),
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
builder: (context, constraints) {
// Save the textStyle and available width so _onTextChanged can
// accurately compute the amount of visual lines when wrapping.
_currentTextStyle = textStyle;
// Subtract left padding that will be applied inside the
// SingleChildScrollView when horizontal scrolling is disabled.
var availableWidth = constraints.maxWidth;
if (widget.lineNumbers) {
availableWidth -= widget.lineNumberStyle.width;
}

if ((availableWidth - _codeColumnWidth).abs() > 0.5) {
_codeColumnWidth = max(0, availableWidth);
// Recalculate the line numbers because wrapping may have
// changed due to a width change.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
_onTextChanged();
}
});
}

// Control horizontal scrolling
return widget.wrap
? codeField
Expand Down
19 changes: 12 additions & 7 deletions lib/src/line_numbers/line_number_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ class LineNumberController extends TextEditingController {

for (int k = 0; k < list.length; k++) {
final el = list[k];
final number = int.parse(el);
var textSpan = TextSpan(text: el, style: style);

if (lineNumberBuilder != null) {
textSpan = lineNumberBuilder!(number, style);
// Blank lines are placeholders inserted to align with wrapped
// visual lines. They should render as empty text spans to
// preserve vertical spacing.
if (el.trim().isEmpty) {
children.add(TextSpan(text: '', style: style));
} else {
final number = int.tryParse(el) ?? 0;
var textSpan = TextSpan(text: el, style: style);
if (lineNumberBuilder != null && number != 0) {
textSpan = lineNumberBuilder!(number, style);
}
children.add(textSpan);
}

children.add(textSpan);
if (k < list.length - 1) {
children.add(const TextSpan(text: '\n'));
}
Expand Down