Whole Tomato Software Support Forum
Whole Tomato Software Support Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Visual Assist X
 VA Snippets and IDE Macros
 Macro to swap files like Alt-O
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

feline
Moderator

United Kingdom
12804 Posts

Posted - Aug 20 2008 :  10:53:18 AM  Show Profile  Reply with Quote
This macro was posted in the forum thread http://www.wholetomato.com/forum/topic.asp?TOPIC_ID=6374 by MHaggag.

I am re-posting this here for reference, and also since it is a useful starting point if someone wants an alt-o like feature that works a bit differently.

'' Attempts to open the given file, and returns a boolean indicating success or failure
'' Never throws.
Function OpenFile(ByVal filePath As String)
    Try
        DTE.ItemOperations.OpenFile(filePath)
        Return True
    Catch ex As Exception
        Return False
    End Try
End Function

'' Opens the corresponding header/source file
'' Current file | Target file(s)
'' X.hpp        | X.cpp
'' X.h          | X.c, else X.cpp
'' X.cpp        | X.hpp, else X.h
'' X.c          | X.h
Public Sub SwitchHeaderToOrFromSource()
    If DTE.ActiveDocument Is Nothing Then Exit Sub

    Dim fileInfo As FileInfo = New FileInfo(DTE.ActiveDocument.FullName)

    Dim extensionMap As New Dictionary(Of String, String())
    extensionMap.Add(".hpp", New String() {".cpp"})
    extensionMap.Add(".h", New String() {".c", ".cpp"})
    extensionMap.Add(".cpp", New String() {".hpp", ".h"})
    extensionMap.Add(".c", New String() {".h"})

    Dim currentExt As String = LCase(fileInfo.Extension)
    If extensionMap.ContainsKey(currentExt) = False Then Exit Sub

    Dim extensions As String() = extensionMap(currentExt)
    Dim baseName = Path.GetFileNameWithoutExtension(fileInfo.FullName)
    For Each ext As String In extensions
        'MsgBox("Trying: " + baseName + ext)
        If OpenFile(baseName + ext) = True Then Exit Sub
    Next

    MsgBox("Could not find any corresponding h/c/hpp/cpp files to open")
End Sub

zen is the art of being at one with the two'ness

nsimeonov
Junior Member

14 Posts

Posted - Nov 20 2009 :  2:38:53 PM  Show Profile  Reply with Quote
2 minor modifications to make it work on VS 2008 for web site projects:

(4 new mappings):


extensionMap.Add(".master", New String() {".master.cs"})
extensionMap.Add(".cs", New String() {""})
extensionMap.Add(".aspx", New String() {".aspx.cs"})
extensionMap.Add(".ascx", New String() {".ascx.cs"})


for .cs files the mapping is empty as file names are like "blabla.aspx.cs" and all we have to do is remove ".cs" at the end


....

also when opening the file it's complete path should be specified:

If OpenFile(fileInfo.DirectoryName + "\" + baseName + ext) = True Then Exit Sub



Here is the entire code I use at this point:


    '' Attempts to open the given file, and returns a boolean indicating success or failure
    '' Never throws.
    Function OpenFile(ByVal filePath As String)
        Try
            DTE.ItemOperations.OpenFile(filePath)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    '' Opens the corresponding header/source file
    '' Current file | Target file(s)
    '' X.hpp        | X.cpp
    '' X.h          | X.c, else X.cpp
    '' X.cpp        | X.hpp, else X.h
    '' X.c          | X.h
    Public Sub nss_ToggleDesignAndCode_macro()
        If DTE.ActiveDocument Is Nothing Then Exit Sub

        Dim fileInfo As FileInfo = New FileInfo(DTE.ActiveDocument.FullName)

        Dim extensionMap As New Dictionary(Of String, String())
        extensionMap.Add(".hpp", New String() {".cpp"})
        extensionMap.Add(".h", New String() {".c", ".cpp"})
        extensionMap.Add(".cpp", New String() {".hpp", ".h"})
        extensionMap.Add(".c", New String() {".h"})
        extensionMap.Add(".master", New String() {".master.cs"})
        extensionMap.Add(".cs", New String() {""})
        extensionMap.Add(".aspx", New String() {".aspx.cs"})
        extensionMap.Add(".ascx", New String() {".ascx.cs"})

        Dim currentExt As String = LCase(fileInfo.Extension)
        If extensionMap.ContainsKey(currentExt) = False Then Exit Sub

        Dim extensions As String() = extensionMap(currentExt)
        Dim baseName = Path.GetFileNameWithoutExtension(fileInfo.FullName)
        For Each ext As String In extensions
            If OpenFile(fileInfo.DirectoryName + "\" + baseName + ext) = True Then Exit Sub
        Next

        MsgBox("Could not find any corresponding file to open")
    End Sub



Edited by - nsimeonov on Apr 06 2010 11:37:50 PM
Go to Top of Page

sphair
Starting Member

Norway
1 Posts

Posted - Sep 04 2011 :  07:40:30 AM  Show Profile  Reply with Quote
Here is an updated version for Visual Studio 2010.


Imports System
Imports System.Collections.Generic

'' Attempts to open the given file, and returns a boolean indicating success or failure
    '' Never throws.
    Function OpenFile(ByVal filePath As String)
        Try
            DTE.ItemOperations.OpenFile(filePath)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    '' Opens the corresponding header/source file
    '' Current file | Target file(s)
    '' X.hpp        | X.cpp
    '' X.h          | X.c, else X.cpp
    '' X.cpp        | X.hpp, else X.h
    '' X.c          | X.h
    Public Sub SwitchHeaderToOrFromSource()
        If DTE.ActiveDocument Is Nothing Then Exit Sub

        Dim fileInfo As IO.FileInfo = New IO.FileInfo(DTE.ActiveDocument.FullName)

        Dim extensionMap As New Dictionary(Of String, String())
        extensionMap.Add(".hpp", New String() {".cpp"})
        extensionMap.Add(".h", New String() {".c", ".cpp"})
        extensionMap.Add(".cpp", New String() {".hpp", ".h"})
        extensionMap.Add(".c", New String() {".h"})

        Dim currentExt As String = LCase(fileInfo.Extension)
        If extensionMap.ContainsKey(currentExt) = False Then Exit Sub

        Dim extensions As String() = extensionMap(currentExt)
        Dim baseName = IO.Path.GetFileNameWithoutExtension(fileInfo.FullName)
        Dim path = fileInfo.DirectoryName

        For Each ext As String In extensions
            'MsgBox("Trying: " + baseName + ext)
            If OpenFile(path + "\" + baseName + ext) = True Then Exit Sub
        Next

        MsgBox("Could not find any corresponding h/c/hpp/cpp files to open")
    End Sub

Edited by - sphair on Sep 04 2011 07:42:03 AM
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Whole Tomato Software Support Forum © 2013 Whole Tomato Software, Inc Go To Top Of Page
Snitz Forums 2000