Click here to Skip to main content
15,885,954 members
Articles / Programming Languages / C#

Swap Assignments Macro

Rate me:
Please Sign up or sign in to vote.
4.67/5 (12 votes)
20 Sep 2006CPOL 33.7K   8   11
A Visual Studio macro to swap assignments in source code

Introduction

I find this useful and couldn't find anything like it before I wrote it, hence this article.

This is a simple little macro I came up with to alleviate the tedium of reversing assignments in source code. This is something I come across quite frequently when working with business objects and in ASP.NET forms where you initially set a series of objects with a value, then later want to reverse the assignment.

For example, you might want to convert this...

C#
TextBox.Text=myobject.name; 

... to this:

C#
myobject.name=TextBox.Text; 

Multiply that by dozens of assignments and it's quite tedious to reverse all that original code you wrote. Since this is very simple, I've just included it here in the article content. You can copy and paste it into your own macro in Visual Studio.

Note that it appends each line of code with a semicolon for C#, but you could easily modify it to not do that for VB or whatever you happen to be editing.

VB.NET
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.Text
Imports System.Text.RegularExpressions   

Public Module JohnUtils
    'Get currently selected text function
    Private Function GetCurrentlySelectedText() As String
        If Not DTE.ActiveDocument Is Nothing Then
            Dim txt As TextSelection
            txt = CType(DTE.ActiveDocument.Selection, TextSelection)
            Return txt.Text
        Else
            Return String.Empty
        End If
    End Function 
    'Swap assignments, go through each line in the currently selected text
    'and swap the left and right side and output as a block below the selected text
    Sub SwapAssignmentRegion()
        Dim line, originalCode As String
        originalCode = GetCurrentlySelectedText()
        If Not originalCode = String.Empty Then
            Dim leftside As String
            Dim rightside As String
            Dim output As New System.Text.StringBuilder
            Dim lines() As String
            lines = Split(originalCode, vbLf)
            Dim r As Regex
            'Use a regular expression to parse out the text on either side of
            'the equals sign in the line being processed
            r = New Regex( _
                "\s*(?<leftside>\S*)" & _
                "\s*(=)\s*(?<rightside>\S*)", _
                RegexOptions.IgnoreCase Or _
                RegexOptions.ExplicitCapture) 
            For Each line In lines
                line = line.Trim
                If Not line = String.Empty Then
                    Dim mtch As Match
                    mtch = r.Match(line)
                    If mtch.Success Then
                        leftside = _
                          mtch.Groups("leftside").Value.Trim
                        rightside = _
                          mtch.Groups("rightside").Value.Trim.Replace(";", "")
                        'Remove the semicolon replace statement above and 
                        'in the output format below for VB
                        output.AppendFormat( _
                               "{0}{1} = {2};", _
                             vbCrLf, rightside, _
                             leftside)
                    End If
                End If
            Next 
            'Output the swapped region with undo context
            'so user can undo the whole operation at once
            DTE.UndoContext.Open("SwapAssignmentRegion")
            Dim txt As TextSelection
            txt = CType(DTE.ActiveDocument.Selection, TextSelection)
            txt.Insert(vbCrLf + output.ToString, _
                vsInsertFlags.vsInsertFlagsInsertAtEnd _
                Or vsInsertFlags.vsInsertFlagsContainNewText)
            txt.SmartFormat()
            DTE.UndoContext.Close() 
        End If 
    End Sub 
End Module

History

  • 20th September, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
AmirHashemZadeh25-Oct-11 0:07
professionalAmirHashemZadeh25-Oct-11 0:07 
GeneralGreat! Pin
Vertexwahn4-Jan-10 11:25
Vertexwahn4-Jan-10 11:25 
GeneralRe: Great! Pin
Member 964-Jan-10 11:32
Member 964-Jan-10 11:32 
GeneralGreat!! Pin
BigJoe71419-Feb-09 5:12
BigJoe71419-Feb-09 5:12 
GeneralMy vote of 1 Pin
Howard Richards29-Jan-09 22:09
Howard Richards29-Jan-09 22:09 
GeneralRe: My vote of 1 Pin
KramII22-Jul-09 6:22
KramII22-Jul-09 6:22 
GeneralRe: My vote of 1 Pin
Member 9622-Jul-09 7:57
Member 9622-Jul-09 7:57 
GeneralNice, but extend it Pin
PJ Arends20-Sep-06 13:46
professionalPJ Arends20-Sep-06 13:46 
GeneralRe: Nice, but extend it Pin
Member 9620-Sep-06 14:04
Member 9620-Sep-06 14:04 
QuestionWhat about trying DataBinding? Pin
yesildal20-Sep-06 12:57
yesildal20-Sep-06 12:57 
AnswerRe: What about trying DataBinding? [modified] Pin
Member 9620-Sep-06 14:01
Member 9620-Sep-06 14:01 
Yes I use DataBinding all the time in Winform projects and that is always the best method when available or appropriate, however when working at a business object layer or funnelling data into and out of a database into a business object below the UI layer or working with a custom business object that doesn't fit into the asp.net Microsoft DataSource controls it's still necessary to assign back and forth like this.


-- modified at 20:06 Wednesday 20th September, 2006

I *do* know that there is an objectdatasource for asp.net however it requires modification of existing objects or that they be written for it from the start and I'm working with an existing business object library I wrote that is in use with databinding in a winform application that is released.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.