Wednesday, December 7, 2011

TUTORIAL: VS - Creating Code Snippets - CodeProject

I was in the middle of creating my experiance/article about the snippets subject when I got an email from http://www.codeproject.com/ with this wonderful article.

Extending Visual Studio Part 1 - Creating Code Snippets - CodeProject

I've already created some snippets, feel free to use them:

'Decorator-Pattern' snippet (activation: dp_decorator + TAB):
Code Snippet
  1. xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3. <CodeSnippet Format="1.0.0">
  4. <Header>
  5. <Title>Decorator DPTitle>
  6. <Shortcut>dp_decoratorShortcut>
  7. <Description>Code snippet for decorator design patternDescription>
  8. <Author>S.OAuthor>
  9. <SnippetTypes>
  10. <SnippetType>ExpansionSnippetType>
  11. SnippetTypes>
  12. Header>
  13. <Snippet>
  14. <Declarations>
  15. <Literal>
  16. <ID>ComponentID>
  17. <ToolTip>Component nameToolTip>
  18. <Default>ComponentDefault>
  19. Literal>
  20. <Literal>
  21. <ID>DecoratorID>
  22. <ToolTip>Decorator nameToolTip>
  23. <Default>DecoratorDefault>
  24. Literal>
  25. Declarations>
  26. <Code Language="csharp">
  27. // Decorator patern:
  28. // http://en.wikipedia.org/wiki/Decorator_pattern#Structure
  29. // http://www.dofactory.com/Patterns/PatternDecorator.aspx
  30. abstract class $Component$
  31. {
  32. public abstract void Operation();
  33. }
  34. abstract class $Decorator$ : $Component$
  35. {
  36. protected $Component$ _$Component$;
  37. public void SetComponent($Component$ component)
  38. {
  39. this._$Component$ = component;
  40. }
  41. public override void Operation()
  42. {
  43. if (_$Component$ != null)
  44. {
  45. _$Component$.Operation();
  46. // + [your operation here]
  47. }
  48. }
  49. }
  50. $end$
  51. ]]>
  52. Code>
  53. Snippet>
  54. CodeSnippet>
  55. CodeSnippets>

The 'Event changed property' snippet (eprop + TAB):
Code Snippet
  1. xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3. <CodeSnippet Format="1.0.0">
  4. <Header>
  5. <Title>event changed propertyTitle>
  6. <Shortcut>epropShortcut>
  7. <Description>Code snippet for eprop...Description>
  8. <Author>SOAuthor>
  9. <SnippetTypes>
  10. <SnippetType>ExpansionSnippetType>
  11. SnippetTypes>
  12. Header>
  13. <Snippet>
  14. <Declarations>
  15. <Literal Editable="true">
  16. <ID>prop_nameID>
  17. <ToolTip>property nameToolTip>
  18. <Function>PropName()Function>
  19. <Default>PropNameDefault>
  20. Literal>
  21. <Literal Editable="true">
  22. <ID>prop_typeID>
  23. <ToolTip>property typeToolTip>
  24. <Function>PropType()Function>
  25. <Default>ObjectDefault>
  26. Literal>
  27. Declarations>
  28. <Code Language="csharp">
  29. #region $prop_name$
  30. ///
  31. /// Occurs when $prop_name$ changes.
  32. ///
  33. public event EventHandler $prop_name$Changed;
  34. ///
  35. /// Raises the event.
  36. ///
  37. /// The instance containing the event data.
  38. protected virtual void On$prop_name$Changed(EventArgs e)
  39. {
  40. if ($prop_name$Changed != null)
  41. $prop_name$Changed(this, e);
  42. }
  43. private $prop_type$ _$prop_name$ = null;$end$
  44. ///
  45. /// Gets or sets the type of the region.
  46. ///
  47. ///
  48. /// The type of the region.
  49. ///
  50. public $prop_type$ $prop_name$
  51. {
  52. get { return _$prop_name$; }
  53. set
  54. {
  55. if (_$prop_name$ != value)
  56. {
  57. _$prop_name$ = value;
  58. On$prop_name$Changed(new EventArgs());
  59. }
  60. }
  61. }
  62. #endregion
  63. ]]>
  64. Code>
  65. Snippet>
  66. CodeSnippet>
  67. CodeSnippets>

And the 'TODO' snippet (TD + TAB), need some polish like auto date/time stamp, priority...:
Code Snippet
  1. xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3. <CodeSnippet Format="1.0.0">
  4. <Header>
  5. <Title>TODOTitle>
  6. <Shortcut>TDShortcut>
  7. <Description>Code snippet for TODO...Description>
  8. <Author>SOAuthor>
  9. <SnippetTypes>
  10. <SnippetType>ExpansionSnippetType>
  11. SnippetTypes>
  12. Header>
  13. <Snippet>
  14. <Declarations>
  15. Declarations>
  16. <Code Language="csharp">// TODO:P, TimeStamp, S.O: $end$]]>
  17. Code>
  18. Snippet>
  19. CodeSnippet>
  20. CodeSnippets>

My advice: if you already created a code pattern twice (meaning - you copied and pasted a code and renamed some variables and function/property names), add it to your snippets, you'll need it and it will save time.

REMARK: Please follow the codeproject artical if you wish to install/use them...


No comments:

Post a Comment