You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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