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

#include "detect-indent.h"
#include "codecore.h"
#include <glib-object.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <libpeas.h>

#define SCRATCH_PLUGINS_DETECT_INDENT_MAX_LINES 500
#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_DETECT_INDENT_0_PROPERTY,
	SCRATCH_PLUGINS_DETECT_INDENT_OBJECT_PROPERTY,
	SCRATCH_PLUGINS_DETECT_INDENT_NUM_PROPERTIES
};
static GParamSpec* scratch_plugins_detect_indent_properties[SCRATCH_PLUGINS_DETECT_INDENT_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))

struct _ScratchPluginsDetectIndentPrivate {
	ScratchServicesInterface* plugins;
	GObject* _object;
};

static gint ScratchPluginsDetectIndent_private_offset;
static gpointer scratch_plugins_detect_indent_parent_class = NULL;
static ScratchServicesActivatablePluginIface * scratch_plugins_detect_indent_scratch_services_activatable_plugin_parent_iface = NULL;
static GType scratch_plugins_detect_indent_type_id = 0;

static void scratch_plugins_detect_indent_real_update_state (ScratchServicesActivatablePlugin* base);
static void scratch_plugins_detect_indent_real_activate (ScratchServicesActivatablePlugin* base);
static void __lambda4_ (ScratchPluginsDetectIndent* self,
                 ScratchServicesDocument* d);
static void ___lambda4__scratch_services_interface_hook_document (ScratchServicesInterface* _sender,
                                                           ScratchServicesDocument* doc,
                                                           gpointer self);
static void scratch_plugins_detect_indent_real_deactivate (ScratchServicesActivatablePlugin* base);
static void scratch_plugins_detect_indent_finalize (GObject * obj);
static void _vala_scratch_plugins_detect_indent_get_property (GObject * object,
                                                       guint property_id,
                                                       GValue * value,
                                                       GParamSpec * pspec);
static void _vala_scratch_plugins_detect_indent_set_property (GObject * object,
                                                       guint property_id,
                                                       const GValue * value,
                                                       GParamSpec * pspec);

static inline gpointer
scratch_plugins_detect_indent_get_instance_private (ScratchPluginsDetectIndent* self)
{
	return G_STRUCT_MEMBER_P (self, ScratchPluginsDetectIndent_private_offset);
}

static void
scratch_plugins_detect_indent_real_update_state (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsDetectIndent * self;
	self = (ScratchPluginsDetectIndent*) base;
}

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

static gboolean
string_valid_char (const gchar* self,
                   gint index)
{
	guint8 c = 0U;
	guint8 _tmp0_;
	gboolean _tmp1_ = FALSE;
	gboolean _tmp2_ = FALSE;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = ((guint8*) self)[index];
	c = _tmp0_;
	if (((gint) c) == 0x00) {
		_tmp2_ = TRUE;
	} else {
		gboolean _tmp3_ = FALSE;
		if (((gint) c) >= 0x80) {
			_tmp3_ = ((gint) c) < 0xc2;
		} else {
			_tmp3_ = FALSE;
		}
		_tmp2_ = _tmp3_;
	}
	if (_tmp2_) {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = ((gint) c) >= 0xf5;
	}
	if (_tmp1_) {
		result = FALSE;
		return result;
	} else {
		result = TRUE;
		return result;
	}
}

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 gchar
string_get (const gchar* self,
            glong index)
{
	gchar _tmp0_;
	gchar result;
	g_return_val_if_fail (self != NULL, '\0');
	_tmp0_ = ((gchar*) self)[index];
	result = _tmp0_;
	return result;
}

