I would say this is where the DM comes in. I want my games to work somewhat like the real world with addition of magic and monsters, not a computer game where you can take advantage of an edge case.
You have to lean out far enough to see, far enough to throw/shoot. You don't need to move into a square with no obstruction but you can't shoot through a wall. If you lean out far enough to attack, you're leaning out far enough to be seen. Whether or not you are seen is situational.
For me, this all works out via the rules as I've read them.
Using a rogue for the sake of example... the rogue out in the open throws a dagger at an enemy also out in the open using his Action. He then Moves behind a stone wall gaining total cover. He then uses his Bonus (cunning) action to make a DEX (Stealth) check to try and Hide, rendering him not only unseen (due to the cover) but also unheard, unsmelt, and unfelt (assuming the Stealth check is higher than the enemy's Passive Perception.)
This rogue is now considered Hidden. Thus in the next round, the rogue can now lean out or above the wall, use his Action to make an attack with Advantage (after which point he's no longer considered Hidden), then lean back behind or below the wall again and regain total cover. He then can still Move if he'd like, and use his Bonus action to try and Hide again if he remains behind total cover (or other Heavily Obscuring terrain.) And the rogue can do this ad infinitum-- attack with Advantage, move behind total cover, make a DEX (Stealth) check, and if that check is higher than his next target's Passive Perception, the next attack is with Advantage. This is how I have always interpreted the Stealth rules personally. And the way to counter the rogue is for enemies to either move their own position such that the Heavily Obscuring terrain no longer blocks sight (IE they move around the wall and see the rogue standing there in broad daylight)... or other attackers Ready their actions to attack the rogue when he pops out from behind the total cover. And this attack occurs directly after the rogue has made their attack with Advantage (which has broken his Hidden status) and before the rogue can duck back behind the cover. For this particular attack against the rogue, I usually grant the "half-cover" +2 AC bonus to the rogue in that time of the lean.
Now that being said, I also have added a personal house-rule on top of these rules just because I wanted to make my rogue players have to think a little bit more in terms of positioning. I made the rule in my game that you cannot gain Advantage from being Hidden on an attack if it comes from the same position you attacked with Advantage from being Hidden last time. Your next attack must come from a position at least 10 feet away the previous one to gain Advantage on it.
So while a rogue could be behind a tree, attack with Advantage (assuming a higher Stealth check), then duck back behind that tree again to regain total cover for the rest of the round... if he remains behind that tree he cannot make another Stealth check to re-Hide. In my visualization, the enemy knows exactly where the rogue is and can defend from him as normal. For the rogue to re-Hide, he would have to move from behind that tree, through some obscuring terrain at least 10 feet, then arrive behind some other tree or Heavily Obscuring terrain and
then make the new DEX (Stealth) check.
But that's just a personal add-on, and is not I don't believe, a part of the actual generic Stealth rules as they appear in the books as I've read them.