Search code examples
cgtk3gtksourceview

GtkSourceView-CRITICAL **: Highlighting a single line took too much time, syntax highlighting will be disabled


I'm experiencing this issue related to GtkSourceView4 and I want to understand how to avoid it

(litos:4353): GtkSourceView-CRITICAL **: 12:57:09.105: Highlighting a single line took too much time, syntax highlighting will be disabled

It happens when I try to insert tags such as <h1>, <h2> but not with other tags like <p>, <code>, etc. It seems a bug that has already been filed https://bugzilla.redhat.com/show_bug.cgi?id=1882163 without solution.

Since I'm using other programs using GtkSourceView4 and highlighting works correctly it could be due to an improper usage of the API in particular the function gtk_source_buffer_set_highlight_syntax. I'm using Gtksourceview4 and GTK3. This is part of the code interested in highlighting the text

void open_file(struct lit *litos, gboolean template)
{
    gboolean read_file_status;
    GError *error;
    char* contents;

    gint page = gtk_notebook_get_current_page(litos->notebook);

    char *filename = litos->filename[page];

    read_file_status = g_file_get_contents(filename, &contents, NULL, &error);

    if (read_file_status == FALSE)
    {
        g_error("error opening file: %s\n",error && error->message ? error->message : "No Detail");
        return;
    }

    GtkTextBuffer *current_buffer = get_current_buffer(litos);

    gtk_text_buffer_set_text(GTK_TEXT_BUFFER(current_buffer), contents, -1);

    highlight_buffer(litos);

    if (template)
    {
        litos->filename[page] = NULL;
        filename = "Unsaved";
    }

    gtk_notebook_set_tab_label_text(
        litos->notebook,
        gtk_notebook_get_nth_page(
        litos->notebook,
        page
        ),
        filename
    );

    gtk_notebook_set_menu_label_text(
        litos->notebook,
        gtk_notebook_get_nth_page(
        litos->notebook,
        page
        ),
        filename
    );
}

void highlight_buffer(struct lit *litos) /* Apply different font styles depending on file extension .html .c, etc */
{
    gint page = gtk_notebook_get_current_page(litos->notebook);

    GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default();

    GtkSourceLanguage *lang = gtk_source_language_manager_guess_language(lm, litos->filename[page], NULL);
        
    gtk_source_buffer_set_language (litos->buffer, lang);

    if (lang != NULL)
        gtk_source_buffer_set_highlight_syntax (litos->buffer, TRUE);
}

you can find the complete program here https://github.com/gioretikto/litos


Solution

  • It's a bug in GtkSouceview 4; I noticed the same critical alert in gedit and mousepad as well. After downgrading to version GtkSourceview3 the issue was fixed.