Author Topic: Code Snippets  (Read 9501 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Code Snippets
« on: December 26, 2006, 07:57:46 PM »
This will be a bit of a mixed post ...

I've been playing around with code snippets for VS2005.

Code Snippets are based on XML format files.
The MSDN files have reasonable documentation ..
this will help ..

Investigating Code Snippet Technology 
Andrew W. Troelsen, Microsoft MVP
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/codesnippets.asp
Refactoring C# Code Using Visual Studio 2005 
Andrew W. Troelsen, Microsoft MVP
http://msdn.microsoft.com/library/en-us/dnvs05/html/VS05_refac.asp

If your brain is up to it, this is raw the XML
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>WriteMessageShortcut</Title>
      <Author>kwb 2006Dec</Author>
      <Description>WriteMessage Shortcut</Description>
      <Shortcut>kwbWriteMessageShortcut</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>Editor</ID>
          <ToolTip>Autocad CammandLine Editor object variable</ToolTip>
          <Default>ed</Default>
        </Literal>
        <Literal Editable="true">
          <ID>option_1</ID>
          <ToolTip>tooltip parameters default_1 Greeting</ToolTip>
          <Default>Hello</Default>
        </Literal>
        <Literal Editable="true">
          <ID>option_2</ID>
          <ToolTip>tooltip parameters default_2 </ToolTip>
          <Default>Swampers.</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp" Format="CData" Kind="type decl" Delimiter="$">
      <![CDATA[
  // Comment test Text
  $Editor$.WriteMessage("\n$option_1$ " +"$option_2$");
  $selected$ $end$ ]]>
  </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

... however, for something a little easier to work with try XML Notepad 2007 :-
Article : http://msdn2.microsoft.com/en-us/library/aa905339.aspx
Download : http://www.microsoft.com/downloads/details.aspx?familyid=72d6aa49-787d-4118-ba5f-4f30fe913628&displaylang=en

.. and here's the piccy of the same snippet to compare :-
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #1 on: December 26, 2006, 08:07:07 PM »
These piccys show that particular Snippet in operation ..

.... The highlighted fields can be edited as required :-

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #2 on: December 26, 2006, 08:11:39 PM »
Something to play with ...

If you are getting tired from adding the user statements to a file each time , try something like this :-
defaultUsing_01.snippet :
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>UsingStatements_01</Title>
      <Author>kwb 2006Dec</Author>
      <Description>Default using statements</Description>
      <Shortcut>kwbUsingStatements_01</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="CSharp" Format="CData" Kind="any" Delimiter="$"><![CDATA[//
// KDUB
// Codehimbelonga kwb@theSwamp 2006xxxx
// For AC2007: VS2005

#region usingdeclarations
using System;
//// Assembly acdbmgd .. ObjectDBX.NET Managed Wrapper
//// Assembly acmgd .. Autocad.NET Managed Wrapper
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
//
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
#endregion

$end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #3 on: December 26, 2006, 08:15:29 PM »
.. this will insert a class template ..

Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Class Definition Shortcut</Title>
      <Author>kwb 2006Dec</Author>
      <Description>Class Definition [Empty] Shortcut</Description>
      <Shortcut>kwbEmptyClass</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>namespacex</ID>
          <ToolTip>Namespace Name</ToolTip>
          <Default>testStuff</Default>
        </Literal>
        <Literal Editable="true">
          <ID>classx</ID>
          <ToolTip>Class Name</ToolTip>
          <Default>testClass</Default>
        </Literal>
        <Literal Editable="true">
          <ID>CommandName</ID>
          <ToolTip>CommandLine Command Name</ToolTip>
          <Default>CommandName</Default>
        </Literal>
        <Literal Editable="true">
          <ID>methodName</ID>
          <ToolTip>Internal Method Name</ToolTip>
          <Default>testMethod</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp" Format="CData" Kind="any" Delimiter="$"><![CDATA[
[assembly: CommandClass(typeof($namespacex$.$classx$))]
namespace $namespacex$
{
    public class $classx$
    {
        [CommandMethod("$CommandName$")]
        static public void $methodName$()
        { 
     
        // Mojo goes here !!
         $selected$$end$

        }
    }
}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

with this result :

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #4 on: December 26, 2006, 08:21:40 PM »
... and there will probably be the most used line we ever write :-

DocDbEd.snippet
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>DocumentDatabaseEditor</Title>
      <Author>kwb 2006Dec</Author>
      <Description>Document Database Editor Initialiser</Description>
      <Shortcut>kwbDocumentDatabaseEditor</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="CSharp" Format="CData" Kind="type decl" Delimiter="$"><![CDATA[           
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

$end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

with this result :

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #5 on: December 26, 2006, 08:28:08 PM »
.. and the final result .....
[in acad2007] [or '06]

the potential is huge !
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #6 on: December 26, 2006, 09:40:47 PM »
More stuff ... forgot about these  :oops:
... it was a while ago after all ... :-)

http://www.theswamp.org/index.php?topic=9130.0
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Draftek

  • Guest
Re: Code Snippets
« Reply #7 on: December 27, 2006, 07:50:15 AM »
Thanks for the reminder Kerry,

I WILL be implementing some or all of these in the next few days.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: Code Snippets
« Reply #8 on: December 27, 2006, 05:37:46 PM »
Thanks for the cool tips Kerry,  :-)
 Does anyone know why this does not work with C++/CLI? I see where I can add
Snippits for C++, but how can I add the code to the editor?

Thanks

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #9 on: December 27, 2006, 06:15:58 PM »
Do you mean the API editor Daniel ?

You have to use the SnippetManager to set the folder ..
.. accessed from the Tools menu,

. set the language
. select the source folder [ using Add] .. the folder can be located anywhere you like.


You insert the code into the IDE by either ;
. using Ctrl-K,X in the editor pane
. or, type the defined shortcut then TAB ..

try typing 'class' then TAB [no space] to use the C++ default class template.

hth;
///kwb
« Last Edit: December 27, 2006, 06:17:45 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #10 on: December 27, 2006, 06:23:58 PM »
Here's the default attribute.snippet installed with the VS package.

note that the XML editor file select dialog should be set for *.* files, not *.XML

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: Code Snippets
« Reply #11 on: December 27, 2006, 06:29:49 PM »
Do you mean the API editor Daniel ?

yes.

I have the default snippit folder @
C:\Program Files\Microsoft Visual Studio 8\VC\Snippets\1033\Visual C++\

When I hit Ctrl-K,X or Ctrl-K,S

I get this ?

It does work for C# though

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #12 on: December 27, 2006, 06:36:24 PM »
That's weird ..

Does this work ?  see piccy:-
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: Code Snippets
« Reply #13 on: December 27, 2006, 06:45:32 PM »
Got it,  I needed to re-install Visual Studio 2005 IDE Enhancements.
Maybe the upgrade to SP1 biffed it….

http://www.microsoft.com/downloads/details.aspx?FamilyID=cd7c6e48-e41b-48e3-881e-a0e6e97f9534&displaylang=en

Thanks  :-)

Now I just need to decide what language I am going to use so I can start writing snippets
C# vs C++/CLI ?? got a coin?  :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #14 on: December 27, 2006, 06:51:11 PM »
Use C# ... then I can borrow yours :lol:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #15 on: December 27, 2006, 09:27:44 PM »
Someone want to test this ... ?
kwbGetInteger.snippet
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>GetInteger</Title>
      <Shortcut>kwbGetInteger</Shortcut>
      <Description>User input for an integer.</Description>
      <Author>kwb 2006Dec</Author>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>Editor</ID>
          <ToolTip>ACAD commandLine object</ToolTip>
          <Default>ed</Default>
        </Literal>
        <Literal Editable="true">
          <ID>intOpt</ID>
          <ToolTip>parameters of the prompt</ToolTip>
          <Default>getIntOpt</Default>
        </Literal>
        <Literal Editable="true">
          <ID>intRes</ID>
          <ToolTip>result of a prompt that returns</ToolTip>
          <Default>getIntRes</Default>
        </Literal>
        <Literal Editable="true">
          <ID>promptMsgString</ID>
          <ToolTip>prompt message to be displayed</ToolTip>
          <Default>\nNeed an integer dude ?</Default>
        </Literal>
        <Literal Editable="true">
          <ID>cancelMsg</ID>
          <ToolTip>User has canceled.</ToolTip>
          <Default>\nUser canceled.</Default>
        </Literal>
        <Literal Editable="true">
          <ID>errorMsg</ID>
          <ToolTip>Oooops, User input error.</ToolTip>
          <Default>\nUser input error.</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp" Format="CData" Kind="type decl" Delimiter="$"><![CDATA[// Prompt for an integer
PromptIntegerOptions $intOpt$ = new PromptIntegerOptions("$promptMsgString$");
int intResultValue;
PromptIntegerResult $intRes$ = $Editor$.GetInteger($intOpt$);
switch($intRes$.Status)
{
case PromptStatus.Cancel:
    $Editor$.WriteMessage("$cancelMsg$");
    return;
    break;
case PromptStatus.Error:
    $Editor$.WriteMessage("$errorMsg$");
    return;
    break;
case PromptStatus.OK:
        intResultValue = getIntRes.Value;
        ed.WriteMessage("\nInteger was " + intResultValue.ToString());
        // all's well, so add Mojo here
        $selected$ $end$
    break;
}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: Code Snippets
« Reply #16 on: December 27, 2006, 10:03:33 PM »
Very nice Kerry, :-)
I wonder if you can have something like
Code: [Select]
//getIntOpt.AllowNone = false;
//getIntOpt.AllowZero = true;
Pre-commented and highlighted that way you could just uncomment and modify the property if you wanted to use it
….Just a thought.
« Last Edit: December 27, 2006, 10:04:41 PM by Danielm103 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #17 on: December 27, 2006, 10:14:52 PM »
Hi Daniel
I have been looking at that, it's easier to remove lines not required than to add them :-)

.. for instance , the
ed.WriteMessage("\nInteger was " + intResultValue.ToString());
is intended as a debug aid only.

I am continuing to see a lot of potential here.
.. have sketched out heaps of stuff I intend to 'snippet'


added:
The piccy above shows code that requires about 8 words to be typed .... not bad, heh !


« Last Edit: December 27, 2006, 10:18:11 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: Code Snippets
« Reply #18 on: December 27, 2006, 10:38:35 PM »
I have been looking at that, it's easier to remove lines not required than to add them :-)

Yep that’s exactly what I was thinking.

I guess I will need to transfer all my stuff from treepad to snippets

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #19 on: December 27, 2006, 10:39:59 PM »
Daniel,
I think the default for AllowZero is true,
so the optional code would be ;
Code: [Select]
//getIntOpt.AllowZero = false;.. yes ?


might be interesting to play with defaults too.
... and keywords.
[ aside: now there is a subject for a tutorial :-) ]
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: Code Snippets
« Reply #20 on: December 27, 2006, 10:53:33 PM »
Daniel,
I think the default for AllowZero is true,
so the optional code would be ;
Code: [Select]
//getIntOpt.AllowZero = false;.. yes ?


might be interesting to play with defaults too.
... and keywords.
[ aside: now there is a subject for a tutorial :-) ]

Your right, I just threw that in there, But you got where I was going with the idea.   :-)
« Last Edit: December 27, 2006, 11:26:27 PM by Danielm103 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Code Snippets
« Reply #21 on: December 27, 2006, 10:57:17 PM »
Daniel, That TreePad piccy looks interesting .. is it a code Library ?


added:
DUH .. trust me to ask the obvious 
http://www.treepad.com/treepadfreeware/
« Last Edit: December 27, 2006, 10:59:01 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: Code Snippets
« Reply #22 on: December 27, 2006, 11:25:15 PM »
It’s just something I was using to get me by until Troy
Is done with his project. Hint Hint…   :-D

See
http://www.theswamp.org/index.php?topic=13126.0

Dan

Glenn R

  • Guest
Re: Code Snippets
« Reply #23 on: February 01, 2007, 11:40:29 PM »
I'd forgotten about these in the pursuit of coding nirvana...nice work there Kerry.