Macro per VS.NET per creare le #region nel codice

Come sapete su VS.NET c'è la possibità di raggruppare delle aree di codice tra i tag #region e #endregion (o #Region e #End Region se parlate VB.NET).
 
Metterlo sempre a mano prima e dopo la zona di codice da racchiudere è un po' una palla, quindi ho cercato un po' e ho trovato questa macro per VS.NET che ho sistemato un po' perchè per come seleziono io il testo non sempre funzionava.Smile stupito
 
    ' MakeRegion inserts #region and #endregion tags 
    ' around selected text in the VS editor.
    
Sub MakeRegion()
        
Dim rName As String = ""
        
Dim pad As String = ""
        
Dim junk As String
        Dim 
count, i As Integer
        Dim 
startpoint, endpoint As EditPoint

        
With DTE.ActiveDocument.Selection
            startpoint = .TopPoint.CreateEditPoint()
            endpoint = .BottomPoint.CreateEditPoint
        
End With

        If 
startpoint.EqualTo(endpoint) Then
            Exit Sub
        End If

        
'ELR: ADDED THIS, to move the startpoint to the start of the line
        'so that the Pad function works correctly
        
If Not startpoint.AtStartOfLine Then
            
startpoint.StartOfLine()
        
End If

        
rName = InputBox("Region Name:")

        DTE.UndoContext.
Open("Insert A Region")
        
Try
            
junk = startpoint.GetText(startpoint.LineLength)

            pad = 
String.Empty

            For 
count = 0 To junk.Length - 1
                
If junk.Substring(count, 1).Equals(" ") Or _
                junk.Substring(count, 1).Equals(vbTab) 
Then
                    
pad += junk.Substring(count, 1)
                
Else
                    Exit For
                End If
            Next
            
'ELR: ADDED Test for Languages
            
If DTE.ActiveDocument.Language = "CSharp" Then
                
' C Sharp Code
                
startpoint.Insert(String.Format("{0}#region {1}{2}", pad, rName, vbCrLf))
                
if endpoint.LineLength=0 then
                    
endpoint.Insert(String.Format("{0}#endregion // {1}{2}", pad, rName, vbCrLf))
                
Else
                    
endpoint.Insert(String.Format("{0}#endregion // {1}{2}", vbCrLf & pad, rName, vbCrLf))
                
End If
            Else
                
' VB Code
                
startpoint.Insert(String.Format("{0}#Region ""{1}""{2}", pad, rName, vbCrLf))
                
If endpoint.LineLength = 0 Then
                    
endpoint.Insert(String.Format("{0}#End Region '{1}{2}", pad, rName, vbCrLf))
                
Else
                    
endpoint.Insert(String.Format("{0}#End Region ' {1}{2}", vbCrLf & pad, rName, vbCrLf))
                
End If
            End If
        Finally
            
DTE.UndoContext.Close()
        
End Try
    End Sub
 
Suggerisco di copiare tutto il codice in una nuova macro e di assegnare un bel shortcut da tastiera (tipo ALT-R) per creare le region.

powered by IMHO

posted @ domenica 12 dicembre 2004 18:02

Print

Comments on this entry:

# re: Macro per VS.NET per creare le #region nel codice

Left by Simone Chiaretta at 12/12/2004 21:38
Gravatar
esatto.... con lo snippet editor
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011