mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-15 02:40:56 -05:00
20 lines
427 B
TypeScript
20 lines
427 B
TypeScript
|
import { Text } from '@codemirror/state'
|
||
|
|
||
|
export const findValidPosition = (
|
||
|
doc: Text,
|
||
|
lineNumber: number, // 1-indexed
|
||
|
columnNumber: number // 0-indexed
|
||
|
): number => {
|
||
|
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)
|
||
|
}
|