( cursor: number, deltaX: number, deltaY: number, value: string )
| 18 | } |
| 19 | |
| 20 | export function findTextCursor( |
| 21 | cursor: number, |
| 22 | deltaX: number, |
| 23 | deltaY: number, |
| 24 | value: string |
| 25 | ): number { |
| 26 | const lines = value.split('\n'); |
| 27 | let cursorY = 0; |
| 28 | let cursorX = cursor; |
| 29 | |
| 30 | for (const line of lines) { |
| 31 | if (cursorX <= line.length) { |
| 32 | break; |
| 33 | } |
| 34 | cursorX -= line.length + 1; |
| 35 | cursorY++; |
| 36 | } |
| 37 | |
| 38 | cursorY = Math.max(0, Math.min(lines.length - 1, cursorY + deltaY)); |
| 39 | |
| 40 | cursorX = Math.min(cursorX, lines[cursorY].length) + deltaX; |
| 41 | while (cursorX < 0 && cursorY > 0) { |
| 42 | cursorY--; |
| 43 | cursorX += lines[cursorY].length + 1; |
| 44 | } |
| 45 | while (cursorX > lines[cursorY].length && cursorY < lines.length - 1) { |
| 46 | cursorX -= lines[cursorY].length + 1; |
| 47 | cursorY++; |
| 48 | } |
| 49 | cursorX = Math.max(0, Math.min(lines[cursorY].length, cursorX)); |
| 50 | |
| 51 | let newCursor = 0; |
| 52 | for (let i = 0; i < cursorY; i++) { |
| 53 | newCursor += lines[i].length + 1; |
| 54 | } |
| 55 | return newCursor + cursorX; |
| 56 | } |
no outgoing calls
no test coverage detected