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