Author Topic: Code Snippets  (Read 9498 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: 8795
  • 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: 8795
  • 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: 8795
  • 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.