From ad0a00bbb384ce432bc7ed4850beecb4414a32e1 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 26 Mar 2018 12:31:31 -0500 Subject: [PATCH 1/2] Export classification type definitions for new classification types Fixes #25716 --- .../ClassificationTypeDefinitions.cs | 56 ++++++++++++++++++- .../ClassificationTypeNamesTests.cs | 54 ++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs diff --git a/src/EditorFeatures/Core/Implementation/Classification/ClassificationTypeDefinitions.cs b/src/EditorFeatures/Core/Implementation/Classification/ClassificationTypeDefinitions.cs index 891e66afe5273..e5863ff88b49e 100644 --- a/src/EditorFeatures/Core/Implementation/Classification/ClassificationTypeDefinitions.cs +++ b/src/EditorFeatures/Core/Implementation/Classification/ClassificationTypeDefinitions.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.ComponentModel.Composition; -using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis.Classification; using Microsoft.VisualStudio.Language.StandardClassification; using Microsoft.VisualStudio.Text.Classification; @@ -73,6 +72,61 @@ internal sealed class ClassificationTypeDefinitions internal readonly ClassificationTypeDefinition UserTypeTypeParametersTypeDefinition; #endregion + #region Field Name + [Export] + [Name(ClassificationTypeNames.FieldName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition FieldNameTypeDefinition; + #endregion + #region Enum Member Name + [Export] + [Name(ClassificationTypeNames.EnumMemberName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition EnumMemberNameTypeDefinition; + #endregion + #region Constant Name + [Export] + [Name(ClassificationTypeNames.ConstantName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition ConstantNameTypeDefinition; + #endregion + #region Local Name + [Export] + [Name(ClassificationTypeNames.LocalName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition LocalNameTypeDefinition; + #endregion + #region Parameter Name + [Export] + [Name(ClassificationTypeNames.ParameterName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition ParameterNameTypeDefinition; + #endregion + #region Method Name + [Export] + [Name(ClassificationTypeNames.MethodName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition MethodNameTypeDefinition; + #endregion + #region Extension Method Name + [Export] + [Name(ClassificationTypeNames.ExtensionMethodName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition ExtensionMethodNameTypeDefinition; + #endregion + #region Property Name + [Export] + [Name(ClassificationTypeNames.PropertyName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition PropertyNameTypeDefinition; + #endregion + #region Event Name + [Export] + [Name(ClassificationTypeNames.EventName)] + [BaseDefinition(PredefinedClassificationTypeNames.Identifier)] + internal readonly ClassificationTypeDefinition EventNameTypeDefinition; + #endregion + #region XML Doc Comments - Attribute Name [Export] [Name(ClassificationTypeNames.XmlDocCommentAttributeName)] diff --git a/src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs b/src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs new file mode 100644 index 0000000000000..bc809a2cda82f --- /dev/null +++ b/src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using Microsoft.CodeAnalysis.Classification; +using Microsoft.VisualStudio.Text.Classification; +using Roslyn.Test.Utilities; +using Xunit; + +namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces +{ + public class ClassificationTypeNamesTests + { + public static IEnumerable AllClassificationTypeNames + { + get + { + foreach (var field in typeof(ClassificationTypeNames).GetFields(BindingFlags.Static | BindingFlags.Public)) + { + yield return new object[] { field.Name, field.GetRawConstantValue() }; + } + } + } + + [Theory] + [MemberData(nameof(AllClassificationTypeNames))] + [WorkItem(25716, "https://github.com/dotnet/roslyn/issues/25716")] + public void ClassificationTypeExported(string fieldName, object constantValue) + { + Assert.IsType(constantValue); + string classificationTypeName = (string)constantValue; + var exportProvider = TestExportProvider.ExportProviderWithCSharpAndVisualBasic; + var exports = exportProvider.GetExports(); + var export = exports.FirstOrDefault(x => x.Metadata.Name == classificationTypeName); + Assert.True(export != null, $"{nameof(ClassificationTypeNames)}.{fieldName} has value \"{classificationTypeName}\", but no matching {nameof(ClassificationTypeDefinition)} was exported."); + } + + public interface IClassificationTypeDefinitionMetadata + { + string Name + { + get; + } + + [DefaultValue(null)] + IEnumerable BaseDefinition + { + get; + } + } + } +} From d1770cc9c2214b0db6a3e68b6742223849e20db4 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 26 Mar 2018 14:27:12 -0500 Subject: [PATCH 2/2] Use IClassificationTypeRegistryService instead of faking the behavior This change suggested by @jasonmalinowski makes the ClassificationTypeExported test simpler and more accurate. --- .../ClassificationTypeNamesTests.cs | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs b/src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs index bc809a2cda82f..8c7734364498a 100644 --- a/src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs +++ b/src/EditorFeatures/Test/Workspaces/ClassificationTypeNamesTests.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; using System.Reflection; using Microsoft.CodeAnalysis.Classification; using Microsoft.VisualStudio.Text.Classification; @@ -29,26 +27,11 @@ public static IEnumerable AllClassificationTypeNames [WorkItem(25716, "https://github.com/dotnet/roslyn/issues/25716")] public void ClassificationTypeExported(string fieldName, object constantValue) { - Assert.IsType(constantValue); - string classificationTypeName = (string)constantValue; + var classificationTypeName = Assert.IsType(constantValue); var exportProvider = TestExportProvider.ExportProviderWithCSharpAndVisualBasic; - var exports = exportProvider.GetExports(); - var export = exports.FirstOrDefault(x => x.Metadata.Name == classificationTypeName); - Assert.True(export != null, $"{nameof(ClassificationTypeNames)}.{fieldName} has value \"{classificationTypeName}\", but no matching {nameof(ClassificationTypeDefinition)} was exported."); - } - - public interface IClassificationTypeDefinitionMetadata - { - string Name - { - get; - } - - [DefaultValue(null)] - IEnumerable BaseDefinition - { - get; - } + var classificationTypeRegistryService = exportProvider.GetExport().Value; + var classificationType = classificationTypeRegistryService.GetClassificationType(classificationTypeName); + Assert.True(classificationType != null, $"{nameof(ClassificationTypeNames)}.{fieldName} has value \"{classificationTypeName}\", but no matching {nameof(ClassificationTypeDefinition)} was exported."); } } }