From 28c6acc3453edf4feb96398685ac611d3c207fa6 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Tue, 12 Sep 2023 22:14:31 -0500 Subject: [PATCH] Added scroll to on file passed --- src/core/controller.py | 5 +-- .../widgets/base/notebook/editor_events.py | 13 +++---- .../widgets/base/notebook/editor_notebook.py | 11 ++++-- .../widgets/base/sourceview/source_view.py | 2 ++ .../base/sourceview/source_view_events.py | 36 ++++++++++++------- src/core/widgets/base/sourceview_container.py | 6 ++-- src/utils/ipc_server.py | 2 +- 7 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/core/controller.py b/src/core/controller.py index 0666948..d5cce5f 100644 --- a/src/core/controller.py +++ b/src/core/controller.py @@ -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) diff --git a/src/core/widgets/base/notebook/editor_events.py b/src/core/widgets/base/notebook/editor_events.py index 62972f9..6835dde 100644 --- a/src/core/widgets/base/notebook/editor_events.py +++ b/src/core/widgets/base/notebook/editor_events.py @@ -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() \ No newline at end of file + source_view.toggle_highlight_line() diff --git a/src/core/widgets/base/notebook/editor_notebook.py b/src/core/widgets/base/notebook/editor_notebook.py index 86589f9..778bb5f 100644 --- a/src/core/widgets/base/notebook/editor_notebook.py +++ b/src/core/widgets/base/notebook/editor_notebook.py @@ -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 diff --git a/src/core/widgets/base/sourceview/source_view.py b/src/core/widgets/base/sourceview/source_view.py index 15c1186..9d1f05c 100644 --- a/src/core/widgets/base/sourceview/source_view.py +++ b/src/core/widgets/base/sourceview/source_view.py @@ -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 diff --git a/src/core/widgets/base/sourceview/source_view_events.py b/src/core/widgets/base/sourceview/source_view_events.py index 8981df0..cc0400a 100644 --- a/src/core/widgets/base/sourceview/source_view_events.py +++ b/src/core/widgets/base/sourceview/source_view_events.py @@ -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: diff --git a/src/core/widgets/base/sourceview_container.py b/src/core/widgets/base/sourceview_container.py index 500ed6f..9ec94ca 100644 --- a/src/core/widgets/base/sourceview_container.py +++ b/src/core/widgets/base/sourceview_container.py @@ -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() diff --git a/src/utils/ipc_server.py b/src/utils/ipc_server.py index 2ad105f..29c4776 100644 --- a/src/utils/ipc_server.py +++ b/src/utils/ipc_server.py @@ -105,4 +105,4 @@ class IPCServer: except ConnectionRefusedError as e: print("Connection refused...") except Exception as e: - print(repr(e)) + print(repr(e)) \ No newline at end of file