Use the tree-view in Atom editor init script

2019-02-19 15:12发布

问题:

I'm trying to write a init script for the Atom editor to add a custom command to be able to reveal the currently opened editor file in the tree-view with one key combination, instead of two.

Here is an example code (which makes something different) to make clear how it generally has to look like.

atom.commands.add 'atom-editor', 'custom:cut-line', ->
  editor = atom.workspace.getActiveEditor()
  editor.selectLine()
  editor.cutSelectedText()

The two commands I need should not be sent to the editor, but to the tree-view. Here are the two commands:

  tree-view:toggle-focus
  tree-view:reveal-active-file

I assume I have to do something similar as above, like getActiveTreeView or something like that. I tried to google it but it doesn't seem to be obvious. Does someone know how to do this?

It could look something like this:

atom.commands.add 'atom-editor', 'custom:show-active-file', ->
  tree-view.toggle-focus()
  tree-view.reveal-active-file()

回答1:

You can use the atom.commands.dispatch() method to send a command when getting a hold of the object to send the commands to is hard. In your case, you can use:

atom.commands.add 'atom-editor', 'custom:show-active-file', ->
  atom.commands.dispatch(atom.workspaceView.element, 'tree-view:toggle-focus')
  atom.commands.dispatch(atom.workspaceView.element, 'tree-view:reveal-active-file')