1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.printLocation = printLocation;
|
7 | exports.printSourceLocation = printSourceLocation;
|
8 |
|
9 | var _location = require("../language/location");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function printLocation(location) {
|
15 | return printSourceLocation(location.source, (0, _location.getLocation)(location.source, location.start));
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function printSourceLocation(source, sourceLocation) {
|
23 | var firstLineColumnOffset = source.locationOffset.column - 1;
|
24 | var body = whitespace(firstLineColumnOffset) + source.body;
|
25 | var lineIndex = sourceLocation.line - 1;
|
26 | var lineOffset = source.locationOffset.line - 1;
|
27 | var lineNum = sourceLocation.line + lineOffset;
|
28 | var columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
|
29 | var columnNum = sourceLocation.column + columnOffset;
|
30 | var locationStr = "".concat(source.name, ":").concat(lineNum, ":").concat(columnNum, "\n");
|
31 | var lines = body.split(/\r\n|[\n\r]/g);
|
32 | var locationLine = lines[lineIndex];
|
33 |
|
34 | if (locationLine.length > 120) {
|
35 | var sublineIndex = Math.floor(columnNum / 80);
|
36 | var sublineColumnNum = columnNum % 80;
|
37 | var sublines = [];
|
38 |
|
39 | for (var i = 0; i < locationLine.length; i += 80) {
|
40 | sublines.push(locationLine.slice(i, i + 80));
|
41 | }
|
42 |
|
43 | return locationStr + printPrefixedLines([["".concat(lineNum), sublines[0]]].concat(sublines.slice(1, sublineIndex + 1).map(function (subline) {
|
44 | return ['', subline];
|
45 | }), [[' ', whitespace(sublineColumnNum - 1) + '^'], ['', sublines[sublineIndex + 1]]]));
|
46 | }
|
47 |
|
48 | return locationStr + printPrefixedLines([
|
49 | ["".concat(lineNum - 1), lines[lineIndex - 1]], ["".concat(lineNum), locationLine], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1), lines[lineIndex + 1]]]);
|
50 | }
|
51 |
|
52 | function printPrefixedLines(lines) {
|
53 | var existingLines = lines.filter(function (_ref) {
|
54 | var _ = _ref[0],
|
55 | line = _ref[1];
|
56 | return line !== undefined;
|
57 | });
|
58 | var padLen = Math.max.apply(Math, existingLines.map(function (_ref2) {
|
59 | var prefix = _ref2[0];
|
60 | return prefix.length;
|
61 | }));
|
62 | return existingLines.map(function (_ref3) {
|
63 | var prefix = _ref3[0],
|
64 | line = _ref3[1];
|
65 | return lpad(padLen, prefix) + (line ? ' | ' + line : ' |');
|
66 | }).join('\n');
|
67 | }
|
68 |
|
69 | function whitespace(len) {
|
70 | return Array(len + 1).join(' ');
|
71 | }
|
72 |
|
73 | function lpad(len, str) {
|
74 | return whitespace(len - str.length) + str;
|
75 | }
|