/* brackets-completion.c generated by valac 0.56.19, the Vala compiler
 * generated from brackets-completion.vala, do not modify */

/*
 * SPDX-License-Identifier: LGPL-3.0-or-later
 * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
 *                         2013 Mario Guerriero <mario@elementaryos.org>
 */

#include "brackets-completion.h"
#include <glib-object.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include "codecore.h"
#include <gdk/gdk.h>
#include <gtksourceview/gtksource.h>
#include <libpeas.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	SCRATCH_PLUGINS_BRACKETS_COMPLETION_0_PROPERTY,
	SCRATCH_PLUGINS_BRACKETS_COMPLETION_OBJECT_PROPERTY,
	SCRATCH_PLUGINS_BRACKETS_COMPLETION_NUM_PROPERTIES
};
static GParamSpec* scratch_plugins_brackets_completion_properties[SCRATCH_PLUGINS_BRACKETS_COMPLETION_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define __vala_GdkEventKey_free0(var) ((var == NULL) ? NULL : (var = (_vala_GdkEventKey_free (var), NULL)))

struct _ScratchPluginsBracketsCompletionPrivate {
	GObject* _object;
	GeeHashMap* brackets;
	GeeHashMap* keys;
	GtkTextBuffer* current_buffer;
	ScratchServicesInterface* plugins;
	ScratchWidgetsSourceView* current_source_view;
	gchar* previous_selection;
};

static gint ScratchPluginsBracketsCompletion_private_offset;
static gpointer scratch_plugins_brackets_completion_parent_class = NULL;
static ScratchServicesActivatablePluginIface * scratch_plugins_brackets_completion_scratch_services_activatable_plugin_parent_iface = NULL;
static GType scratch_plugins_brackets_completion_type_id = 0;

static void scratch_plugins_brackets_completion_real_activate (ScratchServicesActivatablePlugin* base);
static void scratch_plugins_brackets_completion_on_hook_document (ScratchPluginsBracketsCompletion* self,
                                                           ScratchServicesDocument* doc);
static void _scratch_plugins_brackets_completion_on_hook_document_scratch_services_interface_hook_document (ScratchServicesInterface* _sender,
                                                                                                     ScratchServicesDocument* doc,
                                                                                                     gpointer self);
static void scratch_plugins_brackets_completion_real_deactivate (ScratchServicesActivatablePlugin* base);
static void scratch_plugins_brackets_completion_real_update_state (ScratchServicesActivatablePlugin* base);
static gboolean scratch_plugins_brackets_completion_on_key_down (ScratchPluginsBracketsCompletion* self,
                                                          GdkEventKey* event);
static gboolean _scratch_plugins_brackets_completion_on_key_down_gtk_widget_key_press_event (GtkWidget* _sender,
                                                                                      GdkEventKey* event,
                                                                                      gpointer self);
static void scratch_plugins_brackets_completion_on_event_after (ScratchPluginsBracketsCompletion* self,
                                                         GdkEvent* root_event);
static void _scratch_plugins_brackets_completion_on_event_after_gtk_widget_event_after (GtkWidget* _sender,
                                                                                 GdkEvent* event,
                                                                                 gpointer self);
static void scratch_plugins_brackets_completion_on_backspace (ScratchPluginsBracketsCompletion* self);
static void _scratch_plugins_brackets_completion_on_backspace_gtk_text_view_backspace (GtkTextView* _sender,
                                                                                gpointer self);
static gchar* scratch_plugins_brackets_completion_get_next_char (ScratchPluginsBracketsCompletion* self);
static gboolean _gtk_text_iter_equal (const GtkTextIter * s1,
                               const GtkTextIter * s2);
static gchar* scratch_plugins_brackets_completion_get_previous_char (ScratchPluginsBracketsCompletion* self);
static void scratch_plugins_brackets_completion_complete_brackets (ScratchPluginsBracketsCompletion* self,
                                                            const gchar* opening_bracket);
static gboolean scratch_plugins_brackets_completion_has_valid_next_char (ScratchPluginsBracketsCompletion* self,
                                                                  const gchar* next_char);
static gboolean _vala_string_array_contains (const gchar* * stack,
                                      gssize stack_length,
                                      const const gchar* needle);
static void scratch_plugins_brackets_completion_delete_next_char (ScratchPluginsBracketsCompletion* self);
static void scratch_plugins_brackets_completion_check_bracket_indent (ScratchPluginsBracketsCompletion* self);
static GdkEventKey* _vala_GdkEventKey_copy (GdkEventKey* self);
static void _vala_GdkEventKey_free (GdkEventKey* self);
static void scratch_plugins_brackets_completion_finalize (GObject * obj);
static void _vala_scratch_plugins_brackets_completion_get_property (GObject * object,
                                                             guint property_id,
                                                             GValue * value,
                                                             GParamSpec * pspec);
static void _vala_scratch_plugins_brackets_completion_set_property (GObject * object,
                                                             guint property_id,
                                                             const GValue * value,
                                                             GParamSpec * pspec);

static const gchar* SCRATCH_PLUGINS_BRACKETS_COMPLETION_VALID_NEXT_CHARS[10] = {"", " ", "\b", "\r", "\n", "\t", ",", ".", ";", ":"};

static inline gpointer
scratch_plugins_brackets_completion_get_instance_private (ScratchPluginsBracketsCompletion* self)
{
	return G_STRUCT_MEMBER_P (self, ScratchPluginsBracketsCompletion_private_offset);
}

static void
_scratch_plugins_brackets_completion_on_hook_document_scratch_services_interface_hook_document (ScratchServicesInterface* _sender,
                                                                                                ScratchServicesDocument* doc,
                                                                                                gpointer self)
{
	scratch_plugins_brackets_completion_on_hook_document ((ScratchPluginsBracketsCompletion*) self, doc);
}

static void
scratch_plugins_brackets_completion_real_activate (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsBracketsCompletion * self;
	GeeHashMap* _tmp0_;
	GeeHashMap* _tmp1_;
	GeeHashMap* _tmp2_;
	GeeHashMap* _tmp3_;
	GeeHashMap* _tmp4_;
	GeeHashMap* _tmp5_;
	GeeHashMap* _tmp6_;
	GeeHashMap* _tmp7_;
	GeeHashMap* _tmp8_;
	GeeHashMap* _tmp9_;
	GeeHashMap* _tmp10_;
	GeeHashMap* _tmp11_;
	GeeHashMap* _tmp12_;
	GeeHashMap* _tmp13_;
	GeeHashMap* _tmp14_;
	GeeHashMap* _tmp15_;
	GeeHashMap* _tmp16_;
	GObject* _tmp17_;
	GObject* _tmp18_;
	ScratchServicesInterface* _tmp19_;
	self = (ScratchPluginsBracketsCompletion*) base;
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->brackets);
	self->priv->brackets = _tmp0_;
	_tmp1_ = self->priv->brackets;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp1_, "(", ")");
	_tmp2_ = self->priv->brackets;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp2_, "[", "]");
	_tmp3_ = self->priv->brackets;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp3_, "{", "}");
	_tmp4_ = self->priv->brackets;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp4_, "'", "'");
	_tmp5_ = self->priv->brackets;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp5_, "\"", "\"");
	_tmp6_ = self->priv->brackets;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp6_, "`", "`");
	_tmp7_ = gee_hash_map_new (G_TYPE_UINT, NULL, NULL, G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->keys);
	self->priv->keys = _tmp7_;
	_tmp8_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp8_, (gpointer) ((guintptr) GDK_KEY_braceleft), "{");
	_tmp9_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp9_, (gpointer) ((guintptr) GDK_KEY_bracketleft), "[");
	_tmp10_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp10_, (gpointer) ((guintptr) GDK_KEY_parenleft), "(");
	_tmp11_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp11_, (gpointer) ((guintptr) GDK_KEY_braceright), "}");
	_tmp12_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp12_, (gpointer) ((guintptr) GDK_KEY_bracketright), "]");
	_tmp13_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp13_, (gpointer) ((guintptr) GDK_KEY_parenright), ")");
	_tmp14_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp14_, (gpointer) ((guintptr) GDK_KEY_quoteright), "'");
	_tmp15_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp15_, (gpointer) ((guintptr) GDK_KEY_quotedbl), "\"");
	_tmp16_ = self->priv->keys;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp16_, (gpointer) ((guintptr) GDK_KEY_grave), "`");
	_tmp17_ = scratch_services_activatable_plugin_get_object ((ScratchServicesActivatablePlugin*) self);
	_tmp18_ = _tmp17_;
	_g_object_unref0 (self->priv->plugins);
	self->priv->plugins = G_TYPE_CHECK_INSTANCE_CAST (_tmp18_, SCRATCH_SERVICES_TYPE_INTERFACE, ScratchServicesInterface);
	_tmp19_ = self->priv->plugins;
	g_signal_connect_object (_tmp19_, "hook-document", (GCallback) _scratch_plugins_brackets_completion_on_hook_document_scratch_services_interface_hook_document, self, 0);
}

