Class SearchContext
- All Implemented Interfaces:
Proxy
A GtkSourceSearchContext is used for the search and replace in a
Buffer. The search settings are represented by a
SearchSettings object. There can be a many-to-many relationship
between buffers and search settings, with the search contexts in-between: a
search settings object can be shared between several search contexts; and a
buffer can contain several search contexts at the same time.
The total number of search occurrences can be retrieved with
getOccurrencesCount(). To know the position of a
certain match, use getOccurrencePosition(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter).
The buffer is scanned asynchronously, so it doesn't block the user interface. For each search, the buffer is scanned at most once. After that, navigating through the occurrences doesn't require to re-scan the buffer entirely.
To search forward, use forward(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.javagi.base.Out<java.lang.Boolean>) or
forwardAsync(org.gnome.gtk.TextIter, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback) for the asynchronous version.
The backward search is done similarly. To replace a search match, or all
matches, use replace(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, java.lang.String, int) and
replaceAll(java.lang.String, int).
The search occurrences are highlighted by default. To disable it, use
setHighlight(boolean). You can enable the search
highlighting for several GtkSourceSearchContexts attached to the
same buffer. Moreover, each of those GtkSourceSearchContexts can
have a different text style associated. Use
setMatchStyle(org.gnome.gtksourceview.Style) to specify the Style
to apply on search matches.
Note that the SearchContext:highlight and
SearchContext:match-style properties are in the
GtkSourceSearchContext class, not SearchSettings. Appearance
settings should be tied to one, and only one buffer, as different buffers can
have different style scheme associated (a SearchSettings object
can be bound indirectly to several buffers).
The concept of "current match" doesn't exist yet. A way to highlight differently the current match is to select it.
A search occurrence's position doesn't depend on the cursor position or other parameters. Take for instance the buffer "aaaa" with the search text "aa". The two occurrences are at positions [0:2] and [2:4]. If you begin to search at position 1, you will get the occurrence [2:4], not [1:3]. This is a prerequisite for regular expression searches. The pattern ".*" matches the entire line. If the cursor is at the middle of the line, you don't want the rest of the line as the occurrence, you want an entire line. (As a side note, regular expression searches can also match multiple lines.)
In the GtkSourceView source code, there is an example of how to use the search and replace API: see the tests/test-search.c file. It is a mini application for the search and replace, with a basic user interface.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSearchContext.Builder<B extends SearchContext.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static classNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new SearchContext.SearchContext(MemorySegment address) Create a SearchContext proxy instance for the provided memory address.SearchContext(Buffer buffer, @Nullable SearchSettings settings) Creates a new search context, associated withbuffer,and customized withsettings. -
Method Summary
Modifier and TypeMethodDescriptionprotected SearchContextasParent()Returns this instance as if it were its parent type.booleanbackward(TextIter iter, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Synchronous backward search.voidbackwardAsync(TextIter iter, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) booleanbackwardFinish(AsyncResult result, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Finishes a backward search started withbackwardAsync(org.gnome.gtk.TextIter, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback).static SearchContext.Builder<? extends SearchContext.Builder> builder()ASearchContext.Builderobject constructs aSearchContextwith the specified properties.booleanforward(TextIter iter, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Synchronous forward search.voidforwardAsync(TextIter iter, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) booleanforwardFinish(AsyncResult result, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Finishes a forward search started withforwardAsync(org.gnome.gtk.TextIter, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback).booleanintgetOccurrencePosition(TextIter matchStart, TextIter matchEnd) Gets the position of a search occurrence.intGets the total number of search occurrences.@Nullable GErrorRegular expression patterns must follow certain rules.static @Nullable TypegetType()Get the GType of the SearchContext classbooleanReplaces a search match by another text.intreplaceAll(String replace, int replaceLength) Replaces all search matches by another text.voidsetHighlight(boolean highlight) Enables or disables the search occurrences highlighting.voidsetMatchStyle(@Nullable Style matchStyle) Set the style to apply on search matches.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newv, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
SearchContext
Create a SearchContext proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
SearchContext
Creates a new search context, associated withbuffer,and customized withsettings.If
settingsisnull, a newSearchSettingsobject will be created, that you can retrieve withgetSettings().- Parameters:
buffer- aGtkSourceBuffer.settings- aGtkSourceSearchSettings, ornull.
-
SearchContext
public SearchContext()Creates a new SearchContext.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName(). This will call the native function pointer of this virtual method in the typeclass of the parent type. -
backward
public boolean backward(TextIter iter, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Synchronous backward search.It is recommended to use the asynchronous functions instead, to not block the user interface. However, if you are sure that the
bufferis small, this function is more convenient to use.If the
SearchSettings:wrap-aroundproperty isfalse, this function doesn't try to wrap around.The
hasWrappedAroundout parameter is set independently of whether a match is found. So if this function returnsfalse,hasWrappedAroundwill have the same value as theSearchSettings:wrap-aroundproperty.- Parameters:
iter- start of search.matchStart- return location for start of match, ornull.matchEnd- return location for end of match, ornull.hasWrappedAround- return location to know whether the search has wrapped around, ornull.- Returns:
- whether a match was found.
-
backwardAsync
public void backwardAsync(TextIter iter, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) The asynchronous version ofbackward(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.javagi.base.Out<java.lang.Boolean>).See the
AsyncResultdocumentation to know how to use this function.If the operation is cancelled, the
callbackwill only be called ifcancellablewas notnull. The method takes ownership ofcancellable,so you can unref it after calling this function.- Parameters:
iter- start of search.cancellable- aGCancellable, ornull.callback- aGAsyncReadyCallbackto call when the operation is finished.
-
backwardFinish
public boolean backwardFinish(AsyncResult result, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) throws GErrorException Finishes a backward search started withbackwardAsync(org.gnome.gtk.TextIter, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback).See the documentation of
backward(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.javagi.base.Out<java.lang.Boolean>)for more details.- Parameters:
result- aGAsyncResult.matchStart- return location for start of match, ornull.matchEnd- return location for end of match, ornull.hasWrappedAround- return location to know whether the search has wrapped around, ornull.- Returns:
- whether a match was found.
- Throws:
GErrorException- seeGError
-
forward
public boolean forward(TextIter iter, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Synchronous forward search.It is recommended to use the asynchronous functions instead, to not block the user interface. However, if you are sure that the
bufferis small, this function is more convenient to use.If the
SearchSettings:wrap-aroundproperty isfalse, this function doesn't try to wrap around.The
hasWrappedAroundout parameter is set independently of whether a match is found. So if this function returnsfalse,hasWrappedAroundwill have the same value as theSearchSettings:wrap-aroundproperty.- Parameters:
iter- start of search.matchStart- return location for start of match, ornull.matchEnd- return location for end of match, ornull.hasWrappedAround- return location to know whether the search has wrapped around, ornull.- Returns:
- whether a match was found.
-
forwardAsync
public void forwardAsync(TextIter iter, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) The asynchronous version offorward(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.javagi.base.Out<java.lang.Boolean>).See the
AsyncResultdocumentation to know how to use this function.If the operation is cancelled, the
callbackwill only be called ifcancellablewas notnull. The method takes ownership ofcancellable,so you can unref it after calling this function.- Parameters:
iter- start of search.cancellable- aGCancellable, ornull.callback- aGAsyncReadyCallbackto call when the operation is finished.
-
forwardFinish
public boolean forwardFinish(AsyncResult result, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) throws GErrorException Finishes a forward search started withforwardAsync(org.gnome.gtk.TextIter, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback).See the documentation of
forward(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.javagi.base.Out<java.lang.Boolean>)for more details.- Parameters:
result- aGAsyncResult.matchStart- return location for start of match, ornull.matchEnd- return location for end of match, ornull.hasWrappedAround- return location to know whether the search has wrapped around, ornull.- Returns:
- whether a match was found.
- Throws:
GErrorException- seeGError
-
getBuffer
-
getHighlight
public boolean getHighlight() -
getMatchStyle
-
getOccurrencePosition
Gets the position of a search occurrence.If the buffer is not already fully scanned, the position may be unknown, and -1 is returned. If 0 is returned, it means that this part of the buffer has already been scanned, and that
matchStartandmatchEnddon't delimit an occurrence.- Parameters:
matchStart- the start of the occurrence.matchEnd- the end of the occurrence.- Returns:
- the position of the search occurrence. The first occurrence has the
position 1 (not 0). Returns 0 if
matchStartandmatchEnddon't delimit an occurrence. Returns -1 if the position is not yet known.
-
getOccurrencesCount
public int getOccurrencesCount()Gets the total number of search occurrences.If the buffer is not already fully scanned, the total number of occurrences is unknown, and -1 is returned.
- Returns:
- the total number of search occurrences, or -1 if unknown.
-
getRegexError
Regular expression patterns must follow certain rules. IfSearchSettings:search-textbreaks a rule, the error can be retrieved with this function.The error domain is
GLib.RegexError.Free the return value with
GError.free().- Returns:
- the
GError, ornullif the pattern is valid.
-
getSettings
-
replace
public boolean replace(TextIter matchStart, TextIter matchEnd, String replace, int replaceLength) throws GErrorException Replaces a search match by another text. IfmatchStartandmatchEnddoesn't correspond to a search match,falseis returned.matchStartandmatchEnditers are revalidated to point to the replacement text boundaries.For a regular expression replacement, you can check if
replaceis valid by callingRegex.checkReplacement(java.lang.String, org.javagi.base.Out<java.lang.Boolean>). Thereplacetext can contain backreferences.- Parameters:
matchStart- the start of the match to replace.matchEnd- the end of the match to replace.replace- the replacement text.replaceLength- the length ofreplacein bytes, or -1.- Returns:
- whether the match has been replaced.
- Throws:
GErrorException- seeGError
-
replaceAll
Replaces all search matches by another text.It is a synchronous function, so it can block the user interface.
For a regular expression replacement, you can check if
replaceis valid by callingRegex.checkReplacement(java.lang.String, org.javagi.base.Out<java.lang.Boolean>). Thereplacetext can contain backreferences.- Parameters:
replace- the replacement text.replaceLength- the length ofreplacein bytes, or -1.- Returns:
- the number of replaced matches.
- Throws:
GErrorException- seeGError
-
setHighlight
public void setHighlight(boolean highlight) Enables or disables the search occurrences highlighting.- Parameters:
highlight- the setting.
-
setMatchStyle
Set the style to apply on search matches.If
matchStyleisnull, default theme's scheme 'match-style' will be used. To enable or disable the search highlighting, usesetHighlight(boolean).- Parameters:
matchStyle- aGtkSourceStyle, ornull.
-
builder
ASearchContext.Builderobject constructs aSearchContextwith the specified properties. Use the variousset...()methods to set properties, and finish construction withSearchContext.Builder.build().- Returns:
- the builder object
-