2023-10-22 00:00:09 +00:00
|
|
|
# Python imports
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
from plugins.plugin_base import PluginBase
|
2023-11-01 03:04:08 +00:00
|
|
|
from . import cson
|
2023-10-22 00:00:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Plugin(PluginBase):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
2023-11-01 03:04:08 +00:00
|
|
|
self.name = "Snippets" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
|
|
|
|
# where self.name should not be needed for message comms
|
|
|
|
self.snippet_data = None
|
2023-11-07 05:01:18 +00:00
|
|
|
self._file_type = None
|
2023-11-01 03:04:08 +00:00
|
|
|
self.active_snippit_group = None
|
|
|
|
self.snippit_groups = []
|
|
|
|
self.snippit_prefixes = []
|
|
|
|
self.snippit_group_keys = []
|
2023-10-22 00:00:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_reference_ui_element(self):
|
2023-11-01 03:04:08 +00:00
|
|
|
...
|
2023-10-22 00:00:09 +00:00
|
|
|
|
|
|
|
def run(self):
|
2023-11-01 03:04:08 +00:00
|
|
|
with open('snippets.cson', 'rb') as f:
|
|
|
|
self.snippet_data = cson.load(f)
|
|
|
|
self.snippit_groups = self.snippet_data.keys()
|
|
|
|
|
|
|
|
def subscribe_to_events(self):
|
|
|
|
self._event_system.subscribe("set_active_src_view", self._set_active_src_view)
|
|
|
|
self._event_system.subscribe("show_snippets_ui", self._show_snippets_ui)
|
|
|
|
self._event_system.subscribe("buffer_changed_first_load", self._buffer_changed_first_load)
|
|
|
|
self._event_system.subscribe("buffer_changed", self._buffer_changed)
|
|
|
|
|
|
|
|
|
|
|
|
def _set_active_src_view(self, source_view):
|
|
|
|
self._active_src_view = source_view
|
2023-11-07 05:01:18 +00:00
|
|
|
self._buffer = source_view.get_buffer()
|
|
|
|
self._file_type = source_view.get_filetype()
|
2023-11-01 03:04:08 +00:00
|
|
|
self._tag_table = self._buffer.get_tag_table()
|
|
|
|
|
|
|
|
self.load_target_snippt_group()
|
|
|
|
|
|
|
|
def load_target_snippt_group(self):
|
|
|
|
self.active_snippit_group = None
|
|
|
|
for group in self.snippit_groups:
|
2023-11-07 05:01:18 +00:00
|
|
|
if group in self._file_type:
|
2023-11-01 03:04:08 +00:00
|
|
|
self.active_snippit_group = group
|
|
|
|
break
|
|
|
|
|
|
|
|
if self.active_snippit_group:
|
|
|
|
self.snippit_prefixes.clear()
|
|
|
|
keys = self.snippet_data[self.active_snippit_group].keys()
|
|
|
|
|
|
|
|
self.snippit_group_keys.clear()
|
|
|
|
for key in keys:
|
|
|
|
self.snippit_group_keys.append(key)
|
|
|
|
prefix = self.snippet_data[self.active_snippit_group][key]["prefix"]
|
|
|
|
self.snippit_prefixes.append(prefix)
|
|
|
|
|
|
|
|
def _buffer_changed_first_load(self, buffer):
|
|
|
|
self._buffer = buffer
|
|
|
|
self._handle_update(buffer)
|
|
|
|
|
|
|
|
def _buffer_changed(self, buffer):
|
|
|
|
self._event_system.emit("pause_event_processing")
|
|
|
|
self._handle_update(buffer)
|
|
|
|
self._event_system.emit("resume_event_processing")
|
|
|
|
|
|
|
|
|
|
|
|
def _show_snippets_ui(self):
|
|
|
|
print(f"Data: {self.snippit_groups}")
|
|
|
|
|
|
|
|
def _handle_update(self, buffer):
|
|
|
|
if not self.active_snippit_group: return
|
|
|
|
|
|
|
|
end_iter = buffer.get_iter_at_mark( buffer.get_insert() )
|
|
|
|
start_iter = end_iter.copy()
|
|
|
|
start_iter.backward_word_start()
|
|
|
|
|
|
|
|
matches = []
|
|
|
|
text = buffer.get_text(start_iter, end_iter, include_hidden_chars = False)
|
|
|
|
for prefix in self.snippit_prefixes:
|
|
|
|
if text in prefix:
|
|
|
|
matches.append(prefix)
|
|
|
|
|
|
|
|
snippits = []
|
|
|
|
for _match in matches:
|
|
|
|
for key in self.snippit_group_keys:
|
|
|
|
prefix = self.snippet_data[self.active_snippit_group][key]["prefix"]
|
|
|
|
if prefix == _match:
|
|
|
|
body = self.snippet_data[self.active_snippit_group][key]["body"]
|
|
|
|
snippits.append(body)
|
2023-10-22 00:00:09 +00:00
|
|
|
|
2023-11-09 05:52:23 +00:00
|
|
|
# print(snippits)
|
|
|
|
print("Snippits Plugin: _handle_update > results > stub...")
|