static void
scratch_plugins_brackets_completion_real_deactivate (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsBracketsCompletion * self;
	ScratchServicesInterface* _tmp0_;
	guint _tmp1_;
	self = (ScratchPluginsBracketsCompletion*) base;
	_tmp0_ = self->priv->plugins;
	g_signal_parse_name ("hook-document", SCRATCH_SERVICES_TYPE_INTERFACE, &_tmp1_, NULL, FALSE);
	g_signal_handlers_disconnect_matched (_tmp0_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp1_, 0, NULL, (GCallback) _scratch_plugins_brackets_completion_on_hook_document_scratch_services_interface_hook_document, self);
}

static void
scratch_plugins_brackets_completion_real_update_state (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsBracketsCompletion * self;
	self = (ScratchPluginsBracketsCompletion*) base;
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static gboolean
_scratch_plugins_brackets_completion_on_key_down_gtk_widget_key_press_event (GtkWidget* _sender,
                                                                             GdkEventKey* event,
                                                                             gpointer self)
{
	gboolean result;
	result = scratch_plugins_brackets_completion_on_key_down ((ScratchPluginsBracketsCompletion*) self, event);
	return result;
}

static void
_scratch_plugins_brackets_completion_on_event_after_gtk_widget_event_after (GtkWidget* _sender,
                                                                            GdkEvent* event,
                                                                            gpointer self)
{
	scratch_plugins_brackets_completion_on_event_after ((ScratchPluginsBracketsCompletion*) self, event);
}

static void
_scratch_plugins_brackets_completion_on_backspace_gtk_text_view_backspace (GtkTextView* _sender,
                                                                           gpointer self)
{
	scratch_plugins_brackets_completion_on_backspace ((ScratchPluginsBracketsCompletion*) self);
}

static void
scratch_plugins_brackets_completion_on_hook_document (ScratchPluginsBracketsCompletion* self,
                                                      ScratchServicesDocument* doc)
{
	ScratchWidgetsSourceView* _tmp0_;
	ScratchWidgetsSourceView* _tmp1_;
	GtkTextBuffer* _tmp2_;
	GtkTextBuffer* _tmp3_;
	GtkTextBuffer* _tmp4_;
	ScratchWidgetsSourceView* _tmp5_;
	ScratchWidgetsSourceView* _tmp12_;
	ScratchWidgetsSourceView* _tmp13_;
	ScratchWidgetsSourceView* _tmp14_;
	ScratchWidgetsSourceView* _tmp15_;
	ScratchWidgetsSourceView* _tmp16_;
	ScratchWidgetsSourceView* _tmp17_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (doc != NULL);
	_tmp0_ = scratch_services_document_get_source_view (doc);
	_tmp1_ = _tmp0_;
	_tmp2_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	_g_object_unref0 (self->priv->current_buffer);
	self->priv->current_buffer = _tmp4_;
	_tmp5_ = self->priv->current_source_view;
	if (_tmp5_ != NULL) {
		ScratchWidgetsSourceView* _tmp6_;
		guint _tmp7_;
		ScratchWidgetsSourceView* _tmp8_;
		guint _tmp9_;
		ScratchWidgetsSourceView* _tmp10_;
		guint _tmp11_;
		_tmp6_ = self->priv->current_source_view;
		g_signal_parse_name ("key-press-event", gtk_widget_get_type (), &_tmp7_, NULL, FALSE);
		g_signal_handlers_disconnect_matched ((GtkWidget*) _tmp6_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp7_, 0, NULL, (GCallback) _scratch_plugins_brackets_completion_on_key_down_gtk_widget_key_press_event, self);
		_tmp8_ = self->priv->current_source_view;
		g_signal_parse_name ("event-after", gtk_widget_get_type (), &_tmp9_, NULL, FALSE);
		g_signal_handlers_disconnect_matched ((GtkWidget*) _tmp8_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp9_, 0, NULL, (GCallback) _scratch_plugins_brackets_completion_on_event_after_gtk_widget_event_after, self);
		_tmp10_ = self->priv->current_source_view;
		g_signal_parse_name ("backspace", gtk_text_view_get_type (), &_tmp11_, NULL, FALSE);
		g_signal_handlers_disconnect_matched ((GtkTextView*) _tmp10_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp11_, 0, NULL, (GCallback) _scratch_plugins_brackets_completion_on_backspace_gtk_text_view_backspace, self);
	}
	_tmp12_ = scratch_services_document_get_source_view (doc);
	_tmp13_ = _tmp12_;
	_tmp14_ = _g_object_ref0 (_tmp13_);
	_g_object_unref0 (self->priv->current_source_view);
	self->priv->current_source_view = _tmp14_;
	_tmp15_ = self->priv->current_source_view;
	g_signal_connect_object ((GtkWidget*) _tmp15_, "key-press-event", (GCallback) _scratch_plugins_brackets_completion_on_key_down_gtk_widget_key_press_event, self, 0);
	_tmp16_ = self->priv->current_source_view;
	g_signal_connect_object ((GtkWidget*) _tmp16_, "event-after", (GCallback) _scratch_plugins_brackets_completion_on_event_after_gtk_widget_event_after, self, 0);
	_tmp17_ = self->priv->current_source_view;
	g_signal_connect_object ((GtkTextView*) _tmp17_, "backspace", (GCallback) _scratch_plugins_brackets_completion_on_backspace_gtk_text_view_backspace, self, 0);
}

static gboolean
_gtk_text_iter_equal (const GtkTextIter * s1,
                      const GtkTextIter * s2)
{
	if (s1 == s2) {
		return TRUE;
	}
	if (s1 == NULL) {
		return FALSE;
	}
	if (s2 == NULL) {
		return FALSE;
	}
	return FALSE;
}

static gchar*
scratch_plugins_brackets_completion_get_next_char (ScratchPluginsBracketsCompletion* self)
{
	GtkTextIter start = {0};
	GtkTextIter end = {0};
	GtkTextBuffer* _tmp0_;
	GtkTextIter _tmp1_ = {0};
	GtkTextIter _tmp2_;
	GtkTextIter _tmp3_;
	GtkTextIter _tmp4_;
	GtkTextBuffer* _tmp6_;
	GtkTextIter _tmp7_;
	GtkTextIter _tmp8_;
	gchar* _tmp9_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->current_buffer;
	gtk_text_buffer_get_selection_bounds (_tmp0_, NULL, &_tmp1_);
	end = _tmp1_;
	_tmp2_ = end;
	start = _tmp2_;
	gtk_text_iter_forward_char (&end);
	_tmp3_ = start;
	_tmp4_ = end;
	if (_gtk_text_iter_equal (&_tmp3_, &_tmp4_) == TRUE) {
		gchar* _tmp5_;
		_tmp5_ = g_strdup ("");
		result = _tmp5_;
		return result;
	}
	_tmp6_ = self->priv->current_buffer;
	_tmp7_ = start;
	_tmp8_ = end;
	_tmp9_ = gtk_text_buffer_get_text (_tmp6_, &_tmp7_, &_tmp8_, TRUE);
	result = _tmp9_;
	return result;
}

static gchar*
scratch_plugins_brackets_completion_get_previous_char (ScratchPluginsBracketsCompletion* self)
{
	GtkTextIter start = {0};
	GtkTextIter end = {0};
	GtkTextBuffer* _tmp0_;
	GtkTextIter _tmp1_ = {0};
	GtkTextIter _tmp2_;
	GtkTextIter _tmp3_;
	GtkTextIter _tmp4_;
	GtkTextBuffer* _tmp6_;
	GtkTextIter _tmp7_;
	GtkTextIter _tmp8_;
	gchar* _tmp9_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->current_buffer;
	gtk_text_buffer_get_selection_bounds (_tmp0_, &_tmp1_, NULL);
	start = _tmp1_;
	_tmp2_ = start;
	end = _tmp2_;
	gtk_text_iter_backward_char (&start);
	_tmp3_ = start;
	_tmp4_ = end;
	if (_gtk_text_iter_equal (&_tmp3_, &_tmp4_) == TRUE) {
		gchar* _tmp5_;
		_tmp5_ = g_strdup ("");
		result = _tmp5_;
		return result;
	}
	_tmp6_ = self->priv->current_buffer;
	_tmp7_ = start;
	_tmp8_ = end;
	_tmp9_ = gtk_text_buffer_get_text (_tmp6_, &_tmp7_, &_tmp8_, TRUE);
	result = _tmp9_;
	return result;
}

static void
scratch_plugins_brackets_completion_on_backspace (ScratchPluginsBracketsCompletion* self)
{
	GtkTextBuffer* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->current_buffer;
	_tmp1_ = gtk_text_buffer_get_has_selection (_tmp0_);
	_tmp2_ = _tmp1_;
	if (!_tmp2_) {
		gchar* left_char = NULL;
		gchar* _tmp3_;
		gchar* right_char = NULL;
		gchar* _tmp4_;
		gboolean _tmp5_ = FALSE;
		gboolean _tmp6_ = FALSE;
		GeeHashMap* _tmp7_;
		const gchar* _tmp8_;
		_tmp3_ = scratch_plugins_brackets_completion_get_previous_char (self);
		left_char = _tmp3_;
		_tmp4_ = scratch_plugins_brackets_completion_get_next_char (self);
		right_char = _tmp4_;
		_tmp7_ = self->priv->brackets;
		_tmp8_ = left_char;
		if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp7_, _tmp8_)) {
			GeeHashMap* _tmp9_;
			GeeCollection* _tmp10_;
			GeeCollection* _tmp11_;
			GeeCollection* _tmp12_;
			const gchar* _tmp13_;
			_tmp9_ = self->priv->brackets;
			_tmp10_ = gee_abstract_map_get_values ((GeeAbstractMap*) _tmp9_);
			_tmp11_ = _tmp10_;
			_tmp12_ = _tmp11_;
			_tmp13_ = right_char;
			_tmp6_ = gee_collection_contains (_tmp12_, _tmp13_);
			_g_object_unref0 (_tmp12_);
		} else {
			_tmp6_ = FALSE;
		}
		if (_tmp6_) {
			GeeHashMap* _tmp14_;
			const gchar* _tmp15_;
			gpointer _tmp16_;
			gchar* _tmp17_;
			const gchar* _tmp18_;
			_tmp14_ = self->priv->brackets;
			_tmp15_ = left_char;
			_tmp16_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp14_, _tmp15_);
			_tmp17_ = (gchar*) _tmp16_;
			_tmp18_ = right_char;
			_tmp5_ = g_strcmp0 (_tmp17_, _tmp18_) == 0;
			_g_free0 (_tmp17_);
		} else {
			_tmp5_ = FALSE;
		}
		if (_tmp5_) {
			GtkTextIter start = {0};
			GtkTextIter end = {0};
			GtkTextBuffer* _tmp19_;
			GtkTextIter _tmp20_ = {0};
			GtkTextIter _tmp21_ = {0};
			GtkTextBuffer* _tmp22_;
			GtkTextIter _tmp23_;
			GtkTextIter _tmp24_;
			_tmp19_ = self->priv->current_buffer;
			gtk_text_buffer_get_selection_bounds (_tmp19_, &_tmp20_, &_tmp21_);
			start = _tmp20_;
			end = _tmp21_;
			gtk_text_iter_backward_char (&start);
			gtk_text_iter_forward_char (&end);
			_tmp22_ = self->priv->current_buffer;
			_tmp23_ = start;
			_tmp24_ = end;
			gtk_text_buffer_select_range (_tmp22_, &_tmp23_, &_tmp24_);
		}
		_g_free0 (right_char);
		_g_free0 (left_char);
	}
}

