1.21 gigawatts
1.21 gigawatts

Reputation: 17762

How to make search work as described behavior

I'm using Ace editor and I'm using the search and as I use it it is searching through the document on each key press change.

So what I mean is as I type into my search box I'm calling this method on each text change:

editor.find(searchInput.text);

If I have a document with ten items named "list" and I type "L" it finds the first instance and highlights "L". Then I type "i" and it finds the second instance and highlights "Li". Then I type "s" and it finds the third instance and "Lis" is highlighted.

What I'd like it to do is stay on the first instance and highlight "Lis". I think I need to set the cursor position. But how do I get the cursor position of the beginning of the highlighted word? Or better yet is it possible to continue subsequent calls to find() starting at the current search position? I hope this makes sense.

Upvotes: 0

Views: 471

Answers (2)

1.21 gigawatts
1.21 gigawatts

Reputation: 17762

It looks like I can ensure the search continues from the current anchor position by using the following code:

var position = editor.getSelectionAnchor();
editor.find("needle", {row:position.row, column:position.column});

FYI It did not seem to work with integers but I could be wrong (try casting to string).

As suggested by @auser, setting skipCurrent to false works as well.

editor.find("needle", {skipCurrent:false});

Upvotes: 0

a user
a user

Reputation: 24104

There is a skipCrrent option for search see https://github.com/ajaxorg/ace/blob/v1.2.0/lib/ace/ext/searchbox.js#L220 which is from built in ace search which already works as you describe

Upvotes: 1

Related Questions