<commit_before>/*
 * This file is part of GtkSourceView
 *
 * Copyright (C) 2007 - <NAME> <<EMAIL>>
 * Copyright (C) 2013 - <NAME> <<EMAIL>>
 *
 * GtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>

#define TEST_TYPE_PROPOSAL (test_proposal_get_type())
G_DECLARE_FINAL_TYPE (TestProposal, test_proposal, TEST, PROPOSAL, GObject)

struct _TestProposal
{
	GObject parent_instance;
	char *label;
	char *text;
	char *markup;
	char *info;
	char *icon_name;
	GIcon *gicon;
	GtkIconPaintable *icon;
};

G_DEFINE_TYPE_WITH_CODE (TestProposal, test_proposal, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GTK_SOURCE_TYPE_COMPLETION_PROPOSAL, NULL))

static void
test_proposal_dispose (GObject *object)
{
	TestProposal *p = TEST_PROPOSAL (object);
	g_clear_pointer (&p->label, g_free);
	g_clear_pointer (&p->text, g_free);
	g_clear_pointer (&p->markup, g_free);
	g_clear_pointer (&p->info, g_free);
	g_clear_pointer (&p->icon_name, g_free);
	g_clear_object (&p->gicon);
	g_clear_object (&p->icon);
	G_OBJECT_CLASS (test_proposal_parent_class)->dispose (object);
}

static void
test_proposal_class_init (TestProposalClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = test_proposal_dispose;
}

static void
test_proposal_init (TestProposal *self)
{
}

static TestProposal *
test_proposal_new (void)
{
	return g_object_new (TEST_TYPE_PROPOSAL, NULL);
}

typedef struct _TestProvider TestProvider;
typedef struct _TestProviderClass TestProviderClass;

static GtkSourceCompletionWords *word_provider;
static TestProvider *fixed_provider;
static TestProvider *random_provider;
static GMainLoop *main_loop;

struct _TestProvider
{
	GObject parent;

	GList *proposals;
	gint priority;
	gchar *title;

	GtkIconPaintable *provider_icon;

	GtkIconPaintable *item_icon;
	GIcon *item_gicon;

	/* If it's a random provider, a subset of 'proposals' are choosen on
	 * each populate. Otherwise, all the proposals are shown. */
	guint is_random : 1;
};

struct _TestProviderClass
{
	GObjectClass parent_class;
};

static void test_provider_iface_init (GtkSourceCompletionProviderInterface *iface);
GType test_provider_get_type (void);

G_DEFINE_TYPE_WITH_CODE (TestProvider,
			 test_provider,
			 G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_SOURCE_TYPE_COMPLETION_PROVIDER,
				 		test_provider_iface_init))

static gchar *
test_provider_get_title (GtkSourceCompletionProvider *provider)
{
	return g_strdup (((TestProvider *)provider)->title);
}

static gint
test_provider_get_priority (GtkSourceCompletionProvider *provider,
                            GtkSourceCompletionContext  *context)
{
	return ((TestProvider *)provider)->priority;
}

static GList *
select_random_proposals (GList *all_proposals)
{
	GList *selection = NULL;
	GList *prop;

	for (prop = all_proposals; prop != NULL; prop = g_list_next (prop))
	{
		if (g_random_boolean ())
		{
			selection = g_list_prepend (selection, prop->data);
		}
	}

	return selection;
}

static void
test_provider_populate_async (GtkSourceCompletionProvider *completion_provider,
                              GtkSourceCompletionContext  *context,
                              GCancellable                *cancellable,
                              GAsyncReadyCallback          callback,
                              gpointer                     user_data)
{
	TestProvider *provider = (TestProvider *)completion_provider;
	GListStore *results;
	GList *proposals;
	GTask *task;

	if (provider->is_random)
	{
		proposals = select_random_proposals (provider->proposals);
	}
	else
	{
		proposals = provider->proposals;
	}

	task = g_task_new (completion_provider, cancellable, callback, user_data);

	results = g_list_store_new (GTK_SOURCE_TYPE_COMPLETION_PROPOSAL);

	for (const GList *iter = proposals; iter; iter = iter->next)
	{
		g_list_store_append (results, iter->data);
	}

	g_task_return_pointer (task, results, g_object_unref);

	if (provider->is_random)
	{
		g_list_free (proposals);
	}
}

