Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

add an api usage example #92

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 1.9.2-dev

* Require Dart 2.18
* Add an API usage example in `example/`.

# 1.9.1

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down