Refactor controller architecture and multi-insert state
- Rename ControllerContext to ControllerMessageBus for clarity - Switch ControllerBase to use SingletonRaised - Replace MarkEventsMixin with MarkerManager for multi-cursor editing - Add populate_popup event support for source view context menus - Remove unused swap file events - Moved JSON prettify feature to plugin - Fix event name: "removed_file" -> "remove_file"
This commit is contained in:
55
plugins/code/prettify_json/plugin.py
Normal file
55
plugins/code/prettify_json/plugin.py
Normal file
@@ -0,0 +1,55 @@
|
||||
# Python imports
|
||||
import json
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
|
||||
gi.require_version('Gtk', '3.0')
|
||||
|
||||
from gi.repository import Gtk
|
||||
|
||||
# Application imports
|
||||
from libs.event_factory import Event_Factory, Code_Event_Types
|
||||
|
||||
from plugins.plugin_types import PluginCode
|
||||
|
||||
|
||||
|
||||
def _load_prettify_json(buffer, menu):
|
||||
menu.append( Gtk.SeparatorMenuItem() )
|
||||
|
||||
def on_prettify_json(menuitem, buffer):
|
||||
start_itr, \
|
||||
end_itr = buffer.get_start_iter(), buffer.get_end_iter()
|
||||
data = buffer.get_text(start_itr, end_itr, False)
|
||||
text = json.dumps(json.loads(data), separators = (',', ':'), indent = 4)
|
||||
|
||||
buffer.begin_user_action()
|
||||
buffer.delete(start_itr, end_itr)
|
||||
buffer.insert(start_itr, text)
|
||||
buffer.end_user_action()
|
||||
|
||||
item = Gtk.MenuItem(label = "Prettify JSON")
|
||||
item.connect("activate", on_prettify_json, buffer)
|
||||
menu.append(item)
|
||||
|
||||
|
||||
|
||||
class Plugin(PluginCode):
|
||||
def __init__(self):
|
||||
super(Plugin, self).__init__()
|
||||
|
||||
|
||||
def _controller_message(self, event: Code_Event_Types.CodeEvent):
|
||||
if isinstance(event, Code_Event_Types.PopulateSourceViewPopupEvent):
|
||||
language = event.buffer.get_language()
|
||||
if not language: return
|
||||
|
||||
if language.get_id() == "json":
|
||||
_load_prettify_json(event.buffer, event.menu)
|
||||
|
||||
def load(self):
|
||||
...
|
||||
|
||||
def run(self):
|
||||
...
|
||||
Reference in New Issue
Block a user