Collapse All Items *At* Level?

Is there an easy way/script that will allow me to collapse all items in the current view that are at the same level as the item I have my cursor on.

I know there is the “Collapse All Items By Level” but what I’m looking for is something that doesn’t work it’s way up the tree collapsing all it’s children but rather something that collapses all items at the same level as the item that has the cursor on.

Project A:
	- A Test **<cursor>** task 1
		- A Test task 1.1
		- A Test task 1.2 

Project B:
	- B Task 1
		- B Task 1.1
	- B Task 2 

Project C: 
	- C Task 1
		- C Task 1.1
	- C Task 2 

Project D:
	- D Task 1
	- D Task 2
		- D Task 2.1

For example in the file above I have my cursor on a 2nd level item and would like a way to hit a key and have only have all the 2nd level items visible in the current view, as shown below:

Project A:
	- A Test **<cursor>** task 1

Project B:
	- B Task 1
	- B Task 2 

Project C: 
	- C Task 1
	- C Task 2 

Project D:
	- D Task 1
	- D Task 2

My reason for this is that most of my files use a similar hierarchy like h1, h2, h3, etc… and often times I want to only see one level at a time.

Thx

Well, I was going to say “Collapse All By Level” did that, and then I tried it, and now I don’t know what the heck “Collapse All By Level” does at all.

“Collapse All Completely” collapses all projects to the top level. That’s exactly what I expected.
But given that, I expected “Collapse All By Level” to collapse all projects to the current level, i.e. the level the cursor is on. It doesn’t do that. Not only doesn’t it do that, it doesn’t collapse all projects at all. Not only does it not collapse all projects, it seems to collapse a random project, as it’s not the project the cursor is on. And, not only does it not collapse the cursor the cursor is on, it doesn’t collapse to the level the cursor is on.

To use your example above, I positioned the cursor at the beginning of Project D: and selected Collapse All By Level. It collapsed the equivalent of B Task 1.2, if B Task 1.2 had sub-tasks and there was a B Task 1.3. IOW, not Project D:, not to the project level but two levels below, and a seemingly random project and level (not the project I was on, not the project prior to the project I was on, but a project two projects away, and then a random task at a random level beneath it.)

Jesse, what is it that “Collapse/Expand All by Level” is supposed to do? I’ve read the User’s Guide paragraph four times, and it still doesn’t make sense. Not only is the paragraph ambiguous (to me, anyway), but the behavior doesn’t seem to have any correlation to the description at all.

Yea, that was my experience as well.

As best I can tell what this command seems to do is start collapsing the entire document starting from the lowest level working it’s way up?

Yes, I think that’s close, but it works on the current hoisted view, not the entier document. It has two phases.

  1. Look to see level of deepest visible item.
  2. Depending on the command expand/collapse at that level.

So when you are collapsing… each time you trigger the command it will hide one more level of items. When you are expanding it will show one more level of items.

Try this script. It uses a private API, but it’s short and it’s unlikely that API will change anytime soon:

var TaskPaper = Application('TaskPaper')

function TPContextCollapseAtLevel(editor, options) {
  editor.setExpansionLevel(editor.selection.startItem.depth - 1)
}

TaskPaper.documents[0].evaluate({
  script: TPContextCollapseAtLevel.toString(),
  withOptions: { }
});
1 Like

Not the OP, but I love trying out new scripts. :slight_smile: When I tried this one on a nonsense Taskpaper file, had some unexpected behavior. Instead of the result outlined by the OP, I got the below:

but

You can adjust the behavior by changing the line:

editor.setExpansionLevel(editor.selection.startItem.depth - 1)

Change the 1 to a 2 for to collapse more, change to 0 to collapse less.

1 Like

Awesome, thanks!

Thx @jessegrosjean!!!

I also ran into a similar issue of collapsing one level more than I wanted but fixed by removing the “-1” from the line Jesse mentioned.

@jessegrosjean is it possible to maintain the cursor position after executing the script? I noticed that it moves it to the end of the view?

Yes, I think that’s close, but it works on the current hoisted view, not the entier[sic] document.

But why? The command is all, as in Collapse ALL By Level. If Collapse ALL Completely collapses the entire document, then why wouldn’t Collapse ALL By Level also collapse the entire document? That’s a really bad and undocumented (except for this thread) inconsistency, IMO.

For me, the expected behavior is as stated — collapsing the entire document (ALL), and collapsing it to whatever level I’m currently sitting on. Not only is that what I expected, it’s also the most useful; that gives us complete control over the collapse level, we just have to put the cursor on whatever level we want to collapse to.

Please consider this a request. :slight_smile: