Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding font in custom Visual Studio editor

The problem is in making custom editor inside VS extension look differently than the current theme dictates. The editor is hosted inside a dialog and should have the same font the hosting dialog defines.

The content type of the editor is defined like this:

[Export]
[Name("MyContent")]
[BaseDefinition("code")]
public static readonly ContentTypeDefinition ExportContentTypeDefinition = null;

And there is a classification type definition:

[Export]
[Name("MyContentText")]
[BaseDefinition("text")]
public static readonly ClassificationTypeDefinition MyTextDefinition = null;

The classifier provider is defined as below:

[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"));
    }
}

While the classifier just provides the same format for any snapshot:

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

Now, in code, while creating the editor, I'm trying to override the properties of the matching 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));

However, the change doesn't affect my editor instance.

Moreover, the format map returned from the classificationFomatMapService.GetClassificationFormatMap(ITextView) overload is different from the one returned from the overload I use above. And changing this another instance of format also affects all the code editors in the running Visual Studio instance, so I have to conclude that despite my efforts the textView somehow maps to the default editor's classification.

My question is: what should I do in order to control text appearance of a custom editor designated for a custom content type?

like image 440
galenus Avatar asked Dec 01 '14 09:12

galenus


People also ask

How do I change the font style in Visual Studio?

On the menu bar, choose Tools > Options. In the options list, choose Environment > Fonts and Colors. In Show settings for list, select Text Editor. Modify the Font and Size options to change the font and text size for the editor.

What is the default font family of VS code?

By default, the font used for coding in VS Code is Consolas.


1 Answers

I think you're on the right path, but you need to do something similar to the ViewCreationListener of the italicizing comments extension. Specifically, use the GetClassificationFormatMap for the view (with a view creation listener keyed on your content type) and instead of setting the default text properties, set the properties for your classification type. As you've observed, the format maps do get shared among views, so you don't want to change the default.

You may need to provide a ClassificationFormatDefinition for that type. Maybe want to do it anyways, just to have something show up in Fonts & Colors.


For posterity: I don't think the GetClassificationFormatMap(String) method takes a ContentType. I don't have the code handy anymore and I don't remember at all how this works, but I don't think an "appearance category" is related to content types.

like image 137
Noah Richards Avatar answered Oct 06 '22 00:10

Noah Richards