[gnome-builder] editor: start extracting editor commands into their own file.



commit 9e31750eb0779910434dc986eceaf11fdecb7535
Author: Christian Hergert <christian hergert me>
Date:   Thu Sep 11 22:07:31 2014 -0700

    editor: start extracting editor commands into their own file.

 src/editor/gb-editor-commands.c  |  134 ++++++++++++++++++++++++++++++++++++++
 src/editor/gb-editor-commands.h  |   30 +++++++++
 src/editor/gb-editor-tab.c       |  112 -------------------------------
 src/editor/gb-editor-workspace.c |    6 +-
 src/gnome-builder.mk             |    2 +
 5 files changed, 168 insertions(+), 116 deletions(-)
---
diff --git a/src/editor/gb-editor-commands.c b/src/editor/gb-editor-commands.c
new file mode 100644
index 0000000..6ad0f5b
--- /dev/null
+++ b/src/editor/gb-editor-commands.c
@@ -0,0 +1,134 @@
+/* gb-editor-commands.c
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gb-editor-commands.h"
+#include "gb-editor-tab.h"
+#include "gb-editor-tab-private.h"
+#include "gb-log.h"
+#include "gb-source-formatter.h"
+
+/**
+ * gb_editor_commands_reformat:
+ * @tab: A #GbEditorTab.
+ *
+ * Begin a source reformatting operation.
+ *
+ * TODO:
+ *    - Use source reformatting rules based on the document language.
+ *    - Perform operation asynchronously, while locking the editor.
+ *    - Track editor state (loading/saving/operation/etc)
+ *    - Maybe add GbSourceOperation? These could do lots of
+ *      transforms, useful for FixIt's too?
+ */
+void
+gb_editor_commands_reformat (GbEditorTab *tab)
+{
+  GbEditorTabPrivate *priv;
+  GbSourceFormatter *formatter;
+  GtkSourceLanguage *language;
+  GtkTextBuffer *buffer;
+  GtkTextIter begin;
+  GtkTextIter end;
+  GtkTextIter iter;
+  GtkTextMark *insert;
+  gboolean fragment = TRUE;
+  GError *error = NULL;
+  gchar *input = NULL;
+  gchar *output = NULL;
+  guint line_number;
+  guint char_offset;
+
+  ENTRY;
+
+  /*
+   * TODO: Do this asynchronously, add tab state, propagate errors.
+   */
+
+  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
+
+  priv = tab->priv;
+
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->source_view));
+
+  gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
+
+  if (gtk_text_iter_compare (&begin, &end) == 0)
+    {
+      gtk_text_buffer_get_bounds (buffer, &begin, &end);
+      fragment = FALSE;
+    }
+
+  input = gtk_text_buffer_get_text (buffer, &begin, &end, TRUE);
+
+  insert = gtk_text_buffer_get_insert (buffer);
+  gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
+  char_offset = gtk_text_iter_get_line_offset (&iter);
+  line_number = gtk_text_iter_get_line (&iter);
+
+  language = gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (buffer));
+  formatter = gb_source_formatter_new_from_language (language);
+
+  if (!gb_source_formatter_format (formatter, input, fragment, NULL, &output,
+                                   &error))
+    {
+      g_warning ("%s", error->message);
+      g_clear_error (&error);
+      GOTO (cleanup);
+    }
+
+  gtk_text_buffer_begin_user_action (buffer);
+
+  gb_source_view_clear_snippets (priv->source_view);
+
+  /* TODO: Keep the cursor on same CXCursor from Clang instead of the
+   *       same character offset within the buffer. We probably want
+   *       to defer this to the formatter API since it will be language
+   *       specific.
+   */
+
+  gtk_text_buffer_delete (buffer, &begin, &end);
+  gtk_text_buffer_insert (buffer, &begin, output, -1);
+
+  if (line_number >= gtk_text_buffer_get_line_count (buffer))
+    {
+      gtk_text_buffer_get_bounds (buffer, &begin, &iter);
+      goto select_range;
+    }
+
+  gtk_text_buffer_get_iter_at_line (buffer, &iter, line_number);
+  gtk_text_iter_forward_to_line_end (&iter);
+
+  if (gtk_text_iter_get_line (&iter) != line_number)
+    gtk_text_iter_backward_char (&iter);
+  else if (gtk_text_iter_get_line_offset (&iter) > char_offset)
+    gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, line_number, char_offset);
+
+select_range:
+  gtk_text_buffer_select_range (buffer, &iter, &iter);
+  gtk_text_buffer_end_user_action (buffer);
+
+  gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->source_view), &iter,
+                                0.25, TRUE, 0.5, 0.5);
+
+cleanup:
+  g_free (input);
+  g_free (output);
+  g_clear_object (&formatter);
+
+  EXIT;
+}
diff --git a/src/editor/gb-editor-commands.h b/src/editor/gb-editor-commands.h
new file mode 100644
index 0000000..25d923e
--- /dev/null
+++ b/src/editor/gb-editor-commands.h
@@ -0,0 +1,30 @@
+/* gb-editor-commands.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GB_EDITOR_COMMANDS_H
+#define GB_EDITOR_COMMANDS_H
+
+#include "gb-editor-tab.h"
+
+G_BEGIN_DECLS
+
+void gb_editor_commands_reformat (GbEditorTab *tab);
+
+G_END_DECLS
+
+#endif /* GB_EDITOR_COMMANDS_H */
diff --git a/src/editor/gb-editor-tab.c b/src/editor/gb-editor-tab.c
index 7c45d7c..4813864 100644
--- a/src/editor/gb-editor-tab.c
+++ b/src/editor/gb-editor-tab.c
@@ -24,7 +24,6 @@
 #include "gb-editor-tab-private.h"
 #include "gb-log.h"
 #include "gb-rgba.h"
