在Visual Studio 2010中突出显示Goto

时间:2013-03-13 08:30:51

标签: c# visual-studio-2010

我目前正在翻译旧生成的C#代码,其中包含 goto 。请不要评论,我知道它太可怕了。 无论如何,有没有一种方法/扩展/什么使goto语句更具可读性?找到代码跳转到的地方很痛苦。我不想使用搜索功能,因为这会让我失去方向。

我发现的全是: http://visualstudiogallery.msdn.microsoft.com/4b286b9c-4dd5-416b-b143-e31d36dc622b 它不起作用。

你能推荐什么吗?

4 个答案:

答案 0 :(得分:2)

您可以考虑DevExpress CodeRushgoto语句会得到如下箭头:goto arrow。将鼠标悬停在箭头上会突出显示标签后面的语句(如果它已经可见),单击它会使其可见并将光标移动到该语句。

答案 1 :(得分:2)

可能有点晚了,但您可以使用Visual Studio Extensibility构建自己的(并因此也添加自定义行为):Inside the Editor: Tags and Classifier。步骤是:

1)创建一个Editor Classifier项目(内置项目类型)

2)删除现有的类文件

3)添加以下代码。它将使用红色代码部分中的所有'goto'语句着色:

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

答案 2 :(得分:1)

尝试ReSharper的Navigate to function exits

或Coderush的flow break icons

答案 3 :(得分:0)

另请注意,您可以使用“Ctrl”+“ - ”跳回代码中的最后一个位置,您正在查看。 这可能非常明显,但在我看来,像CSharpie可能不知道这个热键。