mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-24 09:39:35 +02:00
24 lines
465 B
TypeScript
24 lines
465 B
TypeScript
import { Text } from '@codemirror/state'
|
|
|
|
export const findValidPosition = (
|
|
doc: Text,
|
|
lineNumber: number, // 1-indexed
|
|
columnNumber = 0 // 0-indexed
|
|
): number => {
|
|
if (lineNumber < 1) {
|
|
return 0
|
|
}
|
|
|
|
const lines = doc.lines
|
|
|
|
if (lineNumber > lines) {
|
|
// end of the doc
|
|
return doc.length
|
|
}
|
|
|
|
const line = doc.line(lineNumber)
|
|
|
|
// requested line and column, or the end of the line
|
|
return Math.min(line.from + columnNumber, line.to)
|
|
}
|