1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | (function (factory) {
|
---|
9 | if (typeof module === "object" && typeof module.exports === "object") {
|
---|
10 | var v = factory(require, exports);
|
---|
11 | if (v !== undefined) module.exports = v;
|
---|
12 | }
|
---|
13 | else if (typeof define === "function" && define.amd) {
|
---|
14 | define("@angular/core/schematics/utils/line_mappings", ["require", "exports"], factory);
|
---|
15 | }
|
---|
16 | })(function (require, exports) {
|
---|
17 | "use strict";
|
---|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
19 | exports.computeLineStartsMap = exports.getLineAndCharacterFromPosition = void 0;
|
---|
20 | const LF_CHAR = 10;
|
---|
21 | const CR_CHAR = 13;
|
---|
22 | const LINE_SEP_CHAR = 8232;
|
---|
23 | const PARAGRAPH_CHAR = 8233;
|
---|
24 | /** Gets the line and character for the given position from the line starts map. */
|
---|
25 | function getLineAndCharacterFromPosition(lineStartsMap, position) {
|
---|
26 | const lineIndex = findClosestLineStartPosition(lineStartsMap, position);
|
---|
27 | return { character: position - lineStartsMap[lineIndex], line: lineIndex };
|
---|
28 | }
|
---|
29 | exports.getLineAndCharacterFromPosition = getLineAndCharacterFromPosition;
|
---|
30 | /**
|
---|
31 | * Computes the line start map of the given text. This can be used in order to
|
---|
32 | * retrieve the line and character of a given text position index.
|
---|
33 | */
|
---|
34 | function computeLineStartsMap(text) {
|
---|
35 | const result = [0];
|
---|
36 | let pos = 0;
|
---|
37 | while (pos < text.length) {
|
---|
38 | const char = text.charCodeAt(pos++);
|
---|
39 | // Handles the "CRLF" line break. In that case we peek the character
|
---|
40 | // after the "CR" and check if it is a line feed.
|
---|
41 | if (char === CR_CHAR) {
|
---|
42 | if (text.charCodeAt(pos) === LF_CHAR) {
|
---|
43 | pos++;
|
---|
44 | }
|
---|
45 | result.push(pos);
|
---|
46 | }
|
---|
47 | else if (char === LF_CHAR || char === LINE_SEP_CHAR || char === PARAGRAPH_CHAR) {
|
---|
48 | result.push(pos);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | result.push(pos);
|
---|
52 | return result;
|
---|
53 | }
|
---|
54 | exports.computeLineStartsMap = computeLineStartsMap;
|
---|
55 | /** Finds the closest line start for the given position. */
|
---|
56 | function findClosestLineStartPosition(linesMap, position, low = 0, high = linesMap.length - 1) {
|
---|
57 | while (low <= high) {
|
---|
58 | const pivotIdx = Math.floor((low + high) / 2);
|
---|
59 | const pivotEl = linesMap[pivotIdx];
|
---|
60 | if (pivotEl === position) {
|
---|
61 | return pivotIdx;
|
---|
62 | }
|
---|
63 | else if (position > pivotEl) {
|
---|
64 | low = pivotIdx + 1;
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | high = pivotIdx - 1;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | // In case there was no exact match, return the closest "lower" line index. We also
|
---|
71 | // subtract the index by one because want the index of the previous line start.
|
---|
72 | return low - 1;
|
---|
73 | }
|
---|
74 | });
|
---|
75 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibGluZV9tYXBwaW5ncy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvcmUvc2NoZW1hdGljcy91dGlscy9saW5lX21hcHBpbmdzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRzs7Ozs7Ozs7Ozs7OztJQUVILE1BQU0sT0FBTyxHQUFHLEVBQUUsQ0FBQztJQUNuQixNQUFNLE9BQU8sR0FBRyxFQUFFLENBQUM7SUFDbkIsTUFBTSxhQUFhLEdBQUcsSUFBSSxDQUFDO0lBQzNCLE1BQU0sY0FBYyxHQUFHLElBQUksQ0FBQztJQUU1QixtRkFBbUY7SUFDbk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