Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Region "Imports"
- Imports System
- Imports EnvDTE
- Imports EnvDTE80
- Imports EnvDTE90
- Imports System.Diagnostics
- Imports System.IO
- Imports System.Collections.Generic
- #End Region
- Public Module AHMacros
- #Region "Load and Save View"
- Sub AHLoadView()
- Dim name As String
- Dim winConfi As EnvDTE.WindowConfiguration
- Dim winConfigList As String = Environment.NewLine
- Dim configCount As Integer
- configCount = DTE.WindowConfigurations.Count
- For I As Integer = 1 To DTE.WindowConfigurations.Count
- Try
- winConfi = DTE.WindowConfigurations.Item(I)
- winConfigList += I.ToString() + " " + winConfi.Name + Environment.NewLine
- Catch ex As Exception
- End Try
- Next
- name = InputBox("Which window layout would you like to load, Enter the Number?" + winConfigList, "Load Window Layout")
- If (name <> "") Then
- Dim index As Integer = Integer.Parse(name)
- Dim configName As String = DTE.WindowConfigurations.Item(index).Name
- DTE.WindowConfigurations.Item(configName).Apply()
- End If
- End Sub
- Sub AHSaveView()
- Dim name As String
- name = InputBox("Enter the name you want to save as:", "Save window layout")
- If (name = "") Then
- MsgBox("Empty string, enter a valid name.")
- Else
- DTE.WindowConfigurations.Add(name)
- End If
- End Sub
- #End Region
- #Region "Code Editor Formating"
- Public Sub AHWrapRegion()
- If Not (ValidateWindow()) Then
- Return
- End If
- Dim startPoint As EnvDTE.EditPoint
- Dim endPoint As EnvDTE.EditPoint
- Dim textDocument As TextDocument
- Dim regionStart As String = Environment.NewLine & "#region New Region" & Environment.NewLine
- Dim regionEnd As String = Environment.NewLine & "#endregion" & Environment.NewLine
- Dim objSel As TextSelection = DTE.ActiveDocument.Selection
- Dim StartLine As Integer = DTE.ActiveDocument.Selection.TopLine
- Dim EndLine As Integer = DTE.ActiveDocument.Selection.BottomLine
- startPoint = objSel.TopPoint.CreateEditPoint()
- endPoint = objSel.BottomPoint.CreateEditPoint()
- startPoint.Insert(regionStart)
- endPoint.Insert(regionEnd)
- objSel.FindText("New Region", vsFindOptions.vsFindOptionsFromStart)
- End Sub
- Public Sub AHCleanDoubleLinesSelection()
- If Not (ValidateWindow()) Then
- Return
- End If
- Dim startPoint As EnvDTE.EditPoint
- Dim endPoint As EnvDTE.EditPoint
- Dim textDocument As TextDocument
- Dim text As String
- Dim objSel As TextSelection = DTE.ActiveDocument.Selection
- startPoint = objSel.TopPoint.CreateEditPoint()
- endPoint = objSel.BottomPoint.CreateEditPoint()
- text = startPoint.GetText(endPoint)
- startPoint.LineUp(1)
- Dim whatToReplace As String = Environment.NewLine & Environment.NewLine
- Dim whatToReplace1 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine
- Dim whatToReplace2 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
- Dim whatToReplace3 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
- Dim whatToReplaceWith As String = Environment.NewLine
- text = text.Replace(whatToReplace3, whatToReplaceWith)
- text = text.Replace(whatToReplace2, whatToReplaceWith)
- text = text.Replace(whatToReplace1, whatToReplaceWith)
- text = text.Replace(whatToReplace, whatToReplaceWith)
- startPoint.ReplaceText(endPoint, text, EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)
- End Sub
- Public Sub AHCleanDoubleLines()
- If Not (ValidateWindow()) Then
- Return
- End If
- Dim startPoint As EnvDTE.EditPoint
- Dim endPoint As EnvDTE.EditPoint
- Dim textDocument As TextDocument
- Dim text As String
- textDocument = DTE.ActiveDocument.Object
- startPoint = textDocument.StartPoint.CreateEditPoint
- endPoint = textDocument.EndPoint.CreateEditPoint
- text = startPoint.GetText(endPoint)
- Dim whatToReplace As String = Environment.NewLine & Environment.NewLine
- Dim whatToReplace1 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine
- Dim whatToReplace2 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
- Dim whatToReplace3 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
- Dim whatToReplaceWith As String = Environment.NewLine
- text = text.Replace(whatToReplace3, whatToReplaceWith)
- text = text.Replace(whatToReplace2, whatToReplaceWith)
- text = text.Replace(whatToReplace1, whatToReplaceWith)
- text = text.Replace(whatToReplace, whatToReplaceWith)
- startPoint.ReplaceText(endPoint, text, EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)
- End Sub
- #End Region
- #Region "Code File Backup"
- Public Const VS_BACKUP_PATH As String = "C:\DATA\temp\__VSBACKUP"
- Sub AHSaveBackup()
- If Not (ValidateWindow()) Then
- Return
- End If
- Dim fileName As String
- Dim startPoint As EnvDTE.EditPoint
- Dim endPoint As EnvDTE.EditPoint
- Dim textDocument As TextDocument
- Dim text As String
- Dim tempFileDirectory As String
- Dim tempFullFileName As String
- Dim td As String
- Dim dsep As String = ""
- Dim tsep As String = "."
- tempFileDirectory = VS_BACKUP_PATH
- If Not (Directory.Exists(tempFileDirectory)) Then
- Dim msg As String = "Directory " & tempFileDirectory & " doesn't exists"
- msg += Environment.NewLine & " Do you want to create?"
- Dim result As Microsoft.VisualBasic.MsgBoxResult = MsgBox(msg, MsgBoxStyle.YesNo, "???")
- If (result = MsgBoxResult.No) Then
- Return
- End If
- Directory.CreateDirectory(tempFileDirectory)
- End If
- fileName = DTE.ActiveDocument.Name ' + ".bak"
- 'td = DateAndTime.Today.Month.ToString() & dsep + DateAndTime.Today.Day.ToString() & dsep & DateAndTime.Today.Year.ToString()
- ' td += "_" & Now.Hour.ToString() & tsep & Now.Minute.ToString() & tsep & Now.Second.ToString()
- td = "_" & DateAndTime.Now.ToString().Replace("/", "").Replace(":", ".")
- Dim ext As String = Path.GetExtension(fileName)
- fileName = fileName.Replace(ext, "") & "_" & td & ext
- tempFullFileName = Path.Combine(tempFileDirectory, fileName)
- ' Find the text within the document.
- If (DTE.ActiveDocument Is Nothing) Then
- ' No document to backup.
- Return
- End If
- textDocument = DTE.ActiveDocument.Object
- startPoint = textDocument.StartPoint.CreateEditPoint
- endPoint = textDocument.EndPoint.CreateEditPoint
- text = startPoint.GetText(endPoint)
- If (File.Exists(tempFullFileName)) Then
- File.Delete(tempFullFileName)
- End If
- File.WriteAllText(tempFullFileName, text)
- ' Create the temp document, save, then close.
- 'DTE.ItemOperations.NewFile("General\Text File")
- 'DTE.ActiveDocument.Object("TextDocument").Selection.Insert(text)
- 'DTE.ActiveDocument.Save(fileName)
- 'DTE.ActiveDocument.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo) 'Already saved with line above, don't need to save again.
- End Sub
- Sub AHOpenBackup()
- Dim fileName As String
- Dim startPoint As EnvDTE.EditPoint
- Dim endPoint As EnvDTE.EditPoint
- Dim textDocument As TextDocument
- Dim text As String
- Dim tempFileDirectory As String
- Dim tempFullFileName As String
- Dim td As String
- Dim dsep As String = ""
- Dim tsep As String = "."
- tempFileDirectory = VS_BACKUP_PATH
- If Not (Directory.Exists(tempFileDirectory)) Then
- Dim msg As String = "Directory " & tempFileDirectory & " doesn't exists"
- msg += Environment.NewLine & " Do you want to create?"
- Dim result As Microsoft.VisualBasic.MsgBoxResult = MsgBox(msg, MsgBoxStyle.YesNo, "???")
- If (result = MsgBoxResult.No) Then
- Return
- End If
- Directory.CreateDirectory(tempFileDirectory)
- End If
- Dim procInfo As System.Diagnostics.ProcessStartInfo = New ProcessStartInfo(tempFileDirectory)
- procInfo.UseShellExecute = True
- System.Diagnostics.Process.Start(procInfo)
- End Sub
- #End Region
- #Region "Snippets"
- Public Const SNIPPETS_DIRECTORY As String = "C:\DATA\Projects\!Code Snippets\"
- Public Const SNIPPETS_TEMPLATE_NAME As String = "AH_SNIPPET_TEMPLATE.snippet.xml"
- Sub AHOpenSnippets()
- OpenSnippets(SNIPPETS_DIRECTORY)
- End Sub
- Sub OpenSnippets(ByVal snippetsDirectory As String)
- Dim snippetsDir As String = snippetsDirectory
- Dim procInfo As System.Diagnostics.ProcessStartInfo = New ProcessStartInfo(snippetsDir)
- procInfo.WindowStyle = ProcessWindowStyle.Normal
- procInfo.UseShellExecute = True
- System.Diagnostics.Process.Start(procInfo)
- End Sub
- Sub AHCreateQuickSnippet()
- Const CSHARP_SNIPPETS_DIRECTORY As String = "C:\DATA\Projects\!Code Snippets\"
- CreateQuickSnippet("csharp", CSHARP_SNIPPETS_DIRECTORY, SNIPPETS_TEMPLATE_NAME)
- End Sub
- Sub AHCreateQuickSnippet_HTML()
- Dim tityChars As Boolean = True
- Const HTML_SNIPPETS_DIRECTORY As String = "C:\DATA\Andrew\Documents\Visual Studio 2010\Code Snippets\Visual Web Developer\My HTML Snippets"
- CreateQuickSnippet("html", HTML_SNIPPETS_DIRECTORY, SNIPPETS_TEMPLATE_NAME, tityChars)
- End Sub
- Sub AHCreateQuickSnippet_JS()
- Dim tityChars As Boolean = True
- Const JS_SNIPPETS_DIRECTORY As String = "C:\DATA\Andrew\Documents\Visual Studio 2010\Code Snippets\Visual Web Developer\My JScript Snippets"
- CreateQuickSnippet("jscript", JS_SNIPPETS_DIRECTORY, SNIPPETS_TEMPLATE_NAME, tityChars)
- End Sub
- Sub CreateQuickSnippet(ByVal language As String, ByVal snippetsDirectory As String, ByVal templateName As String, Optional ByVal tityChars As Boolean = False)
- If Not (ValidateWindow()) Then
- Return
- End If
- Dim startPoint As EnvDTE.EditPoint
- Dim endPoint As EnvDTE.EditPoint
- Dim textDocument As TextDocument
- Dim text As String
- Dim objSel As TextSelection = DTE.ActiveDocument.Selection
- startPoint = objSel.TopPoint.CreateEditPoint()
- endPoint = objSel.BottomPoint.CreateEditPoint()
- text = startPoint.GetText(endPoint)
- If (tityChars) Then
- text = text.Replace("$", "$$")
- End If
- Dim message As String = "Enter the " + language + " snippet's name you want to save as:"
- message += Environment.NewLine
- message += Environment.NewLine
- message += "(DON'T PREFIX WITH 'ah_')"
- message += Environment.NewLine
- message += Environment.NewLine
- message += "----------------------------------------"
- message += Environment.NewLine
- message += Environment.NewLine
- message += "If '$end$' is missing then '$end$' will be added to the end of the code"
- message += Environment.NewLine
- message += "'PROP_NAME' will be replaced by the literal Property Name = '$property$'"
- message += Environment.NewLine
- message += Environment.NewLine
- message += "'PROP_TYPE' will be replaced by the literal Property Type = '$type$'"
- message += Environment.NewLine
- message += Environment.NewLine
- Dim snippetName As String = InputBox(message, "Create Quick " + language + " Snippet")
- If (String.IsNullOrEmpty(snippetName)) Then
- Return
- End If
- Dim snippetFileName As String = "ah" + snippetName + ".snippet"
- Dim snippetTitle As String = "AH " + snippetName
- Dim snippetShortcut As String = "ah" + snippetName
- Dim snippetDescription As String = "Code snippet created on: " + DateTime.Now.ToString()
- Dim snippetCode As String = text
- Dim snippetEnd As String = "$end$"
- If snippetCode.Contains(snippetEnd) = False Then
- snippetCode = snippetCode + snippetEnd
- End If
- If snippetCode.Contains("PROP_NAME") = True Then
- snippetCode = snippetCode.Replace("PROP_NAME", "$property$")
- End If
- If snippetCode.Contains("PROP_TYPE") = True Then
- snippetCode = snippetCode.Replace("PROP_TYPE", "$type$")
- End If
- Dim templatePath As String = Path.Combine(snippetsDirectory, templateName)
- Dim templateText = File.ReadAllText(templatePath)
- Dim templateData As String = String.Format(templateText, snippetTitle, snippetShortcut, snippetDescription, snippetCode, language)
- Dim snippetPath As String = Path.Combine(snippetsDirectory, snippetFileName)
- If File.Exists(snippetPath) = True Then
- Dim ret As MsgBoxResult
- ret = MsgBox(snippetFileName + " already exists?" + Environment.NewLine + "Do you want to overwrite the snippet?", MsgBoxStyle.YesNo)
- If ret <> MsgBoxResult.Yes Then
- Return
- Else
- File.Delete(snippetPath)
- End If
- End If
- File.WriteAllText(snippetPath, templateData)
- End Sub
- #End Region
- #Region "Helpers"
- Private Function ValidateWindow() As Boolean
- Dim win As EnvDTE.Window
- win = DTE.ActiveWindow
- 'If win.Type <> EnvDTE.vsWindowType.vsWindowTypeDocument Then
- ' MsgBox("This macro can only be run when a text editor window is active.")
- ' Return False
- 'End If
- Return True
- End Function
- #End Region
- End Module
- #Region "Load Projects"
- 'Private Function LoadAllProjectsInSolution(ByVal solution As Solution) As List(Of Project)
- ' Dim lst As List(Of Project) = New List(Of Project)
- ' For Each prj As Project In solution.Projects
- ' lst.Add(prj)
- ' Next
- ' Return lst
- 'End Function
- 'Private Function LoadProjecItemsInProject(ByVal prj As Project) As List(Of ProjectItem)
- ' Dim lst As List(Of ProjectItem) = New List(Of ProjectItem)
- ' For Each prjItem As ProjectItem In prj.ProjectItems
- ' lst.Add(prjItem)
- ' Next
- ' Return lst
- 'End Function
- 'Private Function LoadProjectItemdataInProjectItem(ByVal prjItem As ProjectItem) As String
- ' Select Case prjItem.Kind
- ' Case EnvDTE.Constants.vsProjectItemKindMisc
- ' Return "Misc"
- ' Exit Select
- ' Case EnvDTE.Constants.vsProjectItemKindPhysicalFile
- ' Return "PhysicalFile"
- ' Exit Select
- ' Case EnvDTE.Constants.vsProjectItemKindPhysicalFolder
- ' Return "PhysicalFolder"
- ' Exit Select
- ' Case EnvDTE.Constants.vsProjectItemKindSubProject
- ' Return "SubProject"
- ' Exit Select
- ' End Select
- ' Return "???"
- 'End Function
- 'Sub AHBuildAuditTables()
- ' Dim proj As Project
- ' Dim projNames As String = ""
- ' Dim solProjects As List(Of Project) = LoadAllProjectsInSolution(DTE.Solution)
- ' For Each proj In solProjects
- ' Dim projItem As ProjectItem
- ' For Each projItem In LoadProjecItemsInProject(proj)
- ' If (projItem.Kind = EnvDTE.Constants.vsProjectItemKindPhysicalFolder) Then
- ' MsgBox(projItem.Name)
- ' End If
- ' Dim piType As String = LoadProjectItemdataInProjectItem(projItem)
- ' projNames += projItem.Name + Environment.NewLine
- ' Next
- ' Next
- ' MsgBox(projNames)
- 'End Sub
- 'Sub AHBuildAuditTables1()
- ' 'DTE.ActiveWindow.Object.GetItem("InventorySiteSolution\Database\Inventory_Database\Inventory Namespace\Audit\Generated\SQL_AUDIT_TABLES_CC_Inventory_DB.sql").Select(vsUISelectionType.vsUISelectionTypeSelect)
- ' ''DTE.Windows.Item(Constants.vsWindowKindOutput).Activate()
- ' ''DTE.ExecuteCommand("Project.Run")
- ' Dim wiz As System.Reflection.Assembly
- ' Dim wizTypeName As String = "My_Wizard_Sample.WizardSample"
- ' Dim wizType As Type
- ' Dim wizObject As Object
- ' wiz = System.Reflection.Assembly.LoadFile("C:\DATA\Downloads\_SourceCode\DTE\ExtndingWorkEnv2\Wizard Sample\bin\Debug\Wizard Sample.dll")
- ' wizType = wiz.GetType(wizTypeName)
- ' wizObject = wizType.InvokeMember("", Reflection.BindingFlags.CreateInstance, Nothing, Nothing, Nothing)
- ' 'MsgBox(wizObject.GetType().GetMethod("Go").Name)
- ' wizType.InvokeMember("Go", Reflection.BindingFlags.InvokeMethod, Nothing, wizObject, DTE.ActiveWindow.DTE)
- ' wizObject = Nothing
- ' Return
- ' 'Dim sqlItem As Object = DTE.ActiveWindow.Object.GetItem("InventorySiteSolution\Database\Inventory_Database\Inventory Namespace\Audit\Generated\SQL_AUDIT_TABLES_CC_Inventory_DB.sql")
- ' 'Dim wtf As Object = sqlItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
- ' 'MsgBox(wtf)
- ' 'DTE.Windows.Item(Constants.vsWindowKindOutput).Activate()
- ' 'DTE.ExecuteCommand("Project.Run")
- ' 'Dim p As Project
- ' 'Dim lst As List(Of Project)
- ' 'Dim projNames As String = ""
- ' 'Dim pi As ProjectItem
- ' 'For Each p In DTE.Solution.Projects
- ' ' If (p.Name = "Database") Then
- ' ' For Each pi In p.ProjectItems
- ' ' projNames += pi.Name + Environment.NewLine
- ' ' Next
- ' ' End If
- ' ' 'If (p.Name = "Inventory_Database") Then
- ' ' ' MsgBox(p.Name)
- ' ' ' Return
- ' ' 'End If
- ' 'Next
- ' 'Dim dbProject As ProjectItem
- ' 'dbProject = FindProject("Inventory_Database")
- ' 'If (dbProject Is Nothing) Then
- ' ' MsgBox("vb sucks")
- ' ' Return
- ' 'End If
- ' 'MsgBox(dbProject.)
- ' 'Return
- ' 'Dim pi As ProjectItem
- ' 'Dim projNames As String = ""
- ' 'For Each pi In dbProject.ProjectItems
- ' ' projNames += pi.Name + Environment.NewLine
- ' 'Next
- ' 'Dim proj As VCProject = DTE.ActiveSolutionProjects(0).Object
- ' 'Dim fileColl As IVCCollection = proj.Files
- ' 'Dim file As VCFile = fileColl.Item("MyFile.cpp")
- ' 'Dim projItem As ProjectItem = file.Object
- 'End Sub
- 'Private Function FindProject(ByVal projectName As String) As ProjectItem
- ' Dim p As Project
- ' Dim pi As ProjectItem
- ' For Each p In DTE.Solution.Projects
- ' For Each pi In p.ProjectItems
- ' If (pi.Name = projectName) Then
- ' Return pi
- ' End If
- ' Next
- ' Next
- 'End Function
- ''Public Sub AHMakeUpper()
- '' If Not (ValidateWindow()) Then
- '' Return
- '' End If
- '' Dim objSel As TextSelection = DTE.ActiveDocument.Selection
- '' Dim text As String
- '' text = objSel.Text
- '' objSel.Text = objSel.Text.ToUpper()
- ''End Sub
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment