
VS2010 - do your syntax highlighting
I put in my VS2010 RC1. On a hard drive, seven gigs takes, however.
I looked at how add-ons are made and, in particular, syntax coloring. I read about this a year ago, and here, in connection with the release of RC, I decided to try it out.
For example, I decided to make syntax highlighting for the Z80 assembler.
I managed to get the first results in a couple of hours, in a couple of hours I did a one-line analysis - labels, comments, operators and pseudo-operators:

Visual Studio 10 uses a new extension model - all add-on code is written exclusively in managed code, no COM. This makes life very serious and the overall stability of the system has a very positive effect.
Actually, to the point. Firstly, 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 I understand it, you can create them like that. Using the VS2010 SDK, a new project for custom syntax highlighting is created through Extensibility> Editor Classifier .
Syntax highlighting is determined by the class that implements the IClassifier interface - this class sets the logic for parsing text. To use the classifier, you need a provider that implements IClassifierProvider and determines how our classifier will be used.
The main classifier method is GetClassificationSpans () - for a given fragment of text, the function should return a list of fragments indicating the type of fragment — a class that implements the IClassificationType interface. At the same time, from GetClassificationSpans (), of course, you can access the parsed text through the span.Snapshot property. In my case, I was interested in a simple syntax, which boils down to parsing a single line. Inside the ProcessLine, it remains to identify the individual fragments of the string and add them to the list, indicating for each type of fragment. For example, this is how the highlight for the comment is added: Types of fragments can be described all at once in one class:
The classifier can be associated with a formatter - a class derived from ClassificationFormatDefinition and defining how text is highlighted. In general, for such a small result - this is almost everything you need to know. You can look in more detail 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 available here: narod.ru/disk/18104996000/Z80Editor.zip.html
I looked at how add-ons are made and, in particular, syntax coloring. I read about this a year ago, and here, in connection with the release of RC, I decided to try it out.
For example, I decided to make syntax highlighting for the Z80 assembler.
I managed to get the first results in a couple of hours, in a couple of hours I did a one-line analysis - labels, comments, operators and pseudo-operators:

Visual Studio 10 uses a new extension model - all add-on code is written exclusively in managed code, no COM. This makes life very serious and the overall stability of the system has a very positive effect.
Actually, to the point. Firstly, 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 I understand it, you can create them like that. Using the VS2010 SDK, a new project for custom syntax highlighting is created through Extensibility> Editor Classifier .
Syntax highlighting is determined by the class that implements the IClassifier interface - this class sets the logic for parsing text. To use the classifier, you need a provider that implements IClassifierProvider and determines 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(
delegate { return new Z80EditorClassifier(ClassificationRegistry); });
}
}
* This source code was highlighted with Source Code Highlighter.
The main classifier method is GetClassificationSpans () - for a given fragment of text, the function should return a list of fragments indicating the type of fragment — a class that implements the IClassificationType interface. At the same time, from GetClassificationSpans (), of course, you can access the parsed text through the span.Snapshot property. In my case, I was interested in a simple syntax, which boils down to parsing a single line. Inside the ProcessLine, it remains to identify the individual fragments of the string and add them to the list, indicating for each type of fragment. For example, this is how the highlight for the comment is added: Types of fragments can be described all at once in one class:
class Z80EditorClassifier : IClassifier
{
public IList GetClassificationSpans(SnapshotSpan span)
{
List result = new List();
...
return result;
}
}
* This source code was highlighted with Source Code Highlighter.
public IList GetClassificationSpans(SnapshotSpan span)
{
List result = new List();
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.
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.
internal static class Z80EditorClassifierClassificationDefinition
{
///
/// Defines the "z80operator" classification type - Z80 Assembly Operator.
///
[Export(typeof(ClassificationTypeDefinition))]
[Name("z80operator")]
internal static ClassificationTypeDefinition Z80OperatorDefinition = null;
///
/// Defines the "z80comment" classification type - Z80 Assembly Comment.
///
[Export(typeof(ClassificationTypeDefinition))]
[Name("z80comment")]
internal static ClassificationTypeDefinition Z80CommentDefinition = null;
}
* This source code was highlighted with Source Code Highlighter.
The classifier can be associated with a formatter - a class derived from ClassificationFormatDefinition and defining how text is highlighted. In general, for such a small result - this is almost everything you need to know. You can look in more detail 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 available here: narod.ru/disk/18104996000/Z80Editor.zip.html
[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.