Code snippets

With the code snippets functionality you can quickly select commonly used pieces of code that you use. For instance you can select the for code snippet. The code snippet enters a special editing mode in which you can update the various fields of the snippet. If you have a field that is multiple times, you only have to enter it once. For example in a for statement typically you have a variable that is repeated three times.

for ($i = 0; $i < 10; $i++)
{
}

Once you enter the $i the first time, it will update the snippet in every location where the field is present.

Inserting snippets

The default way to insert snippets is by right-clicking on the editor and selecting Insert Snippet. You will be presented with the list of snippets available to you. Once you select the snippet you will enter in a special editing mode that lets you move from field to field. Once you press enter, you will leave this special editing mode and will go back to the standard editing mode.


Accessing snippets via intellisense

You can also have quick access to code snippets via the normal intellisense menu. This option may not be enabled by default so you would have to go to the Php text editor options and under the intellisense section select the checkbox that enables including code snippets into the intellisense menu.

Code snippet editing mode

The image on the left shows the code snippet editing mode. Please note how the various fields are highlighted. In this mode you can only edit the fields and can move from one to another using TAB and Shift-TAB (backward). Once you completed updating the fields, pressing enter returns you to the standard editor mode.



Creating your own code snippets

Creating your own code snippets is very easy. Snippets are defined in an xml format. VS.Php lets you put your own snippets in the following location: My Documents\Visual Studio 2005\Code Snippets\PHP\My Code Snippets. The following is a sample code snippet:


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>class</Title>
			<Shortcut>class</Shortcut>
			<Description>Code snippet for class</Description>
			<Author>Jcx.Software Corp.</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>Class name</ToolTip>
					<Default>MyClass</Default>
				</Literal>
			</Declarations>
			<Code Language="PHP">
        <![CDATA[/**
  Class $name$
*/
class $name$
{
  $selected$$end$
}]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

More information about code snippets