static void
__lambda4_ (ScratchPluginsDetectIndent* self,
            ScratchServicesDocument* d)
{
	ScratchWidgetsSourceView* view = NULL;
	ScratchWidgetsSourceView* _tmp0_;
	ScratchWidgetsSourceView* _tmp1_;
	ScratchWidgetsSourceView* _tmp2_;
	ScratchWidgetsSourceView* _tmp3_;
	GtkSourceBuffer* source_buffer = NULL;
	ScratchWidgetsSourceView* _tmp4_;
	GtkTextBuffer* _tmp5_;
	GtkTextBuffer* _tmp6_;
	GtkSourceBuffer* _tmp7_;
	GtkTextIter it = {0};
	GtkSourceBuffer* _tmp8_;
	GtkTextIter _tmp9_ = {0};
	gint tabs_found = 0;
	gint spaces_found = 0;
	gint lines_processed = 0;
	gfloat sr = 0.0F;
	gfloat tr = 0.0F;
	g_return_if_fail (d != NULL);
	_tmp0_ = scratch_services_document_get_source_view (d);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	view = _tmp2_;
	_tmp3_ = view;
	if (!gtk_text_view_get_editable ((GtkTextView*) _tmp3_)) {
		_g_object_unref0 (view);
		return;
	}
	_tmp4_ = view;
	_tmp5_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp6_, gtk_source_buffer_get_type (), GtkSourceBuffer));
	source_buffer = _tmp7_;
	_tmp8_ = source_buffer;
	gtk_text_buffer_get_iter_at_line ((GtkTextBuffer*) _tmp8_, &_tmp9_, 0);
	it = _tmp9_;
	tabs_found = 0;
	spaces_found = 0;
	lines_processed = 0;
	while (TRUE) {
		gboolean _tmp10_ = FALSE;
		if (!(lines_processed < SCRATCH_PLUGINS_DETECT_INDENT_MAX_LINES)) {
			break;
		}
		if (!gtk_text_iter_ends_line (&it)) {
			GtkSourceBuffer* _tmp11_;
			GtkTextIter _tmp12_;
			_tmp11_ = source_buffer;
			_tmp12_ = it;
			_tmp10_ = !gtk_source_buffer_iter_has_context_class (_tmp11_, &_tmp12_, "comment");
		} else {
			_tmp10_ = FALSE;
		}
		if (_tmp10_) {
			GtkTextIter line_end = {0};
			GtkTextIter _tmp13_;
			gchar* text = NULL;
			GtkTextIter _tmp14_;
			gchar* _tmp15_;
			gboolean empty = FALSE;
			_tmp13_ = it;
			line_end = _tmp13_;
			gtk_text_iter_forward_to_line_end (&line_end);
			_tmp14_ = line_end;
			_tmp15_ = gtk_text_iter_get_text (&it, &_tmp14_);
			text = _tmp15_;
			empty = TRUE;
			{
				gint i = 0;
				i = 0;
				{
					gboolean _tmp16_ = FALSE;
					_tmp16_ = TRUE;
					while (TRUE) {
						gboolean _tmp18_ = FALSE;
						const gchar* _tmp19_;
						gint _tmp20_;
						gint _tmp21_;
						gboolean _tmp22_ = FALSE;
						const gchar* _tmp23_;
						if (!_tmp16_) {
							gint _tmp17_;
							_tmp17_ = i;
							i = _tmp17_ + 1;
						}
						_tmp16_ = FALSE;
						_tmp19_ = text;
						_tmp20_ = strlen (_tmp19_);
						_tmp21_ = _tmp20_;
						if (i < _tmp21_) {
							_tmp18_ = empty;
						} else {
							_tmp18_ = FALSE;
						}
						if (!_tmp18_) {
							break;
						}
						_tmp23_ = text;
						if (string_valid_char (_tmp23_, i)) {
							const gchar* _tmp24_;
							_tmp24_ = text;
							_tmp22_ = !g_unichar_isspace (string_get_char (_tmp24_, (glong) i));
						} else {
							_tmp22_ = FALSE;
						}
						if (_tmp22_) {
							empty = FALSE;
						}
					}
				}
			}
			if (!empty) {
				const gchar* _tmp25_;
				_tmp25_ = text;
				if (string_get (_tmp25_, (glong) 0) == '\t') {
					tabs_found += 1;
				} else {
					gboolean _tmp26_ = FALSE;
					gboolean _tmp27_ = FALSE;
					const gchar* _tmp28_;
					gint _tmp29_;
					gint _tmp30_;
					_tmp28_ = text;
					_tmp29_ = strlen (_tmp28_);
					_tmp30_ = _tmp29_;
					if (_tmp30_ > 1) {
						const gchar* _tmp31_;
						_tmp31_ = text;
						_tmp27_ = string_get (_tmp31_, (glong) 0) == ' ';
					} else {
						_tmp27_ = FALSE;
					}
					if (_tmp27_) {
						const gchar* _tmp32_;
						_tmp32_ = text;
						_tmp26_ = string_get (_tmp32_, (glong) 1) == ' ';
					} else {
						_tmp26_ = FALSE;
					}
					if (_tmp26_) {
						spaces_found += 1;
					}
				}
				lines_processed += 1;
			}
			_g_free0 (text);
		}
		if (!gtk_text_iter_forward_line (&it)) {
			break;
		}
	}
	sr = ((gfloat) spaces_found) / lines_processed;
	tr = ((gfloat) tabs_found) / lines_processed;
	if (fabsf (tr - sr) > 0.1f) {
		ScratchWidgetsSourceView* _tmp33_;
		_tmp33_ = view;
		gtk_source_view_set_insert_spaces_instead_of_tabs ((GtkSourceView*) _tmp33_, sr > tr);
	}
	_g_object_unref0 (source_buffer);
	_g_object_unref0 (view);
}

