Click here to Skip to main content
Click here to Skip to main content

Swap Assignments Macro

, 20 Sep 2006
Rate this:
Please Sign up or sign in to vote.
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...

TextBox.Text=myobject.name; 

... to this:

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.

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)

Share

About the Author

Member 96

Canada Canada
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberamirhosseinhashemzadeh25-Oct-11 0:07 
GeneralGreat! PinmemberVertexwahn4-Jan-10 11:25 
GeneralRe: Great! PinmemberJohn C4-Jan-10 11:32 
GeneralGreat!! PinmemberBigJoe71419-Feb-09 5:12 
GeneralMy vote of 1 PinmemberHoward Richards29-Jan-09 22:09 
GeneralRe: My vote of 1 PinmemberKramII22-Jul-09 6:22 
GeneralRe: My vote of 1 PinmemberJohn C22-Jul-09 7:57 
GeneralNice, but extend it PinmemberPJ Arends20-Sep-06 13:46 
GeneralRe: Nice, but extend it PinmemberJohn Cardinal20-Sep-06 14:04 
QuestionWhat about trying DataBinding? Pinmemberyesildal20-Sep-06 12:57 
AnswerRe: What about trying DataBinding? [modified] PinmemberJohn Cardinal20-Sep-06 14:01 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 20 Sep 2006
Article Copyright 2006 by Member 96
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid