[gtksourceview/wip/search] search: various small code improvements
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/search] search: various small code improvements
- Date: Sat, 6 Jul 2013 10:10:09 +0000 (UTC)
commit 0145d31761345f6f82e449f7ddafdebb28f070c5
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Jul 6 11:57:52 2013 +0200
search: various small code improvements
Add comments, check if dispose_has_run(), move functions in a more
logical order, add missing functions to the documentation.
docs/reference/gtksourceview-3.0-sections.txt | 3 +
gtksourceview/gtksourcebuffer.c | 90 ++++----
gtksourceview/gtksourcebuffer.h | 14 +-
gtksourceview/gtksourcesearch.c | 313 ++++++++++++++-----------
gtksourceview/gtksourcesearch.h | 28 ++--
5 files changed, 246 insertions(+), 202 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index 2d1a80e..a9565cd 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -45,12 +45,15 @@ gtk_source_buffer_get_search_at_word_boundaries
gtk_source_buffer_set_search_wrap_around
gtk_source_buffer_get_search_wrap_around
gtk_source_buffer_get_search_occurrences_count
+gtk_source_buffer_get_search_occurrence_position
gtk_source_buffer_forward_search
gtk_source_buffer_forward_search_async
gtk_source_buffer_forward_search_finish
gtk_source_buffer_backward_search
gtk_source_buffer_backward_search_async
gtk_source_buffer_backward_search_finish
+gtk_source_buffer_search_replace
+gtk_source_buffer_search_replace_all
<SUBSECTION Standard>
GtkSourceBufferClass
GTK_SOURCE_IS_BUFFER
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index d8a476c..7327100 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -2672,23 +2672,6 @@ gtk_source_buffer_get_search_text (GtkSourceBuffer *buffer)
return _gtk_source_search_get_text (buffer->priv->search);
}
-/**
- * gtk_source_buffer_get_search_occurrences_count:
- * @buffer: a #GtkSourceBuffer.
- *
- * Gets the total number of search occurrences.
- *
- * Returns: the total number of search occurrences.
- * Since: 3.10
- */
-guint
-gtk_source_buffer_get_search_occurrences_count (GtkSourceBuffer *buffer)
-{
- g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), 0);
-
- return _gtk_source_search_get_occurrences_count (buffer->priv->search);
-}
-
void
gtk_source_buffer_set_case_sensitive_search (GtkSourceBuffer *buffer,
gboolean case_sensitive)
@@ -2771,6 +2754,51 @@ gtk_source_buffer_get_search_wrap_around (GtkSourceBuffer *buffer)
}
/**
+ * gtk_source_buffer_get_search_occurrences_count:
+ * @buffer: a #GtkSourceBuffer.
+ *
+ * Gets the total number of search occurrences.
+ *
+ * Returns: the total number of search occurrences.
+ * Since: 3.10
+ */
+guint
+gtk_source_buffer_get_search_occurrences_count (GtkSourceBuffer *buffer)
+{
+ g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), 0);
+
+ return _gtk_source_search_get_occurrences_count (buffer->priv->search);
+}
+
+/**
+ * gtk_source_buffer_get_search_occurrence_position:
+ * @buffer: a #GtkSourceBuffer.
+ * @match_start: the start of the occurrence.
+ * @match_end: the end of the occurrence.
+ *
+ * Gets the position of a search occurrence. If the buffer is not already fully
+ * scanned, the position may be unknown, and -1 is returned. Therefore you
+ * should call this function when you know that the buffer is fully scanned.
+ *
+ * Returns: the position of the search occurrence. The first occurrence has the
+ * position 1 (not 0). Returns 0 if @match_start and @match_end doesn't delimit
+ * an occurrence. Returns -1 if the position is not yet known.
+ */
+gint
+gtk_source_buffer_get_search_occurrence_position (GtkSourceBuffer *buffer,
+ const GtkTextIter *match_start,
+ const GtkTextIter *match_end)
+{
+ g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), -1);
+ g_return_val_if_fail (match_start != NULL, -1);
+ g_return_val_if_fail (match_end != NULL, -1);
+
+ return _gtk_source_search_get_occurrence_position (buffer->priv->search,
+ match_start,
+ match_end);
+}
+
+/**
* gtk_source_buffer_forward_search:
* @buffer: a #GtkSourceBuffer.
* @iter: start of search, or %NULL.
@@ -2944,31 +2972,3 @@ gtk_source_buffer_search_replace_all (GtkSourceBuffer *buffer,
replace,
replace_length);
}
-
-/**
- * gtk_source_buffer_get_search_occurrence_position:
- * @buffer: a #GtkSourceBuffer.
- * @match_start: the start of the occurrence.
- * @match_end: the end of the occurrence.
- *
- * Gets the position of a search occurrence. If the buffer is not already fully
- * scanned, the position may be unknown, and -1 is returned. Therefore you
- * should call this function when you know that the buffer is fully scanned.
- *
- * Returns: the position of the search occurrence. The first occurrence has the
- * position 1 (not 0). Returns 0 if @match_start and @match_end doesn't delimit
- * an occurrence. Returns -1 if the position is not yet known.
- */
-gint
-gtk_source_buffer_get_search_occurrence_position (GtkSourceBuffer *buffer,
- const GtkTextIter *match_start,
- const GtkTextIter *match_end)
-{
- g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), -1);
- g_return_val_if_fail (match_start != NULL, -1);
- g_return_val_if_fail (match_end != NULL, -1);
-
- return _gtk_source_search_get_occurrence_position (buffer->priv->search,
- match_start,
- match_end);
-}
diff --git a/gtksourceview/gtksourcebuffer.h b/gtksourceview/gtksourcebuffer.h
index 2029df7..e9fd66e 100644
--- a/gtksourceview/gtksourcebuffer.h
+++ b/gtksourceview/gtksourcebuffer.h
@@ -180,8 +180,6 @@ void gtk_source_buffer_set_search_text
(GtkSourceBuffer *buffer,
const gchar *gtk_source_buffer_get_search_text (GtkSourceBuffer
*buffer);
-guint gtk_source_buffer_get_search_occurrences_count (GtkSourceBuffer
*buffer);
-
void gtk_source_buffer_set_case_sensitive_search (GtkSourceBuffer
*buffer,
gboolean
case_sensitive);
@@ -197,6 +195,13 @@ void gtk_source_buffer_set_search_wrap_around
(GtkSourceBuffer *buffer,
gboolean gtk_source_buffer_get_search_wrap_around (GtkSourceBuffer
*buffer);
+guint gtk_source_buffer_get_search_occurrences_count (GtkSourceBuffer
*buffer);
+
+gint gtk_source_buffer_get_search_occurrence_position (GtkSourceBuffer
*buffer,
+ const GtkTextIter
*match_start,
+ const GtkTextIter
*match_end);
+
+
gboolean gtk_source_buffer_forward_search (GtkSourceBuffer *buffer,
const GtkTextIter *iter,
GtkTextIter *match_start,
@@ -242,11 +247,6 @@ guint gtk_source_buffer_search_replace_all (GtkSourceBuffer
*buffer,
const gchar *replace,
gint replace_length);
-gint gtk_source_buffer_get_search_occurrence_position
- (GtkSourceBuffer *buffer,
- const GtkTextIter *match_start,
- const GtkTextIter *match_end);
-
/* private */
void _gtk_source_buffer_update_highlight (GtkSourceBuffer *buffer,
const GtkTextIter *start,
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index f06f62e..58c8b2a 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -29,7 +29,7 @@
/* Implementation overview:
*
- * When the state of the search changes (the text to search or the flags), we
+ * When the state of the search changes (the text to search or the options), we
* have to update the highlighting and the properties values (the number of
* occurrences). To do so, a simple solution is to first remove all the
* found_tag, so we have a clean buffer to analyze. The problem with this
@@ -119,11 +119,6 @@ struct _GtkSourceSearchPrivate
{
GtkTextBuffer *buffer;
- /* State of the search. If text is NULL, the search is disabled. */
- gchar *text;
- gint text_nb_lines;
- GtkTextSearchFlags flags;
-
/* The region to scan and highlight. If NULL, the scan is finished. */
GtkTextRegion *scan_region;
@@ -144,10 +139,15 @@ struct _GtkSourceSearchPrivate
GtkTextTag *found_tag;
+ /* State of the search. If text is NULL, the search is disabled. */
+ gchar *text;
+ gint text_nb_lines;
+ GtkTextSearchFlags flags;
guint at_word_boundaries : 1;
guint wrap_around : 1;
};
+/* Data for the asynchronous forward and backward search tasks. */
typedef struct
{
GtkTextMark *start_at;
@@ -196,8 +196,8 @@ sync_found_tag (GtkSourceSearch *search)
_gtk_source_style_apply (style, search->priv->found_tag);
}
-/* Make sure to call this function when the buffer is constructed, else there is
- * a problem with the tag table already initialized while it shouldn't.
+/* Make sure to call this function when the buffer is constructed, else the tag
+ * table is created too early.
*/
static void
init_found_tag (GtkSourceSearch *search)
@@ -1620,68 +1620,6 @@ _gtk_source_search_get_text (GtkSourceSearch *search)
}
void
-_gtk_source_search_update_highlight (GtkSourceSearch *search,
- const GtkTextIter *start,
- const GtkTextIter *end,
- gboolean synchronous)
-{
- GtkTextRegion *region_to_highlight;
-
- g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
- g_return_if_fail (start != NULL);
- g_return_if_fail (end != NULL);
-
- if (dispose_has_run (search) ||
- is_text_region_empty (search->priv->scan_region))
- {
- return;
- }
-
- region_to_highlight = gtk_text_region_intersect (search->priv->scan_region,
- start,
- end);
-
- if (is_text_region_empty (region_to_highlight))
- {
- if (region_to_highlight != NULL)
- {
- gtk_text_region_destroy (region_to_highlight, TRUE);
- }
-
- return;
- }
-
- if (synchronous)
- {
- scan_all_region (search, region_to_highlight);
- gtk_text_region_destroy (region_to_highlight, TRUE);
- }
- else
- {
- if (search->priv->high_priority_region != NULL)
- {
- /* The high priority region is used to highlight the
- * region visible on screen. So if we are here, that
- * means that the visible region has changed. So we can
- * destroy the old high_priority_region.
- */
- gtk_text_region_destroy (search->priv->high_priority_region, TRUE);
- }
-
- search->priv->high_priority_region = region_to_highlight;
- install_idle_scan (search);
- }
-}
-
-guint
-_gtk_source_search_get_occurrences_count (GtkSourceSearch *search)
-{
- g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), 0);
-
- return search->priv->occurrences_count;
-}
-
-void
_gtk_source_search_set_case_sensitive (GtkSourceSearch *search,
gboolean case_sensitive)
{
@@ -1743,6 +1681,139 @@ _gtk_source_search_get_wrap_around (GtkSourceSearch *search)
return search->priv->wrap_around;
}
+guint
+_gtk_source_search_get_occurrences_count (GtkSourceSearch *search)
+{
+ g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), 0);
+
+ return search->priv->occurrences_count;
+}
+
+gint
+_gtk_source_search_get_occurrence_position (GtkSourceSearch *search,
+ const GtkTextIter *match_start,
+ const GtkTextIter *match_end)
+{
+ GtkTextIter m_start;
+ GtkTextIter m_end;
+ GtkTextIter iter;
+ gboolean found;
+ gint position = 0;
+
+ g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), -1);
+ g_return_val_if_fail (match_start != NULL, -1);
+ g_return_val_if_fail (match_end != NULL, -1);
+
+ if (dispose_has_run (search))
+ {
+ return -1;
+ }
+
+ /* Verify that the occurrence is correct. */
+
+ found = basic_forward_search (search,
+ match_start,
+ &m_start,
+ &m_end,
+ match_end);
+
+ if (!found ||
+ !gtk_text_iter_equal (match_start, &m_start) ||
+ !gtk_text_iter_equal (match_end, &m_end))
+ {
+ return 0;
+ }
+
+ /* Verify that the scan region is empty between the start of the buffer
+ * and the end of the occurrence.
+ */
+
+ gtk_text_buffer_get_start_iter (search->priv->buffer, &iter);
+
+ if (search->priv->scan_region != NULL)
+ {
+ GtkTextRegion *region = gtk_text_region_intersect (search->priv->scan_region,
+ &iter,
+ match_end);
+
+ gboolean empty = is_text_region_empty (region);
+
+ if (region != NULL)
+ {
+ gtk_text_region_destroy (region, TRUE);
+ }
+
+ if (!empty)
+ {
+ return -1;
+ }
+ }
+
+ /* Everything is fine, count the number of previous occurrences. */
+
+ while (smart_forward_search_without_scanning (search, &iter, &m_start, &m_end, match_start))
+ {
+ position++;
+ iter = m_end;
+ }
+
+ return position + 1;
+}
+
+void
+_gtk_source_search_update_highlight (GtkSourceSearch *search,
+ const GtkTextIter *start,
+ const GtkTextIter *end,
+ gboolean synchronous)
+{
+ GtkTextRegion *region_to_highlight;
+
+ g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
+ g_return_if_fail (start != NULL);
+ g_return_if_fail (end != NULL);
+
+ if (dispose_has_run (search) ||
+ is_text_region_empty (search->priv->scan_region))
+ {
+ return;
+ }
+
+ region_to_highlight = gtk_text_region_intersect (search->priv->scan_region,
+ start,
+ end);
+
+ if (is_text_region_empty (region_to_highlight))
+ {
+ if (region_to_highlight != NULL)
+ {
+ gtk_text_region_destroy (region_to_highlight, TRUE);
+ }
+
+ return;
+ }
+
+ if (synchronous)
+ {
+ scan_all_region (search, region_to_highlight);
+ gtk_text_region_destroy (region_to_highlight, TRUE);
+ }
+ else
+ {
+ if (search->priv->high_priority_region != NULL)
+ {
+ /* The high priority region is used to highlight the
+ * region visible on screen. So if we are here, that
+ * means that the visible region has changed. So we can
+ * destroy the old high_priority_region.
+ */
+ gtk_text_region_destroy (search->priv->high_priority_region, TRUE);
+ }
+
+ search->priv->high_priority_region = region_to_highlight;
+ install_idle_scan (search);
+ }
+}
+
gboolean
_gtk_source_search_forward (GtkSourceSearch *search,
const GtkTextIter *iter,
@@ -1754,6 +1825,11 @@ _gtk_source_search_forward (GtkSourceSearch *search,
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
+ if (dispose_has_run (search))
+ {
+ return FALSE;
+ }
+
if (iter != NULL)
{
start_at = *iter;
@@ -1789,6 +1865,11 @@ _gtk_source_search_forward_async (GtkSourceSearch *search,
g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
+ if (dispose_has_run (search))
+ {
+ return;
+ }
+
task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
if (iter != NULL)
@@ -1816,6 +1897,12 @@ _gtk_source_search_forward_finish (GtkSourceSearch *search,
gboolean found;
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
+
+ if (dispose_has_run (search))
+ {
+ return FALSE;
+ }
+
g_return_val_if_fail (g_task_is_valid (result, search->priv->buffer), FALSE);
data = g_task_propagate_pointer (G_TASK (result), error);
@@ -1855,6 +1942,11 @@ _gtk_source_search_backward (GtkSourceSearch *search,
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
+ if (dispose_has_run (search))
+ {
+ return FALSE;
+ }
+
if (iter != NULL)
{
start_at = *iter;
@@ -1890,6 +1982,11 @@ _gtk_source_search_backward_async (GtkSourceSearch *search,
g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
+ if (dispose_has_run (search))
+ {
+ return;
+ }
+
task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
if (iter != NULL)
@@ -1935,6 +2032,11 @@ _gtk_source_search_replace (GtkSourceSearch *search,
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
g_return_val_if_fail (replace != NULL, FALSE);
+ if (dispose_has_run (search))
+ {
+ return FALSE;
+ }
+
if (match_start != NULL)
{
start = *match_start;
@@ -1992,6 +2094,11 @@ _gtk_source_search_replace_all (GtkSourceSearch *search,
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), 0);
g_return_val_if_fail (replace != NULL, 0);
+ if (dispose_has_run (search))
+ {
+ return 0;
+ }
+
gtk_text_buffer_get_start_iter (search->priv->buffer, &iter);
gtk_text_buffer_begin_user_action (search->priv->buffer);
@@ -2009,69 +2116,3 @@ _gtk_source_search_replace_all (GtkSourceSearch *search,
return nb_matches_replaced;
}
-
-gint
-_gtk_source_search_get_occurrence_position (GtkSourceSearch *search,
- const GtkTextIter *match_start,
- const GtkTextIter *match_end)
-{
- GtkTextIter m_start;
- GtkTextIter m_end;
- GtkTextIter iter;
- gboolean found;
- gint position = 0;
-
- g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), -1);
- g_return_val_if_fail (match_start != NULL, -1);
- g_return_val_if_fail (match_end != NULL, -1);
-
- /* Verify that the occurrence is correct. */
-
- found = basic_forward_search (search,
- match_start,
- &m_start,
- &m_end,
- match_end);
-
- if (!found ||
- !gtk_text_iter_equal (match_start, &m_start) ||
- !gtk_text_iter_equal (match_end, &m_end))
- {
- return 0;
- }
-
- /* Verify that the scan region is empty between the start of the buffer
- * and the end of the occurrence.
- */
-
- gtk_text_buffer_get_start_iter (search->priv->buffer, &iter);
-
- if (search->priv->scan_region != NULL)
- {
- GtkTextRegion *region = gtk_text_region_intersect (search->priv->scan_region,
- &iter,
- match_end);
-
- gboolean empty = is_text_region_empty (region);
-
- if (region != NULL)
- {
- gtk_text_region_destroy (region, TRUE);
- }
-
- if (!empty)
- {
- return -1;
- }
- }
-
- /* Everything is fine, count the number of previous occurrences. */
-
- while (smart_forward_search_without_scanning (search, &iter, &m_start, &m_end, match_start))
- {
- position++;
- iter = m_end;
- }
-
- return position + 1;
-}
diff --git a/gtksourceview/gtksourcesearch.h b/gtksourceview/gtksourcesearch.h
index a13bf31..19c6cfa 100644
--- a/gtksourceview/gtksourcesearch.h
+++ b/gtksourceview/gtksourcesearch.h
@@ -65,15 +65,6 @@ G_GNUC_INTERNAL
const gchar * _gtk_source_search_get_text (GtkSourceSearch *search);
G_GNUC_INTERNAL
-void _gtk_source_search_update_highlight (GtkSourceSearch *search,
- const GtkTextIter *start,
- const GtkTextIter *end,
- gboolean synchronous);
-
-G_GNUC_INTERNAL
-guint _gtk_source_search_get_occurrences_count (GtkSourceSearch *search);
-
-G_GNUC_INTERNAL
void _gtk_source_search_set_case_sensitive (GtkSourceSearch *search,
gboolean
case_sensitive);
@@ -95,6 +86,20 @@ G_GNUC_INTERNAL
gboolean _gtk_source_search_get_wrap_around (GtkSourceSearch *search);
G_GNUC_INTERNAL
+guint _gtk_source_search_get_occurrences_count (GtkSourceSearch *search);
+
+G_GNUC_INTERNAL
+gint _gtk_source_search_get_occurrence_position (GtkSourceSearch *search,
+ const GtkTextIter *match_start,
+ const GtkTextIter *match_end);
+
+G_GNUC_INTERNAL
+void _gtk_source_search_update_highlight (GtkSourceSearch *search,
+ const GtkTextIter *start,
+ const GtkTextIter *end,
+ gboolean synchronous);
+
+G_GNUC_INTERNAL
gboolean _gtk_source_search_forward (GtkSourceSearch *search,
const GtkTextIter *iter,
GtkTextIter *match_start,
@@ -146,11 +151,6 @@ guint _gtk_source_search_replace_all
(GtkSourceSearch *search,
const gchar *replace,
gint
replace_length);
-G_GNUC_INTERNAL
-gint _gtk_source_search_get_occurrence_position (GtkSourceSearch *search,
- const GtkTextIter *match_start,
- const GtkTextIter *match_end);
-
G_END_DECLS
#endif /* __GTK_SOURCE_SEARCH_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]