Contact Us 1-800-596-4880

Working with Code Snippets

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Open Beta Release: The cloud IDE is in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

Code snippets are code patterns that you insert from the IDE into the configuration XML files of your Mule application flows or subflows. Using code snippets for reusable code patterns helps you work more efficiently and reduces the potential for configuration errors.

Use snippets to provide configurations for any element supported by the Mule DSL, such as commonly used connector operations, message processing patterns supported by Core components, or multi-element configurations in the configuration XML. You can configure snippets for connector operations, Core processors, and other processors in the XML configuration file for your Mule applications. See Adding Snippets to a Mule Application

In addition to providing built-in snippets, Anypoint Code Builder enables you to configure your own user snippets in JSON format. See Creating User Snippets.

Anypoint Code Builder converts the snippets to XML when you add them to the configuration XML of your Mule application.

Snippets are templates created in JSON format. When you insert a snippet into the configuration XML for a Mule application flow or subflow, the snippet JSON transforms to XML automatically. Create your user snippets in the mule-xml.json file.

Code snippets are a VS Code feature. For more information, see Snippets in Visual Studio Code.

Benefits of Using Built-in Snippets

Using the XML generated from snippets helps you configure applications more quickly and consistently than with the basic XML elements available from the configuration XML, for example:

  • Built-in Logger snippet:

    <logger level="" message='$2' doc:name="Logger" doc:id="xgsmzm" />

    To configure the snippet XML in a Mule application, specify the values to the attributes, such as the message for the Logger.

  • Basic Logger XML:

    <logger doc:name="Logger" doc:id="xlkhkz" />

    To configure the basic Logger XML, you must provide the attributes and values.

Adding Snippets to a Mule Application

Add snippets from either the canvas or the configuration XML:

  • From the canvas, Anypoint Code Builder inserts all user snippets into the <flow/> or <sub-flow/> element that you are working on.

  • From the configuration XML, use auto-complete to find and insert user snippets.

    For example, for a global configuration that must reside at the same level as the <flow/> or <sub-flow/>, and not within either element, use auto-complete. From the auto-complete menu, snippets are identified by their name and a two-dimensional box icon.

Add built-in or user snippets to your integration or implementation:

Add a Built-In Snippet to a Flow or Subflow

To add a built-in snippet:

  1. On the canvas, click the (Add component) icon where you want to add the snippet:

    Mule application canvas
  2. Select Snippets > Built-in Snippets.

  3. Click the built-in snippet to add to your flow or subflow.

    The IDE inserts XML for the snippet within the <flow/> or <sub-flow/> element of your application at the location you selected from the canvas.

You can also use the auto-complete feature to insert built-in snippets directly into the XML configuration file. See Use Auto-Complete to Insert Snippets into the Configuration XML.

Add a User Snippet to a Flow or Subflow

Add user snippets that you create to your integration or implementation. To create snippets, see Creating User Snippets.

To add a user snippet to a flow or subflow:

  1. In the canvas, click the (Add component) icon where you want to add the snippet:

    Mule application canvas
  2. Select Snippets > User Snippets.

    If no snippets are defined, Anypoint Code Builder creates a new mule-xml.json snippet file with a sample snippet to edit. See Creating User Snippets.

  3. Click the user snippet to add to your flow or subflow.

    The IDE inserts XML for the snippet within the <flow/> or <sub-flow/> element of your application at the location you selected from the canvas.

Creating User Snippets

Create user snippets from either the canvas or from the Command Palette.

Create User Snippets from the Canvas

Create reusable user snippets for your Mule applications.

User snippets are specific to your IDE instance and not shared in projects for applications that you export for others to import and work on.

  1. In the canvas, click the (Add component) icon where you want to add the snippet:

    Mule application canvas
  2. Select Snippets > User Snippets.

  3. Click the (Create user snippet) icon.

    Anypoint Code Builder opens mule-xml.json, where you configure user snippets. If no snippets are defined, Anypoint Code Builder creates a new mule-xml.json snippet file with a sample snippet to edit.

    For example, uncomment all the fields after Example to create the Log a message user snippet:

    {
        // Example:
    	  "Log a message": { (1)
    	 	"prefix": "log", (2)
    	 	"body": [ (3)
    	 		"<logger level=\"${1:INFO}\" message='$2' doc:name=\"Logger\" doc:id=\"$RANDOM_HEX\" />$0",
    	 	],
    	 	"description": "Log output to console"
    	 }
    }
    1 Name that appears in the snippet list
    2 Trigger text to auto-complete the snippet
    3 Reusable code
  4. Save the snippet and click the (Refresh snippet list) icon.

  5. Add the sample snippet to your flow:

    1. In User Snippets, click Log a message.

      Anypoint Code Builder adds the snippet to your configuration XML:

      <logger level="" message='$2' doc:name="Logger" doc:id="sgxbfo" />

      The value of doc:id is randomly generated because of the $RANDOM_HEX value in the snippet.

  6. Add any required values or configurations to the XML, such as a level and message value, for example:

    <logger level="WARN" message='This is a warning message.' doc:name="Logger" doc:id="sgxbfo" />
  7. Add any other snippets to your mule-xml.json by adding a comma after the } and defining your new snippet, for example:

    {
        // Example:
    	 "Log a message": {
    	 	"prefix": "log",
    	 	"body": [
    	 		"<logger level=\"${1:INFO}\" message='$2' doc:name=\"Logger\" doc:id=\"$RANDOM_HEX\" />$0",
    	 	],
    	 	"description": "Log output to console"
    	 },
    	 "Add your snippet title here": {
    	 	"prefix": "Add your snippet trigger here",
    	 	"body": [
    	 		"Add your code here",
    	 	],
    	 	"description": "Add your description here"
    	 }
    }

