From 6d6f22e67efe3eacc87f24374d821f44f5dc06c0 Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:58:57 +0100 Subject: [PATCH 01/12] stata support --- apps/stata/stata.py | 14 +++ apps/stata/stata.talon | 7 ++ apps/stata/stata_do_file_editor.talon | 25 ++++ lang/stata/stata.py | 170 ++++++++++++++++++++++++++ lang/stata/stata.talon | 28 +++++ 5 files changed, 244 insertions(+) create mode 100644 apps/stata/stata.py create mode 100644 apps/stata/stata.talon create mode 100644 apps/stata/stata_do_file_editor.talon create mode 100644 lang/stata/stata.py create mode 100644 lang/stata/stata.talon diff --git a/apps/stata/stata.py b/apps/stata/stata.py new file mode 100644 index 0000000000..aa9d667b2e --- /dev/null +++ b/apps/stata/stata.py @@ -0,0 +1,14 @@ +from talon import Context + +ctx = Context() +ctx.matches = r""" +app: stata +""" + +@ctx.action_class("code") +class CodeActions: + def language(): + return "stata" + + + \ No newline at end of file diff --git a/apps/stata/stata.talon b/apps/stata/stata.talon new file mode 100644 index 0000000000..00e4beb821 --- /dev/null +++ b/apps/stata/stata.talon @@ -0,0 +1,7 @@ +# Commands for the Stata Do-File Editor +os: windows +app: stata +- +settings(): + key_wait = 4 + insert_wait = 7 diff --git a/apps/stata/stata_do_file_editor.talon b/apps/stata/stata_do_file_editor.talon new file mode 100644 index 0000000000..480c3c668d --- /dev/null +++ b/apps/stata/stata_do_file_editor.talon @@ -0,0 +1,25 @@ +# Commands for the Stata Do-File Editor +os: windows +app: stata +win.title: Do-file Editor +- +do this: key(ctrl-d) + +do line: + edit.select_line() + key(ctrl-d) + +do (all | file): + edit.select_all() + edit.copy() + key(ctrl-d) + +do way up: + edit.extend_file_start() + edit.copy() + key(ctrl-d) + +do way down: + edit.extend_file_end() + edit.copy() + key(ctrl-d) diff --git a/lang/stata/stata.py b/lang/stata/stata.py new file mode 100644 index 0000000000..dd2da87219 --- /dev/null +++ b/lang/stata/stata.py @@ -0,0 +1,170 @@ +from talon import Context, actions, settings + +ctx = Context() + +ctx.matches = r""" +code.language: stata +""" + +# functions.py +ctx.lists["user.code_parameter_name"] = { + # regressions + "v c e cluster": "vce(cluster)", + "v c e robust": "vce(robust)", +} + +# functions_common.py +ctx.lists["user.code_common_function"] = { + # base stata + "global": "global", + "local": "local", + "reg": "reg", + "regress": "reg", + # packages + "estadd": "estadd", + "estout": "estout", + "estpost": "estpost", + "eststo": "eststo", + "esttab": "esttab", +} + +# libraries_gui.py +ctx.lists["user.code_libraries"] = { + "estout": "estout", +} + + +@ctx.action_class("user") +class UserActions: + # comment_line.py + def code_comment_line_prefix(): + actions.auto_insert("* ") + # actions.auto_insert("// ") + + # functions.py + def code_private_function(text: str): + result = "program {} \n\nend".format( + actions.user.formatted_text( + text, settings.get("user.code_private_function_formatter") + ) + ) + actions.user.paste(result) + actions.edit.up() + actions.key("tab") + + def code_default_function(text: str): + actions.user.code_private_function(text) + + def code_insert_named_argument(parameter_name: str): + actions.insert(f"{parameter_name} ") + + # functions_common.py + def code_insert_function(text: str, selection: str): + text += f" {selection or ''}" + actions.user.paste(text) + + # imperative.py + def code_block(): + actions.auto_insert("\n") + + def code_state_if(): + actions.insert("if {") + actions.key("enter tab enter left") + actions.insert("}") + actions.key("up:2 right:2") + + def code_state_else_if(): + actions.insert("else if {") + actions.key("enter tab enter left") + actions.insert("}") + actions.key("up:2 right:7") + + def code_state_else(): + actions.insert("else {") + actions.key("enter tab enter left") + actions.insert("}") + actions.key("up:2 right:4") + + def code_state_do(): + actions.insert("do {") + actions.key("enter tab enter left") + actions.insert("} while ()") + actions.key("up tab") + + def code_state_for(): + actions.insert("for () {") + actions.key("enter tab enter left") + actions.insert("}") + actions.key("up:2 right:4") + + def code_state_for_each(): + actions.insert("foreach in {") + actions.key("enter tab enter left") + actions.insert("}") + actions.key("up:2 right:7") + + def code_break(): + actions.insert("break") + + def code_next(): + actions.insert("continue") + + # libraries.py + def code_import(): + actions.auto_insert("ssc install ") + + # libraries_gui.py + def code_insert_library(text: str, selection: str): + actions.auto_insert("ssc install ") + actions.user.paste(text + selection) + + # operators_array.py + def code_operator_subscript(): + actions.user.insert_between("[", "]") + + # operators_assignment.py + def code_operator_assignment(): + actions.auto_insert(" = ") + + # operators_math.py + def code_operator_subtraction(): + actions.auto_insert(" - ") + + def code_operator_addition(): + actions.auto_insert(" + ") + + def code_operator_multiplication(): + actions.auto_insert(" * ") + + def code_operator_division(): + actions.auto_insert(" / ") + + def code_operator_modulo(): + actions.user.insert_between("mod(", ")") + + def code_operator_exponent(): + actions.auto_insert(" ^ ") + + def code_operator_equal(): + actions.auto_insert(" == ") + + def code_operator_not_equal(): + actions.auto_insert(" != ") + + def code_operator_greater_than(): + actions.auto_insert(" > ") + + def code_operator_less_than(): + actions.auto_insert(" < ") + + def code_operator_greater_than_or_equal_to(): + actions.auto_insert(" >= ") + + def code_operator_less_than_or_equal_to(): + actions.auto_insert(" <= ") + + def code_operator_and(): + actions.auto_insert(" & ") + + def code_operator_or(): + actions.auto_insert(" | ") diff --git a/lang/stata/stata.talon b/lang/stata/stata.talon new file mode 100644 index 0000000000..51e7062b62 --- /dev/null +++ b/lang/stata/stata.talon @@ -0,0 +1,28 @@ +code.language: stata +- +tag(): user.code_imperative + +tag(): user.code_comment_block_c_like +tag(): user.code_comment_block +tag(): user.code_comment_line +tag(): user.code_functions +tag(): user.code_functions_common +tag(): user.code_libraries +tag(): user.code_libraries_gui +tag(): user.code_operators_array +tag(): user.code_operators_assignment + +settings(): + user.code_private_function_formatter = "SNAKE_CASE" + +arg {user.code_parameter_name}: + user.code_insert_named_argument(code_parameter_name) + +# alternative to saying ""state import"" +s s c install: user.code_import() + +s s c install : + user.code_insert_library(code_libraries, "") + +toggle imports: user.code_toggle_libraries() +toggle packages: user.code_toggle_libraries() From 2d2db612e0720b2f04c962ff631f99cae070ba8d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:07:55 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- apps/stata/stata.py | 4 +--- lang/stata/stata.talon | 12 +++++------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/apps/stata/stata.py b/apps/stata/stata.py index aa9d667b2e..d72284ba5d 100644 --- a/apps/stata/stata.py +++ b/apps/stata/stata.py @@ -5,10 +5,8 @@ app: stata """ + @ctx.action_class("code") class CodeActions: def language(): return "stata" - - - \ No newline at end of file diff --git a/lang/stata/stata.talon b/lang/stata/stata.talon index 51e7062b62..2313cf3d48 100644 --- a/lang/stata/stata.talon +++ b/lang/stata/stata.talon @@ -15,14 +15,12 @@ tag(): user.code_operators_assignment settings(): user.code_private_function_formatter = "SNAKE_CASE" -arg {user.code_parameter_name}: - user.code_insert_named_argument(code_parameter_name) +arg {user.code_parameter_name}: user.code_insert_named_argument(code_parameter_name) # alternative to saying ""state import"" -s s c install: user.code_import() +s s c install: user.code_import() -s s c install : - user.code_insert_library(code_libraries, "") +s s c install : user.code_insert_library(code_libraries, "") -toggle imports: user.code_toggle_libraries() -toggle packages: user.code_toggle_libraries() +toggle imports: user.code_toggle_libraries() +toggle packages: user.code_toggle_libraries() From df5bc29671fed7b344307bdd112240d55650b780 Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Fri, 15 Mar 2024 16:14:30 +0100 Subject: [PATCH 03/12] add stata file extensions to language_modes.py --- core/modes/language_modes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/core/modes/language_modes.py b/core/modes/language_modes.py index 9a4f74f7fd..686574195f 100644 --- a/core/modes/language_modes.py +++ b/core/modes/language_modes.py @@ -36,6 +36,7 @@ "scss": "scss", # 'snippets': 'snippets', "sql": "sql", + "stata": "do ado", "talon": "talon", "terraform": "tf", "tex": "tex", From af5680a255a32fa890912adca8ef9a509ed52d61 Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:59:51 +0200 Subject: [PATCH 04/12] capitals for single letter spelling --- lang/stata/stata.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lang/stata/stata.py b/lang/stata/stata.py index dd2da87219..cd8b9b7580 100644 --- a/lang/stata/stata.py +++ b/lang/stata/stata.py @@ -9,8 +9,8 @@ # functions.py ctx.lists["user.code_parameter_name"] = { # regressions - "v c e cluster": "vce(cluster)", - "v c e robust": "vce(robust)", + "V C E cluster": "vce(cluster)", + "V C E robust": "vce(robust)", } # functions_common.py From 5ab28253a599ca7f2d8beb9db775a8acc3d0d748 Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:01:54 +0200 Subject: [PATCH 05/12] remove line that was commented out --- lang/stata/stata.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lang/stata/stata.py b/lang/stata/stata.py index cd8b9b7580..daccdbf3a0 100644 --- a/lang/stata/stata.py +++ b/lang/stata/stata.py @@ -39,7 +39,6 @@ class UserActions: # comment_line.py def code_comment_line_prefix(): actions.auto_insert("* ") - # actions.auto_insert("// ") # functions.py def code_private_function(text: str): From 160d8227d7109bc4c540a1dcd51856c662f460ae Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:01:42 +0200 Subject: [PATCH 06/12] Rework the statements --- lang/stata/stata.py | 42 +++++++++++++++--------------------------- lang/stata/stata.talon | 2 ++ 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/lang/stata/stata.py b/lang/stata/stata.py index daccdbf3a0..ed284f814a 100644 --- a/lang/stata/stata.py +++ b/lang/stata/stata.py @@ -67,40 +67,28 @@ def code_block(): actions.auto_insert("\n") def code_state_if(): - actions.insert("if {") - actions.key("enter tab enter left") - actions.insert("}") - actions.key("up:2 right:2") - + actions.insert("if {\n\n}\n") + actions.key("up:2 tab up left") + def code_state_else_if(): - actions.insert("else if {") - actions.key("enter tab enter left") - actions.insert("}") - actions.key("up:2 right:7") + actions.insert("else if {\n\n}\n") + actions.key("up:2 tab up right:4") def code_state_else(): - actions.insert("else {") - actions.key("enter tab enter left") - actions.insert("}") - actions.key("up:2 right:4") - - def code_state_do(): - actions.insert("do {") - actions.key("enter tab enter left") - actions.insert("} while ()") - actions.key("up tab") + actions.insert("else {\n\n}\n") + actions.key("up:2 tab") def code_state_for(): - actions.insert("for () {") - actions.key("enter tab enter left") - actions.insert("}") - actions.key("up:2 right:4") + actions.insert("forval {\n\n}\n") + actions.key("up:2 tab up right:3") def code_state_for_each(): - actions.insert("foreach in {") - actions.key("enter tab enter left") - actions.insert("}") - actions.key("up:2 right:7") + actions.insert("foreach in {\n\n}\n") + actions.key("up:2 tab up right:4") + + def code_state_while(): + actions.insert("while {\n\n}\n") + actions.key("up:2 tab up right:2") def code_break(): actions.insert("break") diff --git a/lang/stata/stata.talon b/lang/stata/stata.talon index 2313cf3d48..13975e6792 100644 --- a/lang/stata/stata.talon +++ b/lang/stata/stata.talon @@ -17,6 +17,8 @@ settings(): arg {user.code_parameter_name}: user.code_insert_named_argument(code_parameter_name) +state for val: user.code_state_for() + # alternative to saying ""state import"" s s c install: user.code_import() From 3a77a1d8574209cdf8d5a4ea9e2ebcaa87ebdaa4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:02:01 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lang/stata/stata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/stata/stata.py b/lang/stata/stata.py index ed284f814a..96bddc1c6e 100644 --- a/lang/stata/stata.py +++ b/lang/stata/stata.py @@ -69,7 +69,7 @@ def code_block(): def code_state_if(): actions.insert("if {\n\n}\n") actions.key("up:2 tab up left") - + def code_state_else_if(): actions.insert("else if {\n\n}\n") actions.key("up:2 tab up right:4") From f6e082e5360b711fb9fc978edf131f2d353db748 Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Tue, 4 Jun 2024 09:59:34 +0200 Subject: [PATCH 08/12] remove stata.talon --- apps/stata/stata.talon | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 apps/stata/stata.talon diff --git a/apps/stata/stata.talon b/apps/stata/stata.talon deleted file mode 100644 index 00e4beb821..0000000000 --- a/apps/stata/stata.talon +++ /dev/null @@ -1,7 +0,0 @@ -# Commands for the Stata Do-File Editor -os: windows -app: stata -- -settings(): - key_wait = 4 - insert_wait = 7 From 1e150aabeca1c375e8ad2cb3bd5a3d88b7ca0d79 Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Tue, 4 Jun 2024 10:13:54 +0200 Subject: [PATCH 09/12] improve matching for the do-file editor --- apps/stata/stata_do_file_editor.talon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/stata/stata_do_file_editor.talon b/apps/stata/stata_do_file_editor.talon index 480c3c668d..0362b7b191 100644 --- a/apps/stata/stata_do_file_editor.talon +++ b/apps/stata/stata_do_file_editor.talon @@ -1,7 +1,7 @@ # Commands for the Stata Do-File Editor os: windows app: stata -win.title: Do-file Editor +win.title: /^Do-file Editor/ - do this: key(ctrl-d) From 9e1c1e750ababd782c986498510ad88a31da5e33 Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Wed, 5 Jun 2024 12:10:17 +0200 Subject: [PATCH 10/12] Explicitly declare stata app --- apps/stata/stata.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/stata/stata.py b/apps/stata/stata.py index d72284ba5d..dde2dba911 100644 --- a/apps/stata/stata.py +++ b/apps/stata/stata.py @@ -1,11 +1,19 @@ -from talon import Context +from talon import Module, Context +mod = Module() ctx = Context() + +mod.apps.stata = r""" +os: windows +and app.name: Stata +os: windows +and app.exe: /^statase\-64\.exe$/i +""" + ctx.matches = r""" app: stata """ - @ctx.action_class("code") class CodeActions: def language(): From fadbe3d50eb5b2e2f73702461661dbf948ace593 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 5 Jun 2024 10:10:51 +0000 Subject: [PATCH 11/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- apps/stata/stata.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/stata/stata.py b/apps/stata/stata.py index dde2dba911..9223987b11 100644 --- a/apps/stata/stata.py +++ b/apps/stata/stata.py @@ -1,4 +1,4 @@ -from talon import Module, Context +from talon import Context, Module mod = Module() ctx = Context() @@ -14,6 +14,7 @@ app: stata """ + @ctx.action_class("code") class CodeActions: def language(): From 04d68bf8e4e96bcf524cd466480987abfcf09cde Mon Sep 17 00:00:00 2001 From: "max.bruening" <56445556+maxbruening@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:35:06 +0200 Subject: [PATCH 12/12] Make loop statements more robust --- lang/stata/stata.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lang/stata/stata.py b/lang/stata/stata.py index 96bddc1c6e..30821c33f7 100644 --- a/lang/stata/stata.py +++ b/lang/stata/stata.py @@ -67,28 +67,38 @@ def code_block(): actions.auto_insert("\n") def code_state_if(): - actions.insert("if {\n\n}\n") - actions.key("up:2 tab up left") + actions.insert("if {\n\n}") + actions.key("up tab up") + actions.edit.line_end() + actions.key("left:2") def code_state_else_if(): - actions.insert("else if {\n\n}\n") - actions.key("up:2 tab up right:4") + actions.insert("else if {\n\n}") + actions.key("up tab up") + actions.edit.line_end() + actions.key("left:2") def code_state_else(): - actions.insert("else {\n\n}\n") - actions.key("up:2 tab") + actions.insert("else {\n\n}") + actions.key("up tab") def code_state_for(): - actions.insert("forval {\n\n}\n") - actions.key("up:2 tab up right:3") + actions.insert("forval {\n\n}") + actions.key("up tab up") + actions.edit.line_end() + actions.key("left:2") def code_state_for_each(): - actions.insert("foreach in {\n\n}\n") - actions.key("up:2 tab up right:4") + actions.insert("foreach in {\n\n}") + actions.key("up tab up") + actions.edit.line_end() + actions.key("left:2") def code_state_while(): - actions.insert("while {\n\n}\n") - actions.key("up:2 tab up right:2") + actions.insert("while {\n\n}") + actions.key("up tab up") + actions.edit.line_end() + actions.key("left:2") def code_break(): actions.insert("break")