Subscription to find-and-replace activation commands


#1

I tried to subscribe to find-and-replace package using following code

@subscriptions.add atom.commands.add 'atom-workspace',
  'find-and-replace:show': => @doStuff()
  'find-and-replace:find-next': => @doStuff()
  'find-and-replace:find-all': => @doStuff()

Only find-and-replace:show works and rest all don’t work.


#2

I cleaned up the indentation in your example to make it more readable. CoffeeScript is an indentation-significant language. Does it look like the way I cleaned it up? Or was it more like the way you had it?


#3

sorry for being so late. I was a bit busy. I guess indentation was all fine.


#5

Sorry to open this again. I’m working with project find-and-replace too, and I would like to bind a global search to a package command. I would like the user to be able to fire a global search with just a key combination (the string to search should be automatically retrieved from the currently open file and passed to the command).

So, in my package I have something like:

@subscriptions.add atom.commands.add('atom-workspace', {
      'my-package:reverseDepSearch': (event) => @doYourThing()
})

And later:

doYourThing: (event) ->
  domElem = atom.views.getView atom.workspace.getActivePane()
  textToSearch = @retrieveFromBuffer domElem # this is the text that should be put in the search bar
  atom.commands.dispatch(domElem, 'project-find:show', textToSearch)
  atom.commands.dispatch(domElem, 'project-find:confirm')

This is just my first guess, and it does not work.
Is there any other way to do it?


#6

I don’t know if there is an easier way, this is just what I quickly coded in my init script:

atom.commands.add 'atom-workspace',
  'custom:test-find': ->
    activeEditor = atom.workspace.getActiveTextEditor()
    activeView = atom.views.getView(activeEditor)
    p = []
    unless atom.packages.isPackageActive('find-and-replace')
      p.push atom.packages.getLoadedPackage('find-and-replace').activate()
    atom.commands.dispatch(activeView, 'project-find:show')
    Promise.all(p).then ->
      projectFindView = atom.packages.getActivePackage('find-and-replace').mainModule.projectFindView
      projectFindView.findEditor.setText('hello')
      atom.commands.dispatch(projectFindView.element, 'project-find:confirm')

Obviously, everything after atom.packages.getActivePackage(...).mainModule is not part of any official API :wink:


#7

Thank you @deprint, that dirty hack works like a charm for me (no offense meant here, it’s just quite a workaround)!
Of course, going through the APIs would be waaaay much better, but this could hold until anything official is available.
Thanks!