[TxMt] Crazy idea: scope assertions in regular expressions

Andy Armstrong andy at hexten.net
Sat Nov 24 16:24:45 UTC 2007


On 24 Nov 2007, at 16:09, Ian Potter wrote:

> Just out of curiosity, where would you want the folding markers in  
> your example?

I wanted to be able to fold an entire chunk of POD as a single entity.  
So that would be

v =head1 A heading

   =head2

^ =cut

Folding to

 > =head A heading ...

POD starts with any line that commences with =whatever apart from =cut  
- which marks the end of POD. So it's no good just to fold at '=head1'.

> It looks like your regex will give you a start-folding-arrow at  
> '=head2' and a stop-folding-arrow at '=cut' but it sounds like you  
> want folds that start at each '=head' section and end at the  
> following section. The problem is, a line can't be both a beginning  
> and an ending. For example:
>
> if (thing) {
> 	doStuff();
> } else {
> 	doOtherStuff();
> }
>
> That will only give you one large folding section. You have to  
> separate the second half from the first if you want two. You could  
> also try to end each fold at the line before each '=head' section,  
> but I don't know how you would go about that.


I don't think you can with the current folding mechanism. My idea was  
that if I could add assertions to the regexen I could make the start- 
of-POD pattern match only in a non-POD scope - so it'd only match the  
first POD directive.

For that use it doesn't have to integrated into the regex engine -  
it'd be enough to have a scope based condition that a folding pattern  
could depend upon:

	foldingStartMarker = '(^=(?!cut)|/\*|(\{|\[|\()\s*$)';
         foldingStartIfScope = '! 
markup.directive.comment.block.documentation.perl';
	foldingStopMarker = '(^=cut|\*/|^\s*(\}|\]|\)))';

But that got me to thinking about regexen for rich text and how a more  
general mechanism to allow scope assertions in regexen would be a fun  
thing to have. It'd allow you to do things like searching and  
replacing only within string literals - for example.

-- 
Andy Armstrong, Hexten







More information about the textmate mailing list