static GListModel *
test_provider_populate_finish (GtkSourceCompletionProvider  *provider,
                               GAsyncResult                 *result,
                               GError                      **error)
{
	return g_task_propagate_pointer (G_TASK (result), error);
}

static void
test_provider_display (GtkSourceCompletionProvider *provider,
                       GtkSourceCompletionContext  *context,
                       GtkSourceCompletionProposal *proposal,
                       GtkSourceCompletionCell     *cell)
{
	if (TEST_IS_PROPOSAL (proposal))
	{
		TestProposal *p = TEST_PROPOSAL (proposal);
		GtkSourceCompletionColumn column;

		column = gtk_source_completion_cell_get_column (cell);

		if (column == GTK_SOURCE_COMPLETION_COLUMN_TYPED_TEXT)
		{
			if (p->markup)
				gtk_source_completion_cell_set_markup (cell, p->markup);
			else if (p->label)
				gtk_source_completion_cell_set_text (cell, p->label);
			else
				gtk_source_completion_cell_set_text (cell, p->text);
		}
		else if (column == GTK_SOURCE_COMPLETION_COLUMN_ICON)
		{
			if (p->icon_name)
				gtk_source_completion_cell_set_icon_name (cell, p->icon_name);
			else if (p->gicon)
				gtk_source_completion_cell_set_gicon (cell, p->gicon);
			else
				gtk_source_completion_cell_set_icon_name (cell, NULL);
		}
	}
}

static void
test_provider_activate (GtkSourceCompletionProvider *provider,
                        GtkSourceCompletionContext  *context,<commit_msg>tests: add comment area for completion<commit_after>/*
 * This file is part of GtkSourceView
 *
 * Copyright (C) 2007 - <NAME> <<EMAIL>>
 * Copyright (C) 2013 - <NAME> <<EMAIL>>
 *
 * GtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>

#define TEST_TYPE_PROPOSAL (test_proposal_get_type())
G_DECLARE_FINAL_TYPE (TestProposal, test_proposal, TEST, PROPOSAL, GObject)

struct _TestProposal
{
	GObject parent_instance;
	char *label;
	char *text;
	char *markup;
	char *info;
	char *icon_name;
	GIcon *gicon;
	GtkIconPaintable *icon;
};

G_DEFINE_TYPE_WITH_CODE (TestProposal, test_proposal, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GTK_SOURCE_TYPE_COMPLETION_PROPOSAL, NULL))

static void
test_proposal_dispose (GObject *object)
{
	TestProposal *p = TEST_PROPOSAL (object);
	g_clear_pointer (&p->label, g_free);
	g_clear_pointer (&p->text, g_free);
	g_clear_pointer (&p->markup, g_free);
	g_clear_pointer (&p->info, g_free);
	g_clear_pointer (&p->icon_name, g_free);
	g_clear_object (&p->gicon);
	g_clear_object (&p->icon);
	G_OBJECT_CLASS (test_proposal_parent_class)->dispose (object);
}

static void
test_proposal_class_init (TestProposalClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = test_proposal_dispose;
}

static void
test_proposal_init (TestProposal *self)
{
}

static TestProposal *
test_proposal_new (void)
{
	return g_object_new (TEST_TYPE_PROPOSAL, NULL);
}

typedef struct _TestProvider TestProvider;
typedef struct _TestProviderClass TestProviderClass;

static GtkSourceCompletionWords *word_provider;
static TestProvider *fixed_provider;
static TestProvider *random_provider;
static GMainLoop *main_loop;

struct _TestProvider
{
	GObject parent;

	GList *proposals;
	gint priority;
	gchar *title;

	GtkIconPaintable *provider_icon;

	GtkIconPaintable *item_icon;
	GIcon *item_gicon;

	/* If it's a random provider, a subset of 'proposals' are choosen on
	 * each populate. Otherwise, all the proposals are shown. */
	guint is_random : 1;
};

struct _TestProviderClass
{
	GObjectClass parent_class;
};

