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

Side by Side Diff: pkg/compiler/lib/src/io/source_map_builder.dart

Issue 2788373002: Add Source.getTextLine and use it to display source snippets in error messages. (Closed)
Patch Set: dartfmt Created 3 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library dart2js.source_map_builder; 5 library dart2js.source_map_builder;
6 6
7 import 'package:kernel/ast.dart' show Location;
7 import '../../compiler_new.dart' show OutputSink, OutputType; 8 import '../../compiler_new.dart' show OutputSink, OutputType;
8 import '../util/uri_extras.dart' show relativize; 9 import '../util/uri_extras.dart' show relativize;
9 import '../util/util.dart'; 10 import '../util/util.dart';
10 import 'line_column_provider.dart'; 11 import 'location_provider.dart';
11 import 'code_output.dart' show SourceLocationsProvider, SourceLocations; 12 import 'code_output.dart' show SourceLocationsProvider, SourceLocations;
12 import 'source_information.dart' show SourceLocation; 13 import 'source_information.dart' show SourceLocation;
13 14
14 class SourceMapBuilder { 15 class SourceMapBuilder {
15 final String version; 16 final String version;
16 17
17 /// The URI of the source map file. 18 /// The URI of the source map file.
18 final Uri sourceMapUri; 19 final Uri sourceMapUri;
19 20
20 /// The URI of the target language file. 21 /// The URI of the target language file.
21 final Uri targetFileUri; 22 final Uri targetFileUri;
22 23
23 final LineColumnProvider lineColumnProvider; 24 final LocationProvider locationProvider;
24 final List<SourceMapEntry> entries = new List<SourceMapEntry>(); 25 final List<SourceMapEntry> entries = new List<SourceMapEntry>();
25 26
26 SourceMapBuilder(this.version, this.sourceMapUri, this.targetFileUri, 27 SourceMapBuilder(this.version, this.sourceMapUri, this.targetFileUri,
27 this.lineColumnProvider); 28 this.locationProvider);
28 29
29 void addMapping(int targetOffset, SourceLocation sourceLocation) { 30 void addMapping(int targetOffset, SourceLocation sourceLocation) {
30 entries.add(new SourceMapEntry(sourceLocation, targetOffset)); 31 entries.add(new SourceMapEntry(sourceLocation, targetOffset));
31 } 32 }
32 33
33 void printStringListOn(Iterable<String> strings, StringBuffer buffer) { 34 void printStringListOn(Iterable<String> strings, StringBuffer buffer) {
34 bool first = true; 35 bool first = true;
35 buffer.write('['); 36 buffer.write('[');
36 for (String string in strings) { 37 for (String string in strings) {
37 if (!first) buffer.write(','); 38 if (!first) buffer.write(',');
38 buffer.write('"'); 39 buffer.write('"');
39 writeJsonEscapedCharsOn(string, buffer); 40 writeJsonEscapedCharsOn(string, buffer);
40 buffer.write('"'); 41 buffer.write('"');
41 first = false; 42 first = false;
42 } 43 }
43 buffer.write(']'); 44 buffer.write(']');
44 } 45 }
45 46
46 String build() { 47 String build() {
47 LineColumnMap<SourceMapEntry> lineColumnMap = 48 LineColumnMap<SourceMapEntry> lineColumnMap =
48 new LineColumnMap<SourceMapEntry>(); 49 new LineColumnMap<SourceMapEntry>();
49 Map<Uri, LineColumnMap<SourceMapEntry>> sourceLocationMap = 50 Map<Uri, LineColumnMap<SourceMapEntry>> sourceLocationMap =
50 <Uri, LineColumnMap<SourceMapEntry>>{}; 51 <Uri, LineColumnMap<SourceMapEntry>>{};
51 entries.forEach((SourceMapEntry sourceMapEntry) { 52 entries.forEach((SourceMapEntry sourceMapEntry) {
52 int line = lineColumnProvider.getLine(sourceMapEntry.targetOffset); 53 Location kernelLocation =
53 int column = 54 locationProvider.getLocation(sourceMapEntry.targetOffset);
54 lineColumnProvider.getColumn(line, sourceMapEntry.targetOffset); 55 int line = kernelLocation.line - 1;
56 int column = kernelLocation.column - 1;
55 lineColumnMap.add(line, column, sourceMapEntry); 57 lineColumnMap.add(line, column, sourceMapEntry);
56 58
57 SourceLocation location = sourceMapEntry.sourceLocation; 59 SourceLocation location = sourceMapEntry.sourceLocation;
58 if (location != null) { 60 if (location != null) {
59 if (location.sourceUri != null) { 61 if (location.sourceUri != null) {
60 LineColumnMap<SourceMapEntry> sourceLineColumnMap = 62 LineColumnMap<SourceMapEntry> sourceLineColumnMap =
61 sourceLocationMap.putIfAbsent(location.sourceUri, 63 sourceLocationMap.putIfAbsent(location.sourceUri,
62 () => new LineColumnMap<SourceMapEntry>()); 64 () => new LineColumnMap<SourceMapEntry>());
63 sourceLineColumnMap.add( 65 sourceLineColumnMap.add(
64 location.line, location.column, sourceMapEntry); 66 location.line, location.column, sourceMapEntry);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 String sourceMapFileName = relativize(fileUri, sourceMapUri, false); 177 String sourceMapFileName = relativize(fileUri, sourceMapUri, false);
176 return ''' 178 return '''
177 179
178 //# sourceMappingURL=$sourceMapFileName 180 //# sourceMappingURL=$sourceMapFileName
179 '''; 181 ''';
180 } 182 }
181 return ''; 183 return '';
182 } 184 }
183 185
184 /// Generates source map files for all [SourceLocations] in 186 /// Generates source map files for all [SourceLocations] in
185 /// [sourceLocationsProvider] for the .js code in [lineColumnProvider] 187 /// [sourceLocationsProvider] for the .js code in [locationProvider]
186 /// [sourceMapUri] is used to relativizes the URIs of the referenced source 188 /// [sourceMapUri] is used to relativizes the URIs of the referenced source
187 /// files and the target [fileUri]. [name] and [outputProvider] are used to 189 /// files and the target [fileUri]. [name] and [outputProvider] are used to
188 /// create the [OutputSink] for the source map text. 190 /// create the [OutputSink] for the source map text.
189 static void outputSourceMap( 191 static void outputSourceMap(
190 SourceLocationsProvider sourceLocationsProvider, 192 SourceLocationsProvider sourceLocationsProvider,
191 LineColumnProvider lineColumnProvider, 193 LocationProvider locationProvider,
192 String name, 194 String name,
193 Uri sourceMapUri, 195 Uri sourceMapUri,
194 Uri fileUri, 196 Uri fileUri,
195 OutputSink outputProvider( 197 OutputSink outputProvider(
196 String name, String extension, OutputType type)) { 198 String name, String extension, OutputType type)) {
197 // Create a source file for the compilation output. This allows using 199 // Create a source file for the compilation output. This allows using
198 // [:getLine:] to transform offsets to line numbers in [SourceMapBuilder]. 200 // [:getLine:] to transform offsets to line numbers in [SourceMapBuilder].
199 int index = 0; 201 int index = 0;
200 sourceLocationsProvider.sourceLocations 202 sourceLocationsProvider.sourceLocations
201 .forEach((SourceLocations sourceLocations) { 203 .forEach((SourceLocations sourceLocations) {
202 SourceMapBuilder sourceMapBuilder = new SourceMapBuilder( 204 SourceMapBuilder sourceMapBuilder = new SourceMapBuilder(
203 sourceLocations.name, sourceMapUri, fileUri, lineColumnProvider); 205 sourceLocations.name, sourceMapUri, fileUri, locationProvider);
204 sourceLocations.forEachSourceLocation(sourceMapBuilder.addMapping); 206 sourceLocations.forEachSourceLocation(sourceMapBuilder.addMapping);
205 String sourceMap = sourceMapBuilder.build(); 207 String sourceMap = sourceMapBuilder.build();
206 String extension = 'js.map'; 208 String extension = 'js.map';
207 if (index > 0) { 209 if (index > 0) {
208 if (name == '') { 210 if (name == '') {
209 name = fileUri != null ? fileUri.pathSegments.last : 'out.js'; 211 name = fileUri != null ? fileUri.pathSegments.last : 'out.js';
210 extension = 'map.${sourceLocations.name}'; 212 extension = 'map.${sourceLocations.name}';
211 } else { 213 } else {
212 extension = 'js.map.${sourceLocations.name}'; 214 extension = 'js.map.${sourceLocations.name}';
213 } 215 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 int register(T element) { 356 int register(T element) {
355 return map.putIfAbsent(element, () => map.length); 357 return map.putIfAbsent(element, () => map.length);
356 } 358 }
357 359
358 /// Returns the index of [element]. 360 /// Returns the index of [element].
359 int operator [](T element) => map[element]; 361 int operator [](T element) => map[element];
360 362
361 /// Returns the indexed elements. 363 /// Returns the indexed elements.
362 Iterable<T> get elements => map.keys; 364 Iterable<T> get elements => map.keys;
363 } 365 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698