cssection: Return locations, not numbers

Now that we have GtkCssLocation, actually use it.
This commit is contained in:
Benjamin Otte 2019-04-10 05:18:14 +02:00
parent 2fb202187c
commit a8f712b09a
10 changed files with 77 additions and 115 deletions

View File

@ -13,17 +13,20 @@ show_parsing_error (GtkCssProvider *provider,
const GError *error,
GtkTextBuffer *buffer)
{
const GtkCssLocation *start_location, *end_location;
GtkTextIter start, end;
const char *tag_name;
start_location = gtk_css_section_get_start_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&start,
gtk_css_section_get_start_line (section),
gtk_css_section_get_start_position (section));
start_location->lines,
start_location->line_bytes);
end_location = gtk_css_section_get_end_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&end,
gtk_css_section_get_end_line (section),
gtk_css_section_get_end_position (section));
end_location->lines,
end_location->line_bytes);
if (error->domain == GTK_CSS_PARSER_WARNING)
tag_name = "warning";

View File

@ -13,17 +13,21 @@ show_parsing_error (GtkCssProvider *provider,
const GError *error,
GtkTextBuffer *buffer)
{
const GtkCssLocation *start_location, *end_location;
GtkTextIter start, end;
const char *tag_name;
start_location = gtk_css_section_get_start_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&start,
gtk_css_section_get_start_line (section),
gtk_css_section_get_start_position (section));
start_location->lines,
start_location->line_bytes);
end_location = gtk_css_section_get_end_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&end,
gtk_css_section_get_end_line (section),
gtk_css_section_get_end_position (section));
end_location->lines,
end_location->line_bytes);
if (error->domain == GTK_CSS_PARSER_WARNING)
tag_name = "warning";

View File

@ -12,17 +12,21 @@ show_parsing_error (GtkCssProvider *provider,
const GError *error,
GtkTextBuffer *buffer)
{
const GtkCssLocation *start_location, *end_location;
GtkTextIter start, end;
const char *tag_name;
start_location = gtk_css_section_get_start_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&start,
gtk_css_section_get_start_line (section),
gtk_css_section_get_start_position (section));
start_location->lines,
start_location->line_bytes);
end_location = gtk_css_section_get_end_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&end,
gtk_css_section_get_end_line (section),
gtk_css_section_get_end_position (section));
end_location->lines,
end_location->line_bytes);
if (error->domain == GTK_CSS_PARSER_WARNING)
tag_name = "warning";

View File

@ -11,17 +11,20 @@ show_parsing_error (GtkCssProvider *provider,
const GError *error,
GtkTextBuffer *buffer)
{
const GtkCssLocation *start_location, *end_location;
GtkTextIter start, end;
const char *tag_name;
start_location = gtk_css_section_get_start_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&start,
gtk_css_section_get_start_line (section),
gtk_css_section_get_start_position (section));
start_location->lines,
start_location->line_bytes);
end_location = gtk_css_section_get_end_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&end,
gtk_css_section_get_end_line (section),
gtk_css_section_get_end_position (section));
end_location->lines,
end_location->line_bytes);
if (error->domain == GTK_CSS_PARSER_WARNING)
tag_name = "warning";

View File

@ -5092,12 +5092,10 @@ GtkCssParserWarning
<SUBSECTION>
GtkCssLocation
GtkCssSection
gtk_css_section_get_end_line
gtk_css_section_get_end_position
gtk_css_section_get_file
gtk_css_section_get_parent
gtk_css_section_get_start_line
gtk_css_section_get_start_position
gtk_css_section_get_start_location
gtk_css_section_get_end_location
gtk_css_section_ref
gtk_css_section_unref
<SUBSECTION Standard>

View File

