add an api usage example (dart-lang/source_span#92)
* add an api usage example
* Apply suggestions from code review
Co-authored-by: Kevin Moore <kevmoo@users.noreply.github.com>
* make method private
---------
Co-authored-by: Kevin Moore <kevmoo@users.noreply.github.com>
diff --git a/pkgs/source_span/CHANGELOG.md b/pkgs/source_span/CHANGELOG.md
index 05c7e71..fa1735a 100644
--- a/pkgs/source_span/CHANGELOG.md
+++ b/pkgs/source_span/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.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/pkgs/source_span/README.md b/pkgs/source_span/README.md
index be91deb..0faf0cb 100644
--- a/pkgs/source_span/README.md
+++ b/pkgs/source_span/README.md
@@ -2,6 +2,8 @@
[](https://pub.dev/packages/source_span)
[](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/pkgs/source_span/example/main.dart b/pkgs/source_span/example/main.dart
new file mode 100644
index 0000000..e296765
--- /dev/null
+++ b/pkgs/source_span/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<String> 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<SourceSpan> _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/pkgs/source_span/pubspec.yaml b/pkgs/source_span/pubspec.yaml
index 4531a74..d00730c 100644
--- a/pkgs/source_span/pubspec.yaml
+++ b/pkgs/source_span/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: