Swap-Aggregator-Subgraph/node_modules/yaml/browser/dist/cst/source-utils.js
Richa-iitr d211083153 Revert "Revert "added handler""
This reverts commit c36ee8c5ca.
2022-07-03 07:30:05 +05:30

168 lines
4.3 KiB
JavaScript

function findLineStarts(src) {
var ls = [0];
var offset = src.indexOf('\n');
while (offset !== -1) {
offset += 1;
ls.push(offset);
offset = src.indexOf('\n', offset);
}
return ls;
}
function getSrcInfo(cst) {
var lineStarts, src;
if (typeof cst === 'string') {
lineStarts = findLineStarts(cst);
src = cst;
} else {
if (Array.isArray(cst)) cst = cst[0];
if (cst && cst.context) {
if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src);
lineStarts = cst.lineStarts;
src = cst.context.src;
}
}
return {
lineStarts: lineStarts,
src: src
};
}
/**
* @typedef {Object} LinePos - One-indexed position in the source
* @property {number} line
* @property {number} col
*/
/**
* Determine the line/col position matching a character offset.
*
* Accepts a source string or a CST document as the second parameter. With
* the latter, starting indices for lines are cached in the document as
* `lineStarts: number[]`.
*
* Returns a one-indexed `{ line, col }` location if found, or
* `undefined` otherwise.
*
* @param {number} offset
* @param {string|Document|Document[]} cst
* @returns {?LinePos}
*/
export function getLinePos(offset, cst) {
if (typeof offset !== 'number' || offset < 0) return null;
var _getSrcInfo = getSrcInfo(cst),
lineStarts = _getSrcInfo.lineStarts,
src = _getSrcInfo.src;
if (!lineStarts || !src || offset > src.length) return null;
for (var i = 0; i < lineStarts.length; ++i) {
var start = lineStarts[i];
if (offset < start) {
return {
line: i,
col: offset - lineStarts[i - 1] + 1
};
}
if (offset === start) return {
line: i + 1,
col: 1
};
}
var line = lineStarts.length;
return {
line: line,
col: offset - lineStarts[line - 1] + 1
};
}
/**
* Get a specified line from the source.
*
* Accepts a source string or a CST document as the second parameter. With
* the latter, starting indices for lines are cached in the document as
* `lineStarts: number[]`.
*
* Returns the line as a string if found, or `null` otherwise.
*
* @param {number} line One-indexed line number
* @param {string|Document|Document[]} cst
* @returns {?string}
*/
export function getLine(line, cst) {
var _getSrcInfo2 = getSrcInfo(cst),
lineStarts = _getSrcInfo2.lineStarts,
src = _getSrcInfo2.src;
if (!lineStarts || !(line >= 1) || line > lineStarts.length) return null;
var start = lineStarts[line - 1];
var end = lineStarts[line]; // undefined for last line; that's ok for slice()
while (end && end > start && src[end - 1] === '\n') {
--end;
}
return src.slice(start, end);
}
/**
* Pretty-print the starting line from the source indicated by the range `pos`
*
* Trims output to `maxWidth` chars while keeping the starting column visible,
* using `…` at either end to indicate dropped characters.
*
* Returns a two-line string (or `null`) with `\n` as separator; the second line
* will hold appropriately indented `^` marks indicating the column range.
*
* @param {Object} pos
* @param {LinePos} pos.start
* @param {LinePos} [pos.end]
* @param {string|Document|Document[]*} cst
* @param {number} [maxWidth=80]
* @returns {?string}
*/
export function getPrettyContext(_ref, cst) {
var start = _ref.start,
end = _ref.end;
var maxWidth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 80;
var src = getLine(start.line, cst);
if (!src) return null;
var col = start.col;
if (src.length > maxWidth) {
if (col <= maxWidth - 10) {
src = src.substr(0, maxWidth - 1) + '…';
} else {
var halfWidth = Math.round(maxWidth / 2);
if (src.length > col + halfWidth) src = src.substr(0, col + halfWidth - 1) + '…';
col -= src.length - maxWidth;
src = '…' + src.substr(1 - maxWidth);
}
}
var errLen = 1;
var errEnd = '';
if (end) {
if (end.line === start.line && col + (end.col - start.col) <= maxWidth + 1) {
errLen = end.col - start.col;
} else {
errLen = Math.min(src.length + 1, maxWidth) - col;
errEnd = '…';
}
}
var offset = col > 1 ? ' '.repeat(col - 1) : '';
var err = '^'.repeat(errLen);
return "".concat(src, "\n").concat(offset).concat(err).concat(errEnd);
}