<commit_before>/*
 * This file is part of GtkSourceView
 *
 * Copyright 2010 - <NAME>
 *
 * 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 "config.h"

#include "gtksourcegutterrendererlines-private.h"
#include "gtksourcegutterlines.h"
#include "gtksourceutils-private.h"
#include "gtksourceview.h"

struct _GtkSourceGutterRendererLines
{
	GtkSourceGutterRendererText parent_instance;
	gint num_line_digits;
	gint prev_line_num;
	guint cursor_visible : 1;
};

G_DEFINE_TYPE (GtkSourceGutterRendererLines, _gtk_source_gutter_renderer_lines, GTK_SOURCE_TYPE_GUTTER_RENDERER_TEXT)

static inline gint
count_num_digits (gint num_lines)
{
	if (num_lines < 100)
	{
		return 2;
	}
	else if (num_lines < 1000)
	{
		return 3;
	}
	else if (num_lines < 10000)
	{
		return 4;
	}
	else if (num_lines < 100000)
	{
		return 5;
	}
	else if (num_lines < 1000000)
	{
		return 6;
	}
	else
	{
		return 10;
	}
}

static void
recalculate_size (GtkSourceGutterRendererLines *renderer)
{
	GtkSourceBuffer *buffer;
	gint num_lines = 1;
	gint num_digits;

	buffer = gtk_source_gutter_renderer_get_buffer (GTK_SOURCE_GUTTER_RENDERER (renderer));

	if (buffer != NULL)
	{
		num_lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (buffer));
	}

	num_digits = count_num_digits (num_lines);

	if (num_digits < 2)
	{
		num_digits = 2;
	}

	if (num_digits != renderer->num_line_digits)
	{
		renderer->num_line_digits = num_digits;
		gtk_widget_queue_resize (GTK_WIDGET (renderer));
	}
}

static void
on_buffer_changed (GtkSourceBuffer              *buffer,
                   GtkSourceGutterRendererLines *renderer)
{
	recalculate_size (renderer);
}

static void
on_buffer_cursor_moved (GtkSourceBuffer              *buffer,
                        GtkSourceGutterRendererLines *renderer)
{
	if (renderer->cursor_visible)
	{
		/* Redraw if the current-line needs updating */
		gtk_widget_queue_draw (GTK_WIDGET (renderer));
	}
}

static void
gutter_renderer_change_buffer (GtkSourceGutterRenderer *renderer,
                               GtkSourceBuffer         *old_buffer)
{
	GtkSourceGutterRendererLines *lines = GTK_SOURCE_GUTTER_RENDERER_LINES (renderer);
	GtkSourceBuffer *buffer;

	if (old_buffer != NULL)
	{
		g_signal_handlers_disconnect_by_func (old_buffer,
						      on_buffer_changed,
						      lines);
		g_signal_handlers_disconnect_by_func (old_buffer,
						      on_buffer_cursor_moved,
						      lines);
	}

	buffer = gtk_source_gutter_renderer_get_buffer (renderer);

	lines->prev_line_num = 0;

	if (buffer != NULL)
	{
		g_signal_connect_object (buffer,
					 "changed",
					 G_CALLBACK (on_buffer_changed),
					 lines,
					 0);

		g_signal_connect_object (buffer,
					 "cursor-moved",
					 G_CALLBACK (on_buffer_cursor_moved),
					 lines,
					 0);

		recalculate_size (lines);
	}

	GTK_SOURCE_GUTTER_RENDERER_CLASS (_gtk_source_gutter_renderer_lines_parent_class)->change_buffer (renderer, old_buffer);
}

static void
gtk_source_gutter_renderer_lines_css_changed (GtkWidget         *widget,
                                              GtkCssStyleChange *change)
{
	GtkSourceGutterRendererLines *renderer = GTK_SOURCE_GUTTER_RENDERER_LINES (widget);

	GTK_WIDGET_CLASS (_gtk_source_gutter_renderer_lines_parent_class)->css_changed (widget, change);

	/* Force to recalculate the size. */
	renderer->num_line_digits = -1;
	recalculate_size (renderer);
}

static void
on_view_notify_cursor_visible (GtkTextView                  *view,
                               GParamSpec                   *pspec,
                               GtkSourceGutterRendererLines *renderer)
{
	renderer->cursor_visible = gtk_text_view_get_cursor_visible (view);
}

static void
gutter_renderer_change_view (GtkSourceGutterRenderer *renderer,
			     GtkSourceView             *old_view)
{
	GtkSourceView *new_view;

	if (old_view != NULL)
	{
		g_signal_handlers_disconnect_by_func (old_view,
						      on_view_notify_cursor_visible,
						      renderer);
	}

	new_view = gtk_source_gutter_renderer_get_view (renderer);

	if (new_view != NULL)
	{
		g_signal_connect_object (new_view,
					 "notify::cursor-visible",
					 G_CALLBACK (on_view_notify_cursor_visible),
					 renderer,
					 0);

		GTK_SOURCE_GUTTER_RENDERER_LINES (renderer)->cursor_visible = gtk_text_view_get_cursor_visible (GTK_TEXT_VIEW (new_view));
	}

	GTK_SOURCE_GUTTER_RENDERER_CLASS (_gtk_source_gutter_renderer_lines_parent_class)->change_view (renderer, old_view);
}

