问题在于使 VS 扩展中的自定义编辑器看起来与当前主题指示的不同。编辑器托管在一个对话框中,并且应该具有托管对话框定义的相同字体。
编辑器的内容类型定义如下:
[Export]
[Name("MyContent")]
[BaseDefinition("code")]
public static readonly ContentTypeDefinition ExportContentTypeDefinition = null;
还有一个分类类型定义:
[Export]
[Name("MyContentText")]
[BaseDefinition("text")]
public static readonly ClassificationTypeDefinition MyTextDefinition = null;
分类器提供者定义如下:
[Export(typeof(IClassifierProvider))]
[ContentType("MyContent")]
public class ClassifierProvider : IClassifierProvider
{
[Import]
public IClassificationTypeRegistryService ClassificationTypesRegistry { get; set; }
public IClassifier GetClassifier(ITextBuffer textBuffer)
{
return new Classifier(
ClassificationTypesRegistry.GetClassificationType("MyContentText"));
}
}
虽然分类器只是为任何快照提供相同的格式:
public class Classifier : IClassifier
{
private readonly IClassificationType _classificationType;
public Classifier(IClassificationType classificationType)
{
_classificationType = classificationType;
}
public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
{
return new [] { new ClassificationSpan(span, _classificationType)};
}
public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
}
现在,在代码中,在创建编辑器时,我试图覆盖匹配的 IClassificationFormatMap
的属性:
var contentType = contentTypeRegistryService.GetContentType("MyContent");
var textBuffer = textBufferFactoryService.CreateTextBuffer(initialText, contentType);
var textView = textEditorFactoryService.CreateTextView(textBuffer);
...
var formatMap = classificationFomatMapService
.GetClassificationFormatMap("MyContentText");
formatMap.DefaultTextProperties = formatMap.DefaultTextProperties
.SetFontRenderingEmSize(dialog.FontSize)
.SetTypeface(
new Typeface(
dialog.FontFamily,
dialog.FontStyle,
dialog.FontWeight,
dialog.FontStretch));
但是,更改不会影响我的编辑器实例。
此外,从 classificationFomatMapService.GetClassificationFormatMap(ITextView)
重载返回的格式映射与我在上面使用的重载返回的格式映射不同。更改此格式的另一个实例也会影响正在运行的 Visual Studio 实例中的所有代码编辑器,因此我必须得出结论,尽管我做出了努力,textView 仍以某种方式映射到默认编辑器的分类。
我的问题是:我应该怎么做才能控制为自定义内容类型指定的自定义编辑器的文本外观?
最佳答案
我认为你走在正确的道路上,但你需要做类似于 ViewCreationListener of the italicizing comments extension 的事情.具体来说,对 View 使用 GetClassificationFormatMap(使用以您的内容类型为键的 View 创建监听器),而不是设置默认文本属性,而是为您的分类类型设置属性。正如您所观察到的,格式映射确实在 View 之间共享,因此您不想更改默认设置。
您可能需要为该类型提供 ClassificationFormatDefinition。也许无论如何都想这样做,只是为了在字体和颜色中显示一些东西。
为了后代:我不认为 GetClassificationFormatMap(String)方法采用 ContentType。我手边没有代码,我完全不记得它是如何工作的,但我认为“外观类别”与内容类型无关。
关于c# - 在自定义 Visual Studio 编辑器中覆盖字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27225702/