static gunichar
string_get_char (const gchar* self,
                 glong index)
{
	gunichar result;
	g_return_val_if_fail (self != NULL, 0U);
	result = g_utf8_get_char (((gchar*) self) + index);
	return result;
}

static void
scratch_plugins_brackets_completion_complete_brackets (ScratchPluginsBracketsCompletion* self,
                                                       const gchar* opening_bracket)
{
	GtkTextIter start = {0};
	GtkTextIter end = {0};
	gboolean _tmp0_ = FALSE;
	GtkTextBuffer* _tmp9_;
	GtkTextIter _tmp10_ = {0};
	GtkTextIter _tmp11_ = {0};
	gchar* closing_bracket = NULL;
	GeeHashMap* _tmp12_;
	gpointer _tmp13_;
	gchar* text = NULL;
	const gchar* _tmp14_;
	const gchar* _tmp15_;
	gchar* _tmp16_;
	GtkTextBuffer* _tmp17_;
	GtkTextBuffer* _tmp18_;
	const gchar* _tmp19_;
	GtkTextBuffer* _tmp20_;
	GtkTextIter _tmp21_ = {0};
	GtkTextIter _tmp22_ = {0};
	const gchar* _tmp23_;
	GtkTextBuffer* _tmp24_;
	GtkTextIter _tmp25_;
	GtkTextIter _tmp26_;
	GtkTextBuffer* _tmp27_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (opening_bracket != NULL);
	if (g_strcmp0 (opening_bracket, "'") == 0) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (opening_bracket, "\"") == 0;
	}
	if (_tmp0_) {
		GtkTextBuffer* _tmp1_;
		GtkTextIter _tmp2_ = {0};
		GtkTextIter _tmp3_;
		gchar* before_bracket_char = NULL;
		GtkTextBuffer* _tmp4_;
		GtkTextIter _tmp5_;
		GtkTextIter _tmp6_;
		gchar* _tmp7_;
		const gchar* _tmp8_;
		_tmp1_ = self->priv->current_buffer;
		gtk_text_buffer_get_selection_bounds (_tmp1_, &_tmp2_, NULL);
		start = _tmp2_;
		_tmp3_ = start;
		end = _tmp3_;
		gtk_text_iter_backward_chars (&start, 2);
		gtk_text_iter_backward_char (&end);
		_tmp4_ = self->priv->current_buffer;
		_tmp5_ = start;
		_tmp6_ = end;
		_tmp7_ = gtk_text_buffer_get_text (_tmp4_, &_tmp5_, &_tmp6_, FALSE);
		before_bracket_char = _tmp7_;
		_tmp8_ = before_bracket_char;
		if (g_unichar_isalpha (string_get_char (_tmp8_, (glong) 0))) {
			_g_free0 (before_bracket_char);
			return;
		}
		_g_free0 (before_bracket_char);
	}
	_tmp9_ = self->priv->current_buffer;
	gtk_text_buffer_get_selection_bounds (_tmp9_, &_tmp10_, &_tmp11_);
	start = _tmp10_;
	end = _tmp11_;
	_tmp12_ = self->priv->brackets;
	_tmp13_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp12_, opening_bracket);
	closing_bracket = (gchar*) _tmp13_;
	_tmp14_ = self->priv->previous_selection;
	_tmp15_ = closing_bracket;
	_tmp16_ = g_strconcat (_tmp14_, _tmp15_, NULL);
	text = _tmp16_;
	_tmp17_ = self->priv->current_buffer;
	gtk_text_buffer_begin_user_action (_tmp17_);
	_tmp18_ = self->priv->current_buffer;
	_tmp19_ = text;
	gtk_text_buffer_insert (_tmp18_, &start, _tmp19_, -1);
	_tmp20_ = self->priv->current_buffer;
	gtk_text_buffer_get_selection_bounds (_tmp20_, &_tmp21_, &_tmp22_);
	start = _tmp21_;
	end = _tmp22_;
	gtk_text_iter_backward_char (&end);
	_tmp23_ = self->priv->previous_selection;
	gtk_text_iter_backward_chars (&start, g_utf8_strlen (_tmp23_, (gssize) -1) + 1);
	_tmp24_ = self->priv->current_buffer;
	_tmp25_ = start;
	_tmp26_ = end;
	gtk_text_buffer_select_range (_tmp24_, &_tmp25_, &_tmp26_);
	_tmp27_ = self->priv->current_buffer;
	gtk_text_buffer_end_user_action (_tmp27_);
	_g_free0 (text);
	_g_free0 (closing_bracket);
}

