gpt4 book ai didi

c# - 在 Visual Studio 2010 中突出显示转到

转载 作者:行者123 更新时间:2023-11-30 15:00:28 27 4
gpt4 key购买 nike

我目前正在翻译旧生成的 C# 代码,其中充满了 goto。请不要对此发表评论,我知道这很糟糕。无论如何,有没有办法/扩展/任何使 goto-statements 更具可读性的方法?很难找到代码跳转的地方。我不想使用搜索功能,因为它会让我迷失方向。

我只找到了这个: http://visualstudiogallery.msdn.microsoft.com/4b286b9c-4dd5-416b-b143-e31d36dc622b它不起作用。

你能推荐什么吗?

最佳答案

可能有点晚了,但您可以使用 Visual Studio 可扩展性构建您自己的(因此也可以添加自定义行为):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;
}
}

关于c# - 在 Visual Studio 2010 中突出显示转到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380277/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com