This repository was archived by the owner on Jan 14, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +57
-2
lines changed
Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Original file line number Diff line number Diff line change 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
201202# 1.0.0
202203
203204This 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
205206API has many differences. Among them:
206207
207208* ` Span ` has been renamed to ` SourceSpan ` and ` Location ` has been renamed to
Original file line number Diff line number Diff line change 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
68to provide a standard representation for source code locations and spans so that
79disparate packages can easily pass them among one another, and to make it easy
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 11name : source_span
22version : 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.
45repository : https://github.com/dart-lang/source_span
56
67environment :
You can’t perform that action at this time.
0 commit comments