position.ts 427 B

12345678910111213141516171819
  1. import { Text } from '@codemirror/state'
  2. export const findValidPosition = (
  3. doc: Text,
  4. lineNumber: number, // 1-indexed
  5. columnNumber: number // 0-indexed
  6. ): number => {
  7. const lines = doc.lines
  8. if (lineNumber > lines) {
  9. // end of the doc
  10. return doc.length
  11. }
  12. const line = doc.line(lineNumber)
  13. // requested line and column, or the end of the line
  14. return Math.min(line.from + columnNumber, line.to)
  15. }