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): def __init__(self, args, unknownargs):
messages = [] messages = []
for arg in unknownargs + [args.new_tab,]: for arg in unknownargs + [args.new_tab,]:
if os.path.isfile(arg): # NOTE: If passing line number with file split against :
messages.append(f"FILE|{arg}") if os.path.isfile(arg.replace("file://", "").split(":")[0]):
messages.append(f"FILE|{arg.replace('file://', '')}")
if len(messages) > 0: if len(messages) > 0:
settings.set_is_starting_with_file(True) settings.set_is_starting_with_file(True)

View File

@ -8,7 +8,7 @@ from ..sourceview_container import SourceViewContainer
class EditorEventsMixin: 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) container = SourceViewContainer(self.close_tab)
page_num = self.append_page(container, container.get_tab_widget()) page_num = self.append_page(container, container.get_tab_widget())
@ -18,13 +18,14 @@ class EditorEventsMixin:
ctx.add_class("notebook-unselected-focus") ctx.add_class("notebook-unselected-focus")
self.set_tab_reorderable(container, True) self.set_tab_reorderable(container, True)
if gfile:
source_view = container.get_source_view()
source_view.open_file(gfile)
self.show_all() self.show_all()
self.set_current_page(page_num) 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): def open_file(self, gfile):
page_num = self.get_current_page() page_num = self.get_current_page()
container = self.get_nth_page( page_num ) container = self.get_nth_page( page_num )

View File

@ -126,14 +126,19 @@ class EditorNotebook(EditorEventsMixin, EditorControllerMixin, Gtk.Notebook):
source_view.update_cursor_position() source_view.update_cursor_position()
source_view.set_bottom_labels(gfile) 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 if not self.is_editor_focused: # TODO: Find way to converge this
return return
if isinstance(gfile, str): 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): def _keyboard_open_file(self, gfile):
if not self.is_editor_focused: # TODO: Find way to converge this 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 self._file_cdr_watcher = None
def _write_file(self, gfile, save_as = False): def _write_file(self, gfile, save_as = False):
if not gfile: return
with open(gfile.get_path(), 'w') as f: with open(gfile.get_path(), 'w') as f:
if not save_as: if not save_as:
self._ignore_internal_change = True self._ignore_internal_change = True

View File

@ -62,37 +62,44 @@ class SourceViewEventsMixin:
logger.debug(cursor_data) logger.debug(cursor_data)
event_system.emit("set_line_char_label", (f"{row}:{col}",)) 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 # https://github.com/ptomato/inform7-ide/blob/main/src/actions.c
def action_uncomment_selection(self): def action_uncomment_selection(self):
... ...
def action_comment_out_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._current_file = gfile
self.load_file_info(gfile) self.load_file_info(gfile)
self.load_file_async(gfile) self.load_file_async(gfile, line)
self._create_file_watcher(gfile) self._create_file_watcher(gfile)
self.grab_focus()
def save_file(self): def save_file(self):
self.skip_file_load = True 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: if not gfile:
gfile = self.save_file_dialog()
self.skip_file_load = False self.skip_file_load = False
if not gfile: return return
self.open_file( self._write_file(gfile) ) self.open_file( self._write_file(gfile) )
self.skip_file_load = False self.skip_file_load = False
def save_file_as(self): def save_file_as(self):
gfile = self.save_file_dialog() gfile = self.save_file_dialog()
if gfile:
self._write_file(gfile, True) self._write_file(gfile, True)
event_system.emit("create_view", (gfile,)) event_system.emit("create_view", (gfile,))
@ -113,8 +120,10 @@ class SourceViewEventsMixin:
if self._current_filetype == "buffer": if self._current_filetype == "buffer":
self._current_filetype = info.get_content_type() self._current_filetype = info.get_content_type()
def load_file_async(self, gfile): def load_file_async(self, gfile, line: int = 0):
if self.skip_file_load: return if self.skip_file_load:
self.update_labels(gfile)
return
file = GtkSource.File() file = GtkSource.File()
file.set_location(gfile) file.set_location(gfile)
@ -124,6 +133,7 @@ class SourceViewEventsMixin:
self._file_loader.load_finish(res) self._file_loader.load_finish(res)
self._is_changed = False self._is_changed = False
self._document_loaded() self._document_loaded()
self.got_to_line(line)
self.update_labels(gfile) self.update_labels(gfile)
self._file_loader.load_async(io_priority = 98, self._file_loader.load_async(io_priority = 98,
@ -143,7 +153,9 @@ class SourceViewEventsMixin:
def set_bottom_labels(self, gfile = None): def set_bottom_labels(self, gfile = None):
if not gfile: return if not gfile: return
event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None)) event_system.emit("set_bottom_labels", (gfile, None, self._current_filetype, None))
self.update_cursor_position()
def save_file_dialog(self) -> str: def save_file_dialog(self) -> str:

View File

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