does anyone know of a tool or extension to Visual Studio 2010 to count non-whitespace (e.g. all characters but not spaces, new lines etc.) for current selection in a document?
Nice to have for code golfing :)
I have a command line tool, but an integrated tool would be very nice. Also I would prefer something to evaluate current selection.
I finally got to creating this crude macro below by first recording a temporary macro in Visual Studio and then modifying it to look like the below:
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module CountNonWhiteSpaceCharacters
Sub Count()
Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection()
Dim text As String = selection.Text
text = text.Replace(" ", "")
text = text.Replace(vbCrLf, "")
text = text.Replace(vbTab, "")
MsgBox("Count " + text.Length.ToString())
End Sub
End Module
This can be bound to a keybord shortcut if desired. Otherwise, double clicking it in Macro Explorer will run it.
Related
This question was answered before for a previous version of Visual Studio (VS). The offered solutions involved macros, which are no longer available in VS 2015. Could I get a solution for VS 2015?
I would like to do a "find all" in VS and put a debug-breakpoint on every line with a find match.
Link to previous question asked by Noah:
How do I add Debug Breakpoints to lines displayed in a "Find Results" window in Visual Studio
I've converted the old macro to a VB command in Visual Commander (by adding explicit namespaces to classes):
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
Dim findResultsWindow As EnvDTE.Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindFindResults1)
Dim selection As EnvDTE.TextSelection
selection = findResultsWindow.Selection
selection.SelectAll()
Dim findResultsReader As New System.IO.StringReader(selection.Text)
Dim findResult As String = findResultsReader.ReadLine()
Dim findResultRegex As New System.Text.RegularExpressions.Regex("(?<Path>.*?)\((?<LineNumber>\d+)\):")
While Not findResult Is Nothing
Dim findResultMatch As System.Text.RegularExpressions.Match = findResultRegex.Match(findResult)
If findResultMatch.Success Then
Dim path As String = findResultMatch.Groups.Item("Path").Value
Dim lineNumber As Integer = Integer.Parse(findResultMatch.Groups.Item("LineNumber").Value)
Try
DTE.Debugger.Breakpoints.Add("", path, lineNumber)
Catch ex As System.Exception
' breakpoints can't be added everywhere
End Try
End If
findResult = findResultsReader.ReadLine()
End While
End Sub
End Class
If you have JetBrains Resharper and use one of Reharper's search commands, you can do this directly from Resharper's Find Results window (different from VS Find Results).
Example:
Resharper > Navigate > Go to Text... (Ctrl+T,T,T if using Resharper key map)
And then in Find Results (Resharper), right click any node or container in the tree view and choose "Set Breakpoint." This sets a breakpoint on all sub-nodes.
Reference:
https://blog.jetbrains.com/dotnet/2017/12/04/debugger-features-resharper/
While debugging in Visual Studio is there any way to find all pointers pointing to a particular address if it is known that they are within the present scope ?
For VS2010, you can use a macro.
Open Macro window by clicking "Tools -> Macros -> Macro IDE".
Copy and paste the following macro
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module Module1
Sub DumpLocals()
Dim outputWindow As EnvDTE.OutputWindow
Dim address As String = "0x009efedc"
outputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object
Dim currentStackFrame As EnvDTE.StackFrame
currentStackFrame = DTE.Debugger.CurrentStackFrame
outputWindow.ActivePane.OutputString("*Dumping Local Variables*" + vbCrLf)
For Each exp As EnvDTE.Expression In currentStackFrame.Locals
If exp.Value = address Then
outputWindow.ActivePane.OutputString("Match: " + exp.Name + " = " + exp.Value.ToString() + vbCrLf)
End If
Next
End Sub
End Module
Set a breakpoint in your C++ program where you want to list the pointers.
Run your C++ program.
When the breakpoint is hit
Go back to the Macro window.
Change the value of the address variable in the macro. Note that you may need to remove the "0x" if you are not using hex.
Press Ctrl+S to save the macro.
While still in the Macro window, press F5 to run the macro. The result will be in the Output window (Debug -> Windows -> Output).
Maybe you can even add parameter to the macro and call it from Immediate Window.
P/S: This macro is modified from http://weblogs.asp.net/scottgu/archive/2010/08/18/debugging-tips-with-visual-studio-2010.aspx
Err, I have another easier solution for you. When you hit a breakpoint:
Open the Locals window (Debug -> Windows -> Locals).
Press Ctrl+A to select everything in Locals window.
Press Ctrl+C to copy
Paste them into Excel
Sort the Value column in Excel.
Look for the matched pointers.
I do WinForms and Web development in Visual Studio 2010. The web developers use a tab size of four spaces, and the WinForms developers use a tab size of two.
Since I switch backwards and forwards between the two, I was wondering if there's a quick way to do it, without having to navigate the options every single time?
There's not a dedicated shortcut, but let's make one!
Simply hit Alt+F11 to bring up the Macros editor and add the following code to a new module:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module Tabspaces
Sub TwoTabSpaces()
Dim tabs As Properties
tabs = DTE.Properties("TextEditor", "AllLanguages")
tabs.Item("TabSize").Value = 2
tabs.Item("IndentSize").Value = 2
End Sub
Sub FourTabSpaces()
Dim tabs As Properties
tabs = DTE.Properties("TextEditor", "AllLanguages")
tabs.Item("TabSize").Value = 4
tabs.Item("IndentSize").Value = 4
End Sub
End Module
Then save your macros and bring up the Options window from the Tools menu and choose Environment->Keyboard:
Here, you can search for commands containing 'macros' and you should see your TwoTabSpaces() and FourTabSpaces() listed.
You can now assign a keyboard shortcut to these macros. In the picture above, I've chosen Alt+T, Alt+2 and Alt+T, Alt+4 respectively (t=Tabs, 2=two spaces etc.)
You can customize them as you wish, of course.
I thought I'd share my personal macro that does a toggle based on language. You can set up keyboard shortcuts like suggested in Widor's answer.
Public Sub ToggleSpaces()
If DTE.ActiveDocument.Language = "Basic" Then
Dim textEditorSettings = DTE.Properties("TextEditor", "Basic")
Dim currentSize = CShort(textEditorSettings.Item("TabSize").Value)
'If at 2, use 4... else switch to 2
Dim newSpaces As Short = If(currentSize = 2, 4, 2)
textEditorSettings.Item("TabSize").Value = newSpaces
textEditorSettings.Item("IndentSize").Value = newSpaces
End If
End Sub
My macro was adapted from James Alexander's macro to "Toggle Between Leading Tabs or Spaces Per Project" found in the following StackOverflow question.
What is the easiest way to programmatically force capitalization of keywords in Visual Studio 2008?
We work with a proprietary command delimited language (like HTML). We are attempting to migrate from an older editor to Visual Studio 2008. Our coding standards are to capitalize the commands. The old editor is customized to recognize the command begin delimiter and to force capitalization until the end delimiter is typed or the escape key is pressed.
What's the best way to do that in Visual Studio 2008? Can it be done with a macro or an add-in?
(Edited 1-12-2009)
Thank you for the suggestions so far. I don't think they answer my question.
Clarifications:
The previous editor was CodeWright so the customizations there are not portable to visual studio.
The source code is not C#. StyleCop seems to be specifically for C#. Our language is similar to markup languages like HTML but with different delimiter characters and commands.
I am trying to actually capitalize as the developer types, not remind them about proper capitalization. Since the commands are all delimited our current editor actually turns the Caps Lock on when the beginning delimiter is typed. When the end delimiter or the escape key is pressed the caps lock is turned back off. This is independent of the state of the Caps Lock on the keyboard.
Try out StyleCop, available from Microsoft's web site. You might have to adjust the rule-set for your specific coding standards. For the coding standards we use, it was almost perfect out of the box.
While time consuming, this SO post shows you how to add tags to the validation setup in VS2005. I don't think the method changed in 2008.
If you are moving from an older version of Visual Studio you may be able to just import your old settings and custom tags.
This may not be the best solution but here is what I came up with.
Use macros to capture Key Press Events.
Here's how:
In Visual Studio go to the Tools->Macros->Macros IDE menu
Double Click "MyMacros" to see the different parts
Double Click "EnvironmentEvents"
Add the following code within the Environment Events module.
Save the file and return to the regular VS IDE to test.
Private My_AutoCaps As Boolean = False
Private Sub TextDocumentKeyPressEvents_BeforeKeyPress(ByVal Keypress _
As String, ByVal Selection As EnvDTE.TextSelection, _
ByVal InStatementCompletion As Boolean, ByRef CancelKeyPress As Boolean) _
Handles TextDocumentKeyPressEvents.BeforeKeyPress
Dim fileName As String = UCase(Selection.DTE.ActiveDocument.Name)
If ( fileName.EndsWith(".CPI") ) Then
If (My_AutoCaps) Then
'MsgBox(Keypress)
If (Keypress = "(" Or Keypress = ":") Then
'MsgBox("End of command character pressed.")
My_AutoCaps = False
Return
ElseIf (Keypress >= "a" And Keypress <= "z") Then
'MsgBox("Letter pressed.")
Selection.Text = UCase(Keypress)
CancelKeyPress = True
End If
Else 'AutoCap is not on yet
If (Keypress = "^") Then
'MsgBox("You pressed the Start Command character.")
My_AutoCaps = True
Return
End If
End If
End If
End Sub
This macro is limited to *.CPI files.
I have not figured out how to capture the Esc key yet but this will work for now.
I'm currently using DPack as this adds a "Collapse All Projects" option to the Solution node in Solution Explorer. It works pretty well but can take a while to execute and doesn't always collapse everything fully.
Are there any better alternatives? Preferably free and easy to install/setup. There are lots out there but which work best and don't have any bugs or performance issues.
I use the following Macro which works in Visual Studio 2005 and Visual Studio 2008:
View > Other Windows > Macro Explorer (Alt+F8)
Right-click on the MyMacros node in Macro Explorer
New Module...
Name it CollapseAll (or whatever you like)
Replace the default code with the code shown below
File > Save CollapseAll (Ctrl+S)
Close the Macro editor
To set up a keyboard shortcut:
Tools > Customize... > Commands
Keyboard...
Show commands containing: Macros.MyMacros.CollapseAll.CollapseAll
Assign a keyboard shortcut (I use Alt+C)
Code
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module CollapseAll
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
Sub CollapseSelected()
' 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 selected As Array = solutionExplorer.SelectedItems
If (selected.Length = 0) Then Return
Dim rootNode As UIHierarchyItem = selected(0)
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
End Module
I didn't write this code and I'm not sure where this code came from, but there are variations of it online.
For VS2005, I've been using CoolCommands 4.0. The feature description is more complete for the older 3.0 version.
Version 3 had an .msi installer. Version 4 is a .zip file (which was easier for my environment anyway).
My favorite features (a subset of the complete list):
From the Solution explorer:
Collapse All Projects
Open containing folder (Project/file level only)
From the filename tabs above the editor
Locate in Solution Explorer
From the context menu in the editor
Demo Font
Power commands for visual studio will do the trick. Didn't notice any performance\stability issues with them.
Here is a better list of features for CoolCommands 4.0.
To install it for VS 2005, execute the include setup.bat.
To install it for VS 2008, modify the following line from
regpkg CoolCommands.dll /codebase
to:
regpkg CoolCommands.dll /root:Software\Microsoft\VisualStudio\9.0 /codebase
PowerCommands for Visual Studio will work for both VS2008 and VS2010. It is the Microsoft-enabled way to do this quickly.