Skip to content

Commit 5499677

Browse files
devoncarewkevmoo
andauthored
add an api usage example (dart-archive/source_span#92)
* add an api usage example * Apply suggestions from code review Co-authored-by: Kevin Moore <[email protected]> * make method private --------- Co-authored-by: Kevin Moore <[email protected]>
1 parent 8a9307e commit 5499677

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

pkgs/source_span/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1.9.2-dev
22

33
* Require Dart 2.18
4+
* Add an API usage example in `example/`.
45

56
# 1.9.1
67

@@ -201,7 +202,7 @@
201202
# 1.0.0
202203

203204
This package was extracted from the
204-
[`source_maps`](http://pub.dartlang.org/packages/source_maps) package, but the
205+
[`source_maps`](https://pub.dev/packages/source_maps) package, but the
205206
API has many differences. Among them:
206207

207208
* `Span` has been renamed to `SourceSpan` and `Location` has been renamed to

pkgs/source_span/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
[![pub package](https://img.shields.io/pub/v/source_span.svg)](https://pub.dev/packages/source_span)
33
[![package publisher](https://img.shields.io/pub/publisher/source_span.svg)](https://pub.dev/packages/source_span/publisher)
44

5+
## About this package
6+
57
`source_span` is a library for tracking locations in source code. It's designed
68
to provide a standard representation for source code locations and spans so that
79
disparate packages can easily pass them among one another, and to make it easy

pkgs/source_span/example/main.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:source_span/source_span.dart';
8+
9+
void main(List<String> args) {
10+
final file = File('README.md');
11+
final contents = file.readAsStringSync();
12+
13+
final sourceFile = SourceFile.fromString(contents, url: file.uri);
14+
final spans = _parseFile(contents, sourceFile);
15+
16+
for (var span in spans.take(30)) {
17+
print('[${span.start.line + 1}:${span.start.column + 1}] ${span.text}');
18+
}
19+
}
20+
21+
Iterable<SourceSpan> _parseFile(String contents, SourceFile sourceFile) sync* {
22+
var wordStart = 0;
23+
var inWhiteSpace = true;
24+
25+
for (var i = 0; i < contents.length; i++) {
26+
final codeUnit = contents.codeUnitAt(i);
27+
28+
if (codeUnit == _eol || codeUnit == _space) {
29+
if (!inWhiteSpace) {
30+
inWhiteSpace = true;
31+
32+
// emit a word
33+
yield sourceFile.span(wordStart, i);
34+
}
35+
} else {
36+
if (inWhiteSpace) {
37+
inWhiteSpace = false;
38+
39+
wordStart = i;
40+
}
41+
}
42+
}
43+
44+
if (!inWhiteSpace) {
45+
// emit a word
46+
yield sourceFile.span(wordStart, contents.length);
47+
}
48+
}
49+
50+
const int _eol = 10;
51+
const int _space = 32;

pkgs/source_span/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: source_span
22
version: 1.9.2-dev
3-
description: A library for identifying source spans and locations.
3+
description: >-
4+
Provides a standard representation for source code locations and spans.
45
repository: https://github.com/dart-lang/source_span
56

67
environment:

0 commit comments

Comments
 (0)