Use the tree-view in Atom editor init script

﹥>﹥吖頭↗ 提交于 2019-12-08 02:14:43

问题


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')



回答2:


Sadly, Lee's answer is not correct anymore. Within changes in the API the changed the naming of atom.workspaceView to atom.workspace.

So, if anyone gets here (sure questions and answer is a "bit" old), here's the current working script.

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

@Source
https://discuss.atom.io/t/workspaceview-events/14595/4



来源:https://stackoverflow.com/questions/27224269/use-the-tree-view-in-atom-editor-init-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!