Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-1523) Remove duplicate/overlapping folding regions #1525

Merged

Conversation

glennsarti
Copy link
Contributor

@glennsarti glennsarti commented Sep 12, 2018

PR Summary

Previously the syntax folder returned an ordered list of folding ranges which
VS Code would "ignore" overlapping or duplicate ranges. However on manual
testing, it showed that duplicate region did exist and could be folded/unfolded
using the "Fold All" and "Unfold All" commands, but could NOT be manipulated
manually in the editor using the +/- indicator.

This commit adds a filter which removes any duplicate or overlapping regions
which is easily detected via similar region start lines. This commit also adds
a test for this scenario.

Fixes #1523

PR Checklist

Note: Tick the boxes below that apply to this pull request by putting an x between the square brackets.
Please mark anything not applicable to this PR NA.

  • PR has a meaningful title
  • Summarized changes
  • TESTS!
  • This PR is ready to merge and is not work in progress
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready

@glennsarti
Copy link
Contributor Author

Manually tested this works with the script provided in the issue. The unit test went from;

Before;

{ start: 1,  end: 2,  kind: null },
{ start: 1,  end: 2,  kind: null },
{ start: 2,  end: 4,  kind: null },
{ start: 2,  end: 4,  kind: null },

After

{ start: 1,  end: 2,  kind: null },
{ start: 2,  end: 4,  kind: null },

@glennsarti glennsarti force-pushed the gh-1523-fix-overlapping-regions branch from 5b3e355 to ffebf75 Compare September 12, 2018 01:52
return (item.startline !== src[index - 1].startline);
})
// Convert the matched token list into a FoldingRange[]
.forEach((item) => { foldingRanges.push(item.toFoldingRange()); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're using a forEach, could we just put the filter inside of the forEach?

You can grab the index as the second parameter in the forEach:

.forEach((item, index) => { 
    if(index === 0 || item.startline !== src[index - 1].startline) {
        foldingRanges.push(item.toFoldingRange());
    }
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I can fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I liked it with the filter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So did I, but hey, Tyler got in first :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably reduce further

return foldableRegions
    .filter((item, index) => index === 0 || item.startline !== src[index - 1].startline)
    .map((item) => item.toFoldingRange());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the previous PR about "avoiding" functional style and use imperative I'm happy to remove .filter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it sounds capricious, but I like the functional style and I think it applies very well here. I'm also aware that V8 is doing its best to make it all efficient. My concern last time was that I was finding it hard to pick apart the behaviour, but I was looking at that particular code again recently and thinking it might be better implemented as a filter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with whatever @glennsarti would like to do in response to @rjmholt :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@glennsarti glennsarti force-pushed the gh-1523-fix-overlapping-regions branch from ffebf75 to 192ca72 Compare September 12, 2018 07:45
@glennsarti
Copy link
Contributor Author

@tylerl0706 Updated.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glennsarti on fire as always! Thanks for the quick turnaround. LGTM

Copy link
Contributor

@rjmholt rjmholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Previously the syntax folder returned an ordered list of folding ranges which
VS Code would "ignore" overlapping or duplicate ranges.  However on manual
testing, it showed that duplicate region did exist and could be folded/unfolded
using the "Fold All" and "Unfold All" commands, but could NOT be manipulated
manually in the editor using the +/- indicator.

This commit uses a filter which removes any duplicate or overlapping regions
which is easily detected via similar region start lines.  This commit also adds
a test for this scenario.
@glennsarti glennsarti force-pushed the gh-1523-fix-overlapping-regions branch from 192ca72 to 92a5324 Compare September 17, 2018 05:28
@glennsarti
Copy link
Contributor Author

Fixed with newer filter code. Ready for merge @tylerl0706 @rjmholt

Copy link
Contributor

@rjmholt rjmholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unfolding not possible
3 participants