283 line
9.1 KiB

  1. //
  2. // Lol Engine - VsLol add-in for Visual Studio
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel.Composition;
  13. using System.Windows.Media;
  14. using System.Text.RegularExpressions;
  15. using Microsoft.VisualStudio.Text;
  16. using Microsoft.VisualStudio.Text.Classification;
  17. using Microsoft.VisualStudio.Text.Formatting;
  18. using Microsoft.VisualStudio.Language.StandardClassification;
  19. using Microsoft.VisualStudio.Utilities;
  20. namespace lol
  21. {
  22. [Export(typeof(IClassifierProvider))]
  23. [ContentType("c/c++")]
  24. [ContentType("csharp")]
  25. [ContentType("lolfx")]
  26. internal class LolClassifierProvider : IClassifierProvider
  27. {
  28. [Import]
  29. internal IClassificationTypeRegistryService m_type_registry = null;
  30. [Import]
  31. internal IClassifierAggregatorService m_aggregator = null;
  32. [Import]
  33. internal IClassificationFormatMapService m_format_map = null;
  34. internal static bool m_inprogress = false;
  35. public IClassifier GetClassifier(ITextBuffer buffer)
  36. {
  37. /* Avoid infinite recursion */
  38. if (m_inprogress)
  39. return null;
  40. LolGenericFormat.SetRegistry(m_type_registry, m_format_map);
  41. try
  42. {
  43. m_inprogress = true;
  44. return buffer.Properties.GetOrCreateSingletonProperty<CppKeywordClassifier>(delegate { return new CppKeywordClassifier(m_type_registry, m_aggregator.GetClassifier(buffer), buffer.ContentType); });
  45. }
  46. finally { m_inprogress = false; }
  47. }
  48. }
  49. class CppKeywordClassifier : IClassifier
  50. {
  51. private IClassifier m_classifier;
  52. private IClassificationType m_types_type, m_constant_type;
  53. private Regex m_types_regex, m_constant_regex;
  54. private static const string[] m_all_types =
  55. {
  56. "void|bool|int|signed|unsigned|char|short|long|float|double",
  57. "class|struct|union|template|namespace|typename|typedef",
  58. "inline|restrict|export|explicit|mutable",
  59. "static|register|auto|volatile|extern|const"
  60. };
  61. private static const string[] m_cpp_types =
  62. {
  63. "u?int(8|16|32|64|ptr|max)_t",
  64. "u?int_(least|fast)(8|16|32|64)_t",
  65. "(wchar|char16|char32|size|ssize|off|ptrdiff)_t",
  66. "(sig_atomic|fpos|clock|time|div|ldiv)_t",
  67. "va_list|jmp_buf|FILE|DIR",
  68. };
  69. /* ldouble real half
  70. "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)";
  71. */
  72. private static const string[] m_csharp_types =
  73. {
  74. "var|string",
  75. "out|ref|internal|sealed|public|private|protected|override"
  76. };
  77. private static const string[] m_lolfx_types =
  78. {
  79. "attribute|varying|uniform|in|out",
  80. "int|uint",
  81. "(|[dui])(vec|mat)[234]"
  82. };
  83. private static const string[] m_all_constants =
  84. {
  85. "true|false"
  86. };
  87. private static const string[] m_cpp_constants =
  88. {
  89. "NULL|nullptr",
  90. "EXIT_SUCCESS|EXIT_FAILURE",
  91. "M_(E|LOG(2|10)E|LN2|LN10|PI|PI_2|PI_4|1_PI|2_PI|2_SQRTPI|SQRT(2|1_2))",
  92. "SIG(HUP|INT|QUIT|ILL|TRAP|ABRT|FPE|KILL|USR1|SEGV|USR2|PIPE|ALRM)",
  93. "SIG(TERM|CHLD|CONT|STOP|TSTP|TTIN|TTOU)"
  94. };
  95. private static const string[] m_csharp_constants =
  96. {
  97. "null",
  98. };
  99. private static const string[] m_lolfx_constants =
  100. {
  101. "gl_Position|gl_FragColor",
  102. };
  103. internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
  104. IClassifier classifier,
  105. IContentType type)
  106. {
  107. m_classifier = classifier;
  108. /* Regex for types and specifiers */
  109. m_types_type = registry.GetClassificationType("LolAnyType");
  110. List<string> types_list = m_all_types.ToList();
  111. if (type.IsOfType("c/c++"))
  112. types_list = types_list.Concat(m_cpp_types).ToList();
  113. if (type.IsOfType("csharp"))
  114. types_list = types_list.Concat(m_csharp_types).ToList();
  115. if (type.IsOfType("lolfx"))
  116. types_list = types_list.Concat(m_lolfx_types).ToList();
  117. m_types_regex =
  118. new Regex(@"\b(" + String.Join("|", types_list.ToArray()) + @")\b");
  119. /* Regex for constant words */
  120. m_constant_type = registry.GetClassificationType("LolAnyConstant");
  121. List<string> constants_list = m_all_constants.ToList();
  122. if (type.IsOfType("c/c++"))
  123. constants_list = constants_list.Concat(m_cpp_constants).ToList();
  124. if (type.IsOfType("csharp"))
  125. constants_list = constants_list.Concat(m_csharp_constants).ToList();
  126. if (type.IsOfType("lolfx"))
  127. constants_list = constants_list.Concat(m_lolfx_constants).ToList();
  128. m_constant_regex =
  129. new Regex(@"\b(" + String.Join("|", constants_list.ToArray()) + @")\b");
  130. }
  131. public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
  132. {
  133. List<ClassificationSpan> ret = new List<ClassificationSpan>();
  134. foreach (ClassificationSpan cs in m_classifier.GetClassificationSpans(span))
  135. {
  136. string cs_class = cs.ClassificationType.Classification.ToLower();
  137. /* Only apply our rules if we found a keyword or an identifier */
  138. if (cs_class == "keyword" || cs_class == "identifier")
  139. {
  140. if (m_types_regex.IsMatch(cs.Span.GetText()))
  141. {
  142. ret.Add(new ClassificationSpan(cs.Span, m_types_type));
  143. continue;
  144. }
  145. if (m_constant_regex.IsMatch(cs.Span.GetText()))
  146. {
  147. ret.Add(new ClassificationSpan(cs.Span, m_constant_type));
  148. continue;
  149. }
  150. }
  151. ret.Add(cs);
  152. }
  153. return ret;
  154. }
  155. public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
  156. }
  157. internal class LolGenericFormat : ClassificationFormatDefinition
  158. {
  159. static IClassificationTypeRegistryService m_type_registry;
  160. static IClassificationFormatMapService m_format_map;
  161. public static void SetRegistry(IClassificationTypeRegistryService type_registry,
  162. IClassificationFormatMapService format_map)
  163. {
  164. m_type_registry = type_registry;
  165. m_format_map = format_map;
  166. }
  167. protected void CopyStyleColor(string category)
  168. {
  169. if (m_type_registry == null || m_format_map == null)
  170. return;
  171. var map = m_format_map.GetClassificationFormatMap("Text Editor");
  172. if (map == null)
  173. return;
  174. //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" , "User Types", "User Types (Type parameters)", "User Types (Value types)"};
  175. var type = m_type_registry.GetClassificationType(category);
  176. if (type == null)
  177. return;
  178. var prop = map.GetExplicitTextProperties(type);
  179. if (prop == null)
  180. return;
  181. var c1 = prop.ForegroundBrush as SolidColorBrush;
  182. if (c1 != null && c1.Color != Colors.Transparent)
  183. {
  184. this.ForegroundColor = c1.Color;
  185. this.ForegroundOpacity = 1.0;
  186. }
  187. var c2 = prop.BackgroundBrush as SolidColorBrush;
  188. if (c2 != null && c2.Color != Colors.Transparent)
  189. {
  190. this.BackgroundColor = c2.Color;
  191. this.BackgroundOpacity = 1.0;
  192. }
  193. }
  194. }
  195. internal static class LolClassifierClassificationDefinition
  196. {
  197. [Export(typeof(ClassificationTypeDefinition))]
  198. [Name(LolCppTypeFormat.m_name)]
  199. internal static ClassificationTypeDefinition LolCustomClassType = null;
  200. [Export(typeof(ClassificationTypeDefinition))]
  201. [Name(LolCppConstantFormat.m_name)]
  202. internal static ClassificationTypeDefinition LolCustomConstantType = null;
  203. }
  204. [Export(typeof(EditorFormatDefinition))]
  205. [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
  206. [Name(LolCppTypeFormat.m_name)]
  207. [UserVisible(true)]
  208. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  209. internal sealed class LolCppTypeFormat : LolGenericFormat
  210. {
  211. public const string m_name = "LolAnyType";
  212. public LolCppTypeFormat()
  213. {
  214. this.DisplayName = "C/C++ Types and Qualifiers";
  215. this.ForegroundColor = Colors.Lime;
  216. this.ForegroundOpacity = 1.0;
  217. this.IsBold = true;
  218. //CopyStyleColor("User Types");
  219. }
  220. }
  221. [Export(typeof(EditorFormatDefinition))]
  222. [ClassificationType(ClassificationTypeNames = LolCppConstantFormat.m_name)]
  223. [Name(LolCppConstantFormat.m_name)]
  224. [UserVisible(true)]
  225. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  226. internal sealed class LolCppConstantFormat : LolGenericFormat
  227. {
  228. public const string m_name = "LolAnyConstant";
  229. public LolCppConstantFormat()
  230. {
  231. this.DisplayName = "C/C++ Constants";
  232. this.ForegroundColor = Colors.Magenta;
  233. this.ForegroundOpacity = 1.0;
  234. this.IsBold = true;
  235. //CopyStyleColor("User Types");
  236. }
  237. }
  238. } /* namespace lol */