GNOME Bugzilla – Bug 762533
[PATCH] Use style id for tag name when create a new GtkTextTag
Last modified: 2016-10-22 18:43:26 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.
Created attachment 321952 [details] [review] GtkTextTag name on tag creation Properly git patch added.
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?
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.
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.
(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.
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.
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.
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.
(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.
Opened bug #773351.
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 :)