Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(881)

Unified Diff: lib/src/span_with_context.dart

Issue 1041163005: Support multiple occurrences of text in context (Closed) Base URL: git@github.com:dart-lang/source_span.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« CHANGELOG.md ('K') | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/span_with_context.dart
diff --git a/lib/src/span_with_context.dart b/lib/src/span_with_context.dart
index edbb8b65ffe278f3084a4b8aa016c5b15df53a41..0a24e7171627bda34b6b3d9d2992cc635685f3c0 100644
--- a/lib/src/span_with_context.dart
+++ b/lib/src/span_with_context.dart
@@ -29,10 +29,20 @@ class SourceSpanWithContext extends SourceSpanBase {
'The context line "$context" must contain "$text".');
}
- var beginningOfLine = context.lastIndexOf('\n', index) + 1;
- if (start.column != index - beginningOfLine) {
- throw new ArgumentError('The span text "$text" must start at '
- 'column ${start.column + 1} in a line within "$context".');
+ // Note: `text` can appear in multiple places within context, but we don't
+ // know which occurrance of `text` is the one in the right location.
+ while (index != -1) {
+ var beginningOfLine = context.lastIndexOf('\n', index) + 1;
+
+ // if the column matches, we are all set with validation.
+ if (start.column == index - beginningOfLine) return;
+
+ index = context.indexOf(text, index + 1);
}
nweiz 2015/03/30 23:45:51 Shouldn't you also do this logic when constructing
Siggi Cherem (dart-lang) 2015/03/31 01:07:49 good point - fixed too.
+
+ // No match was found, means that all occurrences of `text` were not in the
+ // right place.
+ throw new ArgumentError('The span text "$text" must start at '
+ 'column ${start.column + 1} in a line within "$context".');
}
}
« CHANGELOG.md ('K') | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698