Wednesday, September 23, 2009

Customize how search results are displayed

I created a simple utility that can customize the Visual Studio Find Results output. I think using it should be pretty self-explanatory..

VSFindResultsEditor

I wanted something new to do this morning, what can I say!?

Download it here!

 

You can also check out Sara Ford’s blog for all the glorious details about customizing the Visual Studio Find Results output:

http://blogs.msdn.com/saraford/archive/2008/11/24/did-you-know-you-can-customize-how-search-results-are-displayed-in-the-find-results-window-363.aspx

Excellent Visual Studio tips roundup by Sara Ford

http://blogs.msdn.com/saraford/archive/2009/09/09/teched-australia-25-visual-studio-2008-ide-tips.aspx

 

The ones I found most useful (or didn’t already know) include:

Prevent copying blank lines with Ctrl+C

Look in Tools - Options - Text Editor - All Languages - General. Unchecking 'Apply Cut or Copy commands to blank lines when there is no selection' prevents copying a blank line into the clipboard.  How did I ever miss this checkbox!?

 

Incremental Search

This allows me to jump to the next match as I type it out. I LOVE it!!

   1. Press Ctrl+I
   2. Start typing the text you are searching for.  note: you'll see the cursor jump to the first match, highlighting the current search string.
   3. Press Ctrl+I again to jump to the next occurrence of the search string
   4. (advanced tip) Press Ctrl+Shift+I to search backwards
   5. To stop searching, press ESC

You’ll note that it is case-sensitive, unfortunately. I’ll have to look for a way to change that..

 

And, my total favorite is..

Customize how search results are displayed

This is one of the most irritating things ever about Visual Studio. The Find Results window is almost always filled with only the paths of the found files, instead of the line details. I always have to scroll to the right to see the details. Here is the direct link to the article, bypassing the roundup.

http://blogs.msdn.com/saraford/archive/2008/11/24/did-you-know-you-can-customize-how-search-results-are-displayed-in-the-find-results-window-363.aspx

Friday, September 18, 2009

Build macro for web projects

Using this macro and batch file you can map a keyboard shortcut and/or a toolbar button in Visual Studio to restart IIS and build the selected project asynchronously.

1. Create a batch file that includes the following:

@echo off 
net stop w3svc
net start w3svc


For projects that tend to get hung up more frequently in IIS, you can replace the 'net stop' line above with net 'stop iisadmin' instead. (Don’t change the ‘net start’ line.)



2. Create a new macro project that includes events (or add a new module in existing macro project).



3. Paste the following code into the module, inserting the path to the new batch file:



Sub BuildSelectedWebProject() 
' restart iis
Shell("<your batch file path>", AppWinStyle.MinimizedNoFocus, False)
' start the build
DTE.ExecuteCommand("Build.BuildSelection")
End Sub


4. Map a keyboard shortcut or toolbar button to the new macro: Macros.<Macro Project Name>.BuildSelectedWebProject



 



Build Solution



Optionally, you can also re-map Ctrl+Shift+B (Build Solution) to the following if desired:



Sub BuildSolution() 
' restart iis
Shell("<your batch file path>\iisrestart.bat", AppWinStyle.MinimizedNoFocus, False)
' start the build
DTE.ExecuteCommand("Build.BuildSolution")
End Sub

Visual Studio 2008 Macro Fun

Here are a few macros that I use, that I thought might be of use to others. They should be fairly easy to set up in your environment. I’m always excited to hear about new macros and plugins and other time-saving functionality for Visual Studio, so if you have any – please let me know!!

Misc Built-in Macros

Here are some basic macros that came with Visual Studio 2008 (and VS 2005) that are very useful and easily map to your favorite keyboard shortcut(s).

Macros.Samples.Accessibility.IncreaseTextEditorFontSize
Macros.Samples.Accessibility.DecreaseTextEditorFontSize
Macros.Samples.Accessibility.UpdateTextEditorFontSizeToMatchDisplayProperties
Macros.Samples.Utilities.TurnOnLineNumbers
Macros.Samples.Utilities.TurnOffLineNumbers
Macros.Samples.VSEditor.BeginningOfFunction
Macros.Samples.VSEditor.LineEmUp (Good for aligning variable assignments)
Macros.Samples.DevStudio6Editor.AutoCompleteFromFile


Format Source Code



I map F2 to the standard Edit.FormatDocument command and Ctrl+F2 to the FormatCSharp macro method.



