How to create custom “Text Macros” in Xcode?

2019-01-27 01:21发布

问题:

How to create custom code completion macros in Xcode. I googled and find some methods, but I don't understand them completely. So can anyone tell me how to do it?

Thanks everyone..

回答1:

This post from ablepear has a tutorial to add custom Text Macros. The following are the necessary steps, the tutorial specifies, to create a custom text macro for Objective - C.

  1. Go to your Xcode application file (root/Developer/Applications/). Right-click (control-click) and Show Package Contents. Navigate to (Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/). Select the Objective-C.xctxtmarco file and Copy it (command-c). Open a new Finder window and select your Home folder. Navigate to (Library/Application Support/Developer/Shared/Xcode/). In the Xcode folder Paste (command-v) the Objective-C.xctxtmacro file.

  2. Open Objective-C.cxtxtmacro. It contains an Array with around 26 items, each of which is a Dictionary. Click on the "+" symbol/tab to the right of the selected cell. This will add a new Item to the plist root Array which will be our new entry (text macro definition).

  3. Select the new Item and change the Type from String to Dictionary. Now tap on the disclosure triangle (the left side of the selected cell), this will rotate the triangle from pointing right (collapsed) to pointing down (expanded). You may also notice that the "+" symbol to the right changes into a set of lines when the Item is expanded. This let's us add "children" name/value pairs to our new Item.

  4. There are a few "children" name/value pairs we need to add to our new Item to make it function and they are as follows:

    • Identifier - This describes the macro's language (parent).identifier.
    • BasedOn - This is the (parent) language (objc).
    • IsMenuItem - Boolean value. This creates a menu item in Edit menu.
    • Name - The name listen in the (above) menu item.
    • TextString - The actual string that will be inserted via the text macro.
    • CompletionPrefix - This what you type in as the key for the text macro.
  5. Add the values to the keys. A typical macro looks like this (ex:NSLog text macro).

    • Identifier - objc.flog
    • BasedOn - objc
    • IsMenuItem -YES
    • Name - Function (NSLog)
    • TextString - NSLog(@"FUNCTION: %s", _FUNCTION_);
    • CompletionPrefix - flog

    You can name your Identifier and CompletionPrefix whatever you'd like, as long as it does not conflict with any existing completion identifier. Here. flog, is used for Function Log.

Read the post in the above link, to understand in completely.


IMPORTANT UPDATE: It seems the above v macro doesn't work in Xcode 3.2. To make it work we have to add the key,

OnlyAtBOL = YES; // or NO 

to each macro definition in your xctxtmacro file. This key specifies that the macro works only at Beginning Of Line, or not at the the beginning of line ie., works only after the beginning of line . So the flog macro will look like this.

{
    Identifier = objc.flog;
    BasedOn = objc;
    OnlyAtBOL = YES;
    IsMenuItem = YES;
    Name = "Function (NSLog)";
    TextString = "NSLog(@"FUNCTION: %s", _FUNCTION_)";
    CompletionPrefix = "flog";
}

I hope this will be of some help to someone in future.



标签: xcode macros