Option Strict Off Option Explicit Off Imports EnvDTE Imports System.Diagnostics Imports System.Collections Imports System Imports System.IO Imports System.Text Public Module MvpNavigation Sub OpenTest() Dim v As String v = DTE.ActiveDocument().FullName() Dim slash As String = v.LastIndexOf("\") v = v.Substring(0, slash + 1) + "Tests\Test" + v.Substring(slash + 1) DTE.ActiveDocument.Selection.StartOfDocument() DTE.ExecuteCommand("Edit.Find") DTE.Find.FindWhat = "namespace" DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = False DTE.Find.MatchWholeWord = True DTE.Find.Backwards = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral DTE.Find.Action = vsFindAction.vsFindActionFind DTE.Find.Execute() DTE.Windows.Item(Constants.vsWindowKindFindReplace).Close() DTE.ActiveDocument.Selection.CharRight(False, 2) DTE.ActiveDocument.Selection.EndOfLine(True) DTE.ActiveDocument.Selection.Copy() Try OpenInOtherWindow(v) Catch Dim name As String slash = v.LastIndexOf("\") If slash < 0 Then name = v Else name = v.Substring(slash + 1) End If Dim dot As String = name.LastIndexOf(".") If dot > 0 Then name = name.Substring(0, dot) End If If name.EndsWith("View") Then MsgBox("Views do not have test classes") Else Dim result As MsgBoxResult result = MsgBox("Test file doesn't exist. Do you want to create it?", MsgBoxStyle.YesNo) If (result = MsgBoxResult.Yes) Then Dim folder As String folder = GetFolder(v) AddNewTest(name, folder, v) End If End If End Try End Sub Sub OpenInterface() Dim base As String Dim folder As String Dim type As String Dim target As String base = GetBase(DTE.ActiveDocument().FullName()) folder = GetFolder(DTE.ActiveDocument().FullName()) type = GetMVPType(DTE.ActiveDocument().FullName()) target = folder + "I" + base + type + ".cs" MsgBox(target) OpenInCorrectWindow(target, IsTest(DTE.ActiveDocument().FullName())) End Sub Sub OpenView() Dim base As String Dim folder As String Dim target As String base = GetBase(DTE.ActiveDocument().FullName()) folder = GetFolder(DTE.ActiveDocument().FullName()) target = folder + base + "View.cs" OpenInCorrectWindow(target, IsTest(DTE.ActiveDocument().FullName())) End Sub Sub OpenPresenter() Dim base As String Dim folder As String Dim target As String base = GetBase(DTE.ActiveDocument().FullName()) folder = GetFolder(DTE.ActiveDocument().FullName()) target = folder + base + "Presenter.cs" OpenInCorrectWindow(target, IsTest(DTE.ActiveDocument().FullName())) End Sub Sub OpenModel() Dim base As String Dim folder As String Dim target As String base = GetBase(DTE.ActiveDocument().FullName()) folder = GetFolder(DTE.ActiveDocument().FullName()) target = folder + base + "Model.cs" OpenInCorrectWindow(target, IsTest(DTE.ActiveDocument().FullName())) End Sub Sub OpenAdapter() Dim base As String Dim folder As String Dim target As String base = GetBase(DTE.ActiveDocument().FullName()) folder = GetFolder(DTE.ActiveDocument().FullName()) target = folder + base + "Adapter.cs" OpenInCorrectWindow(target, IsTest(DTE.ActiveDocument().FullName())) End Sub Sub OpenAdapterModel() Dim base As String Dim folder As String Dim target As String base = GetBase(DTE.ActiveDocument().FullName()) folder = GetFolder(DTE.ActiveDocument().FullName()) target = folder + base + "AdapterModel.cs" OpenInCorrectWindow(target, IsTest(DTE.ActiveDocument().FullName())) End Sub Private Sub OpenInCorrectWindow(ByVal file As String, ByVal fromTest As Boolean) Try If fromTest Then OpenInOtherWindow(file) Else DTE.ItemOperations.OpenFile(file) End If Catch REM File does not exist. Try to help DTE.ExecuteCommand("ReSharper.GotoType") End Try End Sub Private Sub OpenInOtherWindow(ByVal spec As String) DTE.ItemOperations.OpenFile(spec) Try DTE.ExecuteCommand("Window.MovetoNextTabGroup") Catch Try DTE.ExecuteCommand("Window.MovetoPreviousTabGroup") Catch DTE.ExecuteCommand("Window.TileVertically") End Try End Try End Sub Private Function IsTest(ByVal spec As String) As Boolean Dim slash As String = spec.LastIndexOf("\") If (slash < 5) Then Return Not spec.StartsWith("Test") End If Dim part As String part = spec.Substring(slash - 5, 10) Return part.Equals("Tests\Test") End Function Private Function GetFolder(ByVal spec As String) As String Dim slash As String = spec.LastIndexOf("\") If (slash < 0) Then Return "" End If Dim folder As String folder = spec.Substring(0, slash + 1) REM Strip off the test folder If folder.EndsWith("Tests\") Then folder = folder.Substring(0, folder.Length - 6) End If Return folder End Function Private Function GetBase(ByVal spec As String) As String Dim name As String Dim slash As String = spec.LastIndexOf("\") If (slash < 0) Then name = spec Else name = spec.Substring(slash + 1) End If Dim dot As String = name.LastIndexOf(".") If (dot > 0) Then name = name.Substring(0, dot) End If REM If the name starts with 'Test', strip it off If name.StartsWith("Test") Then name = name.Substring(4) End If REM If the name starts with 'I', followed by a capital letter, REM remove the [I]nterface designation If name.Length > 1 And name.Chars(0) = Chr(73) And Char.IsUpper(name.Chars(1)) Then name = name.Substring(1) End If REM Remove Presenter, Model, View, Adapter, & AdapterModel suffix If name.EndsWith("Presenter") Then name = name.Substring(0, name.Length - 9) ElseIf name.EndsWith("AdapterModel") Then name = name.Substring(0, name.Length - 12) ElseIf name.EndsWith("Model") Then name = name.Substring(0, name.Length - 5) ElseIf name.EndsWith("View") Then name = name.Substring(0, name.Length - 4) ElseIf name.EndsWith("Adapter") Then name = name.Substring(0, name.Length - 7) End If Return name End Function Private Function GetMVPType(ByVal file As String) As String If file.EndsWith("Presenter.cs") Then Return "Presenter" ElseIf file.EndsWith("AdapterModel.cs") Then Return "AdapterModel" ElseIf file.EndsWith("Model.cs") Then Return "Model" ElseIf file.EndsWith("View.cs") Then Return "View" ElseIf file.EndsWith("Adapter.cs") Then Return "Adapter" End If Return "" End Function Private Sub AddNewTest(ByVal name As String, ByVal folder As String, ByVal file As String) Dim tab As String tab = String.Format("{0}", Chr(9)) Dim sw As StreamWriter sw = System.IO.File.CreateText(file) sw.WriteLine("namespace BepHost.Test") sw.WriteLine("{") sw.WriteLine(tab + "using NUnit.Framework;") sw.WriteLine("") sw.WriteLine(tab + "[TestFixture]") sw.WriteLine(tab + "public class " + name) sw.WriteLine(tab + "{") sw.WriteLine(tab + tab + "//") sw.WriteLine(tab + tab + "// Helpers") sw.WriteLine(tab + tab + "//") sw.WriteLine("") sw.WriteLine(tab + tab + "//") sw.WriteLine(tab + tab + "// Tests") sw.WriteLine(tab + tab + "//") sw.WriteLine("") sw.WriteLine(tab + "}") sw.WriteLine("}") sw.Close() REM Hokiest possible way to get new file added to correct folder Dim files As String() files = Directory.GetFiles(folder + "Tests") Dim i As Int32 For i = 0 To files.Length - 1 Dim test As String = files(i) If (IsTest(test)) Then DTE.ItemOperations.OpenFile(test) DTE.ItemOperations.AddExistingItem(file) Dim delimStr As String = "\/" Dim delim As Char() = delimStr.ToCharArray Dim parts As String() = test.Split(delim) Dim last As Int32 = parts.Length - 1 Dim testName As String = parts(last - 1) + "\" + parts(last) DTE.Windows.Item(testName).Close() OpenTest() DTE.ActiveDocument.Selection.StartOfDocument() DTE.ExecuteCommand("Edit.Find") DTE.Find.FindWhat = "namespace" DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = False DTE.Find.MatchWholeWord = True DTE.Find.Backwards = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral DTE.Find.Action = vsFindAction.vsFindActionFind DTE.Find.Execute() DTE.Windows.Item(Constants.vsWindowKindFindReplace).Close() DTE.ActiveDocument.Selection.CharRight(False, 2) DTE.ActiveDocument.Selection.EndOfLine(True) DTE.ActiveDocument.Selection.Paste() DTE.ActiveDocument.Selection.Text = ".Tests" Return End If Next End Sub End Module