From 9057fcbc11179fcb77d2338325372f6f23762a51 Mon Sep 17 00:00:00 2001 From: Robert Nystrom Date: Wed, 1 Feb 2023 22:40:38 +0000 Subject: [PATCH] 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 Commit-Queue: Bob Nystrom Auto-Submit: Bob Nystrom --- pkg/scrape/lib/scrape.dart | 7 +++++-- pkg/scrape/lib/src/scrape_visitor.dart | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/scrape/lib/scrape.dart b/pkg/scrape/lib/scrape.dart index 19c719c40fd..28c49ae70a9 100644 --- a/pkg/scrape/lib/scrape.dart +++ b/pkg/scrape/lib/scrape.dart @@ -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); } } diff --git a/pkg/scrape/lib/src/scrape_visitor.dart b/pkg/scrape/lib/src/scrape_visitor.dart index 789ed3ae28e..ffd3bb0fb7d 100644 --- a/pkg/scrape/lib/src/scrape_visitor.dart +++ b/pkg/scrape/lib/src/scrape_visitor.dart @@ -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 { 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 { 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 { 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);