Fixed new line indenting logic

This commit is contained in:
itdominator 2023-10-31 00:24:13 -05:00
parent d90ed2fdc8
commit a449ca6807
2 changed files with 45 additions and 21 deletions

View File

@ -11,7 +11,7 @@ from gi.repository import Gtk
class MarkEventsMixin:
def keyboard_insert_mark(self, target_iter = None, is_keyboard_insert = True):
buffer = self.get_buffer()
@ -94,12 +94,19 @@ class MarkEventsMixin:
self.end_user_action(buffer)
def _new_line_on_multi_line_markers(self, buffer):
self.insert_indent_handler(buffer)
self.freeze_multi_line_insert = True
iter = buffer.get_iter_at_mark( buffer.get_insert() )
self._base_indent(buffer, iter)
self.insert_indent_handler(buffer, iter)
for mark in self._multi_insert_marks:
iter = buffer.get_iter_at_mark(mark)
self._base_indent(buffer, iter)
self.insert_indent_handler(buffer, iter)
self.freeze_multi_line_insert = False
self.end_user_action(buffer)
def insert_indent_handler(self, buffer, iter = None):
@ -107,17 +114,20 @@ class MarkEventsMixin:
iter = buffer.get_iter_at_mark( buffer.get_insert() )
iter_copy = iter.copy()
iter_moved = iter_copy.backward_char()
if iter_moved:
_char = iter_copy.get_char()
self._base_indent(buffer, iter, iter_copy)
if _char in ["{", ":"]:
self._indent_deeper(buffer, iter)
iter_copy.backward_sentence_start()
iter_copy.forward_sentence_end()
iter_copy.backward_char()
_char = iter_copy.get_char()
if _char in ["{", ":"]:
self._indent_deeper(buffer, iter)
return True
def _base_indent(self, buffer, iter, iter_copy):
def _base_indent(self, buffer, iter, iter_copy = None):
if not iter_copy:
iter_copy = iter.copy()
line_num = iter_copy.get_line()
iter_copy = buffer.get_iter_at_line(line_num)

View File

@ -83,6 +83,7 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
self.connect("drag-data-received", self._on_drag_data_received)
self.connect("key-press-event", self._key_press_event)
self.connect("key-release-event", self._key_release_event)
self.connect("button-press-event", self._button_press_event)
self.connect("scroll-event", self._scroll_event)
@ -182,21 +183,31 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
with buffer.freeze_notify():
GLib.idle_add(self._delete_on_multi_line_markers, *(buffer,))
if keyname in {"Return", "Enter"}:
self.begin_user_action(buffer)
with buffer.freeze_notify():
GLib.idle_add(self._new_line_on_multi_line_markers, *(buffer,))
else:
...
return True
if keyname in {"Return", "Enter"}:
return self.insert_indent_handler(buffer)
# NOTE: if a plugin recieves the call and handles, it will be the final decider for propigation
return event_system.emit_and_await("autopairs", (keyname, is_control, is_alt, is_shift))
def _key_release_event(self, widget, eve):
if self.freeze_multi_line_insert: return
keyname = Gdk.keyval_name(eve.keyval)
modifiers = Gdk.ModifierType(eve.get_state() & ~Gdk.ModifierType.LOCK_MASK)
is_control = True if modifiers & Gdk.ModifierType.CONTROL_MASK else False
is_shift = True if modifiers & Gdk.ModifierType.SHIFT_MASK else False
buffer = self.get_buffer()
if keyname in {"Return", "Enter"}:
if len(self._multi_insert_marks) > 0:
self.begin_user_action(buffer)
with buffer.freeze_notify():
GLib.idle_add(self._new_line_on_multi_line_markers, *(buffer,))
return
has_selection = buffer.get_has_selection()
if not has_selection:
return self.insert_indent_handler(buffer)
def _button_press_event(self, widget = None, eve = None, user_data = None):
if eve.type == Gdk.EventType.BUTTON_PRESS and eve.button == 1 : # l-click
@ -285,4 +296,7 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
except Exception as e:
gfile = Gio.File.new_for_path(uri)
event_system.emit('create_view', (gfile,))
event_system.emit('create_view', (gfile,))
def get_filetype(self):
return self._current_filetype