|
| 1 | +from talon import Context, app, ui |
| 2 | + |
| 3 | +from .cursorless_everywhere_talon import ( |
| 4 | + EditorEdit, |
| 5 | + EditorState, |
| 6 | + SelectionOffsets, |
| 7 | +) |
| 8 | + |
| 9 | +if app.platform == "windows": |
| 10 | + from talon.windows.ax import TextRange |
| 11 | + |
| 12 | + |
| 13 | +# https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.text.textpatternrange?view=windowsdesktop-8.0 |
| 14 | + |
| 15 | +ctx = Context() |
| 16 | + |
| 17 | +ctx.matches = r""" |
| 18 | +os: windows |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +@ctx.action_class("user") |
| 23 | +class Actions: |
| 24 | + def cursorless_everywhere_get_editor_state() -> EditorState: |
| 25 | + el = ui.focused_element() |
| 26 | + |
| 27 | + if "Text2" not in el.patterns: |
| 28 | + raise ValueError("Focused element is not a text element") |
| 29 | + |
| 30 | + text_pattern = el.text_pattern2 |
| 31 | + document_range = text_pattern.document_range |
| 32 | + caret_range = text_pattern.caret_range |
| 33 | + selection_ranges = text_pattern.selection |
| 34 | + selections: list[SelectionOffsets] = [] |
| 35 | + |
| 36 | + for selection_range in selection_ranges: |
| 37 | + anchor, active = get_selection(document_range, selection_range, caret_range) |
| 38 | + selections.append( |
| 39 | + { |
| 40 | + "anchor": anchor, |
| 41 | + "active": active, |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + return { |
| 46 | + "text": document_range.text, |
| 47 | + "selections": selections, |
| 48 | + } |
| 49 | + |
| 50 | + def cursorless_everywhere_set_selections( |
| 51 | + selections: list[SelectionOffsets], # pyright: ignore [reportGeneralTypeIssues] |
| 52 | + ): |
| 53 | + if selections.length != 1: # pyright: ignore [reportAttributeAccessIssue] |
| 54 | + raise ValueError("Only single selection supported") |
| 55 | + |
| 56 | + selection = selections[0] |
| 57 | + anchor = selection["anchor"] |
| 58 | + active = selection["active"] |
| 59 | + |
| 60 | + el = ui.focused_element() |
| 61 | + |
| 62 | + if "Text2" not in el.patterns: |
| 63 | + raise ValueError("Focused element is not a text element") |
| 64 | + |
| 65 | + text_pattern = el.text_pattern2 |
| 66 | + document_range = text_pattern.document_range |
| 67 | + |
| 68 | + set_selection(document_range, anchor, active) |
| 69 | + |
| 70 | + def cursorless_everywhere_edit_text( |
| 71 | + edit: EditorEdit, # pyright: ignore [reportGeneralTypeIssues] |
| 72 | + ): |
| 73 | + text = edit["text"] |
| 74 | + |
| 75 | + el = ui.focused_element() |
| 76 | + |
| 77 | + if "Value" not in el.patterns: |
| 78 | + raise ValueError("Focused element is not a text element") |
| 79 | + |
| 80 | + el.value_pattern.value = text |
| 81 | + |
| 82 | + |
| 83 | +def set_selection(document_range: TextRange, anchor: int, active: int): |
| 84 | + # This happens in slack, for example. The document range starts with a |
| 85 | + # newline and selecting first character we'll make the selection go outside |
| 86 | + # of the edit box. |
| 87 | + if document_range.text.startswith("\n") and anchor == 0 and active == 0: |
| 88 | + anchor = 1 |
| 89 | + active = 1 |
| 90 | + |
| 91 | + start = min(anchor, active) |
| 92 | + end = max(anchor, active) |
| 93 | + range = document_range.clone() |
| 94 | + range.move_endpoint_by_range("End", "Start", target=document_range) |
| 95 | + range.move_endpoint_by_unit("End", "Character", end) |
| 96 | + range.move_endpoint_by_unit("Start", "Character", start) |
| 97 | + range.select() |
| 98 | + |
| 99 | + |
| 100 | +def get_selection( |
| 101 | + document_range: TextRange, selection_range: TextRange, caret_range: TextRange |
| 102 | +) -> tuple[int, int]: |
| 103 | + # Make copy of the document range to avoid modifying the original |
| 104 | + range_before_selection = document_range.clone() |
| 105 | + # Move the end of the copy to the start of the selection |
| 106 | + # range_before_selection.end = selection_range.start |
| 107 | + range_before_selection.move_endpoint_by_range( |
| 108 | + "End", |
| 109 | + "Start", |
| 110 | + target=selection_range, |
| 111 | + ) |
| 112 | + # The selection start offset is the length of the text before the selection |
| 113 | + start = len(range_before_selection.text) |
| 114 | + |
| 115 | + range_after_selection = document_range.clone() |
| 116 | + range_after_selection.move_endpoint_by_range( |
| 117 | + "Start", |
| 118 | + "End", |
| 119 | + target=selection_range, |
| 120 | + ) |
| 121 | + end = len(document_range.text) - len(range_after_selection.text) |
| 122 | + |
| 123 | + # The selection is reversed if the caret is at the start of the selection |
| 124 | + is_reversed = ( |
| 125 | + caret_range.compare_endpoints("Start", "Start", target=selection_range) == 0 |
| 126 | + ) |
| 127 | + |
| 128 | + # Return as (anchor, active) |
| 129 | + return (end, start) if is_reversed else (start, end) |
0 commit comments