static gboolean
_vala_string_array_contains (const gchar* * stack,
                             gssize stack_length,
                             const const gchar* needle)
{
	gssize i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static gboolean
scratch_plugins_brackets_completion_has_valid_next_char (ScratchPluginsBracketsCompletion* self,
                                                         const gchar* next_char)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (next_char != NULL, FALSE);
	if (_vala_string_array_contains (SCRATCH_PLUGINS_BRACKETS_COMPLETION_VALID_NEXT_CHARS, G_N_ELEMENTS (SCRATCH_PLUGINS_BRACKETS_COMPLETION_VALID_NEXT_CHARS), next_char)) {
		_tmp1_ = TRUE;
	} else {
		GeeHashMap* _tmp2_;
		GeeCollection* _tmp3_;
		GeeCollection* _tmp4_;
		GeeCollection* _tmp5_;
		_tmp2_ = self->priv->brackets;
		_tmp3_ = gee_abstract_map_get_values ((GeeAbstractMap*) _tmp2_);
		_tmp4_ = _tmp3_;
		_tmp5_ = _tmp4_;
		_tmp1_ = gee_collection_contains (_tmp5_, next_char);
		_g_object_unref0 (_tmp5_);
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		GeeHashMap* _tmp6_;
		_tmp6_ = self->priv->brackets;
		_tmp0_ = gee_abstract_map_has_key ((GeeAbstractMap*) _tmp6_, next_char);
	}
	result = _tmp0_;
	return result;
}

