# Code Snippets Plugin

- Arto Jonsson
- Pecan Heber
http://www.codeblocks.org/
http://wiki.codeblocks.org/

### Table of contents

- 1. About
  - 1.1. Creating a new snippet
  - 1.2. Applying a snippet
  - 1.3. Creating a new Category
  - 1.4. Searching
  - 1.5. File Link Snippets
- 2. Development
  - 2.1. Todo
  - 2.2. About the code
  - 2.3. File format

## 1. About

Code Snippets plugin is a simple Snippets management plugin for Code::Blocks. It integrates with the interface by creating a floating docking window which shows all the Snippets. Snippets can be assigned to Categories or they can be in the global Category called All Snippets.

### 1.1. Creating a new Snippet

To create a snippet you may

- 1) write the text in the default snippet editor;
- 2) mark some text and drag it over the top of a snippet tree item; or
- 3) cut, paste or drag text into the snippet editor.

For example, create some external text. Select your text and and drag it over the top of a Category, either to the All Snippets root item or to some other Category you may have created via the Snippets context menu.

You might noticed that the text you dragged disappeared. This is the default behavior of drag and drop; it moves by default. If you want to copy instead, simply hold down the Ctrl key while dragging the text.

To assign text to a existing snippet, you drag and drop the text to that snippet. If you pefer to append the text, use the context menu Edit command.

The default editor is based on Scintilla and contains a context menu of its own for copy/cut/paste/undo etc.

### 1.2. Applying a Snippet

To apply a snippet, double click it. It will be applied to the currently active editor at the current cursor location.

You can, alternatively, right click on the snippet and select Apply.

You may also open the snippet with the editor, mark the text and drag it to its destination.

### 1.3. Creating a new Category

Snippets can be added to Categories. And Categories can be added to Categories. To add a Category, right click the root or other Category and select Add SubCategory.

You may change any Snippet or Category label by slowly left clicking the label, typing a new label, then finishing with the Enter key.

Snippet labels may also be changed from within the editor. Right click the Snippet, choose Edit and change the Label field, Click Ok

Each Snippet is owned by its containing Category. But you may drag Snippets and Categories to other locations on the tree.

The number of Categories and sub-Categories is not limited.

Each Category has a parent Category, whose ultimate base is the All Snippets parent Category. If you want to create a Category to the root, right click All Snippets and select Add SubCategory.

### 1.4. Searching

Search for Snippets by typing their labels into the search pane at the top of the window. Searching is performed by find-as-you-type method. Category labels are included in the default search method.

Select the ">" icon to change the search method and case sensitivity.

### 1.5. File Link Snippets

File Link Snippets contain a file name.

When the editor is invoked on a File Link Snippet, the file data is opened in the editor rather than the Snippet data. The Snippet data containing the file name can be accessed with the context menu Properties command.

To create a File Linked Snippet item either:

- Right click a text Snippet and invoke the context menu Convert To File Link.
- Invoke the Properties context menu and choose a file as a target link to be placed in the Snippet item (use the Link Target button).

Modified data for a File Link is saved with the Ok button and disgarded by Cancel.

The user may specify their own editor by using the Settings entry in the root items context menu.

## 2. Development

### 2.2. About the code

Short description of each class used in plugin.

#### CodeSnippets

CodeSnippets is the main class, subclass of cbPlugin. Most of the functionality is not in this class but in CodeSnippetsWindow.

#### SnippetItemData

SnippetItemData is the class which is used in the tree control. It holds information about the item type and the actual snippet. The class is derived from wxTreeItemData.

#### SnippetsItemTreeCtrl

SnippetsItemTreeCtrl is the tree control used in the docking window. It's subclass of wxTreeCtrl and overloads the OnCompare method. The overloaded compare method uses extra checks for Category and snippet sorting: Categories are first in the tree, sorted A-Z and then the Snippets, sorted A-Z.

In case you're wondering why the control uses wxWidget's dynamic class macros: classes derived from wxTreeCtrl must use these macros so that the overloaded OnCompare method is called, see the wxWidgets tree control example for more info.

#### CodeSnippetsWindow

CodeSnippetsWindow is the docking window class and it holds most of the functionality of the plugin.

### 2.3. File format

Code Snippets plugin uses XML (UTF-8 encoded) format to save internal text Snippets (text contained in the XML file and designated by the T-folder icon). Formats for File Links to external data are user specified (Snippets designated by the L-folder icon).

The XML file starts with root item, named Snippets, which holds all the Categories and code Snippets. Code Snippets and Categories use the same element called item. The item's attributes indicate what type of item it is, Category or code snippet.

Snippets

- Attributes: none
- Subelements: all code Snippets and Categories

item

- Attributes:
  - Name -- Name of the element
  - Type -- Type of the element, either "snippet" or "Category".
- Subelements:
  - Snippet -- The actual code snippet (only if the item is type "snippet").
  - Subitems and Categories (only if the item is type "Category").

#### Example file

>     <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
>     <Snippets>
>     	<item name="Test Category" type="Category" />
>     		<item name="Test item 1" type="snippet" />
>     			<snippet>// Test 1</Snippets>
>     		</item>
>     	</item>
>     	<item name="Test item 2" type="snippet" />
>     		<snippet>// Test 2</Snippets>
>     	<item>
>     </Snippets>

The file would create the following structure to the tree control:

>     All Snippets
>     |
>     |- Test Category
>     |  |
>     |  |- Test item 1
>     |
>     |- Test item 2