mirror of
https://github.com/danbulant/monaco-yaml
synced 2026-06-19 06:21:04 +00:00
62 lines
No EOL
1.5 KiB
TypeScript
62 lines
No EOL
1.5 KiB
TypeScript
"use strict"
|
|
|
|
export function insertionPointReturnValue(pt: number) {
|
|
return ((-pt) - 1)
|
|
}
|
|
|
|
export function binarySearch(array: number[], sought: number) {
|
|
|
|
let lower = 0
|
|
let upper = array.length - 1
|
|
|
|
while (lower <= upper) {
|
|
let idx = Math.floor((lower + upper) / 2)
|
|
const value = array[idx]
|
|
|
|
if (value === sought) {
|
|
return idx;
|
|
}
|
|
|
|
if (lower === upper) {
|
|
const insertionPoint = (value < sought) ? idx + 1 : idx
|
|
return insertionPointReturnValue(insertionPoint)
|
|
}
|
|
|
|
if (sought > value) {
|
|
lower = idx + 1;
|
|
} else if (sought < value) {
|
|
upper = idx - 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function getLineStartPositions(text: string) {
|
|
const lineStartPositions = [0];
|
|
for (var i = 0; i < text.length; i++) {
|
|
const c = text[i];
|
|
|
|
if (c === '\r') {
|
|
// Check for Windows encoding, otherwise we are old Mac
|
|
if (i + 1 < text.length && text[i + 1] == '\n') {
|
|
i++;
|
|
}
|
|
|
|
lineStartPositions.push(i + 1);
|
|
} else if (c === '\n'){
|
|
lineStartPositions.push(i + 1);
|
|
}
|
|
}
|
|
|
|
return lineStartPositions;
|
|
}
|
|
|
|
export function getPosition(pos: number, lineStartPositions: number[]){
|
|
let line = binarySearch(lineStartPositions, pos)
|
|
|
|
if (line < 0){
|
|
const insertionPoint = -1 * line - 1;
|
|
line = insertionPoint - 1;
|
|
}
|
|
|
|
return {line, column: pos - lineStartPositions[line]}
|
|
} |