[gnome-builder] libide: trim trailing whitespace from modified lines when saving
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] libide: trim trailing whitespace from modified lines when saving
- Date: Mon, 23 Mar 2015 23:46:11 +0000 (UTC)
commit e58c9d86b59218e0f8ec9b998e501913d7bea563
Author: Christian Hergert <christian hergert me>
Date: Thu Feb 26 21:56:54 2015 -0800
libide: trim trailing whitespace from modified lines when saving
If the IdeFileSettings dictate that we should trim trailing whitespace,
then trim whitespace from lines that are marked as modified by the
IdeBufferChangeMonitor.
Limiting changes to only these lines helps avoid those giant diffs where
all you did was trim trailing whitespace.
libide/ide-buffer-manager.c | 8 ++++++
libide/ide-buffer.c | 54 +++++++++++++++++++++++++++++++++++++++++++
libide/ide-buffer.h | 1 +
3 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/libide/ide-buffer-manager.c b/libide/ide-buffer-manager.c
index da98e7e..53d02b2 100644
--- a/libide/ide-buffer-manager.c
+++ b/libide/ide-buffer-manager.c
@@ -628,6 +628,14 @@ ide_buffer_manager_save_file__load_settings_cb (GObject *object,
}
}
+ /*
+ * If file-settings dictate that we should trim trailing whitespace, trim it from the modified
+ * lines in the IdeBuffer. This is performed automatically based on line state within
+ * ide_buffer_trim_trailing_whitespace().
+ */
+ if (ide_file_settings_get_trim_trailing_whitespace (file_settings))
+ ide_buffer_trim_trailing_whitespace (state->buffer);
+
gtk_source_file_saver_set_encoding (saver, encoding);
gtk_source_file_saver_set_newline_type (saver, newline_type);
diff --git a/libide/ide-buffer.c b/libide/ide-buffer.c
index ad01b5b..922d2ff 100644
--- a/libide/ide-buffer.c
+++ b/libide/ide-buffer.c
@@ -40,6 +40,8 @@
#define TAG_WARNING "diagnostician::warning"
#define TAG_NOTE "diagnostician::note"
+#define TEXT_ITER_IS_SPACE(ptr) g_unichar_isspace(gtk_text_iter_get_char(ptr))
+
typedef struct _IdeBufferClass
{
GtkSourceBufferClass parent;
@@ -954,3 +956,55 @@ ide_buffer_get_content (IdeBuffer *self)
return g_bytes_ref (self->content);
}
+
+void
+ide_buffer_trim_trailing_whitespace (IdeBuffer *self)
+{
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
+ gint line;
+
+ g_return_if_fail (IDE_IS_BUFFER (self));
+
+ buffer = GTK_TEXT_BUFFER (self);
+
+ gtk_text_buffer_get_end_iter (buffer, &iter);
+
+ for (line = gtk_text_iter_get_line (&iter); line >= 0; line--)
+ {
+ IdeBufferLineChange change = IDE_BUFFER_LINE_CHANGE_CHANGED;
+
+ if (self->change_monitor)
+ {
+ GtkTextIter tmp;
+
+ gtk_text_buffer_get_iter_at_line (buffer, &tmp, line);
+ change = ide_buffer_change_monitor_get_change (self->change_monitor, &tmp);
+ }
+
+ if (change != IDE_BUFFER_LINE_CHANGE_NONE)
+ {
+ gtk_text_buffer_get_iter_at_line (buffer, &iter, line);
+
+ if (gtk_text_iter_forward_to_line_end (&iter) && TEXT_ITER_IS_SPACE (&iter))
+ {
+ GtkTextIter begin = iter;
+
+ while (TEXT_ITER_IS_SPACE (&begin))
+ {
+ if (gtk_text_iter_starts_line (&begin))
+ break;
+
+ if (!gtk_text_iter_backward_char (&begin))
+ break;
+ }
+
+ if (!TEXT_ITER_IS_SPACE (&begin) && !gtk_text_iter_ends_line (&begin))
+ gtk_text_iter_forward_char (&begin);
+
+ if (!gtk_text_iter_equal (&begin, &iter))
+ gtk_text_buffer_delete (buffer, &begin, &iter);
+ }
+ }
+ }
+}
diff --git a/libide/ide-buffer.h b/libide/ide-buffer.h
index d454989..20bfb15 100644
--- a/libide/ide-buffer.h
+++ b/libide/ide-buffer.h
@@ -63,6 +63,7 @@ IdeBufferLineFlags ide_buffer_get_line_flags (IdeBuffer *buf
GBytes *ide_buffer_get_content (IdeBuffer *self);
IdeDiagnostic *ide_buffer_get_diagnostic_at_iter (IdeBuffer *self,
const GtkTextIter *iter);
+void ide_buffer_trim_trailing_whitespace (IdeBuffer *self);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]