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

Make a block with ONLY fallthrough children? #54

Closed
bew opened this issue Aug 31, 2022 · 1 comment
Closed

Make a block with ONLY fallthrough children? #54

bew opened this issue Aug 31, 2022 · 1 comment

Comments

@bew
Copy link

bew commented Aug 31, 2022

With the deprecation of the method pick_child_condition, I have a usecase I'm not sure how to do anymore.

I have a component that is marked as fallthrough, and that itself should be a fallthrough component (with its condition() == false when none of its children have anything to show.

I had the following (brittle I know) condition:

  -- NOTE: We need a condition function to be properly skipped when pick_child_on_condition is
  --       used on the block that calls us.
  condition = function(self)
    hline_utils.pick_child_on_condition(self)
    -- NOTE: pick_child_on_condition fills the `self.pick_child` table when one of the condition
    -- is truthy, when that table is empty, no children matched and the entire block should be
    -- skipped.
    return not vim.tbl_isempty(self.pick_child)
  end,

So with the new fallthrough marker, could we have a function to know if a child is going to be displayed or not?

Or maybe there is a better solution ?


I've currently replicated the behavior I want with:

  condition = function(self)
    for _, child in ipairs(self) do
      if not child.condition or child:condition() then
        return true
      end
    end
  end,

Maybe this could be a builtin function?

@bew bew changed the title Make a block with ONLY fallthrough children? (was possible before pick_child_condition depreciation) Make a block with ONLY fallthrough children? Aug 31, 2022
@rebelot
Copy link
Owner

rebelot commented Sep 1, 2022

Your solution is doing almost exactly what pick_pick_on_condition implemented. However this is inefficient, as we are calculating all of the children conditions ahead to chose the child, and then its condition is going to be evaluated again. Doing this in the parent's condition is also risky, if for some reason the children condition needs to know something that would be computed in the parent's init.

The best way to solve this is to remove the conditions from the children, and put them explicitly in the parent init (or condition if the parent itself is part of a nofallfhrough block), which would then set pick_child accordingly. If the parent has no provider, then the block will remain silent if all conditions fail, otherwise you'll need to set a flag that will tell the provider function whether to return something or not.

I hope I understood your problem and the you found my answer useful, I'll write up an example tomorrow.

If I misunderstood something, please write a small example describing the behavior you're trying to achieve

@rebelot rebelot closed this as completed Oct 5, 2022
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

No branches or pull requests

2 participants