Problem with findMarkers function of TextEditor


#1

I am trying to develop a package for which I need to get all the find-and-replace markers. So I tried this code:

editor = atom.workspace.getActiveTextEditor()
markers = editor.findMarkers({ class: 'find-result' })

but every time it returns an empty array.

I don’t know what’s the problem.


#2

Have you tried just iterating through the markers and seeing which match? Maybe the properties aren’t the way you think they are?


#3

I can get all the markers by using findMarkers() but I don’t know how to match desired properties.


#4

Have you checked to see if any of them have a class property? You could use console.log to dump them to the DevTools console and inspect them. Or you could use the DevTools console to execute your code and see what is returned.


#5

no I don’t see any class property in console

This is what I see.

I think the findMarkers() method doesn’t list the find-result markers


#6

And what about the bufferMarker property there?


#7

All markers have this property.


#8

As I said earlier, I think the find-result markers are not even listed because the number of markers remains the same with or without searching for words( and hence creating find-result markers)


#9

Why is it you need access to the find-and-replace markers? What is it you’re trying to get done?


#10

I want to implement scroll-searchers

For this, I have to find out starting row index of each find-result.

I was able to implement it by doing some changes in find-and-replace package and atom src. But now I want to make an independent package.


#11

Maybe you could take a look at the minimap-find-and-replace package?


#12

In minimap package, Same method is used as I described in first post.
Don’t know why isn’ t it working for me.


#13

I’m guessing you’re running on an Atom built from master?

When running master, find-and-replace changes the way it deals with markers to support markers layer. And that prevents any packages to access all the matches at once. I only noticed that recently and opened an issue to track that on minimap-find-and-replace. But I fear that without more changes in find-and-replace (like providing a service to retrieve all the matches without creating markers for all of them) we won’t be able to achieve such feature.

I raised a concern in that regard when this was first proposed but it seems they didn’t took it in consideration.

As I’m in the midst of converting the whole minimap to ES6 I don’t have much time to dig into this, but I’ll be interested to hear about possible workaround that doesn’t involve modifying the find-and-replace package.


#14

That seems convincing. Atleast now I can stop banging my head why is this not working. Thanks.


#15

Ok, I took 5min to look at the find-and-replace sources, and I believe all the markers are still created, but they are on a specific marker layer, which prevent us from retrieving them using findMarkers. I’ll take some more time tonight to see if we can easily access the find and replace marker layer.


#16

A quick update, I do get access to the results marker layer in a somewhat hacky way, you can try that in the console:

f = atom.packages.getActivePackage('find-and-replace').mainModule
f.findModel.resultsMarkerLayer.findMarkers({class: 'find-result'})

I noticed the result marker is always the same whatever the text editor you’re in.


#17

Perhaps what needs to happen is just that the findMarkers API should be updated to search through all the available marker layers?


#18

Sure, and it would be great to have a name for the marker layer, so that we could do something like textEditor.getMarkerLayer('find-and-replace') rather than relying on the package itself.


#19

yeah that works :grinning:


#20

Nice! I guess until it comes out officially we should take to check for the presence of marker layers before using a marker API.
And you should probably support both API if you plan to release your package as we don’t really know when this feature will make it to stable.