static void
scratch_plugins_brackets_completion_delete_next_char (ScratchPluginsBracketsCompletion* self)
{
	GtkTextBuffer* _tmp0_;
	GtkTextIter start = {0};
	GtkTextBuffer* _tmp1_;
	GtkTextBuffer* _tmp2_;
	GtkTextMark* _tmp3_;
	GtkTextIter _tmp4_ = {0};
	GtkTextBuffer* _tmp5_;
	const gchar* _tmp6_;
	GtkTextBuffer* _tmp7_;
	GtkTextBuffer* _tmp8_;
	GtkTextMark* _tmp9_;
	GtkTextIter _tmp10_ = {0};
	GtkTextIter end = {0};
	GtkTextIter _tmp11_;
	GtkTextIter _tmp12_;
	GtkTextIter _tmp13_;
	GtkTextBuffer* _tmp15_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->current_buffer;
	gtk_text_buffer_begin_user_action (_tmp0_);
	_tmp1_ = self->priv->current_buffer;
	_tmp2_ = self->priv->current_buffer;
	_tmp3_ = gtk_text_buffer_get_insert (_tmp2_);
	gtk_text_buffer_get_iter_at_mark (_tmp1_, &_tmp4_, _tmp3_);
	start = _tmp4_;
	gtk_text_iter_backward_char (&start);
	_tmp5_ = self->priv->current_buffer;
	_tmp6_ = self->priv->previous_selection;
	gtk_text_buffer_insert (_tmp5_, &start, _tmp6_, -1);
	_tmp7_ = self->priv->current_buffer;
	_tmp8_ = self->priv->current_buffer;
	_tmp9_ = gtk_text_buffer_get_insert (_tmp8_);
	gtk_text_buffer_get_iter_at_mark (_tmp7_, &_tmp10_, _tmp9_);
	start = _tmp10_;
	_tmp11_ = start;
	end = _tmp11_;
	gtk_text_iter_forward_char (&end);
	_tmp12_ = end;
	_tmp13_ = start;
	if (_gtk_text_iter_equal (&_tmp12_, &_tmp13_) != TRUE) {
		GtkTextBuffer* _tmp14_;
		_tmp14_ = self->priv->current_buffer;
		gtk_text_buffer_delete (_tmp14_, &start, &end);
	}
	_tmp15_ = self->priv->current_buffer;
	gtk_text_buffer_end_user_action (_tmp15_);
}

static gboolean
scratch_plugins_brackets_completion_on_key_down (ScratchPluginsBracketsCompletion* self,
                                                 GdkEventKey* event)
{
	gboolean _tmp0_ = FALSE;
	GtkTextBuffer* _tmp1_;
	gboolean _tmp2_;
	gboolean _tmp3_;
	gboolean _tmp5_ = FALSE;
	GeeHashMap* _tmp6_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (event != NULL, FALSE);
	if ((event->state & GDK_MOD1_MASK) == GDK_MOD1_MASK) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = (event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK;
	}
	if (_tmp0_) {
		result = FALSE;
		return result;
	}
	_tmp1_ = self->priv->current_buffer;
	_tmp2_ = gtk_text_buffer_get_has_selection (_tmp1_);
	_tmp3_ = _tmp2_;
	if (!_tmp3_) {
		gchar* _tmp4_;
		_tmp4_ = g_strdup ("");
		_g_free0 (self->priv->previous_selection);
		self->priv->previous_selection = _tmp4_;
		result = FALSE;
		return result;
	}
	_tmp6_ = self->priv->keys;
	if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp6_, (gpointer) ((guintptr) event->keyval))) {
		GtkTextBuffer* _tmp7_;
		gboolean _tmp8_;
		gboolean _tmp9_;
		_tmp7_ = self->priv->current_buffer;
		_tmp8_ = gtk_text_buffer_get_has_selection (_tmp7_);
		_tmp9_ = _tmp8_;
		_tmp5_ = _tmp9_;
	} else {
		_tmp5_ = FALSE;
	}
	if (_tmp5_) {
		GtkTextIter start = {0};
		GtkTextIter end = {0};
		GtkTextBuffer* _tmp10_;
		GtkTextIter _tmp11_ = {0};
		GtkTextIter _tmp12_ = {0};
		GtkTextBuffer* _tmp13_;
		GtkTextIter _tmp14_;
		GtkTextIter _tmp15_;
		gchar* _tmp16_;
		_tmp10_ = self->priv->current_buffer;
		gtk_text_buffer_get_selection_bounds (_tmp10_, &_tmp11_, &_tmp12_);
		start = _tmp11_;
		end = _tmp12_;
		_tmp13_ = self->priv->current_buffer;
		_tmp14_ = start;
		_tmp15_ = end;
		_tmp16_ = gtk_text_buffer_get_text (_tmp13_, &_tmp14_, &_tmp15_, FALSE);
		_g_free0 (self->priv->previous_selection);
		self->priv->previous_selection = _tmp16_;
	}
	result = FALSE;
	return result;
}

