Return the line in a cell at a given cursor position Used for calling line-based APIs that don't support multi-line input, yet. Parameters ---------- cell: str multiline block of text cursor_pos: integer the cursor position Returns ----
(cell, cursor_pos=0)
| 22 | return |
| 23 | |
| 24 | def line_at_cursor(cell, cursor_pos=0): |
| 25 | """Return the line in a cell at a given cursor position |
| 26 | |
| 27 | Used for calling line-based APIs that don't support multi-line input, yet. |
| 28 | |
| 29 | Parameters |
| 30 | ---------- |
| 31 | |
| 32 | cell: str |
| 33 | multiline block of text |
| 34 | cursor_pos: integer |
| 35 | the cursor position |
| 36 | |
| 37 | Returns |
| 38 | ------- |
| 39 | |
| 40 | (line, offset): (string, integer) |
| 41 | The line with the current cursor, and the character offset of the start of the line. |
| 42 | """ |
| 43 | offset = 0 |
| 44 | lines = cell.splitlines(True) |
| 45 | for line in lines: |
| 46 | next_offset = offset + len(line) |
| 47 | if not line.endswith('\n'): |
| 48 | # If the last line doesn't have a trailing newline, treat it as if |
| 49 | # it does so that the cursor at the end of the line still counts |
| 50 | # as being on that line. |
| 51 | next_offset += 1 |
| 52 | if next_offset > cursor_pos: |
| 53 | break |
| 54 | offset = next_offset |
| 55 | else: |
| 56 | line = "" |
| 57 | return (line, offset) |
| 58 | |
| 59 | def token_at_cursor(cell, cursor_pos=0): |
| 60 | """Get the token at a given cursor |
no outgoing calls