Akuli / porcupine

A decent editor written in tkinter
MIT License
161 stars 47 forks source link

`<<NotebookTabChanged>>` fires before `filetype_name` is known #1363

Open benjamin-kirkbride opened 1 year ago

benjamin-kirkbride commented 1 year ago
    isort_format_tab_action = actions.register_filetab_action(
        name="isort Format Tab",
        description="Sort Imports of open tab with isort",
        callback=partial(format_code_in_textwidget, "isort"),
        availability_callback=lambda tab: tab.settings.get("filetype_name", object) == "Python",
    )

Note availability_callback

When I open Porcupine:

porcupine._state ERROR: Error in tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.11/tkinter/__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "/home/bkirkbride/projects/porcupine/porcupine/menubar.py", line 297, in update_enabledness
    menu.entryconfig(index, state=("normal" if callback(tab) else "disabled"))
                                               ^^^^^^^^^^^^^
  File "/home/bkirkbride/projects/porcupine/porcupine/menubar.py", line 362, in <lambda>
    callback=lambda tab: isinstance(tab, tabs.FileTab) and action.availability_callback(tab),
                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bkirkbride/projects/porcupine/porcupine/plugins/python_tools.py", line 78, in <lambda>
    availability_callback=lambda tab: tab.settings.get("filetype_name", object) == "Python",
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bkirkbride/projects/porcupine/porcupine/settings.py", line 332, in get
    result = self._options[option_name].value
             ~~~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'filetype_name'

And the menu items are not there.

If I switch to another tab and switch back, then it works, no more errors.

I assume this is some sort of dependency issue where the options don't get loaded before the plugins do, or something. Thoughts on how to handle this?

Originally posted by @benjamin-kirkbride in https://github.com/Akuli/porcupine/issues/1349#issuecomment-1629851422

benjamin-kirkbride commented 1 year ago

This is also a problem when opening a tab for the first time. The filetype is not known in time. We need a new event for when the tab has changed and everything is "settled"

benjamin-kirkbride commented 1 year ago

I tried adding setup_after = ["filetypes"] to python_tools.py but that didn't seem to make a difference, not sure why though because I believe this is an issue with the order in which things are getting bound to <<NotebookTabChanged>>

benjamin-kirkbride commented 1 year ago

Not sure if my solution is the best/right way to do it, but it does seem robust (based on the test I made)

benjamin-kirkbride commented 1 year ago

If we are happy with the way this was done this can be closed