[gnome-builder: 117/139] restore-cursor: add restore-cursor plugin



commit 046a74eef6f5a979829584490310bc8dba7e459c
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jan 9 17:36:15 2019 -0800

    restore-cursor: add restore-cursor plugin
    
    This extracts cursor restoration features from the buffer manager into
    a plugin. It is both more self contained and easier to read/modify as
    a buffer addin.

 .../gbp-restore-cursor-buffer-addin.c              | 151 +++++++++++++++++++++
 .../gbp-restore-cursor-buffer-addin.h              |  31 +++++
 src/plugins/restore-cursor/meson.build             |  12 ++
 src/plugins/restore-cursor/restore-cursor-plugin.c |  36 +++++
 .../restore-cursor/restore-cursor.gresource.xml    |   6 +
 src/plugins/restore-cursor/restore-cursor.plugin   |  10 ++
 6 files changed, 246 insertions(+)
---
diff --git a/src/plugins/restore-cursor/gbp-restore-cursor-buffer-addin.c 
b/src/plugins/restore-cursor/gbp-restore-cursor-buffer-addin.c
new file mode 100644
index 000000000..2ad872ab1
--- /dev/null
+++ b/src/plugins/restore-cursor/gbp-restore-cursor-buffer-addin.c
@@ -0,0 +1,151 @@
+/* gbp-restore-cursor-buffer-addin.c
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-restore-cursor-buffer-addin"
+
+#include "config.h"
+
+#include <libide-code.h>
+
+#include "ide-buffer-private.h"
+
+#include "gbp-restore-cursor-buffer-addin.h"
+
+#define IDE_FILE_ATTRIBUTE_POSITION "metadata::libide-position"
+
+struct _GbpRestoreCursorBufferAddin
+{
+  GObject parent_instance;
+};
+
+static void
+gbp_restore_cursor_buffer_addin_file_saved (IdeBufferAddin *addin,
+                                            IdeBuffer      *buffer,
+                                            GFile          *file)
+{
+  g_autofree gchar *position = NULL;
+  g_autoptr(GError) error = NULL;
+  GtkTextMark *insert;
+  GtkTextIter iter;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_RESTORE_CURSOR_BUFFER_ADDIN (addin));
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_FILE (file));
+
+  insert = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (buffer));
+  gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer), &iter, insert);
+  position = g_strdup_printf ("%u:%u",
+                              gtk_text_iter_get_line (&iter),
+                              gtk_text_iter_get_line_offset (&iter));
+
+  if (!g_file_set_attribute_string (file, IDE_FILE_ATTRIBUTE_POSITION, position, 0, NULL, &error))
+    g_warning ("Failed to persist cursor position: %s", error->message);
+}
+
+static void
+gbp_restore_cursor_buffer_addin_file_loaded_cb (GObject      *object,
+                                                GAsyncResult *result,
+                                                gpointer      user_data)
+{
+  GFile *file = (GFile *)object;
+  g_autoptr(IdeBuffer) buffer = user_data;
+  g_autoptr(GFileInfo) file_info = NULL;
+  g_autoptr(GError) error = NULL;
+  const gchar *attr;
+  guint line_offset = 0;
+  guint line = 0;
+
+  g_assert (G_IS_FILE (file));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_BUFFER (buffer));
+
+  /* Don't do anything if the user already moved */
+  if (_ide_buffer_can_restore_cursor (buffer))
+    return;
+
+  if (!(file_info = g_file_query_info_finish (file, result, &error)))
+    return;
+
+  if (!g_file_info_has_attribute (file_info, IDE_FILE_ATTRIBUTE_POSITION) ||
+      !(attr = g_file_info_get_attribute_string (file_info, IDE_FILE_ATTRIBUTE_POSITION)))
+    return;
+
+  if (sscanf (attr, "%u:%u", &line, &line_offset) >= 1)
+    {
+      GtkTextIter iter;
+
+      IDE_TRACE_MSG ("Restoring insert mark to %u:%u", line + 1, line_offset + 1);
+      gtk_text_buffer_get_iter_at_line_offset (GTK_TEXT_BUFFER (buffer),
+                                               &iter,
+                                               line,
+                                               line_offset);
+      gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer), &iter, &iter);
+
+      /* TODO: Notify view that we need to scroll? */
+    }
+}
+
+static void
+gbp_restore_cursor_buffer_addin_file_loaded (IdeBufferAddin *addin,
+                                             IdeBuffer      *buffer,
+                                             GFile          *file)
+{
+  g_autoptr(GSettings) settings = NULL;
+  g_autoptr(GFileInfo) file_info = NULL;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_RESTORE_CURSOR_BUFFER_ADDIN (addin));
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_FILE (file));
+
+  /* Make sure our setting isn't disabled */
+  settings = g_settings_new ("org.gnome.builder.editor");
+  if (!g_settings_get_boolean (settings, "restore-insert-mark"))
+    return;
+
+  g_file_query_info_async (file,
+                           IDE_FILE_ATTRIBUTE_POSITION,
+                           G_FILE_QUERY_INFO_NONE,
+                           G_PRIORITY_HIGH,
+                           NULL,
+                           gbp_restore_cursor_buffer_addin_file_loaded_cb,
+                           g_object_ref (buffer));
+}
+
+static void
+buffer_addin_iface_init (IdeBufferAddinInterface *iface)
+{
+  iface->file_loaded = gbp_restore_cursor_buffer_addin_file_loaded;
+  iface->file_saved = gbp_restore_cursor_buffer_addin_file_saved;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpRestoreCursorBufferAddin, gbp_restore_cursor_buffer_addin, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_BUFFER_ADDIN, buffer_addin_iface_init))
+
+static void
+gbp_restore_cursor_buffer_addin_class_init (GbpRestoreCursorBufferAddinClass *klass)
+{
+}
+
+static void
+gbp_restore_cursor_buffer_addin_init (GbpRestoreCursorBufferAddin *self)
+{
+}
diff --git a/src/plugins/restore-cursor/gbp-restore-cursor-buffer-addin.h 
b/src/plugins/restore-cursor/gbp-restore-cursor-buffer-addin.h
new file mode 100644
index 000000000..e6396e8cf
--- /dev/null
+++ b/src/plugins/restore-cursor/gbp-restore-cursor-buffer-addin.h
@@ -0,0 +1,31 @@
+/* gbp-restore-cursor-buffer-addin.h
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_RESTORE_CURSOR_BUFFER_ADDIN (gbp_restore_cursor_buffer_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpRestoreCursorBufferAddin, gbp_restore_cursor_buffer_addin, GBP, 
RESTORE_CURSOR_BUFFER_ADDIN, GObject)
+
+G_END_DECLS
diff --git a/src/plugins/restore-cursor/meson.build b/src/plugins/restore-cursor/meson.build
new file mode 100644
index 000000000..5ebbe8810
--- /dev/null
+++ b/src/plugins/restore-cursor/meson.build
@@ -0,0 +1,12 @@
+plugins_sources += files([
+  'restore-cursor-plugin.c',
+  'gbp-restore-cursor-buffer-addin.c',
+])
+
+plugin_restore_cursor_resources = gnome.compile_resources(
+  'gbp-restore-cursor-resources',
+  'restore-cursor.gresource.xml',
+  c_name: 'gbp_restore_cursor',
+)
+
+plugins_sources += plugin_restore_cursor_resources[0]
diff --git a/src/plugins/restore-cursor/restore-cursor-plugin.c 
b/src/plugins/restore-cursor/restore-cursor-plugin.c
new file mode 100644
index 000000000..d11164791
--- /dev/null
+++ b/src/plugins/restore-cursor/restore-cursor-plugin.c
@@ -0,0 +1,36 @@
+/* restore-cursor-plugin.c
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "restore-cursor-plugin"
+
+#include "config.h"
+
+#include <libpeas/peas.h>
+#include <libide-code.h>
+
+#include "gbp-restore-cursor-buffer-addin.h"
+
+_IDE_EXTERN void
+_gbp_restore_cursor_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_BUFFER_ADDIN,
+                                              GBP_TYPE_RESTORE_CURSOR_BUFFER_ADDIN);
+}
diff --git a/src/plugins/restore-cursor/restore-cursor.gresource.xml 
b/src/plugins/restore-cursor/restore-cursor.gresource.xml
new file mode 100644
index 000000000..5832a901c
--- /dev/null
+++ b/src/plugins/restore-cursor/restore-cursor.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/plugins/restore-cursor">
+    <file>restore-cursor.plugin</file>
+  </gresource>
+</gresources>
diff --git a/src/plugins/restore-cursor/restore-cursor.plugin 
b/src/plugins/restore-cursor/restore-cursor.plugin
new file mode 100644
index 000000000..3b2d24362
--- /dev/null
+++ b/src/plugins/restore-cursor/restore-cursor.plugin
@@ -0,0 +1,10 @@
+[Plugin]
+Authors=Christian Hergert <christian hergert me>
+Builtin=true
+Copyright=Copyright © 2018 Christian Hergert
+Depends=editor;
+Description=Restore cursors when a buffer is re-opened.
+Embedded=_gbp_restore_cursor_register_types
+Hidden=true
+Module=restore-cursor
+Name=Restore Cursor


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