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, io.github.jwharm.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 GtkSourceSearchContext
s attached to the
same buffer. Moreover, each of those GtkSourceSearchContext
s 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
Modifier and TypeClassDescriptionstatic class
SearchContext.Builder<B extends SearchContext.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionSearchContext
(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 SearchContext
asParent()
Returns this instance as if it were its parent type.boolean
backward
(TextIter iter, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Synchronous backward search.void
backwardAsync
(TextIter iter, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) boolean
backwardFinish
(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.Builder
object constructs aSearchContext
with the specified properties.boolean
forward
(TextIter iter, @Nullable TextIter matchStart, @Nullable TextIter matchEnd, @Nullable Out<Boolean> hasWrappedAround) Synchronous forward search.void
forwardAsync
(TextIter iter, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) boolean
forwardFinish
(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)
.boolean
int
getOccurrencePosition
(TextIter matchStart, TextIter matchEnd) Gets the position of a search occurrence.int
Gets the total number of search occurrences.Regular expression patterns must follow certain rules.static Type
getType()
Get the GType of the SearchContext classboolean
Replaces a search match by another text.int
replaceAll
(String replace, int replaceLength) Replaces all search matches by another text.void
setHighlight
(boolean highlight) Enables or disables the search occurrences highlighting.void
setMatchStyle
(@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, 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, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.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
settings
isnull
, a newSearchSettings
object will be created, that you can retrieve withgetSettings()
.- Parameters:
buffer
- aGtkSourceBuffer
.settings
- aGtkSourceSearchSettings
, ornull
.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, 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 @Nullable TextIter matchStart, @Nullable @Nullable TextIter matchEnd, @Nullable @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
buffer
is small, this function is more convenient to use.If the
SearchSettings:wrap-around
property isfalse
, this function doesn't try to wrap around.The
hasWrappedAround
out parameter is set independently of whether a match is found. So if this function returnsfalse
,hasWrappedAround
will have the same value as theSearchSettings:wrap-around
property.- 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 @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) The asynchronous version ofbackward(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, io.github.jwharm.javagi.base.Out<java.lang.Boolean>)
.See the
AsyncResult
documentation to know how to use this function.If the operation is cancelled, the
callback
will only be called ifcancellable
was notnull
. The method takes ownership ofcancellable
, so you can unref it after calling this function.- Parameters:
iter
- start of search.cancellable
- aGCancellable
, ornull
.callback
- aGAsyncReadyCallback
to call when the operation is finished.
-
backwardFinish
public boolean backwardFinish(AsyncResult result, @Nullable @Nullable TextIter matchStart, @Nullable @Nullable TextIter matchEnd, @Nullable @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, io.github.jwharm.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 @Nullable TextIter matchStart, @Nullable @Nullable TextIter matchEnd, @Nullable @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
buffer
is small, this function is more convenient to use.If the
SearchSettings:wrap-around
property isfalse
, this function doesn't try to wrap around.The
hasWrappedAround
out parameter is set independently of whether a match is found. So if this function returnsfalse
,hasWrappedAround
will have the same value as theSearchSettings:wrap-around
property.- 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 @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) The asynchronous version offorward(org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, org.gnome.gtk.TextIter, io.github.jwharm.javagi.base.Out<java.lang.Boolean>)
.See the
AsyncResult
documentation to know how to use this function.If the operation is cancelled, the
callback
will only be called ifcancellable
was notnull
. The method takes ownership ofcancellable
, so you can unref it after calling this function.- Parameters:
iter
- start of search.cancellable
- aGCancellable
, ornull
.callback
- aGAsyncReadyCallback
to call when the operation is finished.
-
forwardFinish
public boolean forwardFinish(AsyncResult result, @Nullable @Nullable TextIter matchStart, @Nullable @Nullable TextIter matchEnd, @Nullable @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, io.github.jwharm.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
matchStart
andmatchEnd
don'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
matchStart
andmatchEnd
don'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-text
breaks 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
, ornull
if 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. IfmatchStart
andmatchEnd
doesn't correspond to a search match,false
is returned.matchStart
andmatchEnd
iters are revalidated to point to the replacement text boundaries.For a regular expression replacement, you can check if
replace
is valid by callingRegex.checkReplacement(java.lang.String, io.github.jwharm.javagi.base.Out<java.lang.Boolean>)
. Thereplace
text 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 ofreplace
in 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
replace
is valid by callingRegex.checkReplacement(java.lang.String, io.github.jwharm.javagi.base.Out<java.lang.Boolean>)
. Thereplace
text can contain backreferences.- Parameters:
replace
- the replacement text.replaceLength
- the length ofreplace
in 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
matchStyle
isnull
, default theme's scheme 'match-style' will be used. To enable or disable the search highlighting, usesetHighlight(boolean)
.- Parameters:
matchStyle
- aGtkSourceStyle
, ornull
.
-
builder
ASearchContext.Builder
object constructs aSearchContext
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withSearchContext.Builder.build()
.
-