Skip to content

Commit

Permalink
refactor: expose LineInfo on Source (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Jun 12, 2022
1 parent e76ccf9 commit bf94358
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
5 changes: 2 additions & 3 deletions packages/daco/README.md
Expand Up @@ -7,9 +7,8 @@ A tool for maintaining **Da**rt **co**mments (daco).
## TODO

- [ ] Support standalone statements in embedded Dart code
- [ ] Support formatting simple comments
- [ ] Support disabling of formatting for a comment
- [ ] Fix location of fenced code blocks in embedded Dart code
- [ ] Support formatting of end of line comments
- [ ] Support disabling formatting for a comment

## Ideas

Expand Down
57 changes: 29 additions & 28 deletions packages/daco/lib/src/formatter.dart
@@ -1,7 +1,6 @@
// ignore_for_file: parameter_assignments

import 'package:analyzer/error/error.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart';

Expand Down Expand Up @@ -90,13 +89,38 @@ class DacoFormatter {
MarkdownSource source, {
required int lineLength,
}) async {
final formattedText = await _formatMarkdown(source, lineLength);
final formattedSource = MarkdownSource(text: formattedText);
final formattedCodeBlocks = <DartSource, String>{};

await Future.wait(
formattedSource.dartCodeBlocks().map((codeBlock) async {
if (formattedSource
.codeBlockTags(of: codeBlock)
.contains(_noFormatTag)) {
return;
}

formattedCodeBlocks[codeBlock] = await _formatDartSource(
codeBlock,
lineLength: formattedSource.availableLineLength(
of: codeBlock,
lineLength: lineLength,
),
);
}),
);

return formattedSource.replaceEnclosedSources(formattedCodeBlocks);
}

Future<String> _formatMarkdown(Source source, int lineLength) async {
var text = source.text;
final lineInfo = LineInfo.fromContent(text);
final tagMatches = _dartDocTagRegExp.allMatches(text);
final tagLines = <int>{};
for (final match in tagMatches) {
final startLine = lineInfo.getLocation(match.start).lineNumber;
final endLine = lineInfo.getLocation(match.end).lineNumber;
final startLine = source.lineInfo.getLocation(match.start).lineNumber;
final endLine = source.lineInfo.getLocation(match.end).lineNumber;
for (var line = startLine; line <= endLine; line++) {
// LineInfo returns 1-based line numbers, but we want 0-based.
tagLines.add(line - 1);
Expand All @@ -118,32 +142,9 @@ class DacoFormatter {
proseWrap: ProseWrap.always,
);

text = text.replaceAllMapped(
return text.replaceAllMapped(
_dartDocTagWithSpacerRegExp,
(match) => match.group(1)!,
);

final formattedSource = MarkdownSource(text: text);
final formattedCodeBlocks = <DartSource, String>{};

await Future.wait(
formattedSource.dartCodeBlocks().map((codeBlock) async {
if (formattedSource
.codeBlockTags(of: codeBlock)
.contains(_noFormatTag)) {
return;
}

formattedCodeBlocks[codeBlock] = await _formatDartSource(
codeBlock,
lineLength: formattedSource.availableLineLength(
of: codeBlock,
lineLength: lineLength,
),
);
}),
);

return formattedSource.replaceEnclosedSources(formattedCodeBlocks);
}
}
11 changes: 7 additions & 4 deletions packages/daco/lib/src/source.dart
Expand Up @@ -33,6 +33,9 @@ abstract class Source {
/// The text of this source.
String get text;

/// The [LineInfo] for this source's [text].
LineInfo get lineInfo;

/// The document which contains this source.
Document get document;

Expand Down Expand Up @@ -142,7 +145,8 @@ abstract class _AbstractSource extends Source {
final List<int>? _lineStartOffsets;

/// The [LineInfo] for [text].
late final _lineInfo = _provideLineInfo();
@override
late final lineInfo = _provideLineInfo();

LineInfo _provideLineInfo() => LineInfo.fromContent(text);

Expand All @@ -155,7 +159,7 @@ abstract class _AbstractSource extends Source {
}

if (targetSource == enclosingSource) {
final location = _lineInfo.getLocation(offset);
final location = lineInfo.getLocation(offset);
return _lineStartOffsets![location.lineNumber - 1] +
(location.columnNumber - 1);
}
Expand Down Expand Up @@ -210,7 +214,7 @@ class _DartSourceImpl extends _AbstractSource implements DartSource {
LineInfo _provideLineInfo() => _parsedStringResult.lineInfo;

int _commentIndentation(Comment comment) =>
_lineInfo.getLocation(comment.offset).columnNumber - 1;
lineInfo.getLocation(comment.offset).columnNumber - 1;

@override
List<Source> enclosedSources() => documentationComments();
Expand Down Expand Up @@ -402,7 +406,6 @@ class _MarkdownSourceImpl extends _AbstractSource implements MarkdownSource {
final tags = match.group(2)!;
final code = match.group(3)!;

final lineInfo = _lineInfo;
// LineInfo returns one-based line numbers and since the code starts
// on the line after the ```, we need don't need to subtract 1 from
// `lineNumber`.
Expand Down

0 comments on commit bf94358

Please sign in to comment.