Added scroll to on file passed

This commit is contained in:
itdominator 2023-09-12 22:14:31 -05:00
parent 75b3313d27
commit 28c6acc345
7 changed files with 48 additions and 27 deletions

View File

@ -20,8 +20,9 @@ class Controller(SignalsMixins, ControllerData):
def __init__(self, args, unknownargs):
messages = []
for arg in unknownargs + [args.new_tab,]:
if os.path.isfile(arg):
messages.append(f"FILE|{arg}")
# NOTE: If passing line number with file split against :
if os.path.isfile(arg.replace("file://", "").split(":")[0]):
messages.append(f"FILE|{arg.replace('file://', '')}")
if len(messages) > 0:
settings.set_is_starting_with_file(True)

View File

@ -8,7 +8,7 @@ from ..sourceview_container import SourceViewContainer
class EditorEventsMixin:
def create_view(self, widget = None, eve = None, gfile = None):
def create_view(self, widget = None, eve = None, gfile = None, line: int = 0):
container = SourceViewContainer(self.close_tab)
page_num = self.append_page(container, container.get_tab_widget())
@ -18,13 +18,14 @@ class EditorEventsMixin:
ctx.add_class("notebook-unselected-focus")
self.set_tab_reorderable(container, True)
if gfile:
source_view = container.get_source_view()
source_view.open_file(gfile)
self.show_all()
self.set_current_page(page_num)
if gfile:
source_view = container.get_source_view()
source_view.open_file(gfile, line)
source_view.grab_focus()
def open_file(self, gfile):
page_num = self.get_current_page()
container = self.get_nth_page( page_num )
@ -113,4 +114,4 @@ class EditorEventsMixin:
source_view.scale_down_text()
def toggle_highlight_line(self, source_view):
source_view.toggle_highlight_line()
source_view.toggle_highlight_line()

View File

@ -126,14 +126,19 @@ class EditorNotebook(EditorEventsMixin, EditorControllerMixin, Gtk.Notebook):
source_view.update_cursor_position()
source_view.set_bottom_labels(gfile)
def _create_view(self, gfile = None):
def _create_view(self, gfile = None, line: int = 0):
if not self.is_editor_focused: # TODO: Find way to converge this
return
if isinstance(gfile, str):
gfile = Gio.File.new_for_path(gfile)
parts = gfile.split(":")
gfile = Gio.File.new_for_path(parts[0])
try:
line = int(parts[1]) if len(parts) > 1 else 0
except Exception as e:
...
self.create_view(None, None, gfile,)
self.create_view(None, None, gfile, line)
def _keyboard_open_file(self, gfile):
if not self.is_editor_focused: # TODO: Find way to converge this

View File

@ -207,6 +207,8 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
self._file_cdr_watcher = None
def _write_file(self, gfile, save_as = False):
if not gfile: return
with open(gfile.get_path(), 'w') as f:
if not save_as:
self._ignore_internal_change = True

View File

@ -62,39 +62,46 @@ class SourceViewEventsMixin:
logger.debug(cursor_data)
event_system.emit("set_line_char_label", (f"{row}:{col}",))
def got_to_line(self, line: int = 0):
index = line - 1
buffer = self.get_buffer()
line_itr = buffer.get_iter_at_line(index)
char_iter = buffer.get_iter_at_line_offset(index, line_itr.get_bytes_in_line())
buffer.place_cursor(char_iter)
if not buffer.get_mark("starting_cursor"):
buffer.create_mark("starting_cursor", char_iter, True)
self.scroll_to_mark( buffer.get_mark("starting_cursor"), 0.0, True, 0.0, 0.0 )
# https://github.com/ptomato/inform7-ide/blob/main/src/actions.c
def action_uncomment_selection(self):
...
def action_comment_out_selection(self):
pass
...
def open_file(self, gfile, *args):
def open_file(self, gfile, line: int = 0, *args):
self._current_file = gfile
self.load_file_info(gfile)
self.load_file_async(gfile)
self.load_file_async(gfile, line)
self._create_file_watcher(gfile)
self.grab_focus()
def save_file(self):
self.skip_file_load = True
gfile = self._current_file
gfile = self.save_file_dialog() if not self._current_file else self._current_file
if not gfile:
gfile = self.save_file_dialog()
self.skip_file_load = False
if not gfile: return
return
self.open_file( self._write_file(gfile) )
self.skip_file_load = False
def save_file_as(self):
gfile = self.save_file_dialog()
if gfile:
self._write_file(gfile, True)
event_system.emit("create_view", (gfile,))
self._write_file(gfile, True)
event_system.emit("create_view", (gfile,))
def load_file_info(self, gfile):
info = gfile.query_info("standard::*", 0, cancellable=None)
@ -113,8 +120,10 @@ class SourceViewEventsMixin:
if self._current_filetype == "buffer":
self._current_filetype = info.get_content_type()
def load_file_async(self, gfile):
if self.skip_file_load: return
def load_file_async(self, gfile, line: int = 0):
if self.skip_file_load:
self.update_labels(gfile)
return
file = GtkSource.File()
file.set_location(gfile)
@ -124,6 +133,7 @@ class SourceViewEventsMixin:
self._file_loader.load_finish(res)
self._is_changed = False
self._document_loaded()
self.got_to_line(line)
self.update_labels(gfile)
self._file_loader.load_async(io_priority = 98,
@ -143,7 +153,9 @@ class SourceViewEventsMixin:
def set_bottom_labels(self, gfile = None):
if not gfile: return
event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None))
self.update_cursor_position()
def save_file_dialog(self) -> str:

View File

@ -15,9 +15,9 @@ class SourceViewContainer(Gtk.ScrolledWindow):
def __init__(self, close_tab):
super(SourceViewContainer, self).__init__()
self._close_tab = close_tab
self._source_view = None
self._tab_widget = None
self._close_tab: function = close_tab
self._source_view: SourceView = None
self._tab_widget: TabHeaderWidget = None
self._setup_styling()
self._setup_signals()

View File

@ -105,4 +105,4 @@ class IPCServer:
except ConnectionRefusedError as e:
print("Connection refused...")
except Exception as e:
print(repr(e))
print(repr(e))