Mar 24, 2009

(XCode) How to setup a bracket / brace macro in XCode

Before i used XCode and learned Objective-C, i never thought the bracket is a horrible symbol, there is a solution of creating text macro to insert bracket / brace:

1. Open ObjectiveC.xctxtmacro with an text editor, you can find this file with this path:
/Developer/Applications/Xcode.app/Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/ObjectiveC.xctxtmacro 

2.Add follow code in the file:
{
Identifier = objc.bracket;
BasedOn = objc;
IsMenuItem = YES;
Name = "Bracket Expression";
TextString = "[<#!expression!#>]";
CompletionPrefix = bra;
},
This scope create a text macro "bra" to insert "[<#!expression!#>]", you need to type bra in your code and presse "esc" button, there is a window shows "Bracket Expression", press enter "[<#!expression!#>]" will be inserted.

You can create a similar macro for Brace, here is an example:
{
Identifier = objc.brace;
BasedOn = objc;
IsMenuItem = YES;
Name = "Brace Expression";
TextString = "{<#!expression!#>}";
CompletionPrefix = bra;
},
Here you have a same text macro "bra" to choose bracket or brace.

Update: In the XCode 3.2 which is released for Snow Leopard, the above lines wouldn't work until add this line in each section:

OnlyAtBOL = YES;

No comments: