也許有點晚,但您可以使用Visual Studio Extensibility構建自己的應用程序(因此也可以添加自定義行爲):Inside the Editor: Tags and Classifier。的步驟是:
1)創建編輯器分類項目(內建項目類型)
2)刪除現有的類文件
3)添加下面的代碼。它將在着色代碼部分的所有「轉到」語句紅:
internal class GotoTagger : ITagger<GotoTag>
{
private ITextSearchService _textSearchService;
private ITextStructureNavigator _textStructureNavigator;
event EventHandler<SnapshotSpanEventArgs> ITagger<GotoTag>.TagsChanged { add { } remove { } }
public GotoTagger(ITextSearchService textSearchService, ITextStructureNavigator textStructureNavigator)
{
_textSearchService = textSearchService;
_textStructureNavigator = textStructureNavigator;
}
public IEnumerable<ITagSpan<GotoTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
if (spans.Count == 0)
yield break;
if (spans.Count > 0)
{
// look for 'goto' occurrences
foreach (SnapshotSpan span in _textSearchService.FindAll(new FindData("goto", spans[0].Snapshot, FindOptions.WholeWord | FindOptions.MatchCase, _textStructureNavigator)))
{
yield return new TagSpan<GotoTag>(span, new GotoTag());
}
}
}
}
[Export(typeof(IViewTaggerProvider))]
[TagType(typeof(TextMarkerTag))]
[ContentType("code")] // only for code portion. Could be changed to csharp to colorize only C# code for example
internal class GotoTaggerProvider : IViewTaggerProvider
{
[Import]
internal ITextSearchService TextSearchService { get; set; }
[Import]
internal ITextStructureNavigatorSelectorService TextStructureNavigatorSelector { get; set; }
public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag
{
if (textView.TextBuffer != buffer)
return null;
return new GotoTagger(TextSearchService, TextStructureNavigatorSelector.GetTextStructureNavigator(buffer)) as ITagger<T>;
}
}
internal class GotoTag : TextMarkerTag
{
public GotoTag() : base("goto") { }
}
[Export(typeof(EditorFormatDefinition))]
[Name("goto")]
[UserVisible(true)]
internal class GotoFormatDefinition : MarkerFormatDefinition
{
public GotoFormatDefinition()
{
BackgroundColor = Colors.Red;
ForegroundColor = Colors.White;
DisplayName = "Goto Word";
ZOrder = 5;
}
}
是不是壞可讀性正是我們避免使用'goto'的原因是什麼? – Carsten 2013-03-15 09:52:21
嗯,我認爲有一個想法可以嘗試,使用工具 - >選項 - >文本編輯器 - > C# - >格式化 - >縮進 - >標籤縮進 - >將goto標籤放在最左邊的列中,這樣至少可以快速生成所有標籤noticable。 – Agent007 2013-03-15 09:59:31
您提到的擴展是用於在代碼中的符號之間進行導航,而不是用於查找GOTO語句。 – 2013-03-15 10:04:39