Added comment toggleplugin

This commit is contained in:
itdominator 2023-10-15 23:56:06 -05:00
parent 7aca4d6960
commit b1a032776e
12 changed files with 288 additions and 16 deletions

View File

@ -0,0 +1,3 @@
"""
Pligin Module
"""

View File

@ -0,0 +1,3 @@
"""
Pligin Package
"""

View File

@ -0,0 +1,65 @@
# Python imports
# Lib imports
# Application imports
class AddCommentMixin:
def add_comment_characters(self, buffer, start_tag, end_tag, start, end, deselect, oldPos):
smark = buffer.create_mark("start", start, False)
imark = buffer.create_mark("iter", start, False)
emark = buffer.create_mark("end", end, False)
number_lines = end.get_line() - start.get_line() + 1
comment_pos_iter = None
count = 0
buffer.begin_user_action()
for i in range(0, number_lines):
iter = buffer.get_iter_at_mark(imark)
if not comment_pos_iter:
(comment_pos_iter, count) = self.discard_white_spaces(iter)
if self.is_commented(comment_pos_iter, start_tag):
new_code = self.remove_comment_characters(buffer, start_tag, end_tag, start, end)
return
else:
comment_pos_iter = iter
for i in range(count):
c = iter.get_char()
if not c in (" ", "\t"):
break
iter.forward_char()
buffer.insert(comment_pos_iter, start_tag)
buffer.insert(comment_pos_iter, " ")
if end_tag:
if i != number_lines -1:
iter = buffer.get_iter_at_mark(imark)
iter.forward_to_line_end()
buffer.insert(iter, end_tag)
else:
iter = buffer.get_iter_at_mark(emark)
buffer.insert(iter, end_tag)
iter = buffer.get_iter_at_mark(imark)
iter.forward_line()
buffer.delete_mark(imark)
imark = buffer.create_mark("iter", iter, True)
buffer.end_user_action()
buffer.delete_mark(imark)
new_start = buffer.get_iter_at_mark(smark)
new_end = buffer.get_iter_at_mark(emark)
buffer.select_range(new_start, new_end)
buffer.delete_mark(smark)
buffer.delete_mark(emark)
if deselect:
oldPosIter = buffer.get_iter_at_offset(oldPos + 2)
buffer.place_cursor(oldPosIter)

View File

@ -0,0 +1,30 @@
# Python imports
# Lib imports
# Application imports
class CodeCommentTags:
def get_comment_tags(self, lang):
(s, e) = self.get_line_comment_tags(lang)
if (s, e) == (None, None):
(s, e) = self.get_block_comment_tags(lang)
return (s, e)
def get_block_comment_tags(self, lang):
start_tag = lang.get_metadata('block-comment-start')
end_tag = lang.get_metadata('block-comment-end')
if start_tag and end_tag:
return (start_tag, end_tag)
return (None, None)
def get_line_comment_tags(self, lang):
start_tag = lang.get_metadata('line-comment-start')
if start_tag:
return (start_tag, None)
return (None, None)

View File

@ -0,0 +1,13 @@
{
"manifest": {
"name": "Commentzar",
"author": "ITDominator",
"credit": "Hamad Al Marri",
"version": "0.0.1",
"support": "",
"requests": {
"pass_events": "true",
"bind_keys": ["Commentzar||keyboard_tggl_comment:<Control>slash"]
}
}
}

View File

@ -0,0 +1,117 @@
# 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
from .codecomment_tags import CodeCommentTags
from .remove_comment_mixin import RemoveCommentMixin
from .add_comment_mixin import AddCommentMixin
class Plugin(AddCommentMixin, RemoveCommentMixin, CodeCommentTags, PluginBase):
def __init__(self):
super().__init__()
self.name = "Commentzar" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
# where self.name should not be needed for message comms
def generate_reference_ui_element(self):
...
def run(self):
...
def subscribe_to_events(self):
self._event_system.subscribe("keyboard_tggl_comment", self._keyboard_tggl_comment)
self._event_system.subscribe("set_active_src_view", self._set_active_src_view)
def _set_active_src_view(self, source_view):
self._active_src_view = source_view
self._buffer = self._active_src_view.get_buffer()
self._tag_table = self._buffer.get_tag_table()
def _keyboard_tggl_comment(self):
buffer = self._buffer
lang = buffer.get_language()
if lang is None:
return
(start_tag, end_tag) = self.get_comment_tags(lang)
if not start_tag and not end_tag:
return
sel = buffer.get_selection_bounds()
currentPosMark = buffer.get_insert()
oldPos = 0
# if user selected chars or multilines
if sel != ():
deselect = False
(start, end) = sel
if not start.starts_line():
start.set_line_offset(0)
if not end.ends_line():
end.forward_to_line_end()
else:
deselect = True
start = buffer.get_iter_at_mark(currentPosMark)
oldPos = buffer.get_iter_at_mark(currentPosMark).get_offset()
start.set_line_offset(0)
end = start.copy()
if not end.ends_line():
end.forward_to_line_end()
if start.get_offset() == end.get_offset():
buffer.begin_user_action()
buffer.insert(start, start_tag)
buffer.insert(start, " ")
buffer.end_user_action()
return
new_code = self.add_comment_characters(buffer, start_tag, end_tag, start, end, deselect, oldPos)
def discard_white_spaces(self, iter):
count = 0
while not iter.ends_line():
c = iter.get_char()
if not c in (" ", "\t"):
return (iter, count)
iter.forward_char()
count += 1
return (iter, 0)
def is_commented(self, comment_pos_iter, start_tag):
head_iter = comment_pos_iter.copy()
self.forward_tag(head_iter, start_tag)
s = comment_pos_iter.get_slice(head_iter)
if s == start_tag:
return True
return False
def forward_tag(self, iter, tag):
iter.forward_chars(len(tag))
def backward_tag(self, iter, tag):
iter.backward_chars(len(tag))
def get_tag_position_in_line(self, tag, head_iter, iter):
while not iter.ends_line():
s = iter.get_slice(head_iter)
if s == tag:
return True
else:
head_iter.forward_char()
iter.forward_char()
return False