You can also use the auto-complete feature to insert user snippets directly into the XML configuration file.

Create User Snippets in a Configuration File

To configure user snippets:

  1. Navigate to Configure User Snippets.

    Show me how
    • In the desktop IDE, select Code > Settings > Configure User Snippets.

    • In the cloud IDE, click the (menu) icon, and select Preferences > Configure User Snippets.

    • In either IDE:

      1. Use keyboard shortcuts to open the Command Palette:

        • Mac: Cmd+Shift+p

        • Windows: Ctrl+Shift+p

      2. Select this command:

        Snippets: Configure User Snippets
  2. In the Select Snippets File or Create Snippets field that opens, select:

    mule-xml.json (Mule XML)
  3. If the file has no configurations, mule-xml.json provides comments within curly braces:

    {
    	// Place your snippets for mule-xml here. Each snippet is defined under a snippet name and has a prefix, body and
    	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
    	// same ids are connected.
    	// Example:
    	// "Print to console": {
    	// 	"prefix": "log",
    	// 	"body": [
    	// 		"console.log('$1');",
    	// 		"$2"
    	// 	],
    	// 	"description": "Log output to console"
    	// }
    }
  4. Add your snippet code to the file.

    Example Snippet Code
    {
    	"Mule Flow": {
    		"prefix": "muledx:flow",
    		"body": [
    		  "<flow name=\"${1:$TM_FILENAME_BASE-Flow}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\">",
    		   "",
    		  "</flow>"
    		]
    	 },
    	"HTTP Listener Config": {
    		"prefix": "muledx:http-listener-config",
    		"body": [
    		  "<http:listener-config name=\"${1:httpListenerConfig}\" doc:name=\"${2:Listener Config}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\">",
    		   "  <http:listener-connection host=\"${3:Host}\" port=\"${4:Port}\"/>",
    		  "</http:listener-config>"
    		]
    	 },
    	 "HTTP Listener": {
    		"prefix": "muledx:http-listener",
    		"body": [
    		  "<http:listener path=\"/${1:path}\" config-ref=\"${2:httpListenerConfig}\" doc:name=\"${3:Listener}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\"/>"
    		]
    	 },
    	 "Set Payload": {
    		"prefix": "muledx:set-payload",
    		"body": [
    		  "<set-payload value=\"${1:#[payload]}\" doc:name=\"${2:Set Payload}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\"/>"
    		]
    	 },
    	 "Set Variable": {
    		"prefix": "muledx:set-variable",
    		"body": [
    		  "<set-variable variableName=\"${1:name}\" value=\"${2:#[payload]}\" doc:name=\"${3:Set Variable}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\"/>"
    		]
    	 },
    	 "Logger": {
    		"prefix": "muledx:logger",
    		"body": [
    		  "<logger level=\"INFO\" message=\"${1:#[payload]}\" doc:name=\"${2:Logger}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\"/>"
    		]
    	 },
    	 "Database Config - MySQL": {
    		"prefix": "muledx:db-config-mysql",
    	  "body": [
    		 "<db:config name=\"${1:DatabaseConfig}\" doc:name=\"${2:mySQL DB}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\">",
    		 "  <db:my-sql-connection host=\"${3:Host}\" port=\"${4:Port}\" user=\"${5:Username}\" password=\"${6:Password}\" database=\"${7:DatabaseName}\" />",
    		 "</db:config>"
    	  ]
    	 },
    	 "Database Select": {
    		"prefix": "muledx:db-select",
    	  "body": [
    		 "<db:select config-ref=\"${1:DatabaseConfigRef}\" doc:name=\"${2:Select}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\">",
    			"  <db:sql>",
    			"    <![CDATA[${3:SQL}]]>",
    			"  </db:sql>",
    		 "</db:select>"
    	  ]
    	 },
    	 "Transform Message": {
    		"prefix": "muledx:transform-message",
    		"body": [
    		 "<ee:transform doc:name=\"${1:Transform Message}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\">",
    		 "  <ee:message>",
    		   "    <ee:set-payload>",
    		   "      <![CDATA[${2:DataWeave}]]>",
    		   "    </ee:set-payload>",
    		   "  </ee:message>",
    		   "</ee:transform>"
    		]
    	 }
      }

Use Auto-Complete to Insert Snippets into the Configuration XML

To add snippets directly to the configuration XML for a Mule application:

  1. Click in the configuration XML file.

  2. Open the Auto Complete menu.

    Show me how
    • Mac: Ctrl+Space

    • Windows: Cmd+Space

  3. Insert the snippet into your flow or subflow:

    • For a built-in snippet, search for and select the name of the built-in snippet, for example:

      Auto-completion options for the Choice router

      Compare the built-in Choice snippet configuration to the basic XML configuration for this component:

      <choice doc:name="Choice" doc:id="vcfaqr" > (1)
        <when expression='#[]' doc:name="When" >
        </when>
        <otherwise doc:name="Otherwise" >
        </otherwise>
      </choice>
      
      <choice doc:name="Choice" doc:id="hhdehz" > (2)
      
      </choice>
      1 XML for the built-in Choice snippet is flow:choice in the auto-complete list. The icon for snippets in this menu is a one-dimensional box.
      2 XML for the minimally configured Choice component, which is also available by navigating to Core Processors > Flow Control > Choice from the canvas UI. The icon for this XML is a three-dimensional box.
      • For a user snippet, search for and select the name of the user snippet (as configured in mule-xml.json), for example:

        Auto-completion options for the Log a message user snippet