[geany/geany] a03339: When "go to line" is out of bounds, go to beginning or end of document (#2973)

xiota git-noreply at xxxxx
Tue Nov 9 04:17:35 UTC 2021


Branch:      refs/heads/master
Author:      xiota <xiota at users.noreply.github.com>
Committer:   GitHub <noreply at github.com>
Date:        Tue, 09 Nov 2021 04:17:35 UTC
Commit:      a0333989f72e17cc221c41dbec7c16d50549b023
             https://github.com/geany/geany/commit/a0333989f72e17cc221c41dbec7c16d50549b023

Log Message:
-----------
When "go to line" is out of bounds, go to beginning or end of document (#2973)

* When "go to line" is out of bounds, go to beginning or end of document

* change offset to gboolean in editor_goto_line()
remove get_line_and_offset_from_text()

* use gint for line_count


Modified Paths:
--------------
    src/callbacks.c
    src/editor.c
    src/editor.h

Modified: src/callbacks.c
32 lines changed, 4 insertions(+), 28 deletions(-)
===================================================================
@@ -911,21 +911,6 @@ void on_find_in_files1_activate(GtkMenuItem *menuitem, gpointer user_data)
 }
 
 
-static void get_line_and_offset_from_text(const gchar *text, gint *line_no, gint *offset)
-{
-	if (*text == '+' || *text == '-')
-	{
-		*line_no = atoi(text + 1);
-		*offset = (*text == '+') ? 1 : -1;
-	}
-	else
-	{
-		*line_no = atoi(text) - 1;
-		*offset = 0;
-	}
-}
-
-
 void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
 {
 	static gchar value[16] = "";
@@ -936,18 +921,10 @@ void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
 		_("Enter the line you want to go to:"), value);
 	if (result != NULL)
 	{
-		GeanyDocument *doc = document_get_current();
-		gint offset;
-		gint line_no;
-
-		g_return_if_fail(doc != NULL);
+		on_toolbutton_goto_entry_activate(NULL, result, NULL);
 
-		get_line_and_offset_from_text(result, &line_no, &offset);
-		if (! editor_goto_line(doc->editor, line_no, offset))
-			utils_beep();
 		/* remember value for future calls */
 		g_snprintf(value, sizeof(value), "%s", result);
-
 		g_free(result);
 	}
 }
@@ -956,12 +933,11 @@ void on_go_to_line_activate(GtkMenuItem *menuitem, gpointer user_data)
 void on_toolbutton_goto_entry_activate(GtkAction *action, const gchar *text, gpointer user_data)
 {
 	GeanyDocument *doc = document_get_current();
-	gint offset;
-	gint line_no;
-
 	g_return_if_fail(doc != NULL);
 
-	get_line_and_offset_from_text(text, &line_no, &offset);
+	gint line_no = atoi(text);
+	gboolean offset = (*text == '+' || *text == '-');
+
 	if (! editor_goto_line(doc->editor, line_no, offset))
 		utils_beep();
 	else


Modified: src/editor.c
28 lines changed, 14 insertions(+), 14 deletions(-)
===================================================================
@@ -4699,24 +4699,24 @@ void editor_set_indent(GeanyEditor *editor, GeanyIndentType type, gint width)
 }
 
 
-/* Convenience function for editor_goto_pos() to pass in a line number. */
-gboolean editor_goto_line(GeanyEditor *editor, gint line_no, gint offset)
+/* Convenience function for editor_goto_pos() to pass a line number.
+ * line_no is 1 based */
+gboolean editor_goto_line(GeanyEditor *editor, gint line_no, gboolean offset)
 {
-	gint pos;
-
 	g_return_val_if_fail(editor, FALSE);
-	if (line_no < 0 || line_no >= sci_get_line_count(editor->sci))
-		return FALSE;
+	gint line_count = sci_get_line_count(editor->sci);
 
-	if (offset != 0)
-	{
-		gint current_line = sci_get_current_line(editor->sci);
-		line_no *= offset;
-		line_no = current_line + line_no;
-	}
+	if (offset)
+		line_no += sci_get_current_line(editor->sci) + 1;
+
+	/* ensure line_no is in bounds and determine whether to set line marker */
+	gboolean set_marker = line_no > 0 && line_no < line_count;
+	line_no = line_no <= 0          ? 0
+			: line_no >= line_count ? line_count - 1
+			: line_no - 1;
 
-	pos = sci_get_position_from_line(editor->sci, line_no);
-	return editor_goto_pos(editor, pos, TRUE);
+	gint pos = sci_get_position_from_line(editor->sci, line_no);
+	return editor_goto_pos(editor, pos, set_marker);
 }
 
 


Modified: src/editor.h
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -319,7 +319,7 @@ void editor_set_indent(GeanyEditor *editor, GeanyIndentType type, gint width);
 
 void editor_set_line_wrapping(GeanyEditor *editor, gboolean wrap);
 
-gboolean editor_goto_line(GeanyEditor *editor, gint line_no, gint offset);
+gboolean editor_goto_line(GeanyEditor *editor, gint line_no, gboolean offset);
 
 void editor_set_indentation_guides(GeanyEditor *editor);
 



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list