Index: lib/src/utils.dart |
diff --git a/lib/src/utils.dart b/lib/src/utils.dart |
index 09bc4300a810728e1606bcdf6d4e5c65349b3409..f813772aff76ec69002b1d04f65fa08ef7f6c8fe 100644 |
--- a/lib/src/utils.dart |
+++ b/lib/src/utils.dart |
@@ -14,12 +14,15 @@ import 'package:analyzer/src/generated/ast.dart' |
ExportDirective, |
PartDirective, |
CompilationUnit, |
- Identifier; |
+ Identifier, |
+ AnnotatedNode, |
+ AstNode; |
import 'package:analyzer/src/generated/engine.dart' |
show ParseDartTask, AnalysisContext; |
import 'package:analyzer/src/generated/source.dart' show Source; |
import 'package:analyzer/src/generated/element.dart'; |
import 'package:analyzer/analyzer.dart' show parseDirectives; |
+import 'package:source_span/source_span.dart'; |
bool isDartPrivateLibrary(LibraryElement library) { |
var uri = library.source.uri; |
@@ -232,3 +235,33 @@ class OutWriter { |
new File(_path).writeAsStringSync('$_sb'); |
} |
} |
+ |
+SourceLocation locationFor(CompilationUnit unit, Uri uri, int offset) { |
Jennifer Messerly
2015/03/03 02:24:22
not sure the "For" adds much. "getSourceLocation"?
Siggi Cherem (dart-lang)
2015/03/04 04:44:24
What do you think about locationForOffset?
Jennifer Messerly
2015/03/04 15:35:10
yeah that works too.
Just curious, is it intention
Siggi Cherem (dart-lang)
2015/03/04 17:38:16
funny - technically no, but every time I start nam
|
+ var lineInfo = unit.lineInfo.getLocation(offset); |
+ return new SourceLocation(offset, |
+ sourceUrl: uri, |
+ line: lineInfo.lineNumber - 1, |
+ column: lineInfo.columnNumber - 1); |
+} |
+ |
+SourceSpan spanFor(CompilationUnit unit, Uri uri, AstNode node) { |
+ var currentToken = node is AnnotatedNode |
+ ? node.firstTokenAfterCommentAndMetadata |
+ : node.beginToken; |
+ var begin = currentToken.offset; |
+ var endToken = node.endToken; |
+ var text = new StringBuffer(); |
Jennifer Messerly
2015/03/03 02:24:22
I presume there is a method to this madness :-)
g
Siggi Cherem (dart-lang)
2015/03/04 04:44:24
yeah, this was more twisted than it needed to be :
|
+ append(tok) { |
+ var offset = tok.offset; |
+ var diff = offset - begin - text.length; |
+ text.write(' ' * diff); |
+ text.write(tok.lexeme); |
+ } |
+ while (currentToken != endToken) { |
+ append(currentToken); |
+ currentToken = currentToken.next; |
+ } |
+ append(currentToken); |
+ return new SourceSpan( |
+ locationFor(unit, uri, begin), locationFor(unit, uri, node.end), '$text'); |
+} |