|
| 1 | +from talon import Context, actions, settings |
| 2 | + |
| 3 | +ctx = Context() |
| 4 | + |
| 5 | +ctx.matches = r""" |
| 6 | +code.language: stata |
| 7 | +""" |
| 8 | + |
| 9 | +# functions.py |
| 10 | +ctx.lists["user.code_parameter_name"] = { |
| 11 | + # regressions |
| 12 | + "V C E cluster": "vce(cluster)", |
| 13 | + "V C E robust": "vce(robust)", |
| 14 | +} |
| 15 | + |
| 16 | +# functions_common.py |
| 17 | +ctx.lists["user.code_common_function"] = { |
| 18 | + # base stata |
| 19 | + "global": "global", |
| 20 | + "local": "local", |
| 21 | + "reg": "reg", |
| 22 | + "regress": "reg", |
| 23 | + # packages |
| 24 | + "estadd": "estadd", |
| 25 | + "estout": "estout", |
| 26 | + "estpost": "estpost", |
| 27 | + "eststo": "eststo", |
| 28 | + "esttab": "esttab", |
| 29 | +} |
| 30 | + |
| 31 | +# libraries_gui.py |
| 32 | +ctx.lists["user.code_libraries"] = { |
| 33 | + "estout": "estout", |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +@ctx.action_class("user") |
| 38 | +class UserActions: |
| 39 | + # comment_line.py |
| 40 | + def code_comment_line_prefix(): |
| 41 | + actions.auto_insert("* ") |
| 42 | + |
| 43 | + # functions.py |
| 44 | + def code_private_function(text: str): |
| 45 | + result = "program {} \n\nend".format( |
| 46 | + actions.user.formatted_text( |
| 47 | + text, settings.get("user.code_private_function_formatter") |
| 48 | + ) |
| 49 | + ) |
| 50 | + actions.user.paste(result) |
| 51 | + actions.edit.up() |
| 52 | + actions.key("tab") |
| 53 | + |
| 54 | + def code_default_function(text: str): |
| 55 | + actions.user.code_private_function(text) |
| 56 | + |
| 57 | + def code_insert_named_argument(parameter_name: str): |
| 58 | + actions.insert(f"{parameter_name} ") |
| 59 | + |
| 60 | + # functions_common.py |
| 61 | + def code_insert_function(text: str, selection: str): |
| 62 | + text += f" {selection or ''}" |
| 63 | + actions.user.paste(text) |
| 64 | + |
| 65 | + # imperative.py |
| 66 | + def code_block(): |
| 67 | + actions.auto_insert("\n") |
| 68 | + |
| 69 | + def code_state_if(): |
| 70 | + actions.insert("if {\n\n}") |
| 71 | + actions.key("up tab up") |
| 72 | + actions.edit.line_end() |
| 73 | + actions.key("left:2") |
| 74 | + |
| 75 | + def code_state_else_if(): |
| 76 | + actions.insert("else if {\n\n}") |
| 77 | + actions.key("up tab up") |
| 78 | + actions.edit.line_end() |
| 79 | + actions.key("left:2") |
| 80 | + |
| 81 | + def code_state_else(): |
| 82 | + actions.insert("else {\n\n}") |
| 83 | + actions.key("up tab") |
| 84 | + |
| 85 | + def code_state_for(): |
| 86 | + actions.insert("forval {\n\n}") |
| 87 | + actions.key("up tab up") |
| 88 | + actions.edit.line_end() |
| 89 | + actions.key("left:2") |
| 90 | + |
| 91 | + def code_state_for_each(): |
| 92 | + actions.insert("foreach in {\n\n}") |
| 93 | + actions.key("up tab up") |
| 94 | + actions.edit.line_end() |
| 95 | + actions.key("left:2") |
| 96 | + |
| 97 | + def code_state_while(): |
| 98 | + actions.insert("while {\n\n}") |
| 99 | + actions.key("up tab up") |
| 100 | + actions.edit.line_end() |
| 101 | + actions.key("left:2") |
| 102 | + |
| 103 | + def code_break(): |
| 104 | + actions.insert("break") |
| 105 | + |
| 106 | + def code_next(): |
| 107 | + actions.insert("continue") |
| 108 | + |
| 109 | + # libraries.py |
| 110 | + def code_import(): |
| 111 | + actions.auto_insert("ssc install ") |
| 112 | + |
| 113 | + # libraries_gui.py |
| 114 | + def code_insert_library(text: str, selection: str): |
| 115 | + actions.auto_insert("ssc install ") |
| 116 | + actions.user.paste(text + selection) |
| 117 | + |
| 118 | + # operators_array.py |
| 119 | + def code_operator_subscript(): |
| 120 | + actions.user.insert_between("[", "]") |
| 121 | + |
| 122 | + # operators_assignment.py |
| 123 | + def code_operator_assignment(): |
| 124 | + actions.auto_insert(" = ") |
| 125 | + |
| 126 | + # operators_math.py |
| 127 | + def code_operator_subtraction(): |
| 128 | + actions.auto_insert(" - ") |
| 129 | + |
| 130 | + def code_operator_addition(): |
| 131 | + actions.auto_insert(" + ") |
| 132 | + |
| 133 | + def code_operator_multiplication(): |
| 134 | + actions.auto_insert(" * ") |
| 135 | + |
| 136 | + def code_operator_division(): |
| 137 | + actions.auto_insert(" / ") |
| 138 | + |
| 139 | + def code_operator_modulo(): |
| 140 | + actions.user.insert_between("mod(", ")") |
| 141 | + |
| 142 | + def code_operator_exponent(): |
| 143 | + actions.auto_insert(" ^ ") |
| 144 | + |
| 145 | + def code_operator_equal(): |
| 146 | + actions.auto_insert(" == ") |
| 147 | + |
| 148 | + def code_operator_not_equal(): |
| 149 | + actions.auto_insert(" != ") |
| 150 | + |
| 151 | + def code_operator_greater_than(): |
| 152 | + actions.auto_insert(" > ") |
| 153 | + |
| 154 | + def code_operator_less_than(): |
| 155 | + actions.auto_insert(" < ") |
| 156 | + |
| 157 | + def code_operator_greater_than_or_equal_to(): |
| 158 | + actions.auto_insert(" >= ") |
| 159 | + |
| 160 | + def code_operator_less_than_or_equal_to(): |
| 161 | + actions.auto_insert(" <= ") |
| 162 | + |
| 163 | + def code_operator_and(): |
| 164 | + actions.auto_insert(" & ") |
| 165 | + |
| 166 | + def code_operator_or(): |
| 167 | + actions.auto_insert(" | ") |
0 commit comments