From 091db6af16b9ba22564b5fcb49b45100071ae373 Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Tue, 16 Sep 2025 02:48:48 -0300 Subject: [PATCH 1/4] 1st commit --- .../backend/py5_imported_mode_backend.py | 88 +++++++++---------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/thonnycontrib/backend/py5_imported_mode_backend.py b/thonnycontrib/backend/py5_imported_mode_backend.py index 547e8d6..0da87c3 100644 --- a/thonnycontrib/backend/py5_imported_mode_backend.py +++ b/thonnycontrib/backend/py5_imported_mode_backend.py @@ -1,64 +1,58 @@ '''thonny-py5mode backend - interacts with thonny-py5mode frontend (thonny-py5mode > __init__.py) -''' - -import ast -import os -import pathlib -import sys -from py5_tools import imported -from thonny import get_version -from thonny.common import InlineCommand, InlineResponse -try: # thonny 4 package layout - from thonny import jedi_utils - from thonny.plugins.cpython_backend import ( - get_backend, - MainCPythonBackend - ) - # add plug-in packages to packages path - # https://groups.google.com/g/thonny/c/dhMOGXZHTDU - from thonny import get_sys_path_directory_containg_plugins - sys.path.append(get_sys_path_directory_containg_plugins()) -except ImportError: # thonny 3 package layout - from thonny.plugins.cpython.cpython_backend import ( - get_backend, - MainCPythonBackend - ) + interacts with thonny-py5mode frontend (thonny-py5mode > __init__.py)''' + +from os import environ as env +from sys import path + +from typing import Optional, TypedDict, TypeAlias + +from thonny.common import InlineCommand, InlineResponse, CompletionInfo +from thonny.plugins.cpython_backend import MainCPythonBackend +from thonny import jedi_utils, get_sys_path_directory_containg_plugins + +path.append( get_sys_path_directory_containg_plugins() ) +AutoComplete: TypeAlias = dict[str, list[CompletionInfo] | str | None] + +class Command(TypedDict): + source: str + row: int + column: int + filename: str + sys_path: Optional[list[str]] + +def patched_editor_autocomplete(self: MainCPythonBackend, cmd: Command) -> AutoComplete: + '''Add py5 to autocompletion''' -def patched_editor_autocomplete( - self: MainCPythonBackend, cmd: InlineCommand) -> InlineResponse: - '''add py5 to autocompletion''' prefix = 'from py5 import *\n' + cmd['source'] = prefix + cmd['source'] cmd['row'] += 1 - if int(get_version()[0]) >= 4: # thonny 4 package layout - result = dict( - source=cmd.source, - row=cmd.row, - column=cmd.column, - filename=cmd.filename, - ) - result['completions'] = jedi_utils.get_script_completions( - **result, sys_path=[get_sys_path_directory_containg_plugins()] - ) - else: - result = get_backend()._original_editor_autocomplete(cmd) + result: Command = dict( + source=cmd.source, + row=cmd.row, + column=cmd.column, + filename=cmd.filename + ) + + result['completions'] = jedi_utils.get_script_completions( + **result, sys_path=[get_sys_path_directory_containg_plugins()]) result['row'] -= 1 result['source'] = result['source'][len(prefix):] + return result def load_plugin() -> None: - '''every thonny plug-in uses this function to load''' - if os.environ.get('PY5_IMPORTED_MODE', 'False').lower() == 'false': - return + '''Every Thonny plug-in uses this function to load''' + if env.get('PY5_IMPORTED_MODE', 'False').lower() == 'false': return - # note that _cmd_editor_autocomplete is not a public api - # may need to treat different thonny versions differently - # https://groups.google.com/g/thonny/c/wWCeXWpKy8c + # Note that _cmd_editor_autocomplete() is not a public API + # May need to treat different Thonny versions differently: + # https://groups.Google.com/g/thonny/c/wWCeXWpKy8c c_e_a = MainCPythonBackend._cmd_editor_autocomplete - MainCPythonBackend._original_editor_autocomplete = c_e_a + setattr(MainCPythonBackend, '_original_editor_autocomplete', c_e_a) + # MainCPythonBackend._original_editor_autocomplete = c_e_a MainCPythonBackend._cmd_editor_autocomplete = patched_editor_autocomplete From e590b3ea36db0779f6562502fefb5fa367e498c8 Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Tue, 16 Sep 2025 04:11:02 -0300 Subject: [PATCH 2/4] Mostly done --- .../backend/py5_imported_mode_backend.py | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/thonnycontrib/backend/py5_imported_mode_backend.py b/thonnycontrib/backend/py5_imported_mode_backend.py index 0da87c3..08a8f57 100644 --- a/thonnycontrib/backend/py5_imported_mode_backend.py +++ b/thonnycontrib/backend/py5_imported_mode_backend.py @@ -4,9 +4,9 @@ from os import environ as env from sys import path -from typing import Optional, TypedDict, TypeAlias +from typing import TypeAlias -from thonny.common import InlineCommand, InlineResponse, CompletionInfo +from thonny.common import CompletionInfo from thonny.plugins.cpython_backend import MainCPythonBackend from thonny import jedi_utils, get_sys_path_directory_containg_plugins @@ -14,35 +14,30 @@ AutoComplete: TypeAlias = dict[str, list[CompletionInfo] | str | None] -class Command(TypedDict): - source: str - row: int - column: int - filename: str - sys_path: Optional[list[str]] - -def patched_editor_autocomplete(self: MainCPythonBackend, cmd: Command) -> AutoComplete: +def patched_editor_autocomplete(self: MainCPythonBackend, cmd) -> AutoComplete: '''Add py5 to autocompletion''' prefix = 'from py5 import *\n' - cmd['source'] = prefix + cmd['source'] - cmd['row'] += 1 + cmd.source = prefix + cmd.source + cmd.row += 1 + + completions = jedi_utils.get_script_completions( + cmd.source, + cmd.row, + cmd.column, + cmd.filename, + sys_path=[get_sys_path_directory_containg_plugins()]) + + cmd.row -= 1 + cmd.source = cmd.source[len(prefix):] - result: Command = dict( + return dict( source=cmd.source, row=cmd.row, column=cmd.column, - filename=cmd.filename - ) - - result['completions'] = jedi_utils.get_script_completions( - **result, sys_path=[get_sys_path_directory_containg_plugins()]) - - result['row'] -= 1 - result['source'] = result['source'][len(prefix):] - - return result + filename=cmd.filename, + completions=completions) def load_plugin() -> None: @@ -54,5 +49,4 @@ def load_plugin() -> None: # https://groups.Google.com/g/thonny/c/wWCeXWpKy8c c_e_a = MainCPythonBackend._cmd_editor_autocomplete setattr(MainCPythonBackend, '_original_editor_autocomplete', c_e_a) - # MainCPythonBackend._original_editor_autocomplete = c_e_a MainCPythonBackend._cmd_editor_autocomplete = patched_editor_autocomplete From 8780c1c198363c715e668567a68670bbe38f42a4 Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Tue, 16 Sep 2025 16:04:38 -0300 Subject: [PATCH 3/4] dict() to {} --- .../backend/py5_imported_mode_backend.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/thonnycontrib/backend/py5_imported_mode_backend.py b/thonnycontrib/backend/py5_imported_mode_backend.py index 08a8f57..12c3ddb 100644 --- a/thonnycontrib/backend/py5_imported_mode_backend.py +++ b/thonnycontrib/backend/py5_imported_mode_backend.py @@ -27,17 +27,18 @@ def patched_editor_autocomplete(self: MainCPythonBackend, cmd) -> AutoComplete: cmd.row, cmd.column, cmd.filename, - sys_path=[get_sys_path_directory_containg_plugins()]) + sys_path=[ get_sys_path_directory_containg_plugins() ]) cmd.row -= 1 cmd.source = cmd.source[len(prefix):] - return dict( - source=cmd.source, - row=cmd.row, - column=cmd.column, - filename=cmd.filename, - completions=completions) + + return { + 'source': cmd.source, + 'row': cmd.row, + 'column': cmd.column, + 'filename': cmd.filename, + 'completions': completions } def load_plugin() -> None: @@ -47,6 +48,7 @@ def load_plugin() -> None: # Note that _cmd_editor_autocomplete() is not a public API # May need to treat different Thonny versions differently: # https://groups.Google.com/g/thonny/c/wWCeXWpKy8c + c_e_a = MainCPythonBackend._cmd_editor_autocomplete setattr(MainCPythonBackend, '_original_editor_autocomplete', c_e_a) MainCPythonBackend._cmd_editor_autocomplete = patched_editor_autocomplete From 07e844620638f0201bc80c8487ed9160d0d63fe7 Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Tue, 16 Sep 2025 16:05:19 -0300 Subject: [PATCH 4/4] dict() -> {} --- thonnycontrib/backend/py5_imported_mode_backend.py | 1 - 1 file changed, 1 deletion(-) diff --git a/thonnycontrib/backend/py5_imported_mode_backend.py b/thonnycontrib/backend/py5_imported_mode_backend.py index 12c3ddb..3e39500 100644 --- a/thonnycontrib/backend/py5_imported_mode_backend.py +++ b/thonnycontrib/backend/py5_imported_mode_backend.py @@ -32,7 +32,6 @@ def patched_editor_autocomplete(self: MainCPythonBackend, cmd) -> AutoComplete: cmd.row -= 1 cmd.source = cmd.source[len(prefix):] - return { 'source': cmd.source, 'row': cmd.row,