@ -150,97 +150,40 @@ gtk_css_section_get_file (const GtkCssSection *section)
}
/**
* gtk_css_section_get_start_line:
* gtk_css_section_get_start_location:
* @section: the section
*
* Returns the line in the CSS document where this section starts.
* The line number is 0-indexed, so the first line of the document
* will return 0.
* Returns the location in the CSS document where this section starts.
*
* Returns: the line number
* Returns: (tranfer none) (not nullable): The start location of
* this section
**/
guint
gtk_css_section_get_start_line (const GtkCssSection *section)
const GtkCssLocation *
gtk_css_section_get_start_location (const GtkCssSection *section)
{
gtk_internal_return_val_if_fail (section != NULL, 0);
gtk_internal_return_val_if_fail (section != NULL, NULL);
return section->start_location.lines;
return &section->start_location;
}
/**
* gtk_css_section_get_start_position:
* gtk_css_section_get_end_location:
* @section: the section
*
* Returns the offset in bytes from the start of the current line
* returned via gtk_css_section_get_start_line().
* Returns the location in the CSS document where this section ends.
*
* Returns: the offset in bytes from the start of the line.
* Returns: (tranfer none) (not nullable): The end location of
* this section
**/
guint
gtk_css_section_get_start_position (const GtkCssSection *section)
const GtkCssLocation *
gtk_css_section_get_end_location (const GtkCssSection *section)
{
gtk_internal_return_val_if_fail (section != NULL, 0);
gtk_internal_return_val_if_fail (section != NULL, NULL);
return section->start_location.line_chars;
}
if (section->parser)
gtk_css_parser_get_location (section->parser, (GtkCssLocation *) &section->end_location);
/**
* gtk_css_section_get_end_line:
* @section: the section
*
* Returns the line in the CSS document where this section end.
* The line number is 0-indexed, so the first line of the document
* will return 0.
* This value may change in future invocations of this function if
* @section is not yet parsed completely. This will for example
* happen in the GtkCssProvider::parsing-error signal.
* The end position and line may be identical to the start
* position and line for sections which failed to parse anything
* successfully.
*
* Returns: the line number
**/
guint
gtk_css_section_get_end_line (const GtkCssSection *section)
{
GtkCssLocation location;
gtk_internal_return_val_if_fail (section != NULL, 0);
if (!section->parser)
return section->end_location.lines;
gtk_css_parser_get_location (section->parser, &location);
return location.lines;
}
/**
* gtk_css_section_get_end_position:
* @section: the section
*
* Returns the offset in bytes from the start of the current line
* returned via gtk_css_section_get_end_line().
* This value may change in future invocations of this function if
* @section is not yet parsed completely. This will for example
* happen in the GtkCssProvider::parsing-error signal.
* The end position and line may be identical to the start
* position and line for sections which failed to parse anything
* successfully.
*
* Returns: the offset in bytes from the start of the line.
**/
guint
gtk_css_section_get_end_position (const GtkCssSection *section)
{
GtkCssLocation location;
gtk_internal_return_val_if_fail (section != NULL, 0);
if (!section->parser)
return section->end_location.line_chars;
gtk_css_parser_get_location (section->parser, &location);
return location.line_chars;
return &section->end_location;
}
void
@ -268,9 +211,9 @@ _gtk_css_section_print (const GtkCssSection *section,
g_string_append (string, "<data>");
}
g_string_append_printf (string, ":%u:%u",
gtk_css_section_get_end_line (section) + 1,
gtk_css_section_get_end_position (section));
g_string_append_printf (string, ":%zu:%zu",
section->end_location.lines + 1,
section->end_location.line_chars + 1);
}
char *

View File

@ -19,7 +19,7 @@
#define __GTK_CSS_SECTION_H__
#include <gio/gio.h>
#include <gdk/gdk.h>
#include <gtk/css/gtkcss.h>
G_BEGIN_DECLS
@ -47,13 +47,11 @@ GtkCssSection * gtk_css_section_get_parent (const GtkCssSection *se
GDK_AVAILABLE_IN_ALL
GFile * gtk_css_section_get_file (const GtkCssSection *section);
GDK_AVAILABLE_IN_ALL
guint gtk_css_section_get_start_line (const GtkCssSection *section);
const GtkCssLocation *
gtk_css_section_get_start_location (const GtkCssSection *section);
GDK_AVAILABLE_IN_ALL
guint gtk_css_section_get_start_position (const GtkCssSection *section);
GDK_AVAILABLE_IN_ALL
guint gtk_css_section_get_end_line (const GtkCssSection *section);
GDK_AVAILABLE_IN_ALL
guint gtk_css_section_get_end_position (const GtkCssSection *section);
const GtkCssLocation *
gtk_css_section_get_end_location (const GtkCssSection *section);
G_END_DECLS

View File

@ -292,19 +292,22 @@ show_parsing_error (GtkCssProvider *provider,
{
const char *tag_name;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER (ce->priv->text);
const GtkCssLocation *start, *end;
CssError *css_error;
css_error = g_new (CssError, 1);
css_error->error = g_error_copy (error);
start = gtk_css_section_get_start_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&css_error->start,
gtk_css_section_get_start_line (section),
gtk_css_section_get_start_position (section));
start->lines,
start->line_bytes);
end = gtk_css_section_get_end_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&css_error->end,
gtk_css_section_get_end_line (section),
gtk_css_section_get_end_position (section));
end->lines,
end->line_bytes);
if (error->domain == GTK_CSS_PARSER_WARNING)
tag_name = "warning";

View File

@ -49,17 +49,20 @@ show_parsing_error (GtkCssProvider *provider,
const GError *error,
GtkTextBuffer *buffer)
{
const GtkCssLocation *start_location, *end_location;
GtkTextIter start, end;
const char *tag_name;
start_location = gtk_css_section_get_start_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&start,
gtk_css_section_get_start_line (section),
gtk_css_section_get_start_position (section));
start_location->lines,
start_location->line_bytes);
end_location = gtk_css_section_get_end_location (section);
gtk_text_buffer_get_iter_at_line_index (buffer,
&end,
gtk_css_section_get_end_line (section),
gtk_css_section_get_end_position (section));
end_location->lines,
end_location->line_bytes);
if (error->domain == GTK_CSS_PARSER_WARNING)
tag_name = "warning";

View File

@ -142,12 +142,15 @@ parsing_error_cb (GtkCssProvider *provider,
const GError *error,
GString *errors)
{
const GtkCssLocation *end_location;
char *basename;
end_location = gtk_css_section_get_end_location (section);
basename = g_file_get_basename (gtk_css_section_get_file (section));
g_string_append_printf (errors,
"%s:%u: error: ",
basename, gtk_css_section_get_end_line (section) + 1);
"%s:%zu: error: ",
basename, end_location->lines + 1);
g_free (basename);
if (error->domain == GTK_CSS_PARSER_ERROR)