[gnome-builder/wip/libide] libide: use alternative join-lines implementation



commit 5275797f72c4485750c60f4a02fc3cc69514c648
Author: Christian Hergert <christian hergert me>
Date:   Wed Mar 11 14:55:24 2015 -0700

    libide: use alternative join-lines implementation
    
    This wont swallow text after the end of the lines. Not that big of a deal
    to pull all the text out and reinsert it since it's wrapped in a
    begin/end user action. On large chunks of text, its probably easier on
    the buffer too since there are fewer iter changes to propagate.

 libide/ide-source-view.c |   33 ++++++++++++++++++++++++---------
 1 files changed, 24 insertions(+), 9 deletions(-)
---
diff --git a/libide/ide-source-view.c b/libide/ide-source-view.c
index 63ff692..6ccb816 100644
--- a/libide/ide-source-view.c
+++ b/libide/ide-source-view.c
@@ -2208,29 +2208,44 @@ ide_source_view_real_join_lines (IdeSourceView *self)
   GtkTextBuffer *buffer;
   GtkTextIter begin;
   GtkTextIter end;
-  guint line;
+  gchar *text;
+  gchar **lines;
+  gsize i;
 
   g_assert (IDE_IS_SOURCE_VIEW (self));
 
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self));
+
   gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
 
-  if (gtk_text_iter_equal (&begin, &end))
+  if (gtk_text_iter_get_line (&begin) == gtk_text_iter_get_line (&end))
     return;
 
   gtk_text_iter_order (&begin, &end);
 
-  line = gtk_text_iter_get_line (&begin);
+  gtk_text_iter_set_line_offset (&begin, 0);
+  if (!gtk_text_iter_ends_line (&end))
+    gtk_text_iter_forward_to_line_end (&end);
 
-  if (gtk_text_iter_starts_line (&end) && !gtk_text_iter_ends_line (&end))
-    gtk_text_iter_backward_char (&end);
+  text = gtk_text_iter_get_slice (&begin, &end);
+  lines = g_strsplit (text, "\n", 0);
+  g_free (text);
 
-  if (GTK_SOURCE_IS_BUFFER (buffer))
-    gtk_source_buffer_join_lines (GTK_SOURCE_BUFFER (buffer), &begin, &end);
+  for (i = 1; lines [i]; i++)
+    g_strstrip (lines [i]);
 
-  gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
-  gtk_text_iter_set_line (&begin, line);
+  text = g_strchomp (g_strjoinv (" ", lines));
+  g_strfreev (lines);
+
+  gtk_text_buffer_begin_user_action (buffer);
+  gtk_text_buffer_delete (buffer, &begin, &end);
+  gtk_text_buffer_insert (buffer, &begin, text, -1);
+  if (!gtk_text_iter_ends_line (&begin))
+    gtk_text_iter_forward_to_line_end (&begin);
   gtk_text_buffer_select_range (buffer, &begin, &begin);
+  gtk_text_buffer_end_user_action (buffer);
+
+  g_free (text);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]