static void
extend_selection_to_line (GtkSourceGutterRendererLines *renderer,
                          GtkTextIter                  *line_start)<commit_msg>gutterrendererlines: check for background highlight<commit_after>/*
 * This file is part of GtkSourceView
 *
 * Copyright 2010 - <NAME>
 *
 * 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 "config.h"

#include "gtksourcegutterrendererlines-private.h"
#include "gtksourcegutterlines.h"
#include "gtksourceutils-private.h"
#include "gtksourceview.h"

struct _GtkSourceGutterRendererLines
{
	GtkSourceGutterRendererText parent_instance;
	gint num_line_digits;
	gint prev_line_num;
	guint highlight_current_line : 1;
	guint cursor_visible : 1;
};

G_DEFINE_TYPE (GtkSourceGutterRendererLines, _gtk_source_gutter_renderer_lines, GTK_SOURCE_TYPE_GUTTER_RENDERER_TEXT)

static inline gint
count_num_digits (gint num_lines)
{
	if (num_lines < 100)
	{
		return 2;
	}
	else if (num_lines < 1000)
	{
		return 3;
	}
	else if (num_lines < 10000)
	{
		return 4;
	}
	else if (num_lines < 100000)
	{
		return 5;
	}
	else if (num_lines < 1000000)
	{
		return 6;
	}
	else
	{
		return 10;
	}
}

static void
recalculate_size (GtkSourceGutterRendererLines *renderer)
{
	GtkSourceBuffer *buffer;
	gint num_lines = 1;
	gint num_digits;

	buffer = gtk_source_gutter_renderer_get_buffer (GTK_SOURCE_GUTTER_RENDERER (renderer));

	if (buffer != NULL)
	{
		num_lines = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (buffer));
	}

	num_digits = count_num_digits (num_lines);

	if (num_digits < 2)
	{
		num_digits = 2;
	}

	if (num_digits != renderer->num_line_digits)
	{
		renderer->num_line_digits = num_digits;
		gtk_widget_queue_resize (GTK_WIDGET (renderer));
	}
}

static void
on_buffer_changed (GtkSourceBuffer              *buffer,
                   GtkSourceGutterRendererLines *renderer)
{
	recalculate_size (renderer);
}

static void
on_buffer_cursor_moved (GtkSourceBuffer              *buffer,
                        GtkSourceGutterRendererLines *renderer)
{
	if (renderer->cursor_visible || renderer->highlight_current_line)
	{
		/* Redraw if the current-line needs updating */
		gtk_widget_queue_draw (GTK_WIDGET (renderer));
	}
}

static void
gutter_renderer_change_buffer (GtkSourceGutterRenderer *renderer,
                               GtkSourceBuffer         *old_buffer)
{
	GtkSourceGutterRendererLines *lines = GTK_SOURCE_GUTTER_RENDERER_LINES (renderer);
	GtkSourceBuffer *buffer;

	if (old_buffer != NULL)
	{
		g_signal_handlers_disconnect_by_func (old_buffer,
						      on_buffer_changed,
						      lines);
		g_signal_handlers_disconnect_by_func (old_buffer,
						      on_buffer_cursor_moved,
						      lines);
	}

	buffer = gtk_source_gutter_renderer_get_buffer (renderer);

	lines->prev_line_num = 0;

	if (buffer != NULL)
	{
		g_signal_connect_object (buffer,
					 "changed",
					 G_CALLBACK (on_buffer_changed),
					 lines,
					 0);

		g_signal_connect_object (buffer,
					 "cursor-moved",
					 G_CALLBACK (on_buffer_cursor_moved),
					 lines,
					 0);

		recalculate_size (lines);
	}

	GTK_SOURCE_GUTTER_RENDERER_CLASS (_gtk_source_gutter_renderer_lines_parent_class)->change_buffer (renderer, old_buffer);
}

static void
gtk_source_gutter_renderer_lines_css_changed (GtkWidget         *widget,
                                              GtkCssStyleChange *change)
{
	GtkSourceGutterRendererLines *renderer = GTK_SOURCE_GUTTER_RENDERER_LINES (widget);

	GTK_WIDGET_CLASS (_gtk_source_gutter_renderer_lines_parent_class)->css_changed (widget, change);

	/* Force to recalculate the size. */
	renderer->num_line_digits = -1;
	recalculate_size (renderer);
}

static void
on_view_notify (GtkSourceView                *view,
                GParamSpec                   *pspec,
                GtkSourceGutterRendererLines *renderer)
{
	renderer->cursor_visible = gtk_text_view_get_cursor_visible (GTK_TEXT_VIEW (view));
	renderer->highlight_current_line = gtk_source_view_get_highlight_current_line (view);
}

static void
gutter_renderer_change_view (GtkSourceGutterRenderer *renderer,
			     GtkSourceView             *old_view)
{
	GtkSourceView *new_view;

	if (old_view != NULL)
	{
		g_signal_handlers_disconnect_by_func (old_view, on_view_notify, renderer);
	}

	new_view = gtk_source_gutter_renderer_get_view (renderer);

	if (new_view != NULL)
	{
		g_signal_connect_object (new_view,
		                         "notify::cursor-visible",
		                         G_CALLBACK (on_view_notify),
		                         renderer,
		                         0);
		g_signal_connect_object (new_view,
		                         "notify::highlight-current-line",
		                         G_CALLBACK (on_view_notify),
		                         renderer,
		                         0);

		on_view_notify (new_view, NULL, GTK_SOURCE_GUTTER_RENDERER_LINES (renderer));
	}

	GTK_SOURCE_GUTTER_RENDERER_CLASS (_gtk_source_gutter_renderer_lines_parent_class)->change_view (renderer, old_view);
}

static void
extend_selection_to_line (GtkSourceGutterRendererLines *renderer,
                          GtkTextIter                  *line_start)
{'fraction_non_alphanumeric': 0.0770012941393973, 'fraction_numerical': 0.00887409872434831, 'mean_word_length': 3.465125877011969, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3007585', 'n_tokens_mistral': 3939, 'n_tokens_neox': 3618, 'n_words': 762}