Tree view collapse-all command


#1

Hello,

I’m trying to create a custom composed command following the blog post on the topic. I’m having trouble sending a command to the tree-view element in some cases, though…

I have the following example command:

atom.commands.add('atom.workspace', 'example:collapse', () => {
    atom.workspace.getLeftDock().getPaneItems().forEach(paneItem => {
        atom.commands.dispatch(paneItem.element, 'tree-view:collapse-all');
    })
})

Sometimes when I run this command it works, but sometimes it doesn’t. I can consistently getting it to not work if I run it from the search results page (cmd-shift-f). Given that it appears to be due to some reason related to focus or something like that. I’ve added logging statements to the example command to check which element the collapse-all command is being dispatched to, but it appears to always be the tree-view element, even in the cases where it doesn’t work.

Does anyone have an idea of what’s going on here?


#2

You want 'atom-workspace' for your first argument in your atom.commands.add(). I’m surprised that 'atom.workspace' works in any circumstance, since there’s no element called atom in the DOM. The first argument is formatted like a CSS selector, and since <atom-workspace> is one of the custom semantic tags in Atom, that’s what you’ll usually use for commands that should affect the whole environment.


#3

@DamnedScholar, that was a typo from my copying it over into the post, thanks for pointing it out. I’m actually using atom-workspace. Updated my original post to reflect.


#4

Looks like I can’t actually edit the post. Pasting what I’m currently running here:

atom.commands.add('atom-workspace', 'example:collapse', () => {
    atom.workspace.getLeftDock().getPaneItems().forEach(paneItem => {
        atom.commands.dispatch(paneItem.element, 'tree-view:collapse-all');
    })
})

#5

A tool you can use when debugging commands is the Keybinding Resolver, which you can open with ctrl-.. Once you have it open, it will show you every command Atom finds for your keybinding, and whether any of them were selected as the command that Atom thinks you want to run.


#6

Awesome, thanks for that @DamnedScholar, that looks really useful!