Code Snippets plugin

From CodeBlocks
Revision as of 15:22, 11 November 2006 by Artojon (Talk | contribs)

Jump to: navigation, search
Code snippets
Developer(s): Arto Jonsson
Maintainer(s): Arto Jonsson
Version: 0.1

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.

Snippet management

Creating a new snippet

To create a snippet you must first write the code. The plugin does not use any dialogs (currently no message boxes either) so you have to write the code in the editor. Select your code and and drag it to a category, either in the All snippets or in other category you may have created.

You might noticed that the code you wrote is gone, this is how the drag and drop works; you move by default. If you want to copy the code and then create a new snippet, hold Ctrl and then drag it.

To assign a piece of code to a existing snippet you simply drag and drop the code to that snippet.

Applying a snippet

To apply a snippet, double click it and it will be applied to the currently active editor. You can alternatively right click on the snippet and select Apply.

Creating a new category

Snippets can be added to categories. Each snippet can be at one category at a time. The number of categories and sub-categories is not limited however.

Each user created category has a parent category, the categories created in root have the All snippets category as parent. So if you want to create a category to the root, right click All snippets and select Add subcategory.

Searching

Snippets can be searched by typing words to the text edit control at the top of the docking window. Searching is performed by find-as-you-type method so you don't have to press any search buttons or Enter key. Searching by default includes categories.

Searching at the current state can be considered unoptimized. I haven't tested it yet with hundreds of items but I'm pretty sure it's slow. The search could by optimized by saving the search state when the firs item is found and then later use it as the starting point if the user wants to specify more specific search terms.

Searching is case-sensitive so "C++" and "c++" are seen as different search terms.

File format

Code snippets plugin uses XML (UTF-8 encoded) file to save all code snippets.

The XML file starts with root item, snippets, which holds all the categories and code snippets. Code snippets and categories use the same element called item. The item's attributes tell what type of item it is, category or code snippet.

Elements

snippets

  • Attributes: None
  • Sub-elements: All snippets and categories

item

  • Attributes:
    • name -- Name of the element
    • type -- Type of the element, either snippet or category
  • Sub-elements:
    • snippet -- The actual code snippet (only if the item's type is snippet)
    • Sub-items and categories (only if the item's type is 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:

All snippets
|
|- Test category
|  |
|  |- Test item 1
|
|- Test item 2