Simplify _gtk_text_buffer_get_line_log_attrs()

NULL was returned in case of an empty last line. Every users needed to
special-case this. Now it will return the expected result: char_len of 0
with one PangoLogAttr.

In compute_log_attrs(), 'paragraph' will be the empty string "" with
'char_len' == 0.
pango_get_log_attrs() works fine with an empty string, it will return
one correct PangoLogAttr (because there is one text position for the
empty string).

It fixes the unit tests for gtk_text_iter_is_cursor_position().

https://bugzilla.gnome.org/show_bug.cgi?id=156164
This commit is contained in:
Sébastien Wilmet 2014-08-20 19:06:05 +02:00
parent 83bc0c4bb1
commit 706c8e9c8d
3 changed files with 13 additions and 43 deletions

View File

@ -3889,6 +3889,7 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer,
GtkTextIter end;
gboolean retval = FALSE;
const PangoLogAttr *attrs;
gint offset;
gboolean backspace_deletes_character;
g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
@ -3898,17 +3899,8 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer,
end = *iter;
attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
/* For no good reason, attrs is NULL for the empty last line in
* a buffer. Special case that here. (#156164)
*/
if (attrs != NULL)
{
gint offset = gtk_text_iter_get_line_offset (&start);
backspace_deletes_character = attrs[offset].backspace_deletes_character;
}
else
backspace_deletes_character = FALSE;
offset = gtk_text_iter_get_line_offset (&start);
backspace_deletes_character = attrs[offset].backspace_deletes_character;
gtk_text_iter_backward_cursor_position (&start);
@ -4325,8 +4317,6 @@ compute_log_attrs (const GtkTextIter *iter,
char_len = g_utf8_strlen (paragraph, -1);
byte_len = strlen (paragraph);
g_assert (char_len > 0);
if (char_lenp != NULL)
*char_lenp = char_len;
@ -4346,6 +4336,7 @@ compute_log_attrs (const GtkTextIter *iter,
}
/* The return value from this is valid until you call this a second time.
* Returns (char_len + 1) PangoLogAttr's, one for each text position.
*/
const PangoLogAttr *
_gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
@ -4362,15 +4353,6 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
priv = buffer->priv;
/* special-case for empty last line in buffer */
if (gtk_text_iter_is_end (anywhere_in_line) &&
gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
{
if (char_len != NULL)
*char_len = 0;
return NULL;
}
/* FIXME we also need to recompute log attrs if the language tag at
* the start of a paragraph changes
*/

View File

@ -3060,7 +3060,6 @@ test_log_attrs (const GtkTextIter *iter,
gint char_len;
const PangoLogAttr *attrs;
gint offset;
gboolean result = FALSE;
g_return_val_if_fail (iter != NULL, FALSE);
@ -3069,16 +3068,9 @@ test_log_attrs (const GtkTextIter *iter,
offset = gtk_text_iter_get_line_offset (iter);
/* char_len may be 0 and attrs will be NULL if so, if
* iter is the end iter and the last line is empty.
*
* offset may be equal to char_len, since attrs contains an entry
* for one past the end.
*/
if (attrs != NULL && offset <= char_len)
result = (* func) (attrs, offset, 0, char_len);
g_assert (offset <= char_len);
return result;
return (* func) (attrs, offset, 0, char_len);
}
static gboolean
@ -3090,7 +3082,6 @@ find_line_log_attrs (const GtkTextIter *iter,
gint char_len;
const PangoLogAttr *attrs;
gint offset;
gboolean result = FALSE;
g_return_val_if_fail (iter != NULL, FALSE);
@ -3098,15 +3089,12 @@ find_line_log_attrs (const GtkTextIter *iter,
iter, &char_len);
offset = gtk_text_iter_get_line_offset (iter);
/* char_len may be 0 and attrs will be NULL if so, if
* iter is the end iter and the last line is empty.
*/
if (attrs != NULL)
result = (* func) (attrs, offset, char_len, found_offset,
already_moved_initially);
return result;
return (* func) (attrs,
offset,
char_len,
found_offset,
already_moved_initially);
}
static gboolean

View File

@ -538,8 +538,8 @@ test_cursor_positions (void)
check_is_cursor_position ("a\r\n", 0, TRUE);
check_is_cursor_position ("a\r\n", 1, TRUE);
check_is_cursor_position ("a\r\n", 2, FALSE);
check_is_cursor_position ("a\r\n", 3, FALSE); /* FIXME should be TRUE */
check_is_cursor_position ("", 0, FALSE); /* FIXME should be TRUE */
check_is_cursor_position ("a\r\n", 3, TRUE);
check_is_cursor_position ("", 0, TRUE);
/* forward */
check_cursor_position ("a\r\nb", TRUE, 0, 1, TRUE);