Couple of tweaks to the scrape package.

- Trim output lines to 80 characters. This makes it overwrite the line
  correctly instead of scrolling unnecessarily.

- Make the tokens for each parsed file available to a scrape class.

Change-Id: Ib986ec548ac3f8ba1c3c55b1a00673f49d45c0d0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280055
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Robert Nystrom 2023-02-01 22:40:38 +00:00 committed by Commit Queue
parent 6829a9de37
commit 9057fcbc11
2 changed files with 24 additions and 5 deletions

View file

@ -291,8 +291,11 @@ class Scrape {
print(line);
} else {
// Overwrite the same line.
var trimmed = shortPath;
if (trimmed.length > 80) trimmed = trimmed.substring(0, 80);
stdout.write('\u001b[2K\r'
'[$_scrapedFileCount files, $_scrapedLineCount lines] $shortPath');
'[$_scrapedFileCount files, $_scrapedLineCount lines] $trimmed');
_needClearLine = true;
}
}
@ -316,7 +319,7 @@ class Scrape {
for (var visitorFactory in _visitorFactories) {
var visitor = visitorFactory();
bindVisitor(visitor, this, shortPath, source, lineInfo);
bindVisitor(visitor, this, shortPath, source, startToken, lineInfo);
node.accept(visitor);
}
}

View file

@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/source/line_info.dart';
@ -16,10 +17,11 @@ import '../scrape.dart';
/// [ScrapeVisitor] constructor so that subclasses of [ScrapeVisitor] don't
/// need to define a pass-through constructor.
void bindVisitor(ScrapeVisitor visitor, Scrape scrape, String path,
String source, LineInfo info) {
String source, Token startToken, LineInfo info) {
visitor._scrape = scrape;
visitor._path = path;
visitor._source = source;
visitor._startToken = startToken;
visitor.lineInfo = info;
}
@ -29,6 +31,7 @@ class ScrapeVisitor extends RecursiveAstVisitor<void> {
late final Scrape _scrape;
late final String _path;
late final String _source;
late final Token _startToken;
late final LineInfo lineInfo;
/// How many levels deep the visitor is currently nested inside build methods.
@ -36,6 +39,8 @@ class ScrapeVisitor extends RecursiveAstVisitor<void> {
String get path => _path;
Token get startToken => _startToken;
// TODO(rnystrom): Remove this in favor of using surveyor for these kinds of
// analyses.
/// Whether the visitor is currently inside Flutter's "build" method,
@ -72,11 +77,22 @@ class ScrapeVisitor extends RecursiveAstVisitor<void> {
log(nodeToString(node));
}
/// Print the line containing [token].
void printToken(Token token) {
log(_rangeToString(token.offset, token.end));
}
/// Generate a nice string representation of [node] include file path and
/// line information.
String nodeToString(AstNode node) {
var startLine = lineInfo.getLocation(node.offset).lineNumber;
var endLine = lineInfo.getLocation(node.end).lineNumber;
return _rangeToString(node.offset, node.end);
}
/// Generate a nice string representation of [node] include file path and
/// line information.
String _rangeToString(int start, int end) {
var startLine = lineInfo.getLocation(start).lineNumber;
var endLine = lineInfo.getLocation(end).lineNumber;
startLine = startLine.clamp(0, lineInfo.lineCount - 1);
endLine = endLine.clamp(0, lineInfo.lineCount - 1);