2013-03-13 57 views
3

我目前正在翻譯舊版生成的C#代碼,它是完整的轉到。請不要對此發表評論,我知道它很可怕。 無論如何,有沒有一種方法/擴展/什麼使goto語句更具可讀性?它很難找到代碼跳轉的地方。我不想使用搜索功能,因爲它使我失去了我的方向。在Visual Studio 2010中突出顯示轉到

我發現的全部是這樣的: http://visualstudiogallery.msdn.microsoft.com/4b286b9c-4dd5-416b-b143-e31d36dc622b 它不起作用。

你能推薦什麼嗎?

+0

是不是壞可讀性正是我們避免使用'goto'的原因是什麼? – Carsten 2013-03-15 09:52:21

+0

嗯,我認爲有一個想法可以嘗試,使用工具 - >選項 - >文本編輯器 - > C# - >格式化 - >縮進 - >標籤縮進 - >將goto標籤放在最左邊的列中,這樣至少可以快速生成所有標籤noticable。 – Agent007 2013-03-15 09:59:31

+1

您提到的擴展是用於在代碼中的符號之間進行導航,而不是用於查找GOTO語句。 – 2013-03-15 10:04:39

回答

2

你可以考慮DevExpress CodeRushgoto聲明將得到如下的箭頭:goto arrow。懸停在箭頭上突出顯示標籤後面的語句(如果它已經可見),然後單擊它使其可見並將光標移動到該語句。

0

另外請注意,您可以使用「Ctrl」+「 - 」跳回到代碼中的最後一個位置,您正在查看。 這可能是非常明顯的,但在我看來,CSharpie可能不知道這個熱鍵。

+0

我知道,謝謝。主要原因是很難弄清楚代碼的作用。跳回到開始雖然有幫助,但在嵌套的東西,它不幫助找出發生了什麼事情。 – CSharpie 2013-03-15 11:39:06

2

也許有點晚,但您可以使用Visual Studio Extensibility構建自己的應用程序(因此也可以添加自定義行爲):Inside the Editor: Tags and Classifier。的步驟是:

1)創建編輯器分類項目(內建項目類型)

2)刪除現有的類文件

3)添加下面的代碼。它將在着色代碼部分的所有「轉到」語句紅:

goto colorizer

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; 
     } 
    } 
+0

它更多關於它所在的路線。因爲我現在有了更多的想法,所以仍然不會保留你的代碼。 – CSharpie 2013-03-15 11:37:07

相關問題