After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 762533 - [PATCH] Use style id for tag name when create a new GtkTextTag
[PATCH] Use style id for tag name when create a new GtkTextTag
Status: RESOLVED WONTFIX
Product: gtksourceview
Classification: Platform
Component: General
git master
Other All
: Normal enhancement
: ---
Assigned To: GTK Sourceview maintainers
GTK Sourceview maintainers
Depends on:
Blocks:
 
 
Reported: 2016-02-23 13:00 UTC by Everaldo Canuto
Modified: 2016-10-22 18:43 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GtkTextTag name on tag creation (622 bytes, patch)
2016-02-23 13:00 UTC, Everaldo Canuto
none Details | Review
GtkTextTag name on tag creation (1.09 KB, patch)
2016-02-23 13:23 UTC, Everaldo Canuto
needs-work Details | Review

Description Everaldo Canuto 2016-02-23 13:00:40 UTC
Created attachment 321949 [details] [review]
GtkTextTag name on tag creation

Currently we cant use gtk_text_tag_table_lookup to get an specific GtkTextTag from GtkTextTagTable. Also, when we are listening to "tag-added" or "tag-changed" signals on GtkTextTagTable, it is impossible to identify GtkTexTag because all tag names are empty.

Looking at GtkSourceBuffer documentation, we have this sample code:

    GtkTextTagTable *tag_table;
    GtkTextTag *tag;

    tag_table = gtk_text_buffer_get_tag_table (buffer);
    tag = gtk_text_tag_table_lookup (tag_table, "gtksourceview:context-classes:string");

But it also don't works because lookup needs a tag name and GtkSourceContextEngine (create_tag) pass NULL for second parameter when calling gtk_text_buffer_create_tag.

It can be easily fixed since we have style id available on create_tag, it don't affects every other features on GtkSourceView (it is a safe patch) and fixes not only documentation sample but also every call to table_lookup.
Comment 1 Everaldo Canuto 2016-02-23 13:23:19 UTC
Created attachment 321952 [details] [review]
GtkTextTag name on tag creation

Properly git patch added.
Comment 2 Paolo Borelli 2016-02-23 13:34:25 UTC
Historycally we used anonymous tags because of two reasons:

 - avoid possible name clashes
 - save some memory, since a complex syntax may need to add a lot of tags

Also, the tags used by the highlight engine are an implementation detail that I would like to expose as little as possible, so I would prefer to keep using anonymous tags for the highlighting, so that library users cannot  query them and make wrong assumptions.


However I agree that style-classes tags are a special and documented case and those should be created with the documented name. Could you try to change your patch to only add the name for those tags?
Comment 3 Sébastien Wilmet 2016-10-21 15:50:12 UTC
GtkTextTag names are set for context classes (no-spell-check etc) because gspell needed to read the tag without adding a dependency on GtkSourceView. If some code already depends on GtkSourceView, it is more convenient to call the GtkSourceView API, not by looking at the tag table for tag names.

Can you explain in details your use-case?

In create_tag(), style_id refers to a GtkSourceStyle ID. The same GtkSourceStyle can be used for different things (maybe the ContextEngine creates several tags for the same GtkSourceStyle ID?). In a *.lang file, different <style> elements can map-to the same style ID.
Comment 4 Everaldo Canuto 2016-10-22 00:45:16 UTC
I would like to apply different styles for markdown headers like I did in this video:

  https://www.youtube.com/watch?v=PNLE04IbCC0

But my code works only with this patch because I use tag-changed from GtkTextTable and check for tag name to apply a different left margin.

I am also trying to use highlight-updated from GtkSourceBuffer but even after read all GtkSourceView documentation I am not able to figure out how to get style id from GtkTextIter.
Comment 5 Everaldo Canuto 2016-10-22 01:40:36 UTC
(In reply to Sébastien Wilmet from comment #3)
> GtkTextTag names are set for context classes (no-spell-check etc) because
> gspell needed to read the tag without adding a dependency on GtkSourceView.
> If some code already depends on GtkSourceView, it is more convenient to call
> the GtkSourceView API, not by looking at the tag table for tag names.

I cannot look at tag table by tag names because without this patch tags are always create with NULL name.
Comment 6 Georges Basile Stavracas Neto 2016-10-22 01:44:10 UTC
Since I'm interested in using Everaldo's code to have a pretty markdown editor in GNOME To Do, I'm adding myself to the CC list.
Comment 7 Sébastien Wilmet 2016-10-22 12:37:11 UTC
So when gtk_source_style_apply() is called for certain GtkSourceStyles, you need to set additional properties to the GtkTextTag.

A signal could be added somewhere. Applications would just need to connect to the signal to do more stuff with the GtkTextTag.

But I don't know what is the best place to put this signal. Maybe GtkSourceStyleSchemeManager, so that only one signal connection is needed (with parameters for the StyleScheme and the style_id). There could be a gtk_source_style_scheme_manager_apply_style() function, sending the signal with the object handler calling gtk_source_style_apply().

Because what you want to achieve is specific to your application, maybe other text editors don't want that behavior for Markdown. And setting the left-margin property is not specific to a certain style scheme, I suppose you want to apply it for all style schemes. So it is not appropriate to add a left-margin property to GtkSourceStyle and add the corresponding support for the *.xml file.

So a signal would be the best approach in my opinion.
Comment 8 Sébastien Wilmet 2016-10-22 12:46:35 UTC
Maybe another place for the signal is GtkSourceBuffer, because the buffer has access only to the StyleScheme, not the StyleSchemeManager. And there can be several StyleSchemeManagers, created with gtk_source_style_scheme_manager_new() instead of getting the default instance with gtk_source_style_scheme_manager_get_default().

If the signal is added in StyleScheme, it means that the application needs to reconnect the signal each time the GtkSourceBuffer:style-scheme property changes.
Comment 9 Sébastien Wilmet 2016-10-22 13:01:45 UTC
(In reply to Everaldo Canuto from comment #4)
> I would like to apply different styles for markdown headers like I did in
> this video:
> 
>   https://www.youtube.com/watch?v=PNLE04IbCC0

Can you attach a screenshot to this bugzilla entry, with a text description. I think in the future the link might be broken and it would be more difficult to know what was the use-case.
Comment 10 Sébastien Wilmet 2016-10-22 13:06:50 UTC
Opened bug #773351.
Comment 11 Sébastien Wilmet 2016-10-22 18:43:26 UTC
After some discussion on IRC, there is another hack: adding context classes for headings in markdown.lang (modified and shipped by the application). But it's not a clean solution. The above patch is not a clean solution either.

I've expanded my thoughts about the signal:
https://bugzilla.gnome.org/show_bug.cgi?id=773351#c1

Please continue the discussion on bugzilla, to keep an history and take the time for some reflection/reading before saying something :)