View File

@ -0,0 +1,49 @@
# Python imports
# Lib imports
# Application imports
class RemoveCommentMixin:
def remove_comment_characters(self, buffer, start_tag, end_tag, start, end):
smark = buffer.create_mark("start", start, False)
emark = buffer.create_mark("end", end, False)
number_lines = end.get_line() - start.get_line() + 1
iter = start.copy()
head_iter = iter.copy()
self.forward_tag(head_iter, start_tag)
buffer.begin_user_action()
for i in range(0, number_lines):
if self.get_tag_position_in_line(start_tag, head_iter, iter):
dmark = buffer.create_mark("delete", iter, False)
buffer.delete(iter, head_iter)
space_iter = head_iter.copy()
space_iter.forward_char()
s = head_iter.get_slice(space_iter)
if s == " ":
buffer.delete(head_iter, space_iter)
if end_tag:
iter = buffer.get_iter_at_mark(dmark)
head_iter = iter.copy()
self.forward_tag(head_iter, end_tag)
if self.get_tag_position_in_line(end_tag, head_iter, iter):
buffer.delete(iter, head_iter)
buffer.delete_mark(dmark)
iter = buffer.get_iter_at_mark(smark)
iter.forward_line()
buffer.delete_mark(smark)
head_iter = iter.copy()
self.forward_tag(head_iter, start_tag)
smark = buffer.create_mark("iter", iter, True)
buffer.end_user_action()
buffer.delete_mark(smark)
buffer.delete_mark(emark)

View File

@ -54,8 +54,6 @@ class EditorControllerMixin:
self.move_lines_up(source_view)
if action == "move_lines_down":
self.move_lines_down(source_view)
if action == "keyboard_tggl_comment":
self.keyboard_tggl_comment(source_view)
if action == "set_buffer_language":
self.set_buffer_language(source_view, query)
if action == "set_buffer_style":
@ -63,4 +61,4 @@ class EditorControllerMixin:
if action == "save_file":
source_view.save_file()
if action == "save_file_as":
source_view.save_file_as()
source_view.save_file_as()

View File

@ -128,11 +128,8 @@ class EditorEventsMixin:
def move_lines_down(self, source_view):
source_view.move_lines_down()
def keyboard_tggl_comment(self, source_view):
source_view.keyboard_tggl_comment()
def set_buffer_language(self, source_view, language = "python3"):
source_view.set_buffer_language(language)
def set_buffer_style(self, source_view, style = settings.theming.syntax_theme):
source_view.set_buffer_style(style)
source_view.set_buffer_style(style)

View File

@ -76,7 +76,6 @@ class EditorNotebook(EditorEventsMixin, EditorControllerMixin, Gtk.Notebook):
event_system.subscribe("keyboard_move_tab_left", self._keyboard_move_tab_left)
event_system.subscribe("keyboard_move_tab_right", self._keyboard_move_tab_right)
event_system.subscribe("keyboard_insert_mark", self._keyboard_insert_mark)
event_system.subscribe("keyboard_tggl_comment", self._keyboard_tggl_comment)
event_system.subscribe("keyboard_clear_marks", self._keyboard_clear_marks)
event_system.subscribe("keyboard_move_tab_to_1", self._keyboard_move_tab_to_1)
event_system.subscribe("keyboard_move_tab_to_2", self._keyboard_move_tab_to_2)
@ -191,9 +190,6 @@ class EditorNotebook(EditorEventsMixin, EditorControllerMixin, Gtk.Notebook):
def _move_lines_down(self):
self.action_controller("move_lines_down")
def _keyboard_tggl_comment(self):
self.action_controller("keyboard_tggl_comment")
def _keyboard_clear_marks(self):
self.action_controller("keyboard_clear_marks")
@ -216,4 +212,4 @@ class EditorNotebook(EditorEventsMixin, EditorControllerMixin, Gtk.Notebook):
self.action_controller("save_file")
def _keyboard_save_file_as(self):
self.action_controller("save_file_as")
self.action_controller("save_file_as")

View File

@ -156,6 +156,10 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
if is_shift:
if keyname in [ "z", "Up", "Down", "Left", "Right" ]:
# NOTE: For now do like so for completion sake above.
if keyname in ["Left", "Right"]:
return False
return True
if is_alt:

View File

@ -82,9 +82,6 @@ class SourceViewEventsMixin(MarkEventsMixin, FileEventsMixin):
def action_comment_out_selection(self):
...
def keyboard_tggl_comment(self):
logger.info("SourceViewEventsMixin > keyboard_tggl_comment > stub...")
def keyboard_undo(self):
self._buffer.undo()
@ -108,4 +105,4 @@ class SourceViewEventsMixin(MarkEventsMixin, FileEventsMixin):
if not gfile: return
event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None))
self.update_cursor_position()
self.update_cursor_position()