Hi, I'm modifying the default actionscript bundle and I have a regex question that I haven't been able to figure out.
I want to match a function declaration, but I don't want to capture the arguments of the function. In other words, in a function like this:
public function addItem(name: String, item: MenuItem): Void { // ... }
I want to color
"addItem(" and ")"
but not
"name: String, item: MenuItem
Is this possible in one regex expression? Is there some way to match something by looking for a leading (or trailing) indicator without including the indicator in the match? In other words... something like:
[in pseudocode] find ")" preceded by "function [a-zA-Z_]* ( ^)"
I'm currently using this regex to successfully capture the whole thing:
match = "\b(function)\s+([a-zA-Z_]\w*)\s*\([^\)]*\)";
Thanks for the help!
Ed
On Jun 16, 2005, at 19:25, Edmundo Ortega wrote:
I want to color: "addItem(" and ")" but not "name: String, item: MenuItem
Is this possible in one regex expression?
Yes, assuming you're using 1.1b12 you can name the captures, this is done like this: match = "(\b(function)\s+()[a-zA-Z_]\w*)\s*\([^\)]*(\))"; captures = { 1 = { name = "storage.type"; }; 3 = { name = "storage.type"; }; };
That way capture #1 and #3 gets “storage.type” as the name.
Is there some way to match something by looking for a leading (or trailing) indicator without including the indicator in the match?
You can both use look-ahead and look-behind assertions in the regular expression.
E.g.: match = "foo(?=bar); or match = "(?<=foo)bar;
The first one matches foo, when followed by bar. The second matches bar, when preceded by foo.
I'm currently using this regex to successfully capture the whole thing: match = "\b(function)\s+([a-zA-Z_]\w*)\s*\([^\)]*\)";
Another approach is to use begin/end matches. E.g.: begin = "\b(function)\s+([a-zA-Z_]\w*)\s*\("; end = "\)";
You'd still need to put a capture around these patterns and name that capture (to get a name associated only to these two things), but it allows you to provide a new set of rules for the stuff between the begin/end patterns using e.g.: patterns = ( { name = "support.class"; match = "\b(String|MenuItem|...)\ \b"; }, ... );
Thanks a lot Allen, I'm learning a LOT about regex today. The problem I'm running into now is that while the captures work, they don't allow color coding to continue on to the non-captured items.
So if my function is like this:
public function addItem(name: String, item: MenuItem): Void {
Then I want my other color coding rules to properly color "String" and "MenuItem". But it seems as these are left as source code. I assume it's because this string has already been processed and it would take innumerable multiple passes to make it work as I wish.
Unless I'm missing another important aspect of the coloring functionality. Thanks again!
On Jun 16, 2005, at 10:40 AM, Allan Odgaard wrote:
On Jun 16, 2005, at 19:25, Edmundo Ortega wrote:
I want to color: "addItem(" and ")" but not "name: String, item: MenuItem
Is this possible in one regex expression?
Yes, assuming you're using 1.1b12 you can name the captures, this is done like this: match = "(\b(function)\s+()[a-zA-Z_]\w*)\s*\([^\)]*(\))"; captures = { 1 = { name = "storage.type"; }; 3 = { name = "storage.type"; }; };
That way capture #1 and #3 gets “storage.type” as the name.
Is there some way to match something by looking for a leading (or trailing) indicator without including the indicator in the match?
You can both use look-ahead and look-behind assertions in the regular expression.
E.g.: match = "foo(?=bar); or match = "(?<=foo)bar;
The first one matches foo, when followed by bar. The second matches bar, when preceded by foo.
I'm currently using this regex to successfully capture the whole thing: match = "\b(function)\s+([a-zA-Z_]\w*)\s*\([^\)]*\)";
Another approach is to use begin/end matches. E.g.: begin = "\b(function)\s+([a-zA-Z_]\w*)\s*\("; end = "\)";
You'd still need to put a capture around these patterns and name that capture (to get a name associated only to these two things), but it allows you to provide a new set of rules for the stuff between the begin/end patterns using e.g.: patterns = ( { name = "support.class"; match = "\b(String|MenuItem|...)\ \b"; }, ... );
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
Doh. Never mind. I just stopped trying to color the parentheses and now it works as I wish. Now, from this string:
public function addItem(name: String, item: MenuItem): Void {
I'm only capturing:
function addItem
And I'm leaving the coloring of the parenthesis and the rest of the string to the other rules.
On Jun 16, 2005, at 10:40 AM, Allan Odgaard wrote:
On Jun 16, 2005, at 19:25, Edmundo Ortega wrote:
I want to color: "addItem(" and ")" but not "name: String, item: MenuItem
Is this possible in one regex expression?
Yes, assuming you're using 1.1b12 you can name the captures, this is done like this: match = "(\b(function)\s+()[a-zA-Z_]\w*)\s*\([^\)]*(\))"; captures = { 1 = { name = "storage.type"; }; 3 = { name = "storage.type"; }; };
That way capture #1 and #3 gets “storage.type” as the name.
Is there some way to match something by looking for a leading (or trailing) indicator without including the indicator in the match?
You can both use look-ahead and look-behind assertions in the regular expression.
E.g.: match = "foo(?=bar); or match = "(?<=foo)bar;
The first one matches foo, when followed by bar. The second matches bar, when preceded by foo.
I'm currently using this regex to successfully capture the whole thing: match = "\b(function)\s+([a-zA-Z_]\w*)\s*\([^\)]*\)";
Another approach is to use begin/end matches. E.g.: begin = "\b(function)\s+([a-zA-Z_]\w*)\s*\("; end = "\)";
You'd still need to put a capture around these patterns and name that capture (to get a name associated only to these two things), but it allows you to provide a new set of rules for the stuff between the begin/end patterns using e.g.: patterns = ( { name = "support.class"; match = "\b(String|MenuItem|...)\ \b"; }, ... );
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate