Class Buffer

java.lang.Object
All Implemented Interfaces:
Proxy

@Generated("io.github.jwharm.JavaGI") public class Buffer extends TextBuffer
Subclass of TextBuffer.

A GtkSourceBuffer object is the model for View widgets. It extends the TextBuffer class by adding features useful to display and edit source code such as syntax highlighting and bracket matching.

To create a GtkSourceBuffer use Buffer() or withLanguage(org.gnome.gtksourceview.Language). The second form is just a convenience function which allows you to initially set a Language. You can also directly create a View and get its Buffer with TextView.getBuffer().

The highlighting is enabled by default, but you can disable it with setHighlightSyntax(boolean).

Context Classes:
It is possible to retrieve some information from the syntax highlighting engine. The default context classes that are applied to regions of a GtkSourceBuffer:

  • **comment**: the region delimits a comment;
  • **no-spell-check**: the region should not be spell checked;
  • **path**: the region delimits a path to a file;
  • **string**: the region delimits a string.

Custom language definition files can create their own context classes, since the functions like iterHasContextClass(org.gnome.gtk.TextIter, java.lang.String) take a string parameter as the context class.

GtkSourceBuffer provides an API to access the context classes: iterHasContextClass(org.gnome.gtk.TextIter, java.lang.String), getContextClassesAtIter(org.gnome.gtk.TextIter), iterForwardToContextClassToggle(org.gnome.gtk.TextIter, java.lang.String) and iterBackwardToContextClassToggle(org.gnome.gtk.TextIter, java.lang.String).

And the GtkSource.Buffer::highlight-updated signal permits to be notified when a context class region changes.

Each context class has also an associated TextTag with the name gtksourceview:context-classes:<name>. For example to retrieve the TextTag for the string context class, one can write:

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");
 

The tag must be used for read-only purposes.

Accessing a context class via the associated TextTag is less convenient than the GtkSourceBuffer API, because:

  • The tag doesn't always exist, you need to listen to the Gtk.TextTagTable::tag-added and Gtk.TextTagTable::tag-removed signals.
  • Instead of the GtkSource.Buffer::highlight-updated signal, you can listen to the Gtk.TextBuffer::apply-tag and Gtk.TextBuffer::remove-tag signals.

A possible use-case for accessing a context class via the associated TextTag is to read the region but without adding a hard dependency on the GtkSourceView library (for example for a spell-checking library that wants to read the no-spell-check region).