Public Sub FormatCSharp()

Try
' Remove and sort using statements if PowerCommands is installed.
DTE.ExecuteCommand("Edit.RemoveAndSort")
Catch ex As Exception

End Try

Try
DTE.ExecuteCommand("Macros.Samples.VSEditor.FixLineEnds")
Catch ex As Exception
Exit Sub
End Try

DTE.ExecuteCommand("Edit.FormatDocument")

End Sub






Attach to IIS Worker Processes



I map Ctrl+P to this macro, replacing the default Print Current Document shortcut.



Sub MacroAttachToAllWebProcesses() 
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(3) As EnvDTE80.Engine

dbgeng(0) = trans.Engines.Item("T-SQL")
dbgeng(1) = trans.Engines.Item("T-SQL")
dbgeng(2) = trans.Engines.Item("Managed")

' Use . for local machine or type machine name
For Each theProcess As EnvDTE80.Process2 In dbg2.GetProcesses(trans, ".")
If theProcess.Name.Contains("aspnet_wp.exe") Or theProcess.Name.Contains("w3wp.exe") Then
theProcess.Attach2(dbgeng)
End If
Next

Catch ex As System.Exception
MsgBox(ex.Message)
End Try

End Sub


Solution Events



' Automatically close all documents when the solution is closed.
Private Sub SolutionEvents_BeforeClosing() Handles SolutionEvents.BeforeClosing
DTE.ExecuteCommand("Window.CloseAllDocuments")
End Sub


Build Events



' A project failed to compile, so cancel remaining and beep
Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone
If Success = False Then
DTE.ExecuteCommand("Build.Cancel")
' System.Windows.Forms.MessageBox.Show("Build failed!")
Beep()
Threading.Thread.Sleep(500)
Beep()
Threading.Thread.Sleep(500)
Beep()
Threading.Thread.Sleep(500)
Beep()
End If
End Sub

' The build finished successfully
Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
If (Not failed) Then
' System.Windows.Forms.MessageBox.Show("Build is complete!")
Beep()
Threading.Thread.Sleep(250)
Beep()
End If
End Sub

Tuesday, July 21, 2009

Simple Visual Studio 2005 regular expression to swap variable assignments

Here is a simple regular expression to use in Visual Studio 2005 to swap the variable/object and assignment.

Find What: mySettings.{.+} = {.+};
Replace With: \2 = mySettings.\1;

Thursday, July 16, 2009

Collapse folders and projects in Visual Studio

I consider the PowerCommands for Visual Studio 2008 invaluable. And, the collapse all feature is certainly one of my most used features. However, if a solution folder is already collapsed, the projects underneath are skipped.

So, I found a great macro for collapsing all solution folders and their projects, regardless of whether or not the parent solution folder(s) are open or closed.

Visual Studio 2005 Collapse All Macro

(yes, it works for Visual Studio 2008, what I primarily use)

 

For convenience, here is the macro source code:

    Sub CollapseAll()

' Get the the Solution Explorer tree
Dim solutionExplorer As UIHierarchy
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

' Check if there is any open solution
If (solutionExplorer.UIHierarchyItems.Count = 0) Then
Return
End If

' Get the top node (the name of the solution)
Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)
rootNode.DTE.SuppressUI = True

' Collapse each project node
Collapse(rootNode, solutionExplorer)

' Select the solution node, or else when you click
' on the solution window
' scrollbar, it will synchronize the open document
' with the tree and pop
' out the corresponding node which is probably not what you want.

rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI = False

End Sub

Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)

For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then

' Re-cursive call
Collapse(innerItem, solutionExplorer)

' Collapse
If innerItem.UIHierarchyItems.Expanded Then
innerItem.UIHierarchyItems.Expanded = False
If innerItem.UIHierarchyItems.Expanded = True Then
' Bug in VS 2005
innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
solutionExplorer.DoDefaultAction()
End If
End If

End If
Next

End Sub

Tuesday, January 13, 2009

SCRUM vs. Waterfall

I didn’t realize how frustrating it would be to take a new job that doesn’t do SCRUM. I was told it was an iterative development process. But, honestly, it’s more like old-fashioned waterfall than anything else.

I’ve had a few discussions revolving around development methodologies but there is nothing except anti-sentiment and near resentment for even bringing up any way to do things better! What’s the deal?

Why is it so hard for people to even consider trying it or even spending a couple lunches doing some practice exercises?

I guarantee it’s what they want. They just don’t know it.