Click here to Skip to main content
5,787,682 members and growing! (19,635 online)
Email Password   helpLost your password?
General Programming » Macros and Add-ins » VS.NET Macros     Beginner License: The Code Project Open License (CPOL)

Swap Assignments Macro

By John C

A Visual Studio macro to swap assignments in source code
C# 2.0, C#, Windows, .NETVisual Studio, VS2005, Dev

Posted: 20 Sep 2006
Updated: 20 Sep 2006
Views: 8,320
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.53 Rating: 3.63 out of 5
1 vote, 20.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 40.0%
4
2 votes, 40.0%
5

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)

About the Author

John C


Supporter

Location: Canada Canada

Other popular Macros and Add-ins articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralNice, but extend itmemberPJ Arends14:46 20 Sep '06  
GeneralRe: Nice, but extend itsupporterJohn Cardinal15:04 20 Sep '06  
GeneralWhat about trying DataBinding?memberyesildal13:57 20 Sep '06  
GeneralRe: What about trying DataBinding? [modified]supporterJohn Cardinal15:01 20 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Sep 2006
Editor: Deeksha Shenoy
Copyright 2006 by John C
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project