static void
scratch_plugins_brackets_completion_check_bracket_indent (ScratchPluginsBracketsCompletion* self)
{
	gchar* next_char = NULL;
	gchar* _tmp0_;
	GeeHashMap* _tmp1_;
	GeeCollection* _tmp2_;
	GeeCollection* _tmp3_;
	GeeCollection* _tmp4_;
	const gchar* _tmp5_;
	gboolean _tmp6_;
	g_return_if_fail (self != NULL);
	_tmp0_ = scratch_plugins_brackets_completion_get_next_char (self);
	next_char = _tmp0_;
	_tmp1_ = self->priv->brackets;
	_tmp2_ = gee_abstract_map_get_values ((GeeAbstractMap*) _tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = _tmp3_;
	_tmp5_ = next_char;
	_tmp6_ = gee_collection_contains (_tmp4_, _tmp5_);
	_g_object_unref0 (_tmp4_);
	if (_tmp6_) {
		GtkTextIter start = {0};
		GtkTextIter end = {0};
		GtkTextBuffer* _tmp7_;
		GtkTextIter _tmp8_ = {0};
		GtkTextIter _tmp9_;
		gchar* prev_char = NULL;
		GtkTextBuffer* _tmp10_;
		GtkTextIter _tmp11_;
		GtkTextIter _tmp12_;
		gchar* _tmp13_;
		GeeHashMap* _tmp14_;
		const gchar* _tmp15_;
		gpointer _tmp16_;
		gchar* _tmp17_;
		const gchar* _tmp18_;
		gboolean _tmp19_;
		_tmp7_ = self->priv->current_buffer;
		gtk_text_buffer_get_selection_bounds (_tmp7_, &_tmp8_, NULL);
		start = _tmp8_;
		gtk_text_iter_backward_line (&start);
		gtk_text_iter_forward_to_line_end (&start);
		_tmp9_ = start;
		end = _tmp9_;
		gtk_text_iter_backward_char (&start);
		_tmp10_ = self->priv->current_buffer;
		_tmp11_ = start;
		_tmp12_ = end;
		_tmp13_ = gtk_text_buffer_get_text (_tmp10_, &_tmp11_, &_tmp12_, FALSE);
		prev_char = _tmp13_;
		_tmp14_ = self->priv->brackets;
		_tmp15_ = prev_char;
		_tmp16_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp14_, _tmp15_);
		_tmp17_ = (gchar*) _tmp16_;
		_tmp18_ = next_char;
		_tmp19_ = g_strcmp0 (_tmp17_, _tmp18_) == 0;
		_g_free0 (_tmp17_);
		if (_tmp19_) {
			GtkTextBuffer* _tmp20_;
			GtkTextBuffer* _tmp21_;
			GtkTextIter _tmp22_ = {0};
			GtkTextIter _tmp23_ = {0};
			gchar* current_indent = NULL;
			GtkTextBuffer* _tmp24_;
			GtkTextIter _tmp25_;
			GtkTextIter _tmp26_;
			gchar* _tmp27_;
			gboolean spaces = FALSE;
			ScratchWidgetsSourceView* _tmp28_;
			gboolean _tmp29_;
			gboolean _tmp30_;
			gchar* _tmp31_ = NULL;
			gchar* indent = NULL;
			gchar* _tmp37_;
			GtkTextBuffer* _tmp38_;
			const gchar* _tmp39_;
			GtkTextIter iter = {0};
			GtkTextBuffer* _tmp40_;
			GtkTextBuffer* _tmp41_;
			GtkTextMark* _tmp42_;
			GtkTextIter _tmp43_ = {0};
			GtkTextMark* mark = NULL;
			GtkTextBuffer* _tmp44_;
			GtkTextIter _tmp45_;
			GtkTextMark* _tmp46_;
			GtkTextMark* _tmp47_;
			GtkTextBuffer* _tmp48_;
			const gchar* _tmp49_;
			gchar* _tmp50_;
			gchar* _tmp51_;
			GtkTextBuffer* _tmp52_;
			GtkTextMark* _tmp53_;
			GtkTextIter _tmp54_ = {0};
			GtkTextBuffer* _tmp55_;
			GtkTextIter _tmp56_;
			GtkTextBuffer* _tmp57_;
			GtkTextMark* _tmp58_;
			GtkTextBuffer* _tmp59_;
			_tmp20_ = self->priv->current_buffer;
			gtk_text_buffer_begin_user_action (_tmp20_);
			_tmp21_ = self->priv->current_buffer;
			gtk_text_buffer_get_selection_bounds (_tmp21_, &_tmp22_, &_tmp23_);
			start = _tmp22_;
			end = _tmp23_;
			gtk_text_iter_backward_chars (&start, gtk_text_iter_get_line_offset (&start));
			_tmp24_ = self->priv->current_buffer;
			_tmp25_ = start;
			_tmp26_ = end;
			_tmp27_ = gtk_text_buffer_get_text (_tmp24_, &_tmp25_, &_tmp26_, FALSE);
			current_indent = _tmp27_;
			_tmp28_ = self->priv->current_source_view;
			_tmp29_ = gtk_source_view_get_insert_spaces_instead_of_tabs ((GtkSourceView*) _tmp28_);
			_tmp30_ = _tmp29_;
			spaces = _tmp30_;
			if (spaces) {
				ScratchWidgetsSourceView* _tmp32_;
				guint _tmp33_;
				guint _tmp34_;
				gchar* _tmp35_;
				_tmp32_ = self->priv->current_source_view;
				_tmp33_ = gtk_source_view_get_tab_width ((GtkSourceView*) _tmp32_);
				_tmp34_ = _tmp33_;
				_tmp35_ = g_strnfill ((gsize) _tmp34_, ' ');
				_g_free0 (_tmp31_);
				_tmp31_ = _tmp35_;
			} else {
				gchar* _tmp36_;
				_tmp36_ = g_strdup ("\t");
				_g_free0 (_tmp31_);
				_tmp31_ = _tmp36_;
			}
			_tmp37_ = _tmp31_;
			_tmp31_ = NULL;
			indent = _tmp37_;
			_tmp38_ = self->priv->current_buffer;
			_tmp39_ = indent;
			gtk_text_buffer_insert_at_cursor (_tmp38_, _tmp39_, -1);
			_tmp40_ = self->priv->current_buffer;
			_tmp41_ = self->priv->current_buffer;
			_tmp42_ = gtk_text_buffer_get_insert (_tmp41_);
			gtk_text_buffer_get_iter_at_mark (_tmp40_, &_tmp43_, _tmp42_);
			iter = _tmp43_;
			_tmp44_ = self->priv->current_buffer;
			_tmp45_ = iter;
			_tmp46_ = gtk_text_buffer_create_mark (_tmp44_, NULL, &_tmp45_, TRUE);
			_tmp47_ = _g_object_ref0 (_tmp46_);
			mark = _tmp47_;
			_tmp48_ = self->priv->current_buffer;
			_tmp49_ = current_indent;
			_tmp50_ = g_strconcat ("\n", _tmp49_, NULL);
			_tmp51_ = _tmp50_;
			gtk_text_buffer_insert_at_cursor (_tmp48_, _tmp51_, -1);
			_g_free0 (_tmp51_);
			_tmp52_ = self->priv->current_buffer;
			_tmp53_ = mark;
			gtk_text_buffer_get_iter_at_mark (_tmp52_, &_tmp54_, _tmp53_);
			iter = _tmp54_;
			_tmp55_ = self->priv->current_buffer;
			_tmp56_ = iter;
			gtk_text_buffer_place_cursor (_tmp55_, &_tmp56_);
			_tmp57_ = self->priv->current_buffer;
			_tmp58_ = mark;
			gtk_text_buffer_delete_mark (_tmp57_, _tmp58_);
			_tmp59_ = self->priv->current_buffer;
			gtk_text_buffer_end_user_action (_tmp59_);
			_g_object_unref0 (mark);
			_g_free0 (indent);
			_g_free0 (_tmp31_);
			_g_free0 (current_indent);
		}
		_g_free0 (prev_char);
	}
	_g_free0 (next_char);
}

static GdkEventKey*
_vala_GdkEventKey_copy (GdkEventKey* self)
{
	return g_boxed_copy (gdk_event_get_type (), self);
}

static gpointer
__vala_GdkEventKey_copy0 (gpointer self)
{
	return self ? _vala_GdkEventKey_copy (self) : NULL;
}

static void
_vala_GdkEventKey_free (GdkEventKey* self)
{
	g_boxed_free (gdk_event_get_type (), self);
}

