GNOME Bugzilla – Bug 735341
Fix/improve gtk_text_buffer_get_iter_at_*() functions
Last modified: 2018-05-02 16:14:31 UTC
As bug #735337 shows, it would be nice to make gtk_text_buffer_get_iter_at_line_index() a bit nicer in case of an invalid index, instead of crashing. A warning could be printed, or the function could have a boolean return value indicating whether the exact position has been found. In any case the function should return the iter at the end of the line. Or the end iter if the line number is greater than the total number of lines. This applies to the other related functions in GtkTextBuffer, like gtk_text_buffer_get_iter_at_line_offset(). Also, gtk_text_buffer_get_iter_at_line() has a related bug when the line_number is too big, it should return the end iter in this case, but if the last line of the buffer is not empty, it returns the iter at offset 0 of this last line, instead of the end of the last line.
Since there is now an API freeze, I propose to first add a warning message, and for the next development cycle add a boolean return value without the warning.
I agree a boolean return would be nice for all get_iter functions that may fail to return a valid iter.
Created attachment 313363 [details] [review] textbuffer: nicer get_iter functions Avoid crashes when passing an invalid location to a gtk_text_buffer_get_iter_at_*() function. A boolean is returned to know if @iter has been set to the exact location. Unit tests are added.
Created attachment 313364 [details] [review] inspector: remove gtk_text_buffer_get_iter_at_line_index() workaround The function is now safe to use.
Review of attachment 313363 [details] [review]: Loooks good
Attachment 313363 [details] pushed as a9a1c00 - textbuffer: nicer get_iter functions Attachment 313364 [details] pushed as 9d3e9c9 - inspector: remove gtk_text_buffer_get_iter_at_line_index() workaround
Thanks for the review! (the second commit was trivial, so I've assumed it was also accepted).
Sadly, I had to revert this - it broke both the python and javascript bindings by making the function return a [boolean, iter] tuple. Back to the drawing board.
Created attachment 315351 [details] [review] textbuffer: nicer get_iter functions, without return values Avoid crashes when passing an invalid location to a gtk_text_buffer_get_iter_at_*() function. A first attempt added boolean return values to know if @iter has been set to the exact location, but it breaks Python and JS bindings because the out parameter is already a return value in those languages. Unit tests are added.
Review of attachment 315351 [details] [review]: ok
Attachment 315351 [details] pushed as 1685da6 - textbuffer: nicer get_iter functions, without return values
(In reply to Matthias Clasen from comment #8) > Sadly, I had to revert this - it broke both the python and javascript > bindings by making the function return a [boolean, iter] tuple. Back to the > drawing board. Reopening, for GTK+ 4 we can break the API. I'll attach a patch to add back the boolean return values.
Created attachment 342269 [details] [review] textbuffer: nicer get_iter functions, with return values History: 1. commit a9a1c00cc9255ee9a7f73695f69abe172cde7bfa Nicer get_iter functions, *with* the boolean return values. 2. commit fde91c3bece9aaea17b51ce6db8f9cb6088dbf96 Revert first commit because it broke Python and JS bindings. The out GtkTextIter param is already returned as the function return value, so by adding the boolean return value, a tuple is returned instead. 3. commit 1685da6f696166588357b2e999da082cf731518f Nicer get_iter functions, *without* the boolean return values. Now: add back the boolean return values, it makes the functions more convenient to use. Example use-case: implement "goto line" in a text editor, and show error feedback in the GtkEntry if the exact location didn't exist. Also, adapt the unit tests to check the return values. This commit is basically a revert of the last two commits listed above.
Ping. Can I have a review of the above patch? Maybe I should also add a paragraph to the porting guide?
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/gtk/issues/506.