static void
___lambda4__scratch_services_interface_hook_document (ScratchServicesInterface* _sender,
                                                      ScratchServicesDocument* doc,
                                                      gpointer self)
{
	__lambda4_ ((ScratchPluginsDetectIndent*) self, doc);
}

static void
scratch_plugins_detect_indent_real_activate (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsDetectIndent * self;
	GObject* _tmp0_;
	GObject* _tmp1_;
	ScratchServicesInterface* _tmp2_;
	self = (ScratchPluginsDetectIndent*) base;
	_tmp0_ = scratch_services_activatable_plugin_get_object ((ScratchServicesActivatablePlugin*) self);
	_tmp1_ = _tmp0_;
	_g_object_unref0 (self->priv->plugins);
	self->priv->plugins = G_TYPE_CHECK_INSTANCE_CAST (_tmp1_, SCRATCH_SERVICES_TYPE_INTERFACE, ScratchServicesInterface);
	_tmp2_ = self->priv->plugins;
	g_signal_connect_object (_tmp2_, "hook-document", (GCallback) ___lambda4__scratch_services_interface_hook_document, self, 0);
}

static void
scratch_plugins_detect_indent_real_deactivate (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsDetectIndent * self;
	self = (ScratchPluginsDetectIndent*) base;
}

ScratchPluginsDetectIndent*
scratch_plugins_detect_indent_construct (GType object_type)
{
	ScratchPluginsDetectIndent * self = NULL;
	self = (ScratchPluginsDetectIndent*) g_object_new (object_type, NULL);
	return self;
}

ScratchPluginsDetectIndent*
scratch_plugins_detect_indent_new (void)
{
	return scratch_plugins_detect_indent_construct (SCRATCH_PLUGINS_TYPE_DETECT_INDENT);
}

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

static void
scratch_plugins_detect_indent_real_set_object (ScratchServicesActivatablePlugin* base,
                                               GObject* value)
{
	ScratchPluginsDetectIndent* self;
	GObject* old_value;
	self = (ScratchPluginsDetectIndent*) base;
	old_value = scratch_plugins_detect_indent_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_detect_indent_properties[SCRATCH_PLUGINS_DETECT_INDENT_OBJECT_PROPERTY]);
	}
	_g_object_unref0 (old_value);
}

