Files
Newton-Editor/plugins/code/ui/markdown_preview/plugin.py
itdominator 77a3b71d31 feat: Complete plugin lifecycle management with lazy loading and runtime reload
Major changes:
- Add unload() method to all plugins for proper cleanup (unregister commands/providers/LSP clients, destroy widgets, clear state)
- Implement lazy widget loading via "show" signal across all containers
- Add autoload: false manifest option for manual/conditional plugin loading
- Add Plugins UI with runtime load/unload toggle via Ctrl+Shift+p
- Implement controller unregistration system with proper signal disconnection
- Add new events: UnregisterCommandEvent, GetFilesEvent, GetSourceViewsEvent, TogglePluginsUiEvent
- Fix signal leaks by tracking and disconnecting handlers in widgets (search/replace, LSP manager, tabs, telescope, markdown preview)
- Add Save/Save As to tabs context menu
- Improve search/replace behavior (selection handling, focus management)
- Add telescope file initialization from existing loaded files
- Refactor plugin reload watcher to dynamically add/remove plugins on filesystem changes
- Add new plugins: file_history, extend_source_view_menu, godot_lsp_client
- Fix bug in prettify_json (undefined variable reference)
2026-03-21 13:22:20 -05:00

93 lines
2.5 KiB
Python

# Python imports
# Lib imports
# Application imports
from libs.event_factory import Event_Factory, Code_Event_Types
from plugins.plugin_types import PluginCode
from .markdown_preview import MarkdownPreview
markdown_preview = MarkdownPreview()
class Plugin(PluginCode):
def __init__(self):
super(Plugin, self).__init__()
def _set_file(self, buffer):
event = Event_Factory.create_event(
"get_file", buffer = buffer
)
self.emit_to("files", event)
if not event.response or not event.response.get_location(): return
markdown_preview.fpath = event.response.get_location().get_path()
def _controller_message(self, event: Code_Event_Types.CodeEvent):
if isinstance(event, Code_Event_Types.FocusedViewEvent):
self._set_file(event.view.get_buffer())
markdown_preview._do_markdown_translate(event.view.get_buffer())
elif isinstance(event, Code_Event_Types.TextChangedEvent):
self._set_file(event.buffer)
markdown_preview._do_markdown_translate(event.buffer)
def load(self):
separator_right = self.request_ui_element("separator-right")
markdown_preview.set_relative_to(separator_right)
event = Event_Factory.create_event("register_command",
command_name = "tggle_markdown_preview",
command = Handler,
binding_mode = "released",
binding = "<Shift><Control>m"
)
self.emit_to("source_views", event)
def unload(self):
event = Event_Factory.create_event("unregister_command",
command_name = "tggle_markdown_preview",
command = Handler,
binding_mode = "released",
binding = "<Shift><Control>m"
)
self.emit_to("source_views", event)
markdown_preview.destroy()
def run(self):
...
class Handler:
@staticmethod
def execute(
view: any,
*args,
**kwargs
):
logger.debug("Command: Markdown Preview")
if not markdown_preview.can_hide:
markdown_preview.can_hide = True
if markdown_preview.is_visible():
markdown_preview.popdown()
return
file = view.command.exec("get_current_file")
if not file or not file.get_location(): return
buffer = view.get_buffer()
markdown_preview.popup()
markdown_preview.fpath = file.get_location().get_path()
markdown_preview._do_markdown_translate(buffer)