After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 735341 - Fix/improve gtk_text_buffer_get_iter_at_*() functions
Fix/improve gtk_text_buffer_get_iter_at_*() functions
Status: RESOLVED OBSOLETE
Product: gtk+
Classification: Platform
Component: Widget: GtkTextView
3.13.x
Other All
: Normal normal
: ---
Assigned To: gtk-bugs
gtk-bugs
Depends on:
Blocks:
 
 
Reported: 2014-08-24 21:24 UTC by Sébastien Wilmet
Modified: 2018-05-02 16:14 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
textbuffer: nicer get_iter functions (13.46 KB, patch)
2015-10-15 12:19 UTC, Sébastien Wilmet
committed Details | Review
inspector: remove gtk_text_buffer_get_iter_at_line_index() workaround (2.81 KB, patch)
2015-10-15 12:19 UTC, Sébastien Wilmet
committed Details | Review
textbuffer: nicer get_iter functions, without return values (9.40 KB, patch)
2015-11-12 17:02 UTC, Sébastien Wilmet
committed Details | Review
textbuffer: nicer get_iter functions, with return values (13.46 KB, patch)
2016-12-20 15:40 UTC, Sébastien Wilmet
none Details | Review

Description Sébastien Wilmet 2014-08-24 21:24:28 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.
Comment 1 Sébastien Wilmet 2014-08-24 21:59:14 UTC
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.
Comment 2 Matthias Clasen 2014-08-25 12:10:18 UTC
I agree a boolean return would be nice for all get_iter functions that may fail to return a valid iter.
Comment 3 Sébastien Wilmet 2015-10-15 12:19:09 UTC
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.
Comment 4 Sébastien Wilmet 2015-10-15 12:19:15 UTC
Created attachment 313364 [details] [review]
inspector: remove gtk_text_buffer_get_iter_at_line_index() workaround

The function is now safe to use.
Comment 5 Matthias Clasen 2015-10-15 19:09:05 UTC
Review of attachment 313363 [details] [review]:

Loooks good
Comment 6 Sébastien Wilmet 2015-10-16 06:55:35 UTC
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
Comment 7 Sébastien Wilmet 2015-10-16 06:57:30 UTC
Thanks for the review!

(the second commit was trivial, so I've assumed it was also accepted).
Comment 8 Matthias Clasen 2015-11-12 15:06:21 UTC
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.
Comment 9 Sébastien Wilmet 2015-11-12 17:02:01 UTC
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.
Comment 10 Matthias Clasen 2015-11-12 17:30:00 UTC
Review of attachment 315351 [details] [review]:

ok
Comment 11 Sébastien Wilmet 2015-11-12 17:32:42 UTC
Attachment 315351 [details] pushed as 1685da6 - textbuffer: nicer get_iter functions, without return values
Comment 12 Sébastien Wilmet 2016-12-20 15:39:47 UTC
(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.
Comment 13 Sébastien Wilmet 2016-12-20 15:40:20 UTC
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.
Comment 14 Sébastien Wilmet 2017-01-25 10:25:46 UTC
Ping. Can I have a review of the above patch?

Maybe I should also add a paragraph to the porting guide?
Comment 15 GNOME Infrastructure Team 2018-05-02 16:14:31 UTC
-- 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.