static void
scratch_plugins_brackets_completion_on_event_after (ScratchPluginsBracketsCompletion* self,
                                                    GdkEvent* root_event)
{
	GdkEventKey* event = NULL;
	GdkEventKey* _tmp0_;
	GdkEventKey* _tmp1_;
	GdkEventKey* _tmp2_;
	gboolean _tmp3_ = FALSE;
	ScratchWidgetsSourceView* _tmp4_;
	gboolean _tmp5_;
	gboolean _tmp6_;
	gboolean _tmp10_ = FALSE;
	gboolean _tmp11_ = FALSE;
	GeeHashMap* _tmp12_;
	GdkEventKey* _tmp13_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (root_event != NULL);
	if (root_event->type != GDK_KEY_PRESS) {
		return;
	}
	_tmp0_ = (GdkEventKey *) (root_event);
	_tmp1_ = _tmp0_;
	_tmp2_ = __vala_GdkEventKey_copy0 (_tmp1_);
	event = _tmp2_;
	_tmp4_ = self->priv->current_source_view;
	_tmp5_ = gtk_source_view_get_auto_indent ((GtkSourceView*) _tmp4_);
	_tmp6_ = _tmp5_;
	if (_tmp6_) {
		gboolean _tmp7_ = FALSE;
		GdkEventKey* _tmp8_;
		_tmp8_ = event;
		if (_tmp8_->keyval == GDK_KEY_Return) {
			_tmp7_ = TRUE;
		} else {
			GdkEventKey* _tmp9_;
			_tmp9_ = event;
			_tmp7_ = _tmp9_->keyval == GDK_KEY_KP_Enter;
		}
		_tmp3_ = _tmp7_;
	} else {
		_tmp3_ = FALSE;
	}
	if (_tmp3_) {
		scratch_plugins_brackets_completion_check_bracket_indent (self);
	}
	_tmp12_ = self->priv->keys;
	_tmp13_ = event;
	if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp12_, (gpointer) ((guintptr) _tmp13_->keyval))) {
		GdkEventKey* _tmp14_;
		_tmp14_ = event;
		_tmp11_ = !((_tmp14_->state & GDK_MOD1_MASK) == GDK_MOD1_MASK);
	} else {
		_tmp11_ = FALSE;
	}
	if (_tmp11_) {
		GdkEventKey* _tmp15_;
		_tmp15_ = event;
		_tmp10_ = !((_tmp15_->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK);
	} else {
		_tmp10_ = FALSE;
	}
	if (_tmp10_) {
		gchar* bracket = NULL;
		GeeHashMap* _tmp16_;
		GdkEventKey* _tmp17_;
		gpointer _tmp18_;
		gchar* next_char = NULL;
		gchar* _tmp19_;
		gchar* prev_char = NULL;
		gchar* _tmp20_;
		gboolean _tmp21_ = FALSE;
		const gchar* _tmp22_;
		const gchar* _tmp23_;
		gboolean brackets_match = FALSE;
		gboolean _tmp26_ = FALSE;
		gboolean _tmp27_ = FALSE;
		GeeHashMap* _tmp36_;
		const gchar* _tmp37_;
		gboolean _tmp38_ = FALSE;
		GeeHashMap* _tmp39_;
		const gchar* _tmp40_;
		_tmp16_ = self->priv->keys;
		_tmp17_ = event;
		_tmp18_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp16_, (gpointer) ((guintptr) _tmp17_->keyval));
		bracket = (gchar*) _tmp18_;
		_tmp19_ = scratch_plugins_brackets_completion_get_next_char (self);
		next_char = _tmp19_;
		_tmp20_ = scratch_plugins_brackets_completion_get_previous_char (self);
		prev_char = _tmp20_;
		_tmp22_ = next_char;
		_tmp23_ = bracket;
		if (g_strcmp0 (_tmp22_, _tmp23_) == 0) {
			const gchar* _tmp24_;
			const gchar* _tmp25_;
			_tmp24_ = prev_char;
			_tmp25_ = bracket;
			_tmp21_ = g_strcmp0 (_tmp24_, _tmp25_) == 0;
		} else {
			_tmp21_ = FALSE;
		}
		brackets_match = _tmp21_;
		if (brackets_match) {
			GtkTextBuffer* _tmp28_;
			gboolean _tmp29_;
			gboolean _tmp30_;
			_tmp28_ = self->priv->current_buffer;
			_tmp29_ = gtk_text_buffer_get_has_selection (_tmp28_);
			_tmp30_ = _tmp29_;
			_tmp27_ = !_tmp30_;
		} else {
			_tmp27_ = FALSE;
		}
		if (_tmp27_) {
			GeeHashMap* _tmp31_;
			GeeCollection* _tmp32_;
			GeeCollection* _tmp33_;
			GeeCollection* _tmp34_;
			const gchar* _tmp35_;
			_tmp31_ = self->priv->brackets;
			_tmp32_ = gee_abstract_map_get_values ((GeeAbstractMap*) _tmp31_);
			_tmp33_ = _tmp32_;
			_tmp34_ = _tmp33_;
			_tmp35_ = bracket;
			_tmp26_ = gee_collection_contains (_tmp34_, _tmp35_);
			_g_object_unref0 (_tmp34_);
		} else {
			_tmp26_ = FALSE;
		}
		if (_tmp26_) {
			scratch_plugins_brackets_completion_delete_next_char (self);
			_g_free0 (prev_char);
			_g_free0 (next_char);
			_g_free0 (bracket);
			__vala_GdkEventKey_free0 (event);
			return;
		}
		_tmp36_ = self->priv->brackets;
		_tmp37_ = prev_char;
		if (!gee_abstract_map_has_key ((GeeAbstractMap*) _tmp36_, _tmp37_)) {
			_g_free0 (prev_char);
			_g_free0 (next_char);
			_g_free0 (bracket);
			__vala_GdkEventKey_free0 (event);
			return;
		}
		_tmp39_ = self->priv->brackets;
		_tmp40_ = bracket;
		if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp39_, _tmp40_)) {
			gboolean _tmp41_ = FALSE;
			GtkTextBuffer* _tmp42_;
			gboolean _tmp43_;
			gboolean _tmp44_;
			_tmp42_ = self->priv->current_buffer;
			_tmp43_ = gtk_text_buffer_get_has_selection (_tmp42_);
			_tmp44_ = _tmp43_;
			if (_tmp44_) {
				_tmp41_ = TRUE;
			} else {
				const gchar* _tmp45_;
				_tmp45_ = next_char;
				_tmp41_ = scratch_plugins_brackets_completion_has_valid_next_char (self, _tmp45_);
			}
			_tmp38_ = _tmp41_;
		} else {
			_tmp38_ = FALSE;
		}
		if (_tmp38_) {
			const gchar* _tmp46_;
			_tmp46_ = bracket;
			scratch_plugins_brackets_completion_complete_brackets (self, _tmp46_);
		}
		_g_free0 (prev_char);
		_g_free0 (next_char);
		_g_free0 (bracket);
	}
	__vala_GdkEventKey_free0 (event);
	return;
}

ScratchPluginsBracketsCompletion*
scratch_plugins_brackets_completion_construct (GType object_type)
{
	ScratchPluginsBracketsCompletion * self = NULL;
	self = (ScratchPluginsBracketsCompletion*) g_object_new (object_type, NULL);
	return self;
}