static void
scratch_plugins_detect_indent_class_init (ScratchPluginsDetectIndentClass * klass,
                                          gpointer klass_data)
{
	scratch_plugins_detect_indent_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ScratchPluginsDetectIndent_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_scratch_plugins_detect_indent_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_scratch_plugins_detect_indent_set_property;
	G_OBJECT_CLASS (klass)->finalize = scratch_plugins_detect_indent_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), SCRATCH_PLUGINS_DETECT_INDENT_OBJECT_PROPERTY, scratch_plugins_detect_indent_properties[SCRATCH_PLUGINS_DETECT_INDENT_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_detect_indent_scratch_services_activatable_plugin_interface_init (ScratchServicesActivatablePluginIface * iface,
                                                                                  gpointer iface_data)
{
	scratch_plugins_detect_indent_scratch_services_activatable_plugin_parent_iface = g_type_interface_peek_parent (iface);
	iface->update_state = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_detect_indent_real_update_state;
	iface->activate = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_detect_indent_real_activate;
	iface->deactivate = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_detect_indent_real_deactivate;
	iface->get_object = scratch_plugins_detect_indent_real_get_object;
	iface->set_object = scratch_plugins_detect_indent_real_set_object;
}

static void
scratch_plugins_detect_indent_instance_init (ScratchPluginsDetectIndent * self,
                                             gpointer klass)
{
	self->priv = scratch_plugins_detect_indent_get_instance_private (self);
}

static void
scratch_plugins_detect_indent_finalize (GObject * obj)
{
	ScratchPluginsDetectIndent * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SCRATCH_PLUGINS_TYPE_DETECT_INDENT, ScratchPluginsDetectIndent);
	_g_object_unref0 (self->priv->plugins);
	_g_object_unref0 (self->priv->_object);
	G_OBJECT_CLASS (scratch_plugins_detect_indent_parent_class)->finalize (obj);
}

GType
scratch_plugins_detect_indent_get_type (void)
{
	return scratch_plugins_detect_indent_type_id;
}

GType
scratch_plugins_detect_indent_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (ScratchPluginsDetectIndentClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) scratch_plugins_detect_indent_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ScratchPluginsDetectIndent), 0, (GInstanceInitFunc) scratch_plugins_detect_indent_instance_init, NULL };
	static const GInterfaceInfo scratch_services_activatable_plugin_info = { (GInterfaceInitFunc) scratch_plugins_detect_indent_scratch_services_activatable_plugin_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	scratch_plugins_detect_indent_type_id = g_type_module_register_type (module, peas_extension_base_get_type (), "ScratchPluginsDetectIndent", &g_define_type_info, G_TYPE_FLAG_NONE);
	g_type_module_add_interface (module, scratch_plugins_detect_indent_type_id, SCRATCH_SERVICES_TYPE_ACTIVATABLE_PLUGIN, &scratch_services_activatable_plugin_info);
	ScratchPluginsDetectIndent_private_offset = sizeof (ScratchPluginsDetectIndentPrivate);
	return scratch_plugins_detect_indent_type_id;
}

static void
_vala_scratch_plugins_detect_indent_get_property (GObject * object,
                                                  guint property_id,
                                                  GValue * value,
                                                  GParamSpec * pspec)
{
	ScratchPluginsDetectIndent * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_PLUGINS_TYPE_DETECT_INDENT, ScratchPluginsDetectIndent);
	switch (property_id) {
		case SCRATCH_PLUGINS_DETECT_INDENT_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_detect_indent_set_property (GObject * object,
                                                  guint property_id,
                                                  const GValue * value,
                                                  GParamSpec * pspec)
{
	ScratchPluginsDetectIndent * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_PLUGINS_TYPE_DETECT_INDENT, ScratchPluginsDetectIndent);
	switch (property_id) {
		case SCRATCH_PLUGINS_DETECT_INDENT_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_detect_indent_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_DETECT_INDENT);
	_g_object_unref0 (objmodule);
}

