Safer insertwidth adaption by cells pluging
import re
from thonny.globals import get_workbench
-from thonny.codeview import PythonText
+from thonny.codeview import CodeViewText
from thonny import ui_utils
cell_regex = re.compile("(^|\n)(# ?%%|##)[^\n]*", re.MULTILINE) # @UndefinedVariable
@@ -14,14 +14,11 @@ def update_editor_cells(event):
if not getattr(text, "cell_tags_configured", False):
text.tag_configure("CURRENT_CELL",
borderwidth=1, relief="groove",
- #background="#f3ffed"
background="LightYellow"
)
text.tag_configure("CELL_HEADER",
font=get_workbench().get_font("BoldEditorFont"),
foreground="#665843",
- #background="Gray"
- #underline=True
)
text.cell_tags_configured = True
@@ -47,14 +44,6 @@ def update_editor_cells(event):
if prev_marker != 0:
cells.append((text.index("1.0+%dc" % prev_marker), "end"))
- if text.index("insert").endswith(".0"):
- # normal insertion cursor is not well visible when
- # in the left edge of the cell box
- text["insertwidth"] = 3
- else:
- text["insertwidth"] = 2
-
-
for start_index, end_index in cells:
if (text.compare(start_index, "<=", "insert")
and text.compare(end_index, ">", "insert")):
@@ -63,7 +52,7 @@ def update_editor_cells(event):
def _patch_perform_return():
- original_perform_return = PythonText.perform_return
+ original_perform_return = CodeViewText.perform_return
def _patched_perform_return(self, event):
text = event.widget
@@ -101,7 +90,33 @@ def _patch_perform_return():
else:
return original_perform_return(self, event)
- PythonText.perform_return = _patched_perform_return
+ CodeViewText.perform_return = _patched_perform_return
+
+
+
+def _patch_intercept_mark():
+ """Need to make cursor wider when in first column. Otherwise
+ the border of the cell box makes it hard to notice the cursor.
+
+ NB! Need to be careful with setting text["insertwidth"]!
+ My first straightforward solution caused unexplainable
+ infinite loop of insertions and deletions in the text
+ (insert a line and a word, select that word and then do Ctrl-Z).
+
+ Looks like this solution is safe, but I don't dare to include
+ it in the main code"""
+
+ original_intercept_mark = CodeViewText.intercept_mark
+ def _patched_intercept_mark(self, *args):
+ if args[:2] == ('set', 'insert') and args[2].endswith(".0"):
+ self.set_insertwidth(3)
+ else:
+ self.set_insertwidth(2)
+
+ original_intercept_mark(self, *args)
+
+ CodeViewText.intercept_mark = _patched_intercept_mark
+
def dummy(event=None):
"This is dummy method"
@@ -122,6 +137,7 @@ def load_plugin():
wb = get_workbench()
_patch_perform_return()
+ _patch_intercept_mark()
wb.add_command('run_cell', "run", 'Run cell',
handler=dummy, # actual handler is in the patch