static void test_provider_iface_init (GtkSourceCompletionProviderInterface *iface);
GType test_provider_get_type (void);

G_DEFINE_TYPE_WITH_CODE (TestProvider,
			 test_provider,
			 G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_SOURCE_TYPE_COMPLETION_PROVIDER,
				 		test_provider_iface_init))

static gchar *
test_provider_get_title (GtkSourceCompletionProvider *provider)
{
	return g_strdup (((TestProvider *)provider)->title);
}

static gint
test_provider_get_priority (GtkSourceCompletionProvider *provider,
                            GtkSourceCompletionContext  *context)
{
	return ((TestProvider *)provider)->priority;
}

static GList *
select_random_proposals (GList *all_proposals)
{
	GList *selection = NULL;
	GList *prop;

	for (prop = all_proposals; prop != NULL; prop = g_list_next (prop))
	{
		if (g_random_boolean ())
		{
			selection = g_list_prepend (selection, prop->data);
		}
	}

	return selection;
}

static void
test_provider_populate_async (GtkSourceCompletionProvider *completion_provider,
                              GtkSourceCompletionContext  *context,
                              GCancellable                *cancellable,
                              GAsyncReadyCallback          callback,
                              gpointer                     user_data)
{
	TestProvider *provider = (TestProvider *)completion_provider;
	GListStore *results;
	GList *proposals;
	GTask *task;

	if (provider->is_random)
	{
		proposals = select_random_proposals (provider->proposals);
	}
	else
	{
		proposals = provider->proposals;
	}

	task = g_task_new (completion_provider, cancellable, callback, user_data);

	results = g_list_store_new (GTK_SOURCE_TYPE_COMPLETION_PROPOSAL);

	for (const GList *iter = proposals; iter; iter = iter->next)
	{
		g_list_store_append (results, iter->data);
	}

	g_task_return_pointer (task, results, g_object_unref);

	if (provider->is_random)
	{
		g_list_free (proposals);
	}
}

static GListModel *
test_provider_populate_finish (GtkSourceCompletionProvider  *provider,
                               GAsyncResult                 *result,
                               GError                      **error)
{
	return g_task_propagate_pointer (G_TASK (result), error);
}

static void
test_provider_display (GtkSourceCompletionProvider *provider,
                       GtkSourceCompletionContext  *context,
                       GtkSourceCompletionProposal *proposal,
                       GtkSourceCompletionCell     *cell)
{
	if (TEST_IS_PROPOSAL (proposal))
	{
		TestProposal *p = TEST_PROPOSAL (proposal);
		GtkSourceCompletionColumn column;

		column = gtk_source_completion_cell_get_column (cell);

		if (column == GTK_SOURCE_COMPLETION_COLUMN_TYPED_TEXT)
		{
			if (p->markup)
				gtk_source_completion_cell_set_markup (cell, p->markup);
			else if (p->label)
				gtk_source_completion_cell_set_text (cell, p->label);
			else
				gtk_source_completion_cell_set_text (cell, p->text);
		}
		else if (column == GTK_SOURCE_COMPLETION_COLUMN_COMMENT)
		{
			if (p->info)
			{
				gchar *str = g_strstrip (g_strdup (p->info));
				gtk_source_completion_cell_set_text (cell, str);
				g_free (str);
			}
			else
			{
				gtk_source_completion_cell_set_text (cell, NULL);
			}
		}
		else if (column == GTK_SOURCE_COMPLETION_COLUMN_ICON)
		{
			if (p->icon_name)
				gtk_source_completion_cell_set_icon_name (cell, p->icon_name);
			else if (p->gicon)
				gtk_source_completion_cell_set_gicon (cell, p->gicon);
			else
				gtk_source_completion_cell_set_icon_name (cell, NULL);
		}
	}
}

static void
test_provider_activate (GtkSourceCompletionProvider *provider,
                        GtkSourceCompletionContext  *context,
{'fraction_non_alphanumeric': 0.08626858551323845, 'fraction_numerical': 0.0017492247753836368, 'mean_word_length': 3.8941634241245136, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6239463', 'n_tokens_mistral': 4336, 'n_tokens_neox': 4084, 'n_words': 970}