MCPcopy Index your code
hub / github.com/editablejs/editable / findCurrentLineRange

Function findCurrentLineRange

packages/editor/src/utils/lines.ts:29–56  ·  view source on GitHub ↗
(editor: Editable, parentRange: Range)

Source from the content-addressed store, hash-verified

27 * @returns {Range} A valid portion of the parentRange which is one a single line
28 */
29export const findCurrentLineRange = (editor: Editable, parentRange: Range): Range => {
30 const parentRangeBoundary = Editor.range(editor, Range.end(parentRange))
31 const positions = Array.from(Editor.positions(editor, { at: parentRange }))
32
33 let left = 0
34 let right = positions.length
35 let middle = Math.floor(right / 2)
36
37 if (areRangesSameLine(editor, Editor.range(editor, positions[left]), parentRangeBoundary)) {
38 return Editor.range(editor, positions[left], parentRangeBoundary)
39 }
40
41 if (positions.length < 2) {
42 return Editor.range(editor, positions[positions.length - 1], parentRangeBoundary)
43 }
44
45 while (middle !== positions.length && middle !== left) {
46 if (areRangesSameLine(editor, Editor.range(editor, positions[middle]), parentRangeBoundary)) {
47 right = middle
48 } else {
49 left = middle
50 }
51
52 middle = Math.floor((left + right) / 2)
53 }
54
55 return Editor.range(editor, positions[right], parentRangeBoundary)
56}

Callers 1

withEditableFunction · 0.90

Calls 1

areRangesSameLineFunction · 0.85

Tested by

no test coverage detected