⬆️ ⬇️

VS2010 - we do the syntax highlighting

I installed VS2010 RC1. On the hard drive, seven gigs takes, however.



I looked at how the addons are made and in particular the syntax coloring. I read about it a year ago, and here, in connection with the release, RC decided to try it out.



For example, I decided to make syntax highlighting for the Z80 assembler.

The first results were obtained in a couple of hours, in a couple of hours I made a single-line analysis - labels, comments, operators, and pseudo-operators:

')

image



In Visual Studio 10, a new extension model is used - all addon code is written exclusively on managed code, no COM. This makes life very easy, and the overall stability of the system is very positive.



Actually, to the point. First, there are several ways to distribute studio extensions, I used VSIX - the most convenient for debugging. It makes sense to put the Microsoft Visual Studio 2010 SDK - the studio is complemented by project templates for quickly creating extensions. But as far as I understand, you can create them as well. Using the VS2010 SDK, a new project for custom syntax highlighting is created via the Extensibility> Editor Classifier item.



Syntax highlighting is determined by the class that implements the IClassifier interface β€” this class defines the logic for parsing text. To use the classifier, you need a provider - implementing IClassifierProvider and defining how our classifier will be used.



[Export( typeof (IClassifierProvider))]

[ContentType( "text" )]

internal class Z80EditorClassifierProvider : IClassifierProvider

{

[Import]

internal IClassificationTypeRegistryService ClassificationRegistry = null ; // Set via MEF



public IClassifier GetClassifier(ITextBuffer buffer)

{

return buffer.Properties.GetOrCreateSingletonProperty<Z80EditorClassifier>(

delegate { return new Z80EditorClassifier(ClassificationRegistry); });

}

}



* This source code was highlighted with Source Code Highlighter .


The main classifier method β€” GetClassificationSpans () β€”for a given text fragment, the function should return a list of fragments indicating the type of the fragment β€” the class that implements the IClassificationType interface.



class Z80EditorClassifier : IClassifier

{



public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)

{

List <ClassificationSpan> result = new List <ClassificationSpan>();



...



return result;

}



}



* This source code was highlighted with Source Code Highlighter .




In this case, of course, you can access the parsed text from GetClassificationSpans (via the span.Snapshot property). In my case, I was interested in a simple syntax, which amounts to parsing a single line.



public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)

{

List <ClassificationSpan> result = new List <ClassificationSpan>();

if (span.Length == 0) return result;



ITextSnapshot snapshot = span.Snapshot;

ITextSnapshotLine line = snapshot.GetLineFromPosition(span.Start.Position);

ITextSnapshotLine endLine = snapshot.GetLineFromPosition(span.End.Position);



while ( true )

{

// Process current line

ProcessLine(line, result);



if (line.LineNumber == endLine.LineNumber)

break ;

// Next line

line = snapshot.GetLineFromPosition(line.EndIncludingLineBreak + 1);

}



return result;

}



* This source code was highlighted with Source Code Highlighter .




Inside the ProcessLine, it remains to identify individual fragments of the line and add them to the list, specifying the type of the fragment for each. For example, a comment highlighting is added like this:



IClassificationType commentClassifType = registry.GetClassificationType( "z80comment" );

result.Add( new ClassificationSpan(

new SnapshotSpan(line.Snapshot, new Span(line.Start + commentPos, commentLength)),

commentClassifType));



* This source code was highlighted with Source Code Highlighter .




Types of fragments can be described all at once in one class:



internal static class Z80EditorClassifierClassificationDefinition

{

/// <summary>

/// Defines the "z80operator" classification type - Z80 Assembly Operator.

/// </summary>

[Export( typeof (ClassificationTypeDefinition))]

[Name( "z80operator" )]

internal static ClassificationTypeDefinition Z80OperatorDefinition = null ;



/// <summary>

/// Defines the "z80comment" classification type - Z80 Assembly Comment.

/// </summary>

[Export( typeof (ClassificationTypeDefinition))]

[Name( "z80comment" )]

internal static ClassificationTypeDefinition Z80CommentDefinition = null ;

}



* This source code was highlighted with Source Code Highlighter .




A formatter can be associated with a classifier β€” a class derived from ClassificationFormatDefinition and defining how text is highlighted.



[Export( typeof (EditorFormatDefinition))]

[ClassificationType(ClassificationTypeNames = "z80operator" )]

[Name( "z80operator" )]

[UserVisible( true )]

[Order(Before = Priority.Default)]

internal sealed class Z80EditorOperatorFormat : ClassificationFormatDefinition

{

public Z80EditorOperatorFormat()

{

this .DisplayName = "Z80 Assembly Operator" ;

this .BackgroundColor = Color.FromRgb(230,255,230);

this .ForegroundColor = Colors.Blue;

}

}



* This source code was highlighted with Source Code Highlighter .




In general, for such a small result, this is practically all you need to know. A little more detail can be found in the article (carefully, for the current version of the studio there are differences in the nuances):

VS 2010 Editor - Text Coloring Sample Deep Dive

dotneteers.net/blogs/divedeeper/archive/2008/11/04/LearnVSXNowPart38.aspx



UPD: The described example is posted here: narod.ru/disk/18104996000/Z80Editor.zip.html

Source: https://habr.com/ru/post/85038/



All Articles