andrew4582

AHMacros as of Sunday, April 03, 2011:8:17 PM

Apr 3rd, 2011
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.16 KB | None | 0 0
  1. #Region "Imports"
  2. Imports System
  3. Imports EnvDTE
  4. Imports EnvDTE80
  5. Imports EnvDTE90
  6. Imports System.Diagnostics
  7. Imports System.IO
  8. Imports System.Collections.Generic
  9. #End Region
  10.  
  11. Public Module AHMacros
  12.  
  13. #Region "Load and Save View"
  14.     Sub AHLoadView()
  15.         Dim name As String
  16.         Dim winConfi As EnvDTE.WindowConfiguration
  17.         Dim winConfigList As String = Environment.NewLine
  18.         Dim configCount As Integer
  19.         configCount = DTE.WindowConfigurations.Count
  20.  
  21.         For I As Integer = 1 To DTE.WindowConfigurations.Count
  22.             Try
  23.                 winConfi = DTE.WindowConfigurations.Item(I)
  24.                 winConfigList += I.ToString() + " " + winConfi.Name + Environment.NewLine
  25.             Catch ex As Exception
  26.             End Try
  27.         Next
  28.  
  29.         name = InputBox("Which window layout would you like to load, Enter the Number?" + winConfigList, "Load Window Layout")
  30.         If (name <> "") Then
  31.             Dim index As Integer = Integer.Parse(name)
  32.             Dim configName As String = DTE.WindowConfigurations.Item(index).Name
  33.             DTE.WindowConfigurations.Item(configName).Apply()
  34.         End If
  35.     End Sub
  36.     Sub AHSaveView()
  37.         Dim name As String
  38.  
  39.         name = InputBox("Enter the name you want to save as:", "Save window layout")
  40.         If (name = "") Then
  41.             MsgBox("Empty string, enter a valid name.")
  42.         Else
  43.             DTE.WindowConfigurations.Add(name)
  44.         End If
  45.     End Sub
  46. #End Region
  47.  
  48. #Region "Code Editor Formating"
  49.     Public Sub AHWrapRegion()
  50.  
  51.         If Not (ValidateWindow()) Then
  52.             Return
  53.         End If
  54.  
  55.         Dim startPoint As EnvDTE.EditPoint
  56.         Dim endPoint As EnvDTE.EditPoint
  57.         Dim textDocument As TextDocument
  58.         Dim regionStart As String = Environment.NewLine & "#region New Region" & Environment.NewLine
  59.         Dim regionEnd As String = Environment.NewLine & "#endregion" & Environment.NewLine
  60.         Dim objSel As TextSelection = DTE.ActiveDocument.Selection
  61.         Dim StartLine As Integer = DTE.ActiveDocument.Selection.TopLine
  62.         Dim EndLine As Integer = DTE.ActiveDocument.Selection.BottomLine
  63.  
  64.         startPoint = objSel.TopPoint.CreateEditPoint()
  65.         endPoint = objSel.BottomPoint.CreateEditPoint()
  66.         startPoint.Insert(regionStart)
  67.         endPoint.Insert(regionEnd)
  68.  
  69.         objSel.FindText("New Region", vsFindOptions.vsFindOptionsFromStart)
  70.     End Sub
  71.     Public Sub AHCleanDoubleLinesSelection()
  72.         If Not (ValidateWindow()) Then
  73.             Return
  74.         End If
  75.  
  76.         Dim startPoint As EnvDTE.EditPoint
  77.         Dim endPoint As EnvDTE.EditPoint
  78.         Dim textDocument As TextDocument
  79.         Dim text As String
  80.         Dim objSel As TextSelection = DTE.ActiveDocument.Selection
  81.  
  82.         startPoint = objSel.TopPoint.CreateEditPoint()
  83.         endPoint = objSel.BottomPoint.CreateEditPoint()
  84.         text = startPoint.GetText(endPoint)
  85.         startPoint.LineUp(1)
  86.  
  87.  
  88.         Dim whatToReplace As String = Environment.NewLine & Environment.NewLine
  89.         Dim whatToReplace1 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine
  90.         Dim whatToReplace2 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
  91.         Dim whatToReplace3 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
  92.  
  93.         Dim whatToReplaceWith As String = Environment.NewLine
  94.         text = text.Replace(whatToReplace3, whatToReplaceWith)
  95.         text = text.Replace(whatToReplace2, whatToReplaceWith)
  96.         text = text.Replace(whatToReplace1, whatToReplaceWith)
  97.         text = text.Replace(whatToReplace, whatToReplaceWith)
  98.         startPoint.ReplaceText(endPoint, text, EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)
  99.     End Sub
  100.     Public Sub AHCleanDoubleLines()
  101.         If Not (ValidateWindow()) Then
  102.             Return
  103.         End If
  104.  
  105.         Dim startPoint As EnvDTE.EditPoint
  106.         Dim endPoint As EnvDTE.EditPoint
  107.         Dim textDocument As TextDocument
  108.         Dim text As String
  109.  
  110.         textDocument = DTE.ActiveDocument.Object
  111.         startPoint = textDocument.StartPoint.CreateEditPoint
  112.         endPoint = textDocument.EndPoint.CreateEditPoint
  113.         text = startPoint.GetText(endPoint)
  114.  
  115.         Dim whatToReplace As String = Environment.NewLine & Environment.NewLine
  116.         Dim whatToReplace1 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine
  117.         Dim whatToReplace2 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
  118.         Dim whatToReplace3 As String = Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine
  119.         Dim whatToReplaceWith As String = Environment.NewLine
  120.  
  121.         text = text.Replace(whatToReplace3, whatToReplaceWith)
  122.         text = text.Replace(whatToReplace2, whatToReplaceWith)
  123.         text = text.Replace(whatToReplace1, whatToReplaceWith)
  124.         text = text.Replace(whatToReplace, whatToReplaceWith)
  125.         startPoint.ReplaceText(endPoint, text, EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)
  126.     End Sub
  127. #End Region
  128.  
  129. #Region "Code File Backup"
  130.     Public Const VS_BACKUP_PATH As String = "C:\DATA\temp\__VSBACKUP"
  131.  
  132.     Sub AHSaveBackup()
  133.         If Not (ValidateWindow()) Then
  134.             Return
  135.         End If
  136.         Dim fileName As String
  137.         Dim startPoint As EnvDTE.EditPoint
  138.         Dim endPoint As EnvDTE.EditPoint
  139.         Dim textDocument As TextDocument
  140.         Dim text As String
  141.         Dim tempFileDirectory As String
  142.         Dim tempFullFileName As String
  143.         Dim td As String
  144.         Dim dsep As String = ""
  145.         Dim tsep As String = "."
  146.  
  147.         tempFileDirectory = VS_BACKUP_PATH
  148.  
  149.         If Not (Directory.Exists(tempFileDirectory)) Then
  150.             Dim msg As String = "Directory " & tempFileDirectory & " doesn't exists"
  151.             msg += Environment.NewLine & " Do you want to create?"
  152.             Dim result As Microsoft.VisualBasic.MsgBoxResult = MsgBox(msg, MsgBoxStyle.YesNo, "???")
  153.             If (result = MsgBoxResult.No) Then
  154.                 Return
  155.             End If
  156.             Directory.CreateDirectory(tempFileDirectory)
  157.         End If
  158.         fileName = DTE.ActiveDocument.Name ' + ".bak"
  159.  
  160.        'td = DateAndTime.Today.Month.ToString() & dsep + DateAndTime.Today.Day.ToString() & dsep & DateAndTime.Today.Year.ToString()
  161.         ' td += "_" & Now.Hour.ToString() & tsep & Now.Minute.ToString() & tsep & Now.Second.ToString()
  162.        td = "_" & DateAndTime.Now.ToString().Replace("/", "").Replace(":", ".")
  163.        Dim ext As String = Path.GetExtension(fileName)
  164.        fileName = fileName.Replace(ext, "") & "_" & td & ext
  165.        tempFullFileName = Path.Combine(tempFileDirectory, fileName)
  166.        ' Find the text within the document.
  167.         If (DTE.ActiveDocument Is Nothing) Then
  168.             ' No document to backup.
  169.            Return
  170.        End If
  171.  
  172.        textDocument = DTE.ActiveDocument.Object
  173.        startPoint = textDocument.StartPoint.CreateEditPoint
  174.        endPoint = textDocument.EndPoint.CreateEditPoint
  175.        text = startPoint.GetText(endPoint)
  176.  
  177.        If (File.Exists(tempFullFileName)) Then
  178.            File.Delete(tempFullFileName)
  179.        End If
  180.        File.WriteAllText(tempFullFileName, text)
  181.        ' Create the temp document, save, then close.
  182.         'DTE.ItemOperations.NewFile("General\Text File")
  183.        'DTE.ActiveDocument.Object("TextDocument").Selection.Insert(text)
  184.         'DTE.ActiveDocument.Save(fileName)
  185.        'DTE.ActiveDocument.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo) 'Already saved with line above, don't need to save again.
  186.     End Sub
  187.     Sub AHOpenBackup()
  188.  
  189.         Dim fileName As String
  190.         Dim startPoint As EnvDTE.EditPoint
  191.         Dim endPoint As EnvDTE.EditPoint
  192.         Dim textDocument As TextDocument
  193.         Dim text As String
  194.         Dim tempFileDirectory As String
  195.         Dim tempFullFileName As String
  196.         Dim td As String
  197.         Dim dsep As String = ""
  198.         Dim tsep As String = "."
  199.  
  200.         tempFileDirectory = VS_BACKUP_PATH
  201.         If Not (Directory.Exists(tempFileDirectory)) Then
  202.             Dim msg As String = "Directory " & tempFileDirectory & " doesn't exists"
  203.             msg += Environment.NewLine & " Do you want to create?"
  204.             Dim result As Microsoft.VisualBasic.MsgBoxResult = MsgBox(msg, MsgBoxStyle.YesNo, "???")
  205.             If (result = MsgBoxResult.No) Then
  206.                 Return
  207.             End If
  208.             Directory.CreateDirectory(tempFileDirectory)
  209.         End If
  210.  
  211.         Dim procInfo As System.Diagnostics.ProcessStartInfo = New ProcessStartInfo(tempFileDirectory)
  212.         procInfo.UseShellExecute = True
  213.         System.Diagnostics.Process.Start(procInfo)
  214.     End Sub
  215. #End Region
  216.  
  217. #Region "Snippets"
  218.     Public Const SNIPPETS_DIRECTORY As String = "C:\DATA\Projects\!Code Snippets\"
  219.    Public Const SNIPPETS_TEMPLATE_NAME As String = "AH_SNIPPET_TEMPLATE.snippet.xml"
  220.  
  221.    Sub AHOpenSnippets()
  222.        OpenSnippets(SNIPPETS_DIRECTORY)
  223.    End Sub
  224.  
  225.    Sub OpenSnippets(ByVal snippetsDirectory As String)
  226.        Dim snippetsDir As String = snippetsDirectory
  227.        Dim procInfo As System.Diagnostics.ProcessStartInfo = New ProcessStartInfo(snippetsDir)
  228.        procInfo.WindowStyle = ProcessWindowStyle.Normal
  229.        procInfo.UseShellExecute = True
  230.        System.Diagnostics.Process.Start(procInfo)
  231.    End Sub
  232.  
  233.    Sub AHCreateQuickSnippet()
  234.        Const CSHARP_SNIPPETS_DIRECTORY As String = "C:\DATA\Projects\!Code Snippets\"
  235.        
  236.        CreateQuickSnippet("csharp", CSHARP_SNIPPETS_DIRECTORY, SNIPPETS_TEMPLATE_NAME)
  237.    End Sub
  238.  
  239.    Sub AHCreateQuickSnippet_HTML()
  240.        Dim tityChars As Boolean = True
  241.  
  242.        Const HTML_SNIPPETS_DIRECTORY As String = "C:\DATA\Andrew\Documents\Visual Studio 2010\Code Snippets\Visual Web Developer\My HTML Snippets"
  243.  
  244.        CreateQuickSnippet("html", HTML_SNIPPETS_DIRECTORY, SNIPPETS_TEMPLATE_NAME, tityChars)
  245.    End Sub
  246.  
  247.    Sub AHCreateQuickSnippet_JS()
  248.        Dim tityChars As Boolean = True
  249.  
  250.        Const JS_SNIPPETS_DIRECTORY As String = "C:\DATA\Andrew\Documents\Visual Studio 2010\Code Snippets\Visual Web Developer\My JScript Snippets"
  251.  
  252.        CreateQuickSnippet("jscript", JS_SNIPPETS_DIRECTORY, SNIPPETS_TEMPLATE_NAME, tityChars)
  253.    End Sub
  254.  
  255.  
  256.    Sub CreateQuickSnippet(ByVal language As String, ByVal snippetsDirectory As String, ByVal templateName As String, Optional ByVal tityChars As Boolean = False)
  257.  
  258.        If Not (ValidateWindow()) Then
  259.            Return
  260.        End If
  261.  
  262.        Dim startPoint As EnvDTE.EditPoint
  263.        Dim endPoint As EnvDTE.EditPoint
  264.        Dim textDocument As TextDocument
  265.        Dim text As String
  266.        Dim objSel As TextSelection = DTE.ActiveDocument.Selection
  267.        
  268.        startPoint = objSel.TopPoint.CreateEditPoint()
  269.        endPoint = objSel.BottomPoint.CreateEditPoint()
  270.        text = startPoint.GetText(endPoint)
  271.  
  272.        If (tityChars) Then
  273.            text = text.Replace("$", "$$")
  274.        End If
  275.  
  276.        Dim message As String = "Enter the " + language + " snippet's name you want to save as:"
  277.        message += Environment.NewLine
  278.        message += Environment.NewLine
  279.        message += "(DON'T PREFIX WITH 'ah_')"
  280.        message += Environment.NewLine
  281.        message += Environment.NewLine
  282.        message += "----------------------------------------"
  283.        message += Environment.NewLine
  284.        message += Environment.NewLine
  285.        message += "If '$end$' is missing then '$end$' will be added to the end of the code"
  286.        message += Environment.NewLine
  287.        message += "'PROP_NAME' will be replaced by the literal Property Name = '$property$'"
  288.        message += Environment.NewLine
  289.        message += Environment.NewLine
  290.        message += "'PROP_TYPE' will be replaced by the literal Property Type = '$type$'"
  291.        message += Environment.NewLine
  292.        message += Environment.NewLine
  293.  
  294.        Dim snippetName As String = InputBox(message, "Create Quick " + language + " Snippet")
  295.        If (String.IsNullOrEmpty(snippetName)) Then
  296.            Return
  297.        End If
  298.  
  299.  
  300.        Dim snippetFileName As String = "ah" + snippetName + ".snippet"
  301.        Dim snippetTitle As String = "AH " + snippetName
  302.        Dim snippetShortcut As String = "ah" + snippetName
  303.        Dim snippetDescription As String = "Code snippet created on: " + DateTime.Now.ToString()
  304.        Dim snippetCode As String = text
  305.        Dim snippetEnd As String = "$end$"
  306.  
  307.        If snippetCode.Contains(snippetEnd) = False Then
  308.            snippetCode = snippetCode + snippetEnd
  309.        End If
  310.        If snippetCode.Contains("PROP_NAME") = True Then
  311.            snippetCode = snippetCode.Replace("PROP_NAME", "$property$")
  312.        End If
  313.        If snippetCode.Contains("PROP_TYPE") = True Then
  314.            snippetCode = snippetCode.Replace("PROP_TYPE", "$type$")
  315.        End If
  316.  
  317.        Dim templatePath As String = Path.Combine(snippetsDirectory, templateName)
  318.        Dim templateText = File.ReadAllText(templatePath)
  319.        Dim templateData As String = String.Format(templateText, snippetTitle, snippetShortcut, snippetDescription, snippetCode, language)
  320.  
  321.        Dim snippetPath As String = Path.Combine(snippetsDirectory, snippetFileName)
  322.        If File.Exists(snippetPath) = True Then
  323.            Dim ret As MsgBoxResult
  324.            ret = MsgBox(snippetFileName + " already exists?" + Environment.NewLine + "Do you want to overwrite the snippet?", MsgBoxStyle.YesNo)
  325.            If ret <> MsgBoxResult.Yes Then
  326.                Return
  327.            Else
  328.                File.Delete(snippetPath)
  329.            End If
  330.        End If
  331.        File.WriteAllText(snippetPath, templateData)
  332.    End Sub
  333.  
  334. #End Region
  335.  
  336. #Region "Helpers"
  337.    Private Function ValidateWindow() As Boolean
  338.        Dim win As EnvDTE.Window
  339.        win = DTE.ActiveWindow
  340.        'If win.Type <> EnvDTE.vsWindowType.vsWindowTypeDocument Then
  341.        '    MsgBox("This macro can only be run when a text editor window is active.")
  342.        '    Return False
  343.        'End If
  344.        Return True
  345.    End Function
  346. #End Region
  347.  
  348. End Module
  349.  
  350. #Region "Load Projects"
  351.  
  352. 'Private Function LoadAllProjectsInSolution(ByVal solution As Solution) As List(Of Project)
  353. '    Dim lst As List(Of Project) = New List(Of Project)
  354. '    For Each prj As Project In solution.Projects
  355. '        lst.Add(prj)
  356. '    Next
  357. '    Return lst
  358. 'End Function
  359. 'Private Function LoadProjecItemsInProject(ByVal prj As Project) As List(Of ProjectItem)
  360. '    Dim lst As List(Of ProjectItem) = New List(Of ProjectItem)
  361. '    For Each prjItem As ProjectItem In prj.ProjectItems
  362. '        lst.Add(prjItem)
  363. '    Next
  364. '    Return lst
  365. 'End Function
  366. 'Private Function LoadProjectItemdataInProjectItem(ByVal prjItem As ProjectItem) As String
  367. '    Select Case prjItem.Kind
  368. '        Case EnvDTE.Constants.vsProjectItemKindMisc
  369. '            Return "Misc"
  370. '            Exit Select
  371. '        Case EnvDTE.Constants.vsProjectItemKindPhysicalFile
  372. '            Return "PhysicalFile"
  373. '            Exit Select
  374. '        Case EnvDTE.Constants.vsProjectItemKindPhysicalFolder
  375. '            Return "PhysicalFolder"
  376. '            Exit Select
  377. '        Case EnvDTE.Constants.vsProjectItemKindSubProject
  378. '            Return "SubProject"
  379. '            Exit Select
  380. '    End Select
  381. '    Return "???"
  382. 'End Function
  383. 'Sub AHBuildAuditTables()
  384. '    Dim proj As Project
  385. '    Dim projNames As String = ""
  386. '    Dim solProjects As List(Of Project) = LoadAllProjectsInSolution(DTE.Solution)
  387. '    For Each proj In solProjects
  388. '        Dim projItem As ProjectItem
  389. '        For Each projItem In LoadProjecItemsInProject(proj)
  390. '            If (projItem.Kind = EnvDTE.Constants.vsProjectItemKindPhysicalFolder) Then
  391. '                MsgBox(projItem.Name)
  392. '            End If
  393. '            Dim piType As String = LoadProjectItemdataInProjectItem(projItem)
  394. '            projNames += projItem.Name + Environment.NewLine
  395.  
  396. '        Next
  397.  
  398. '    Next
  399. '    MsgBox(projNames)
  400. 'End Sub
  401. 'Sub AHBuildAuditTables1()
  402. '    'DTE.ActiveWindow.Object.GetItem("InventorySiteSolution\Database\Inventory_Database\Inventory Namespace\Audit\Generated\SQL_AUDIT_TABLES_CC_Inventory_DB.sql").Select(vsUISelectionType.vsUISelectionTypeSelect)
  403. '    ''DTE.Windows.Item(Constants.vsWindowKindOutput).Activate()
  404. '    ''DTE.ExecuteCommand("Project.Run")
  405. '    Dim wiz As System.Reflection.Assembly
  406. '    Dim wizTypeName As String = "My_Wizard_Sample.WizardSample"
  407. '    Dim wizType As Type
  408. '    Dim wizObject As Object
  409.  
  410.  
  411. '    wiz = System.Reflection.Assembly.LoadFile("C:\DATA\Downloads\_SourceCode\DTE\ExtndingWorkEnv2\Wizard Sample\bin\Debug\Wizard Sample.dll")
  412. '    wizType = wiz.GetType(wizTypeName)
  413.  
  414.  
  415. '    wizObject = wizType.InvokeMember("", Reflection.BindingFlags.CreateInstance, Nothing, Nothing, Nothing)
  416. '    'MsgBox(wizObject.GetType().GetMethod("Go").Name)
  417. '    wizType.InvokeMember("Go", Reflection.BindingFlags.InvokeMethod, Nothing, wizObject, DTE.ActiveWindow.DTE)
  418. '    wizObject = Nothing
  419.  
  420. '    Return
  421.  
  422. '    'Dim sqlItem As Object = DTE.ActiveWindow.Object.GetItem("InventorySiteSolution\Database\Inventory_Database\Inventory Namespace\Audit\Generated\SQL_AUDIT_TABLES_CC_Inventory_DB.sql")
  423. '    'Dim wtf As Object = sqlItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
  424. '    'MsgBox(wtf)
  425.  
  426. '    'DTE.Windows.Item(Constants.vsWindowKindOutput).Activate()
  427. '    'DTE.ExecuteCommand("Project.Run")
  428. '    'Dim p As Project
  429. '    'Dim lst As List(Of Project)
  430. '    'Dim projNames As String = ""
  431. '    'Dim pi As ProjectItem
  432.  
  433. '    'For Each p In DTE.Solution.Projects
  434. '    '    If (p.Name = "Database") Then
  435. '    '        For Each pi In p.ProjectItems
  436. '    '            projNames += pi.Name + Environment.NewLine
  437. '    '        Next
  438. '    '    End If
  439.  
  440. '    '    'If (p.Name = "Inventory_Database") Then
  441. '    '    '    MsgBox(p.Name)
  442. '    '    '    Return
  443. '    '    'End If
  444. '    'Next
  445. '    'Dim dbProject As ProjectItem
  446. '    'dbProject = FindProject("Inventory_Database")
  447. '    'If (dbProject Is Nothing) Then
  448. '    '    MsgBox("vb sucks")
  449. '    '    Return
  450. '    'End If
  451. '    'MsgBox(dbProject.)
  452. '    'Return
  453.  
  454. '    'Dim pi As ProjectItem
  455. '    'Dim projNames As String = ""
  456. '    'For Each pi In dbProject.ProjectItems
  457. '    '    projNames += pi.Name + Environment.NewLine
  458. '    'Next
  459. '    'Dim proj As VCProject = DTE.ActiveSolutionProjects(0).Object
  460. '    'Dim fileColl As IVCCollection = proj.Files
  461. '    'Dim file As VCFile = fileColl.Item("MyFile.cpp")
  462. '    'Dim projItem As ProjectItem = file.Object
  463.  
  464.  
  465. 'End Sub
  466. 'Private Function FindProject(ByVal projectName As String) As ProjectItem
  467. '    Dim p As Project
  468. '    Dim pi As ProjectItem
  469.  
  470. '    For Each p In DTE.Solution.Projects
  471. '        For Each pi In p.ProjectItems
  472. '            If (pi.Name = projectName) Then
  473. '                Return pi
  474. '            End If
  475. '        Next
  476. '    Next
  477. 'End Function
  478. ''Public Sub AHMakeUpper()
  479. ''    If Not (ValidateWindow()) Then
  480. ''        Return
  481. ''    End If
  482.  
  483. ''    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
  484. ''    Dim text As String
  485. ''    text = objSel.Text
  486. ''    objSel.Text = objSel.Text.ToUpper()
  487.  
  488. ''End Sub
  489.  
  490. #End Region
Advertisement
Add Comment
Please, Sign In to add comment