disabled completion for now; finalized colorize plugin

This commit is contained in:
itdominator 2023-10-14 14:27:42 -05:00
parent d6d9ce54bd
commit eb149270b0
2 changed files with 69 additions and 45 deletions

View File

@ -39,6 +39,7 @@ class Plugin(PluginBase):
self.name = "Colorize" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus self.name = "Colorize" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
# where self.name should not be needed for message comms # where self.name should not be needed for message comms
self.tag_stub_name = "colorize_tag"
def run(self): def run(self):
... ...
@ -48,34 +49,75 @@ class Plugin(PluginBase):
def subscribe_to_events(self): def subscribe_to_events(self):
self._event_system.subscribe("set_active_src_view", self._set_active_src_view) self._event_system.subscribe("set_active_src_view", self._set_active_src_view)
event_system.subscribe("buffer_changed_first_load", self._buffer_changed_first_load) self._event_system.subscribe("buffer_changed_first_load", self._buffer_changed_first_load)
event_system.subscribe("buffer_changed", self._buffer_changed) self._event_system.subscribe("buffer_changed", self._buffer_changed)
def _set_active_src_view(self, source_view): def _set_active_src_view(self, source_view):
self._active_src_view = source_view self._active_src_view = source_view
def _buffer_changed_first_load(self, buffer): def _buffer_changed_first_load(self, buffer):
self._handle(buffer)
def _buffer_changed(self, buffer):
tag_table = buffer.get_tag_table()
mark = buffer.get_insert()
iter = buffer.get_iter_at_mark(mark)
tags = iter.get_tags()
iter.forward_line() # NOTE: Jump to start of next line
end = iter.copy()
iter.backward_line() # NOTE: To now easily get start of prior line
start = iter.copy()
for tag in tags:
if self.tag_stub_name in tag.props.name:
buffer.remove_tag(tag, start, end)
tag_table.remove(tag)
self._handle(buffer, start, end)
def _handle(self, buffer = None, start_itr = None, end_itr = None):
# rgb(a), hsl, hsv # rgb(a), hsl, hsv
results = self.finalize_non_hex_matches( self.collect_preliminary_results(buffer) ) results = self.finalize_non_hex_matches( self.collect_preliminary_results(buffer, start_itr, end_itr) )
self.process_results(buffer, results) self.process_results(buffer, results)
# hex color search # hex color search
results = self.finalize_hex_matches( self.collect_preliminary_hex_results(buffer) ) results = self.finalize_hex_matches( self.collect_preliminary_hex_results(buffer, start_itr, end_itr) )
self.process_results(buffer, results) self.process_results(buffer, results)
def _buffer_changed(self): def collect_preliminary_results(self, buffer = None, start_itr = None, end_itr = None):
... if not buffer: return []
if not start_itr:
start_itr = buffer.get_start_iter()
def search(self, start_itr = None, query = None): results1 = self.search(start_itr, end_itr, "rgb")
results2 = self.search(start_itr, end_itr, "hsl")
results3 = self.search(start_itr, end_itr, "hsv")
return results1 + results2 + results3
def collect_preliminary_hex_results(self, buffer = None, start_itr = None, end_itr = None):
if not buffer: return []
if not start_itr:
start_itr = buffer.get_start_iter()
results1 = self.search(start_itr, end_itr, "#")
return results1
def search(self, start_itr = None, end_itr = None, query = None):
if not start_itr or not query: return None, None if not start_itr or not query: return None, None
results = [] results = []
_flags = Gtk.TextSearchFlags.VISIBLE_ONLY & Gtk.TextSearchFlags.TEXT_ONLY flags = Gtk.TextSearchFlags.VISIBLE_ONLY & Gtk.TextSearchFlags.TEXT_ONLY
while True: while True:
result = start_itr.forward_search(query, flags = _flags, limit = None) result = start_itr.forward_search(query, flags, end_itr)
if not result: break if not result: break
results.append(result) results.append(result)
@ -83,25 +125,6 @@ class Plugin(PluginBase):
return results return results
def collect_preliminary_results(self, buffer = None):
if not buffer: return []
start_itr = buffer.get_start_iter()
results1 = self.search(start_itr, "rgb")
results2 = self.search(start_itr, "hsl")
results3 = self.search(start_itr, "hsv")
return results1 + results2 + results3
def collect_preliminary_hex_results(self, buffer = None):
if not buffer: return []
start_itr = buffer.get_start_iter()
results1 = self.search(start_itr, "#")
return results1
def finalize_non_hex_matches(self, result_hits: [] = []): def finalize_non_hex_matches(self, result_hits: [] = []):
results = [] results = []
@ -142,23 +165,30 @@ class Plugin(PluginBase):
def process_results(self, buffer, results): def process_results(self, buffer, results):
# NOTE: HSV and HSL parsing are available in Gtk 4.0. Not lower... # NOTE: HSV and HSL parsing are available in Gtk 4.0. Not lower...
for start, end in results: for start, end in results:
text = buffer.get_text(start, end, include_hidden_chars = False) text = self.get_color_text(buffer, start, end)
color = Gdk.RGBA() color = Gdk.RGBA()
if color.parse(text):
tag = self.get_colorized_tag(buffer, text, color)
buffer.apply_tag(tag, start, end)
def get_color_text(self, buffer, start, end):
text = buffer.get_text(start, end, include_hidden_chars = False)
try:
if "hsl" in text: if "hsl" in text:
text = self.hsl_to_rgb(text) text = self.hsl_to_rgb(text)
if "hsv" in text: if "hsv" in text:
text = self.hsv_to_rgb(text) text = self.hsv_to_rgb(text)
except Exception as e:
...
if color.parse(text): return text
tag = self.get_colorized_tag(buffer, text, color)
buffer.apply_tag(tag, start, end)
def get_colorized_tag(self, buffer, tag, color: Gdk.RGBA): def get_colorized_tag(self, buffer, tag, color: Gdk.RGBA):
tag_table = buffer.get_tag_table() tag_table = buffer.get_tag_table()
colorize_tag = f"colorize_tag_{tag}" colorize_tag = f"{self.tag_stub_name}_{tag}"
search_tag = tag_table.lookup(f"colorize_tag_{tag}") search_tag = tag_table.lookup(colorize_tag)
if not search_tag: if not search_tag:
search_tag = buffer.create_tag(colorize_tag, background_rgba = color) search_tag = buffer.create_tag(colorize_tag, background_rgba = color)
@ -177,10 +207,7 @@ class Plugin(PluginBase):
s = None s = None
l = None l = None
try: h, s , l = int(_h) / 360, float(_s) / 100, float(_l) / 100
h, s , l = int(_h) / 360, float(_s) / 100, float(_l) / 100
except Exception as e:
raise
rgb = tuple(round(i * 255) for i in colorsys.hls_to_rgb(h, l, s)) rgb = tuple(round(i * 255) for i in colorsys.hls_to_rgb(h, l, s))
rgb_sub = ','.join(map(str, rgb)) rgb_sub = ','.join(map(str, rgb))
@ -201,10 +228,7 @@ class Plugin(PluginBase):
s = None s = None
v = None v = None
try: h, s , v = int(_h) / 360, float(_s) / 100, float(_v) / 100
h, s , v = int(_h) / 360, float(_s) / 100, float(_v) / 100
except Exception as e:
raise
rgb = tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h,s,v)) rgb = tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h,s,v))
rgb_sub = ','.join(map(str, rgb)) rgb_sub = ','.join(map(str, rgb))

View File

@ -108,8 +108,8 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
# example_completion_provider = ExampleCompletionProvider() # example_completion_provider = ExampleCompletionProvider()
# self._completion.add_provider(example_completion_provider) # self._completion.add_provider(example_completion_provider)
py_completion_provider = PythonCompletionProvider(file) # py_completion_provider = PythonCompletionProvider(file)
self._completion.add_provider(py_completion_provider) # self._completion.add_provider(py_completion_provider)
def _create_default_tag(self): def _create_default_tag(self):
@ -119,7 +119,7 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
def _is_modified(self, *args): def _is_modified(self, *args):
if not self._loading_file: if not self._loading_file:
event_system.emit("buffer_changed") event_system.emit("buffer_changed", (self._buffer, ))
else: else:
event_system.emit("buffer_changed_first_load", (self._buffer, )) event_system.emit("buffer_changed_first_load", (self._buffer, ))