[gtksourceview/wip/fix-regex-replace-error-api: 1/2] API break: add a GError parameter to replace() and replace_all()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/fix-regex-replace-error-api: 1/2] API break: add a GError parameter to replace() and replace_all()
- Date: Sat, 31 Aug 2013 14:51:57 +0000 (UTC)
commit cde40d374a762640d61a189a1268a03348c2bedd
Author: Sébastien Wilmet <swilmet gnome org>
Date: Fri Aug 30 14:37:37 2013 +0200
API break: add a GError parameter to replace() and replace_all()
gtk_source_search_context_replace() and
gtk_source_search_context_replace_all() now have a GError parameter.
And GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR has been removed from the
GtkSourceRegexSearchState enum.
The reason: the replacement text is given as a parameter to replace()
and replace_all(), instead of being in the SearchSettings. Thus, the
replacement text is not a part of the "search state". On the other hand,
the regex-error and regex-state properties are part of the search state.
The regex-error property is not a good place for a replace error. It
doesn't make sense to keep a replace error while the replace() or
replace_all() is finished since a long time.
And there was a bug with the previous API: once a replace error was
encountered, it was not possible to search again in the buffer. Except
when the search text (or another setting) change. It would have been
possible to work around this issue, by clearing the regex-error property
if it contains a replace error. And for reporting the replace error to
the user, one has to copy the regex-error as soon as it is reported,
since it can be cleared later, by another (unrelated) operation.
But I think we don't want to keep an error in the API, and have to work
around it until the next major release of GtkSourceView.
Note that the GtkSourceRegexSearchState enum is still available, to
distinguish between a compilation and a matching error.
https://bugzilla.gnome.org/show_bug.cgi?id=707177
gtksourceview/gtksourcesearchcontext.c | 90 ++++++++++++--------------------
gtksourceview/gtksourcesearchcontext.h | 10 ++--
tests/test-search-context.c | 10 ++--
tests/test-search.c | 6 ++-
4 files changed, 48 insertions(+), 68 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index 4b7d118..9ec869b 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -3448,16 +3448,18 @@ gtk_source_search_context_backward_finish (GtkSourceSearchContext *search,
/* Returns %TRUE if replaced. */
static gboolean
-regex_replace (GtkSourceSearchContext *search,
- GtkTextIter *match_start,
- GtkTextIter *match_end,
- const gchar *replace)
+regex_replace (GtkSourceSearchContext *search,
+ GtkTextIter *match_start,
+ GtkTextIter *match_end,
+ const gchar *replace,
+ GError **error)
{
GtkTextIter real_start;
gint start_pos;
gchar *subject;
gchar *subject_replaced;
GRegexMatchFlags match_options;
+ GError *tmp_error = NULL;
if (search->priv->regex == NULL ||
search->priv->regex_error != NULL)
@@ -3477,17 +3479,13 @@ regex_replace (GtkSourceSearchContext *search,
start_pos,
replace,
match_options,
- &search->priv->regex_error);
+ &tmp_error);
g_free (subject);
- if (search->priv->regex_error != NULL)
+ if (tmp_error != NULL)
{
- search->priv->regex_state = GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR;
-
- g_object_notify (G_OBJECT (search), "regex-error");
- g_object_notify (G_OBJECT (search), "regex-state");
-
+ g_propagate_error (error, tmp_error);
g_free (subject_replaced);
return FALSE;
}
@@ -3501,21 +3499,6 @@ regex_replace (GtkSourceSearchContext *search,
return TRUE;
}
-static void
-clear_replace_error (GtkSourceSearchContext *search)
-{
- if (search->priv->regex_state == GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR)
- {
- search->priv->regex_state = GTK_SOURCE_REGEX_SEARCH_NO_ERROR;
-
- g_error_free (search->priv->regex_error);
- search->priv->regex_error = NULL;
-
- g_object_notify (G_OBJECT (search), "regex-error");
- g_object_notify (G_OBJECT (search), "regex-state");
- }
-}
-
/**
* gtk_source_search_context_replace:
* @search: a #GtkSourceSearchContext.
@@ -3523,6 +3506,7 @@ clear_replace_error (GtkSourceSearchContext *search)
* @match_end: the end of the match to replace.
* @replace: the replacement text.
* @replace_length: the length of @replace in bytes, or -1.
+ * @error: location to a #GError, or %NULL to ignore errors.
*
* Replaces a search match by another text. If @match_start and @match_end
* doesn't correspond to a search match, %FALSE is returned.
@@ -3535,11 +3519,12 @@ clear_replace_error (GtkSourceSearchContext *search)
* Since: 3.10
*/
gboolean
-gtk_source_search_context_replace (GtkSourceSearchContext *search,
- const GtkTextIter *match_start,
- const GtkTextIter *match_end,
- const gchar *replace,
- gint replace_length)
+gtk_source_search_context_replace (GtkSourceSearchContext *search,
+ const GtkTextIter *match_start,
+ const GtkTextIter *match_end,
+ const gchar *replace,
+ gint replace_length,
+ GError **error)
{
GtkTextIter start;
GtkTextIter end;
@@ -3548,14 +3533,13 @@ gtk_source_search_context_replace (GtkSourceSearchContext *search,
g_return_val_if_fail (match_start != NULL, FALSE);
g_return_val_if_fail (match_end != NULL, FALSE);
g_return_val_if_fail (replace != NULL, FALSE);
+ g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (dispose_has_run (search))
{
return FALSE;
}
- clear_replace_error (search);
-
if (!smart_forward_search (search, match_start, &start, &end))
{
return FALSE;
@@ -3569,7 +3553,7 @@ gtk_source_search_context_replace (GtkSourceSearchContext *search,
if (gtk_source_search_settings_get_regex_enabled (search->priv->settings))
{
- return regex_replace (search, &start, &end, replace);
+ return regex_replace (search, &start, &end, replace, error);
}
gtk_text_buffer_begin_user_action (search->priv->buffer);
@@ -3585,6 +3569,7 @@ gtk_source_search_context_replace (GtkSourceSearchContext *search,
* @search: a #GtkSourceSearchContext.
* @replace: the replacement text.
* @replace_length: the length of @replace in bytes, or -1.
+ * @error: location to a #GError, or %NULL to ignore errors.
*
* Replaces all search matches by another text. It is a synchronous function, so
* it can block the user interface.
@@ -3597,9 +3582,10 @@ gtk_source_search_context_replace (GtkSourceSearchContext *search,
* Since: 3.10
*/
guint
-gtk_source_search_context_replace_all (GtkSourceSearchContext *search,
- const gchar *replace,
- gint replace_length)
+gtk_source_search_context_replace_all (GtkSourceSearchContext *search,
+ const gchar *replace,
+ gint replace_length,
+ GError **error)
{
GtkTextIter iter;
GtkTextIter match_start;
@@ -3610,16 +3596,17 @@ gtk_source_search_context_replace_all (GtkSourceSearchContext *search,
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), 0);
g_return_val_if_fail (replace != NULL, 0);
+ g_return_val_if_fail (error == NULL || *error == NULL, 0);
if (dispose_has_run (search))
{
return 0;
}
- clear_replace_error (search);
-
if (gtk_source_search_settings_get_regex_enabled (search->priv->settings))
{
+ GError *tmp_error = NULL;
+
if (search->priv->regex == NULL ||
search->priv->regex_error != NULL)
{
@@ -3628,15 +3615,11 @@ gtk_source_search_context_replace_all (GtkSourceSearchContext *search,
g_regex_check_replacement (replace,
&has_regex_references,
- &search->priv->regex_error);
+ &tmp_error);
- if (search->priv->regex_error != NULL)
+ if (tmp_error != NULL)
{
- search->priv->regex_state = GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR;
-
- g_object_notify (G_OBJECT (search), "regex-error");
- g_object_notify (G_OBJECT (search), "regex-state");
-
+ g_propagate_error (error, tmp_error);
return 0;
}
}
@@ -3658,25 +3641,20 @@ gtk_source_search_context_replace_all (GtkSourceSearchContext *search,
while (smart_forward_search (search, &iter, &match_start, &match_end))
{
- gboolean replaced = FALSE;
-
if (has_regex_references)
{
- replaced = regex_replace (search, &match_start, &match_end, replace);
+ if (!regex_replace (search, &match_start, &match_end, replace, error))
+ {
+ break;
+ }
}
else
{
gtk_text_buffer_delete (search->priv->buffer, &match_start, &match_end);
gtk_text_buffer_insert (search->priv->buffer, &match_end, replace, replace_length);
-
- replaced = TRUE;
- }
-
- if (replaced)
- {
- nb_matches_replaced++;
}
+ nb_matches_replaced++;
iter = match_end;
}
diff --git a/gtksourceview/gtksourcesearchcontext.h b/gtksourceview/gtksourcesearchcontext.h
index b57311d..eeb4221 100644
--- a/gtksourceview/gtksourcesearchcontext.h
+++ b/gtksourceview/gtksourcesearchcontext.h
@@ -57,7 +57,6 @@ struct _GtkSourceSearchContextClass
* disabled.
* @GTK_SOURCE_REGEX_SEARCH_COMPILATION_ERROR: pattern compilation error.
* @GTK_SOURCE_REGEX_SEARCH_MATCHING_ERROR: error while matching the buffer.
- * @GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR: replace error.
*
* Regular expression search state.
*
@@ -67,8 +66,7 @@ typedef enum
{
GTK_SOURCE_REGEX_SEARCH_NO_ERROR,
GTK_SOURCE_REGEX_SEARCH_COMPILATION_ERROR,
- GTK_SOURCE_REGEX_SEARCH_MATCHING_ERROR,
- GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR
+ GTK_SOURCE_REGEX_SEARCH_MATCHING_ERROR
} GtkSourceRegexSearchState;
GType gtk_source_search_context_get_type (void) G_GNUC_CONST;
@@ -136,11 +134,13 @@ gboolean gtk_source_search_context_replace
(GtkSourceSearchContext *search,
const GtkTextIter
*match_start,
const GtkTextIter
*match_end,
const gchar
*replace,
- gint
replace_length);
+ gint
replace_length,
+ GError
**error);
guint gtk_source_search_context_replace_all (GtkSourceSearchContext
*search,
const gchar
*replace,
- gint
replace_length);
+ gint
replace_length,
+ GError
**error);
G_GNUC_INTERNAL
void _gtk_source_search_context_update_highlight (GtkSourceSearchContext
*search,
diff --git a/tests/test-search-context.c b/tests/test-search-context.c
index 6e4af4f..de86b4f 100644
--- a/tests/test-search-context.c
+++ b/tests/test-search-context.c
@@ -841,13 +841,13 @@ test_replace (void)
gtk_text_buffer_get_iter_at_offset (text_buffer, &start, 1);
gtk_text_buffer_get_iter_at_offset (text_buffer, &end, 3);
- replaced = gtk_source_search_context_replace (context, &start, &end, "bb", 2);
+ replaced = gtk_source_search_context_replace (context, &start, &end, "bb", 2, NULL);
g_assert (!replaced);
gtk_text_buffer_get_iter_at_offset (text_buffer, &start, 2);
gtk_text_buffer_get_iter_at_offset (text_buffer, &end, 4);
- replaced = gtk_source_search_context_replace (context, &start, &end, "bb", 2);
+ replaced = gtk_source_search_context_replace (context, &start, &end, "bb", 2, NULL);
g_assert (replaced);
gtk_text_buffer_get_start_iter (text_buffer, &start);
@@ -878,7 +878,7 @@ test_replace_all (void)
gtk_source_search_settings_set_search_text (settings, "aa");
flush_queue ();
- nb_replacements = gtk_source_search_context_replace_all (context, "bb", 2);
+ nb_replacements = gtk_source_search_context_replace_all (context, "bb", 2, NULL);
g_assert_cmpint (nb_replacements, ==, 2);
gtk_text_buffer_get_start_iter (text_buffer, &start);
@@ -933,7 +933,7 @@ test_regex (void)
gtk_text_buffer_get_start_iter (text_buffer, &start);
gtk_text_buffer_get_end_iter (text_buffer, &end);
- gtk_source_search_context_replace (context, &start, &end, "\\2#\\1", -1);
+ gtk_source_search_context_replace (context, &start, &end, "\\2#\\1", -1, NULL);
gtk_text_buffer_get_start_iter (text_buffer, &start);
gtk_text_buffer_get_end_iter (text_buffer, &end);
@@ -944,7 +944,7 @@ test_regex (void)
/* Test replace all */
gtk_text_buffer_set_text (text_buffer, "aa#bb cc#dd", -1);
- gtk_source_search_context_replace_all (context, "\\2#\\1", -1);
+ gtk_source_search_context_replace_all (context, "\\2#\\1", -1, NULL);
gtk_text_buffer_get_start_iter (text_buffer, &start);
gtk_text_buffer_get_end_iter (text_buffer, &end);
diff --git a/tests/test-search.c b/tests/test-search.c
index fb5630c..97fdb28 100644
--- a/tests/test-search.c
+++ b/tests/test-search.c
@@ -269,7 +269,8 @@ button_replace_clicked_cb (TestSearch *search,
&match_start,
&match_end,
gtk_entry_get_text (search->priv->replace_entry),
- replace_length);
+ replace_length,
+ NULL);
gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
NULL,
@@ -291,7 +292,8 @@ button_replace_all_clicked_cb (TestSearch *search,
gtk_source_search_context_replace_all (search->priv->search_context,
gtk_entry_get_text (search->priv->replace_entry),
- replace_length);
+ replace_length,
+ NULL);
}
static gboolean
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]