.NET IDE Auto Replace

Posts   
 
    
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 09-Jun-2005 18:59:57   

I find myself typing the following construct (and others) frequently:

using (DataAccessAdapter da = new DataAccessAdapter())
{

}

...and while I am a fast typer, it seems like there should be a way to insert this quickly. I'm using .NET 2003 and I know there is something like that in 2005, but is there something I can use now? What do you all do for this kind of a thing?

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 09-Jun-2005 19:19:44   

NickD wrote:

I'm using .NET 2003 and I know there is something like that in 2005, but is there something I can use now? What do you all do for this kind of a thing?

Re-Sharper's "Live Templates"... couldn't live without them! smile

http://www.jetbrains.com/resharper/

jtgooding
User
Posts: 126
Joined: 26-Apr-2004
# Posted on: 09-Jun-2005 19:46:31   

CodeRush/Refactor also does custom template/macro and much more.

http://www.devexpress.com/Products/NET/Coderush/ http://www.devexpress.com/Products/NET/Refactor/

John

Answer
User
Posts: 363
Joined: 28-Jun-2004
# Posted on: 10-Jun-2005 00:02:51   

You can add the code snipplet to the toolbox on the left. Just highlight the code, and drag it over to the general area of the toolbox. To put it in your code, click and drag it back over wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 10-Jun-2005 09:49:21   

Create a macro!

Here's my macro to create a property using the line the cursor is on. so I type: string foo

hit the key I bound to the macro and I get something like:


/// comments here... 
public string Foo
{
get { return _foo;}
set { set _foo = value;}
}

and no need for fancy plugins which slow your IDE. simple_smile

(VBA, as macros have to be written in VBA


    ''' Creates a property. 
    ''' format: type name  has to be present at the current line.
    ''' Uses caMel casing.
    Sub CreateProperty()
        Dim typeName, propertyName, memberName, nameRead As String
        Dim ts As TextSelection
        Dim sb As New StringBuilder
        Dim boolIsOpened As Boolean

        Try
            ts = DTE.ActiveDocument.Selection
            If (ts.IsEmpty) Then
                ts.SelectLine()
            End If

            typeName = (ts.Text.Split(" ")(0)).Trim()
            nameRead = ((ts.Text.Split(" ")(1))).Trim()
            propertyName = nameRead.Substring(0, 1).ToUpper & nameRead.Substring(1)
            memberName = "_" + nameRead.Substring(0, 1).ToLower() & nameRead.Substring(1)

            ' actual property
            sb.Append("/// <summary>" & Environment.NewLine)
            sb.AppendFormat("/// Gets / sets {0}{1}", nameRead, Environment.NewLine)
            sb.Append("/// </summary>" & Environment.NewLine)
            sb.Append("public " + typeName + " " + propertyName)
            sb.Append(vbCrLf)
            sb.Append("{")
            sb.Append(vbCrLf)

            ' get
            sb.Append("get")
            sb.Append(vbCrLf)
            sb.Append("{")
            sb.Append(vbCrLf)
            sb.AppendFormat("return {0};", memberName)
            sb.Append(vbCrLf)
            sb.Append("}")
            sb.Append(vbCrLf)

            ' set
            sb.Append("set")
            sb.Append(vbCrLf)
            sb.Append("{")
            sb.Append(vbCrLf)
            sb.AppendFormat("{0} = value;", memberName)
            sb.Append(vbCrLf)
            sb.Append("}")
            sb.Append(vbCrLf)
            sb.Append("}")
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)

            'Check to see if UndoContext object is already open.
            If DTE.UndoContext.IsOpen = False Then
                'Open the UndoContext object to track changes.
                Call DTE.UndoContext.Open("CreateProperty " & propertyName, False)
                boolIsOpened = True
            End If

            ' Replace the text
            ts.Delete()
            ts.Insert(sb.ToString(), vsInsertFlags.vsInsertFlagsInsertAtStart)
        Finally

            'If UndoContext was already open, don't close it.
            If boolIsOpened = True Then
                'Close the UndoContext object to commit the changes.
                DTE.UndoContext.Close()
                ' Format the Selection
                ts.SmartFormat()
                ts.MoveToPoint(ts.BottomPoint.CreateEditPoint())
                ts.LineUp()
            End If
        End Try
    End Sub

Frans Bouma | Lead developer LLBLGen Pro
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-Jun-2005 10:09:05   

Ahhh Frans you can't compare that to "Live Templates"... wink

I type "prop", hit tab, and then continue to type the name of the property, "Name", hit tab again and continue to type the type "string" and resharper does the rest. It get:

public string Name { get { return _name; } set { _name= value; } }

but that's not even worth mentioning... you almost expect that. simple_smile

Now I want a "foreach" loop... simple_smile this is where is really comes into its own. I type "foreach" and hit tab. Resharper automatically scans back up my code to find all the collections within scope. I am presented with:

foreach (<Type> <object> in <IEnumerable>){ <cursor ends up here> }

the cursor is on <IEnumerable> and I can choose the closest collection or choose alternatives (down arrow) from a list of IEnumerable objects (ordered by proximity) in my current method or scope. If I choose a strongly typed collection, <Type> is automatiicaly set to the correct type, and <object> is camel cased <type>. Again I hit tab and can choose from list of alternatives...

I have written tons of customs templates... everything from complex event handlers to singletons (which automatically scans for the current class name and creates the entire GetInstance method and all the trimmings....

As I said I could never go back. smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 10-Jun-2005 10:52:14   

Ok these templates are indeed time saving. simple_smile The point with me is always... I can't remember the various different template starting words. Some I remember, but if you have a lot, it takes a lot of time to learn all of them, and I simply don't take the time to learn them well. I have 5 or 6 macro's now. a couple to create regions (member, property regions or a region with a name you specify, all executed with a key stroke, a macro for properties and 2 macro's for forloops (i and j indexed wink ). I found that it is enough for me, I never got used to coderush's templates... old dog new tricks etc. wink . Oh, and ghostdoc plugin of course. Nothing beats ghostdoc's xml comment creation simple_smile

Frans Bouma | Lead developer LLBLGen Pro
alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 10-Jun-2005 17:01:46   

Marcus wrote:

Ahhh Frans you can't compare that to "Live Templates"... wink

How much does resharper slow down your IDE? I think I've tried all the refactoring plugins at one point or another and they all slowed everything down.

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-Jun-2005 18:46:33   

alexdresko wrote:

How much does resharper slow down your IDE? I think I've tried all the refactoring plugins at one point or another and they all slowed everything down.

Opening a solution is slower because it must process and cache all the projects. But honestly if there is any different (which I don't notice) the gains far outweigh the losses...

You can always download the trial and see for yourself.... simple_smile

Answer
User
Posts: 363
Joined: 28-Jun-2004
# Posted on: 10-Jun-2005 20:50:56   

I found it was too slow. On smaller files i didnt notice it, but on larger ones i could tell it really bogged things down. Im running an AMD 2600+ with a gig of ram too.

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-Jun-2005 23:00:52   

Answer wrote:

I found it was too slow. On smaller files i didnt notice it, but on larger ones i could tell it really bogged things down. Im running an AMD 2600+ with a gig of ram too.

Well... I must say my solution has 55 projects, 180,000+ lines of code and I use a re-sharper feature every few seconds especially refactoring and code navigation...

But having said that maybe I'm used to the reduced speed and don't notice... simple_smile

Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 16-Jun-2005 01:45:11   

One thing: JetBrain's technical support is horrible.