ScratchPluginsBracketsCompletion*
scratch_plugins_brackets_completion_new (void)
{
	return scratch_plugins_brackets_completion_construct (SCRATCH_PLUGINS_TYPE_BRACKETS_COMPLETION);
}

static GObject*
scratch_plugins_brackets_completion_real_get_object (ScratchServicesActivatablePlugin* base)
{
	GObject* result;
	ScratchPluginsBracketsCompletion* self;
	GObject* _tmp0_;
	GObject* _tmp1_;
	self = (ScratchPluginsBracketsCompletion*) base;
	_tmp0_ = self->priv->_object;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

static void
scratch_plugins_brackets_completion_real_set_object (ScratchServicesActivatablePlugin* base,
                                                     GObject* value)
{
	ScratchPluginsBracketsCompletion* self;
	GObject* old_value;
	self = (ScratchPluginsBracketsCompletion*) base;
	old_value = scratch_plugins_brackets_completion_real_get_object (base);
	if (old_value != value) {
		GObject* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_object);
		self->priv->_object = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, scratch_plugins_brackets_completion_properties[SCRATCH_PLUGINS_BRACKETS_COMPLETION_OBJECT_PROPERTY]);
	}
	_g_object_unref0 (old_value);
}

static void
scratch_plugins_brackets_completion_class_init (ScratchPluginsBracketsCompletionClass * klass,
                                                gpointer klass_data)
{
	scratch_plugins_brackets_completion_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ScratchPluginsBracketsCompletion_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_scratch_plugins_brackets_completion_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_scratch_plugins_brackets_completion_set_property;
	G_OBJECT_CLASS (klass)->finalize = scratch_plugins_brackets_completion_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), SCRATCH_PLUGINS_BRACKETS_COMPLETION_OBJECT_PROPERTY, scratch_plugins_brackets_completion_properties[SCRATCH_PLUGINS_BRACKETS_COMPLETION_OBJECT_PROPERTY] = g_param_spec_object ("object", "object", "object", G_TYPE_OBJECT, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
}

static void
scratch_plugins_brackets_completion_scratch_services_activatable_plugin_interface_init (ScratchServicesActivatablePluginIface * iface,
                                                                                        gpointer iface_data)
{
	scratch_plugins_brackets_completion_scratch_services_activatable_plugin_parent_iface = g_type_interface_peek_parent (iface);
	iface->activate = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_brackets_completion_real_activate;
	iface->deactivate = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_brackets_completion_real_deactivate;
	iface->update_state = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_brackets_completion_real_update_state;
	iface->get_object = scratch_plugins_brackets_completion_real_get_object;
	iface->set_object = scratch_plugins_brackets_completion_real_set_object;
}

static void
scratch_plugins_brackets_completion_instance_init (ScratchPluginsBracketsCompletion * self,
                                                   gpointer klass)
{
	gchar* _tmp0_;
	self->priv = scratch_plugins_brackets_completion_get_instance_private (self);
	_tmp0_ = g_strdup ("");
	self->priv->previous_selection = _tmp0_;
}

static void
scratch_plugins_brackets_completion_finalize (GObject * obj)
{
	ScratchPluginsBracketsCompletion * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SCRATCH_PLUGINS_TYPE_BRACKETS_COMPLETION, ScratchPluginsBracketsCompletion);
	_g_object_unref0 (self->priv->_object);
	_g_object_unref0 (self->priv->brackets);
	_g_object_unref0 (self->priv->keys);
	_g_object_unref0 (self->priv->current_buffer);
	_g_object_unref0 (self->priv->plugins);
	_g_object_unref0 (self->priv->current_source_view);
	_g_free0 (self->priv->previous_selection);
	G_OBJECT_CLASS (scratch_plugins_brackets_completion_parent_class)->finalize (obj);
}

GType
scratch_plugins_brackets_completion_get_type (void)
{
	return scratch_plugins_brackets_completion_type_id;
}

GType
scratch_plugins_brackets_completion_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (ScratchPluginsBracketsCompletionClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) scratch_plugins_brackets_completion_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ScratchPluginsBracketsCompletion), 0, (GInstanceInitFunc) scratch_plugins_brackets_completion_instance_init, NULL };
	static const GInterfaceInfo scratch_services_activatable_plugin_info = { (GInterfaceInitFunc) scratch_plugins_brackets_completion_scratch_services_activatable_plugin_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	scratch_plugins_brackets_completion_type_id = g_type_module_register_type (module, peas_extension_base_get_type (), "ScratchPluginsBracketsCompletion", &g_define_type_info, G_TYPE_FLAG_NONE);
	g_type_module_add_interface (module, scratch_plugins_brackets_completion_type_id, SCRATCH_SERVICES_TYPE_ACTIVATABLE_PLUGIN, &scratch_services_activatable_plugin_info);
	ScratchPluginsBracketsCompletion_private_offset = sizeof (ScratchPluginsBracketsCompletionPrivate);
	return scratch_plugins_brackets_completion_type_id;
}

static void
_vala_scratch_plugins_brackets_completion_get_property (GObject * object,
                                                        guint property_id,
                                                        GValue * value,
                                                        GParamSpec * pspec)
{
	ScratchPluginsBracketsCompletion * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_PLUGINS_TYPE_BRACKETS_COMPLETION, ScratchPluginsBracketsCompletion);
	switch (property_id) {
		case SCRATCH_PLUGINS_BRACKETS_COMPLETION_OBJECT_PROPERTY:
		g_value_take_object (value, scratch_services_activatable_plugin_get_object ((ScratchServicesActivatablePlugin*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_scratch_plugins_brackets_completion_set_property (GObject * object,
                                                        guint property_id,
                                                        const GValue * value,
                                                        GParamSpec * pspec)
{
	ScratchPluginsBracketsCompletion * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_PLUGINS_TYPE_BRACKETS_COMPLETION, ScratchPluginsBracketsCompletion);
	switch (property_id) {
		case SCRATCH_PLUGINS_BRACKETS_COMPLETION_OBJECT_PROPERTY:
		scratch_services_activatable_plugin_set_object ((ScratchServicesActivatablePlugin*) self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

void
peas_register_types (GTypeModule* module)
{
	PeasObjectModule* objmodule = NULL;
	PeasObjectModule* _tmp0_;
	g_return_if_fail (module != NULL);
	scratch_plugins_brackets_completion_register_type (module);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (module, peas_object_module_get_type ()) ? ((PeasObjectModule*) module) : NULL);
	objmodule = _tmp0_;
	peas_object_module_register_extension_type (objmodule, SCRATCH_SERVICES_TYPE_ACTIVATABLE_PLUGIN, SCRATCH_PLUGINS_TYPE_BRACKETS_COMPLETION);
	_g_object_unref0 (objmodule);
}

