is-cursor-on-empty-line.ts 476 B

123456789101112131415161718
  1. import { EditorState } from '@codemirror/state'
  2. /**
  3. * Returns true when the cursor is on an empty line with no active selection.
  4. *
  5. * Used to disable the "Add comment" action — there is nothing to anchor a
  6. * comment to on a blank line.
  7. */
  8. export function isCursorOnEmptyLine(state: EditorState): boolean {
  9. const { main } = state.selection
  10. if (!main.empty) {
  11. return false
  12. }
  13. const line = state.doc.lineAt(main.head)
  14. return line.text.trim().length === 0
  15. }