-#include "gb-source-formatter.h"
 #include "gb-source-snippet.h"
 #include "gb-source-snippets-manager.h"
 #include "gb-source-snippets.h"
@@ -177,117 +176,6 @@ gb_editor_tab_go_to_end (GbEditorTab *tab)
   EXIT;
 }
 
-/**
- * gb_editor_tab_reformat:
- * @tab: A #GbEditorTab.
- *
- * Begin a source reformatting operation.
- *
- * TODO:
- *    - Use source reformatting rules based on the document language.
- *    - Perform operation asynchronously, while locking the editor.
- *    - Track editor state (loading/saving/operation/etc)
- *    - Maybe add GbSourceOperation? These could do lots of
- *      transforms, useful for FixIt's too?
- */
-void
-gb_editor_tab_reformat (GbEditorTab *tab)
-{
-  GbEditorTabPrivate *priv;
-  GbSourceFormatter *formatter;
-  GtkSourceLanguage *language;
-  GtkTextBuffer *buffer;
-  GtkTextIter begin;
-  GtkTextIter end;
-  GtkTextIter iter;
-  GtkTextMark *insert;
-  gboolean fragment = TRUE;
-  GError *error = NULL;
-  gchar *input = NULL;
-  gchar *output = NULL;
-  guint line_number;
-  guint char_offset;
-
-  ENTRY;
-
-  /*
-   * TODO: Do this asynchronously, add tab state, propagate errors.
-   */
-
-  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
-
-  priv = tab->priv;
-
-  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->source_view));
-
-  gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
-
-  if (gtk_text_iter_compare (&begin, &end) == 0)
-    {
-      gtk_text_buffer_get_bounds (buffer, &begin, &end);
-      fragment = FALSE;
-    }
-
-  input = gtk_text_buffer_get_text (buffer, &begin, &end, TRUE);
-
-  insert = gtk_text_buffer_get_insert (buffer);
-  gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
-  char_offset = gtk_text_iter_get_line_offset (&iter);
-  line_number = gtk_text_iter_get_line (&iter);
-
-  language = gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (buffer));
-  formatter = gb_source_formatter_new_from_language (language);
-
-  if (!gb_source_formatter_format (formatter, input, fragment, NULL, &output,
-                                   &error))
-    {
-      g_warning ("%s", error->message);
-      g_clear_error (&error);
-      GOTO (cleanup);
-    }
-
-  gtk_text_buffer_begin_user_action (buffer);
-
-  gb_source_view_clear_snippets (priv->source_view);
-
-  /* TODO: Keep the cursor on same CXCursor from Clang instead of the
-   *       same character offset within the buffer. We probably want
-   *       to defer this to the formatter API since it will be language
-   *       specific.
-   */
-
-  gtk_text_buffer_delete (buffer, &begin, &end);
-  gtk_text_buffer_insert (buffer, &begin, output, -1);
-
-  if (line_number >= gtk_text_buffer_get_line_count (buffer))
-    {
-      gtk_text_buffer_get_bounds (buffer, &begin, &iter);
-      goto select_range;
-    }
-
-  gtk_text_buffer_get_iter_at_line (buffer, &iter, line_number);
-  gtk_text_iter_forward_to_line_end (&iter);
-
-  if (gtk_text_iter_get_line (&iter) != line_number)
-    gtk_text_iter_backward_char (&iter);
-  else if (gtk_text_iter_get_line_offset (&iter) > char_offset)
-    gtk_text_buffer_get_iter_at_line_offset (buffer, &iter, line_number, char_offset);
-
-select_range:
-  gtk_text_buffer_select_range (buffer, &iter, &iter);
-  gtk_text_buffer_end_user_action (buffer);
-
-  gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->source_view), &iter,
-                                0.25, TRUE, 0.5, 0.5);
-
-cleanup:
-  g_free (input);
-  g_free (output);
-  g_clear_object (&formatter);
-
-  EXIT;
-}
-
 void
 gb_editor_tab_focus_search (GbEditorTab *tab)
 {
diff --git a/src/editor/gb-editor-workspace.c b/src/editor/gb-editor-workspace.c
index 6bddd76..71b4ed0 100644
--- a/src/editor/gb-editor-workspace.c
+++ b/src/editor/gb-editor-workspace.c
@@ -18,6 +18,7 @@
 
 #include <glib/gi18n.h>
 
+#include "gb-editor-commands.h"
 #include "gb-editor-tab.h"
 #include "gb-editor-workspace.h"
 #include "gb-multi-notebook.h"
@@ -117,10 +118,7 @@ on_reformat_activate (GSimpleAction *action,
   tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
 
   if (tab)
-    {
-      g_assert (GB_IS_EDITOR_TAB (tab));
-      gb_editor_tab_reformat (GB_EDITOR_TAB (tab));
-    }
+    gb_editor_commands_reformat (GB_EDITOR_TAB (tab));
 }
 
 static void
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index a693911..35b1cf0 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -16,6 +16,8 @@ gnome_builder_SOURCES = \
        src/devhelp/gb-devhelp-workspace.h \
        src/devhelp/gb-devhelp-tab.c \
        src/devhelp/gb-devhelp-tab.h \
+       src/editor/gb-editor-commands.c \
+       src/editor/gb-editor-commands.h \
        src/editor/gb-editor-document.c \
        src/editor/gb-editor-document.h \
        src/editor/gb-editor-settings.c \


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