有没有办法(设置?“宏”?扩展?),我可以简单地切换大纲,以便仅使用部分和我的方法折叠到他们的签名行,但我的评论(摘要)和双斜线评论)和课程保持扩展?
示例:
1)未收集
using System;
using MachineGun;
namespace Animals
{
/// <summary>
/// Angry animal
/// Pretty Fast, too
/// </summary>
public partial class Lion
{
//
// Dead or Alive
public Boolean Alive;
/// <summary>
/// Bad bite
/// </summary>
public PieceOfAnimal Bite(Animal animalToBite)
{
return animalToBite.Shoulder;
}
/// <summary>
/// Fatal bite
/// </summary>
public PieceOfAnimal Kill(Animal animalToKill)
{
return animalToKill.Head;
}
}
}
2)折叠(以下是我想要的结果):
using[...]
namespace Animals
{
/// <summary>
/// Angry animal
/// Pretty Fast, too
/// </summary>
public partial class Lion
{
//
// Dead or Alive
public Boolean Alive;
/// <summary>
/// Bad bite
/// </summary>
public PieceOfAnimal Bite(Animal animalToBite)[...]
/// <summary>
/// Fatal bite
/// </summary>
public PieceOfAnimal Kill(Animal animalToKill)[...]
}
}
这是我喜欢看到我的类文件(折叠表单)的方式。到目前为止,我已经手动折叠了一百万次,我认为应该有一种方法来自动化/自定义/扩展VS以我想要的方式做到这一点?
每次我调试/点击一个断点时,它都会崩溃并弄乱一些东西。如果我通过上下文菜单的折叠来折叠以概述等等,它也会折叠我不想要的评论。
感谢您的帮助!
答案 0 :(得分:9)
Ctrl M,Ctrl O
折叠定义。
从这一点来说,宏可能不会太难写。
类似于查找/// <summary> ...
并切换大纲。然后起泡,冲洗,重复。
答案 1 :(得分:3)
我创建了一个可以折叠成员的宏。您可以将自定义过滤器放在函数IncludeMember
中,例如在此示例中,我会折叠除注释和枚举之外的所有内容
' filter some mebers. for example using statemets cannot be collapsed so exclude them.
Function IncludeMember(ByVal member As EnvDTE.CodeElement)
If member.Kind = vsCMElement.vsCMElementIDLImport Then
Return False
ElseIf member.Kind = vsCMElement.vsCMElementEnum Then
Return False ' I do not want to colapse enums
End If
Return True
End Function
Sub CollapseNodes()
' activate working window
DTE.Windows.Item(DTE.ActiveDocument.Name).Activate()
' expand everything to start
Try
DTE.ExecuteCommand("Edit.StopOutlining")
Catch
End Try
Try
DTE.ExecuteCommand("Edit.StartAutomaticOutlining")
Catch
End Try
' get text of document and replace all new lines with \r\n
Dim objTextDoc As TextDocument
Dim objEditPt As EnvDTE.EditPoint
Dim text As String
' Get a handle to the new document and create an EditPoint.
objTextDoc = DTE.ActiveDocument.Object("TextDocument")
objEditPt = objTextDoc.StartPoint.CreateEditPoint
' Get all Text of active document
text = objEditPt.GetText(objTextDoc.EndPoint)
text = System.Text.RegularExpressions.Regex.Replace( _
text, _
"(\r\n?|\n\r?)", ChrW(13) & ChrW(10) _
)
' add new line to text so that lines of visual studio match with index of array
Dim lines As String() = System.Text.RegularExpressions.Regex.Split(vbCrLf & text, vbCrLf)
' list where whe will place all colapsable items
Dim targetLines As New System.Collections.Generic.List(Of Integer)
' regex that we will use to check if a line contains a #region
Dim reg As New System.Text.RegularExpressions.Regex(" *#region( |$)")
Dim i As Integer
For i = 1 To lines.Length - 1
If reg.Match(lines(i)).Success Then
targetLines.Add(i)
End If
Next
Dim fileCM As FileCodeModel
Dim elts As EnvDTE.CodeElements
Dim elt As EnvDTE.CodeElement
Dim projectItem = DTE.ActiveDocument.ProjectItem
Dim temp = projectItem.Collection.Count
Dim b = DirectCast(DirectCast(projectItem.Document, EnvDTE.Document).ActiveWindow, EnvDTE.Window).ContextAttributes
fileCM = projectItem.FileCodeModel
elts = fileCM.CodeElements
For i = 1 To elts.Count
elt = elts.Item(i)
CollapseE(elt, elts, i, targetLines)
Next
' now that we have the lines that we will plan to collapse sort them. it is important to go in order
targetLines.Sort()
' go in reverse order so that we can collapse nested regions
For i = targetLines.Count - 1 To 0 Step -1
GotoLine(targetLines(i) & "")
DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
Next
End Sub
'' Helper to OutlineCode. Recursively outlines members of elt.
''
Sub CollapseE(ByVal elt As EnvDTE.CodeElement, ByVal elts As EnvDTE.CodeElements, ByVal loc As Integer, ByRef targetLines As System.Collections.Generic.List(Of Integer))
Dim epStart As EnvDTE.EditPoint
Dim epEnd As EnvDTE.EditPoint
epStart = elt.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint()
epEnd = elt.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint() ' Copy it because we move it later.
epStart.EndOfLine()
If ((elt.IsCodeType()) And (elt.Kind <> EnvDTE.vsCMElement.vsCMElementDelegate) Or elt.Kind = EnvDTE.vsCMElement.vsCMElementNamespace) Then
Dim i As Integer
Dim mems As EnvDTE.CodeElements
mems = elt.Members
For i = 1 To mems.Count
CollapseE(mems.Item(i), mems, i, targetLines)
Next
End If
If (epStart.LessThan(epEnd)) Then
If IncludeMember(elt) Then
targetLines.Add(epStart.Line)
End If
End If
End Sub
答案 2 :(得分:1)
也许此链接可以帮助您:http://weblogs.asp.net/mrdave/archive/2004/09/17/230732.aspx。您可以将所有内容包装在区域中,以便您可以对其进行管理并保留注释。您也可以修改该宏以满足您的需求。
答案 3 :(得分:1)
var selection = dte.ActiveDocument.Selection;
dte.ExecuteCommand("Edit.CollapsetoDefinitions");
dte.ActiveDocument.Selection.StartOfDocument();
dte.ActiveDocument.Selection.FindText("/// <summary>")
var startLine = selection.CurrentLine;
do {
dte.ExecuteCommand("Edit.FindNext");
} while (startLine != selection.CurrentLine);
答案 4 :(得分:1)
尽管这个问题很古老并且可以回答,但我一直在寻找与问题相同的东西,但是我认为这是比编写宏更简单的方法:
Tools > Options > Text Editor > C# > Advanced > Outlining
>取消选中"Show outlining for comments and preprocessor regions"
复选框
如果您执行此操作,而不是使用CTRL+M
和CTRL+O
快捷方式,则方法将折叠,但摘要和区域将保持折叠状态。
答案 5 :(得分:0)
Visual Studio内置的任何内容都不允许您以这种方式折叠代码区域。使用宏可能是可能的,但我不认为编写它会非常简单。 Visual Studio 2010可以通过允许您编写一个对语法分析器具有更直接可访问性的实际插件来提供一些缓解,但这是纯粹的推测。