Skip to content

Paragraph.getLineBoundary doesn't include newline characters #83392

@suragch

Description

@suragch

The documentation for Paragraph.getLineBoundary says:

/// Returns the [TextRange] of the line at the given [TextPosition].
///
/// The newline (if any) is returned as part of the range.
///
/// Not valid until after layout.
///
/// This can potentially be expensive, since it needs to compute the line
/// metrics, so use it sparingly.
TextRange getLineBoundary(TextPosition position)

Specifically it says a newline is returned as part of the range. That doesn't seem to be the case, though.

test('getLineBoundary includes newline characters', () {
  const text = 'aaa\nbbb';

  final paragraphStyle = ui.ParagraphStyle(
    textDirection: ui.TextDirection.ltr,
  );
  final paragraphBuilder = ui.ParagraphBuilder(paragraphStyle)
    ..addText(text);
  final constraints = ui.ParagraphConstraints(width: 1000);
  final paragraph = paragraphBuilder.build();
  paragraph.layout(constraints);

  var range = paragraph.getLineBoundary(const TextPosition(offset: 0));
  expect(
    range.textInside(text),
    equals('aaa\n'),  // actual: aaa
  );
  expect(range.start, 0);
  expect(range.end, 4); // actual: 3

  range = paragraph.getLineBoundary(const TextPosition(offset: 5));
  expect(
    range.textInside(text),
    equals('bbb'),
  );
  expect(range.start, 4);
  expect(range.end, 7);
});

This test does not pass. The newline char is not included in the first line (or the second line). It seems that either the code or the documentation needs to be updated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    d: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions