Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML classifier #40

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions Editor/Classification/ClassificationTypeDefinitions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Language.StandardClassification;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;

namespace MonoDevelop.Xml.Editor.Classification
{
public class ClassificationTypeNames
{
public const string XmlAttributeName = "xml - attribute name";
public const string XmlAttributeQuotes = "xml - attribute quotes";
public const string XmlAttributeValue = "xml - attribute value";
public const string XmlCDataSection = "xml - cdata section";
public const string XmlComment = "xml - comment";
public const string XmlDelimiter = "xml - delimiter";
public const string XmlEntityReference = "xml - entity reference";
public const string XmlName = "xml - name";
public const string XmlProcessingInstruction = "xml - processing instruction";
public const string XmlText = "xml - text";
}

public enum XmlClassificationTypes : byte
{
None,
XmlAttributeName,
XmlAttributeQuotes,
XmlAttributeValue,
XmlCDataSection,
XmlComment,
XmlDelimiter,
XmlEntityReference,
XmlName,
XmlProcessingInstruction,
XmlText,
Count,
}

class ClassificationTypeDefinitions
{
[Export]
[Name (ClassificationTypeNames.XmlAttributeName)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralAttributeNameTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlAttributeQuotes)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralAttributeQuotesTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlAttributeValue)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralAttributeValueTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlCDataSection)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralCDataSectionTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlComment)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralCommentTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlDelimiter)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralDelimiterTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlEntityReference)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralEntityReferenceTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlName)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralNameTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlProcessingInstruction)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralProcessingInstructionTypeDefinition = null;

[Export]
[Name (ClassificationTypeNames.XmlText)]
[BaseDefinition (PredefinedClassificationTypeNames.FormalLanguage)]
internal readonly ClassificationTypeDefinition XmlLiteralTextTypeDefinition = null;
}
}
35 changes: 35 additions & 0 deletions Editor/Classification/IXmlClassifierExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;

namespace MonoDevelop.Xml.Editor.Classification
{
/// <summary>
/// An extension for the XML classifier that can enable or disable the classifier
/// for a given buffer, as well as replace classification spans.
/// </summary>
public interface IXmlClassifierExtension
{
/// <summary>
/// Called when the XML classifier provider is asked to provide a classifier for a new buffer.
/// Return false to avoid classifying the buffer, true to allow classification.
/// </summary>
/// <param name="buffer">A text buffer being created.</param>
/// <returns>True if the buffer should get an XML classifier.</returns>
bool ShouldClassify (ITextBuffer buffer);

/// <summary>
/// Called back for each span classified by the XML classifier.
/// An extension has an opportunity to replace the default classification with zero or more
/// classifications.
/// </summary>
/// <param name="classificationSpan">The default classification provided by the XML classifier.</param>
/// <param name="sink">A callback to consume custom classification spans provided by this extension.
/// When the method returns true, the default classification is not used. If the sink is not called,
/// the classification is effectively removed. When it is called once, the original is being replaced
/// by a single classification. Call it multiple times to replace with multiple classifications.
/// </param>
/// <returns>False if no replacements were made and the classifier should use the default classification.</returns>
bool TryReplace (ClassificationSpan classificationSpan, Action<ClassificationSpan> sink);
}
}
Loading