[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.computeLineStartsMap = exports.getLineAndCharacterFromPosition = void 0;
|
---|
| 11 | /*
|
---|
| 12 | * Line mapping utilities which can be used to retrieve line and character based
|
---|
| 13 | * on an absolute character position in a given file. This functionality is similar
|
---|
| 14 | * to TypeScript's "ts.getLineAndCharacterFromPosition" utility, but we cannot leverage
|
---|
| 15 | * their logic for line mappings as it's internal and we need to generate line mappings
|
---|
| 16 | * for non-TypeScript files such as HTML templates or stylesheets.
|
---|
| 17 | *
|
---|
| 18 | * Line and character can be retrieved by splitting a given source text based on
|
---|
| 19 | * line breaks into line start entries. Later when a specific position is requested,
|
---|
| 20 | * the closest line-start position is determined based on the given position.
|
---|
| 21 | */
|
---|
| 22 | const LF_CHAR = 10;
|
---|
| 23 | const CR_CHAR = 13;
|
---|
| 24 | const LINE_SEP_CHAR = 8232;
|
---|
| 25 | const PARAGRAPH_CHAR = 8233;
|
---|
| 26 | /** Gets the line and character for the given position from the line starts map. */
|
---|
| 27 | function getLineAndCharacterFromPosition(lineStartsMap, position) {
|
---|
| 28 | const lineIndex = findClosestLineStartPosition(lineStartsMap, position);
|
---|
| 29 | return { character: position - lineStartsMap[lineIndex], line: lineIndex };
|
---|
| 30 | }
|
---|
| 31 | exports.getLineAndCharacterFromPosition = getLineAndCharacterFromPosition;
|
---|
| 32 | /**
|
---|
| 33 | * Computes the line start map of the given text. This can be used in order to
|
---|
| 34 | * retrieve the line and character of a given text position index.
|
---|
| 35 | */
|
---|
| 36 | function computeLineStartsMap(text) {
|
---|
| 37 | const result = [0];
|
---|
| 38 | let pos = 0;
|
---|
| 39 | while (pos < text.length) {
|
---|
| 40 | const char = text.charCodeAt(pos++);
|
---|
| 41 | // Handles the "CRLF" line break. In that case we peek the character
|
---|
| 42 | // after the "CR" and check if it is a line feed.
|
---|
| 43 | if (char === CR_CHAR) {
|
---|
| 44 | if (text.charCodeAt(pos) === LF_CHAR) {
|
---|
| 45 | pos++;
|
---|
| 46 | }
|
---|
| 47 | result.push(pos);
|
---|
| 48 | }
|
---|
| 49 | else if (char === LF_CHAR || char === LINE_SEP_CHAR || char === PARAGRAPH_CHAR) {
|
---|
| 50 | result.push(pos);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | result.push(pos);
|
---|
| 54 | return result;
|
---|
| 55 | }
|
---|
| 56 | exports.computeLineStartsMap = computeLineStartsMap;
|
---|
| 57 | /** Finds the closest line start for the given position. */
|
---|
| 58 | function findClosestLineStartPosition(linesMap, position, low = 0, high = linesMap.length - 1) {
|
---|
| 59 | while (low <= high) {
|
---|
| 60 | const pivotIndex = Math.floor((low + high) / 2);
|
---|
| 61 | const pivotEl = linesMap[pivotIndex];
|
---|
| 62 | if (pivotEl === position) {
|
---|
| 63 | return pivotIndex;
|
---|
| 64 | }
|
---|
| 65 | else if (position > pivotEl) {
|
---|
| 66 | low = pivotIndex + 1;
|
---|
| 67 | }
|
---|
| 68 | else {
|
---|
| 69 | high = pivotIndex - 1;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | // In case there was no exact match, return the closest "lower" line index. We also
|
---|
| 73 | // subtract the index by one because want the index of the previous line start.
|
---|
| 74 | return low - 1;
|
---|
| 75 | }
|
---|
| 76 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibGluZS1tYXBwaW5ncy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL3NyYy9jZGsvc2NoZW1hdGljcy91cGRhdGUtdG9vbC91dGlscy9saW5lLW1hcHBpbmdzLnRzIl0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