Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

146 строки
5.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.Windows.Media;
  5. using System.Text.RegularExpressions;
  6. using Microsoft.VisualStudio.Text;
  7. using Microsoft.VisualStudio.Text.Classification;
  8. using Microsoft.VisualStudio.Text.Formatting;
  9. using Microsoft.VisualStudio.Language.StandardClassification;
  10. using Microsoft.VisualStudio.Utilities;
  11. namespace Lol.VisualStudio.Plugin
  12. {
  13. [Export(typeof(IClassifierProvider))]
  14. [ContentType("c/c++")]
  15. [ContentType("csharp")]
  16. [ContentType("lolfx")]
  17. internal class LolClassifierProvider : IClassifierProvider
  18. {
  19. [Import]
  20. internal IClassificationTypeRegistryService m_type_registry = null; /* Set via MEF */
  21. [Import]
  22. internal IClassificationFormatMapService m_format_map = null;
  23. public IClassifier GetClassifier(ITextBuffer buffer)
  24. {
  25. LolGenericFormat.SetRegistry(m_type_registry, m_format_map);
  26. return buffer.Properties.GetOrCreateSingletonProperty<CppKeywordClassifier>(delegate { return new CppKeywordClassifier(m_type_registry, buffer.ContentType); });
  27. }
  28. }
  29. class CppKeywordClassifier : IClassifier
  30. {
  31. private IClassificationType m_customclass_type;
  32. private Regex m_regex;
  33. internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
  34. IContentType type)
  35. {
  36. m_customclass_type = registry.GetClassificationType("LolCustomClass");
  37. string tmp = @"\b(";
  38. tmp += "void|bool|int|unsigned|char|short|long|float|double|ldouble|";
  39. tmp += "class|struct|union|template|const|static|extern|volatile|inline|namespace|";
  40. if (type.IsOfType("lolfx"))
  41. tmp += "attribute|varying|uniform|in|out|";
  42. if (type.IsOfType("csharp"))
  43. tmp += "var|string|internal|sealed|public|private|protected|";
  44. if (!type.IsOfType("csharp"))
  45. tmp += "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)|";
  46. if (type.IsOfType("c/c++"))
  47. {
  48. tmp += "u?int(8|16|32|64|ptr)_t|";
  49. tmp += "real|half|explicit|typename|typedef|";
  50. }
  51. tmp += @")\b";
  52. m_regex = new Regex(tmp);
  53. }
  54. public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
  55. {
  56. List<ClassificationSpan> ret = new List<ClassificationSpan>();
  57. string tmp = span.GetText();
  58. var matches = m_regex.Matches(tmp);
  59. foreach (Match m in matches)
  60. {
  61. Span newspan = new Span(span.Start.Position + m.Index, m.Length);
  62. SnapshotSpan newsnapshot = new SnapshotSpan(span.Snapshot, newspan);
  63. ret.Add(new ClassificationSpan(newsnapshot, m_customclass_type));
  64. }
  65. return ret;
  66. }
  67. public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
  68. }
  69. internal class LolGenericFormat : ClassificationFormatDefinition
  70. {
  71. static IClassificationTypeRegistryService m_type_registry;
  72. static IClassificationFormatMapService m_format_map;
  73. public static void SetRegistry(IClassificationTypeRegistryService type_registry,
  74. IClassificationFormatMapService format_map)
  75. {
  76. m_type_registry = type_registry;
  77. m_format_map = format_map;
  78. }
  79. protected void CopyStyleColor(string category)
  80. {
  81. if (m_type_registry == null || m_format_map == null)
  82. return;
  83. var map = m_format_map.GetClassificationFormatMap("Text Editor");
  84. if (map == null)
  85. return;
  86. //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" , "User Types", "User Types (Type parameters)", "User Types (Value types)"};
  87. var type = m_type_registry.GetClassificationType(category);
  88. if (type == null)
  89. return;
  90. var prop = map.GetExplicitTextProperties(type);
  91. if (prop == null)
  92. return;
  93. var c1 = prop.ForegroundBrush as SolidColorBrush;
  94. if (c1 != null && c1.Color != Colors.Transparent)
  95. this.ForegroundColor = c1.Color;
  96. var c2 = prop.BackgroundBrush as SolidColorBrush;
  97. if (c2 != null && c2.Color != Colors.Transparent)
  98. this.BackgroundColor = c1.Color;
  99. }
  100. }
  101. internal static class LolClassifierClassificationDefinition
  102. {
  103. [Export(typeof(ClassificationTypeDefinition))]
  104. [Name(LolCppTypeFormat.m_name)]
  105. internal static ClassificationTypeDefinition LolCustomClassType = null;
  106. }
  107. [Export(typeof(EditorFormatDefinition))]
  108. [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
  109. [Name(LolCppTypeFormat.m_name)]
  110. [UserVisible(true)]
  111. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  112. internal sealed class LolCppTypeFormat : LolGenericFormat
  113. {
  114. public const string m_name = "LolCustomClass";
  115. public LolCppTypeFormat()
  116. {
  117. this.DisplayName = "C/C++ Types and Qualifiers";
  118. this.ForegroundColor = Colors.Lime;
  119. this.IsBold = true;
  120. //CopyStyleColor("User Types");
  121. }
  122. }
  123. }