From 444db2a373a3378b59c81caf34928c8a5f3d80f8 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Thu, 2 Feb 2023 16:33:05 -0800 Subject: [PATCH 1/3] add an api usage example --- CHANGELOG.md | 3 ++- README.md | 2 ++ example/main.dart | 51 +++++++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 3 ++- 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 example/main.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 05c7e71..0cff320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 1.9.2-dev * Require Dart 2.18 +* Add an API usage example in `example/`. # 1.9.1 @@ -201,7 +202,7 @@ # 1.0.0 This package was extracted from the -[`source_maps`](http://pub.dartlang.org/packages/source_maps) package, but the +[`source_maps`](https://pub.dartlang.org/packages/source_maps) package, but the API has many differences. Among them: * `Span` has been renamed to `SourceSpan` and `Location` has been renamed to diff --git a/README.md b/README.md index be91deb..0faf0cb 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [![pub package](https://img.shields.io/pub/v/source_span.svg)](https://pub.dev/packages/source_span) [![package publisher](https://img.shields.io/pub/publisher/source_span.svg)](https://pub.dev/packages/source_span/publisher) +## About this package + `source_span` is a library for tracking locations in source code. It's designed to provide a standard representation for source code locations and spans so that disparate packages can easily pass them among one another, and to make it easy diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 0000000..2d3c1b2 --- /dev/null +++ b/example/main.dart @@ -0,0 +1,51 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// 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 'dart:io'; + +import 'package:source_span/source_span.dart'; + +void main(List args) { + final file = File('README.md'); + final contents = file.readAsStringSync(); + + final sourceFile = SourceFile.fromString(contents, url: file.uri); + final spans = parseFile(contents, sourceFile); + + for (var span in spans.take(30)) { + print('[${span.start.line + 1}:${span.start.column + 1}] ${span.text}'); + } +} + +Iterable parseFile(String contents, SourceFile sourceFile) sync* { + var wordStart = 0; + var inWhiteSpace = true; + + for (var i = 0; i < contents.length; i++) { + final codeUnit = contents.codeUnitAt(i); + + if (codeUnit == _eol || codeUnit == _space) { + if (!inWhiteSpace) { + inWhiteSpace = true; + + // emit a word + yield sourceFile.span(wordStart, i); + } + } else { + if (inWhiteSpace) { + inWhiteSpace = false; + + wordStart = i; + } + } + } + + if (!inWhiteSpace) { + // emit a word + yield sourceFile.span(wordStart, contents.length); + } +} + +const int _eol = 10; +const int _space = 32; diff --git a/pubspec.yaml b/pubspec.yaml index 4531a74..fc6f97a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,7 @@ name: source_span version: 1.9.2-dev -description: A library for identifying source spans and locations. +description: > + Provides a standard representation for source code locations and spans. repository: https://github.com/dart-lang/source_span environment: From 1ae90e752dbc233f4544f3393fbda83ac984ac74 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Fri, 3 Feb 2023 12:10:04 -0800 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Kevin Moore --- CHANGELOG.md | 2 +- pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cff320..fa1735a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -202,7 +202,7 @@ # 1.0.0 This package was extracted from the -[`source_maps`](https://pub.dartlang.org/packages/source_maps) package, but the +[`source_maps`](https://pub.dev/packages/source_maps) package, but the API has many differences. Among them: * `Span` has been renamed to `SourceSpan` and `Location` has been renamed to diff --git a/pubspec.yaml b/pubspec.yaml index fc6f97a..d00730c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: source_span version: 1.9.2-dev -description: > +description: >- Provides a standard representation for source code locations and spans. repository: https://github.com/dart-lang/source_span From 04bebf1d7d72382100f6468906aa9535b154e028 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Fri, 3 Feb 2023 12:12:37 -0800 Subject: [PATCH 3/3] make method private --- example/main.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/main.dart b/example/main.dart index 2d3c1b2..e296765 100644 --- a/example/main.dart +++ b/example/main.dart @@ -11,14 +11,14 @@ void main(List args) { final contents = file.readAsStringSync(); final sourceFile = SourceFile.fromString(contents, url: file.uri); - final spans = parseFile(contents, sourceFile); + final spans = _parseFile(contents, sourceFile); for (var span in spans.take(30)) { print('[${span.start.line + 1}:${span.start.column + 1}] ${span.text}'); } } -Iterable parseFile(String contents, SourceFile sourceFile) sync* { +Iterable _parseFile(String contents, SourceFile sourceFile) sync* { var wordStart = 0; var inWhiteSpace = true;