Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

330 rindas
10 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, m_normal_type;
  54. private Regex m_types_regex, m_constant_regex, m_normal_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. private static string[] m_all_normal =
  105. {
  106. };
  107. private static string[] m_cpp_normal =
  108. {
  109. "interface|delegate|event|finally",
  110. "gcnew|generic|initonly|property|sealed",
  111. };
  112. internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
  113. IClassifier classifier,
  114. IContentType type)
  115. {
  116. m_classifier = classifier;
  117. m_types_type = registry.GetClassificationType("LolAnyType");
  118. m_normal_type = registry.GetClassificationType("LolAnyIdentifier");
  119. m_constant_type = registry.GetClassificationType("LolAnyConstant");
  120. List<string> types_list = m_all_types.ToList();
  121. List<string> constants_list = m_all_constants.ToList();
  122. List<string> normals_list = m_all_normal.ToList();
  123. if (type.IsOfType("c/c++"))
  124. {
  125. types_list = types_list.Concat(m_cpp_types).ToList();
  126. constants_list = constants_list.Concat(m_cpp_constants).ToList();
  127. normals_list = normals_list.Concat(m_cpp_normal).ToList();
  128. }
  129. if (type.IsOfType("csharp"))
  130. {
  131. types_list = types_list.Concat(m_csharp_types).ToList();
  132. constants_list = constants_list.Concat(m_csharp_constants).ToList();
  133. }
  134. if (type.IsOfType("lolfx"))
  135. {
  136. types_list = types_list.Concat(m_lolfx_types).ToList();
  137. constants_list = constants_list.Concat(m_lolfx_constants).ToList();
  138. }
  139. m_types_regex =
  140. new Regex("^(" + String.Join("|", types_list.ToArray()) + ")$");
  141. m_constant_regex =
  142. new Regex("^(" + String.Join("|", constants_list.ToArray()) + ")$");
  143. m_normal_regex =
  144. new Regex("^(" + String.Join("|", normals_list.ToArray()) + ")$");
  145. }
  146. public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
  147. {
  148. List<ClassificationSpan> ret = new List<ClassificationSpan>();
  149. foreach (ClassificationSpan cs in m_classifier.GetClassificationSpans(span))
  150. {
  151. string cs_class = cs.ClassificationType.Classification.ToLower();
  152. /* Only apply our rules if we found a keyword or an identifier */
  153. if (cs_class == "keyword" || cs_class == "identifier")
  154. {
  155. if (m_types_regex.IsMatch(cs.Span.GetText()))
  156. {
  157. ret.Add(new ClassificationSpan(cs.Span, m_types_type));
  158. continue;
  159. }
  160. if (m_constant_regex.IsMatch(cs.Span.GetText()))
  161. {
  162. ret.Add(new ClassificationSpan(cs.Span, m_constant_type));
  163. continue;
  164. }
  165. if (m_normal_regex.IsMatch(cs.Span.GetText()))
  166. {
  167. ret.Add(new ClassificationSpan(cs.Span, m_normal_type));
  168. continue;
  169. }
  170. }
  171. ret.Add(cs);
  172. }
  173. return ret;
  174. }
  175. public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
  176. }
  177. internal class LolGenericFormat : ClassificationFormatDefinition
  178. {
  179. static IClassificationTypeRegistryService m_type_registry;
  180. static IClassificationFormatMapService m_format_map;
  181. public static void SetRegistry(IClassificationTypeRegistryService type_registry,
  182. IClassificationFormatMapService format_map)
  183. {
  184. m_type_registry = type_registry;
  185. m_format_map = format_map;
  186. }
  187. protected void CopyStyleColor(string category)
  188. {
  189. if (m_type_registry == null || m_format_map == null)
  190. return;
  191. var map = m_format_map.GetClassificationFormatMap("Text Editor");
  192. if (map == null)
  193. return;
  194. //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" , "User Types", "User Types (Type parameters)", "User Types (Value types)"};
  195. var type = m_type_registry.GetClassificationType(category);
  196. if (type == null)
  197. return;
  198. var prop = map.GetExplicitTextProperties(type);
  199. if (prop == null)
  200. return;
  201. var c1 = prop.ForegroundBrush as SolidColorBrush;
  202. if (c1 != null && c1.Color != Colors.Transparent)
  203. {
  204. this.ForegroundColor = c1.Color;
  205. this.ForegroundOpacity = 1.0;
  206. }
  207. var c2 = prop.BackgroundBrush as SolidColorBrush;
  208. if (c2 != null && c2.Color != Colors.Transparent)
  209. {
  210. this.BackgroundColor = c2.Color;
  211. this.BackgroundOpacity = 1.0;
  212. }
  213. }
  214. }
  215. internal static class LolClassifierClassificationDefinition
  216. {
  217. [Export(typeof(ClassificationTypeDefinition))]
  218. [Name(LolCppTypeFormat.m_name)]
  219. internal static ClassificationTypeDefinition LolCustomClassType = null;
  220. [Export(typeof(ClassificationTypeDefinition))]
  221. [Name(LolCppConstantFormat.m_name)]
  222. internal static ClassificationTypeDefinition LolCustomConstantType = null;
  223. [Export(typeof(ClassificationTypeDefinition))]
  224. [Name(LolCppIdentifierFormat.m_name)]
  225. internal static ClassificationTypeDefinition LolCustomIdentifierType = null;
  226. }
  227. [Export(typeof(EditorFormatDefinition))]
  228. [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
  229. [Name(LolCppTypeFormat.m_name)]
  230. [UserVisible(true)]
  231. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  232. internal sealed class LolCppTypeFormat : LolGenericFormat
  233. {
  234. public const string m_name = "LolAnyType";
  235. public LolCppTypeFormat()
  236. {
  237. this.DisplayName = "C/C++ Types and Qualifiers (VsLol)";
  238. this.ForegroundColor = Colors.Lime;
  239. this.ForegroundOpacity = 1.0;
  240. this.IsBold = true;
  241. //CopyStyleColor("User Types");
  242. }
  243. }
  244. [Export(typeof(EditorFormatDefinition))]
  245. [ClassificationType(ClassificationTypeNames = LolCppConstantFormat.m_name)]
  246. [Name(LolCppConstantFormat.m_name)]
  247. [UserVisible(true)]
  248. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  249. internal sealed class LolCppConstantFormat : LolGenericFormat
  250. {
  251. public const string m_name = "LolAnyConstant";
  252. public LolCppConstantFormat()
  253. {
  254. this.DisplayName = "C/C++ Constants (VsLol)";
  255. this.ForegroundColor = Colors.Magenta;
  256. this.ForegroundOpacity = 1.0;
  257. this.IsBold = true;
  258. //CopyStyleColor("User Types");
  259. }
  260. }
  261. [Export(typeof(EditorFormatDefinition))]
  262. [ClassificationType(ClassificationTypeNames = LolCppIdentifierFormat.m_name)]
  263. [Name(LolCppIdentifierFormat.m_name)]
  264. [UserVisible(true)]
  265. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  266. internal sealed class LolCppIdentifierFormat : LolGenericFormat
  267. {
  268. public const string m_name = "LolAnyIdentifier";
  269. public LolCppIdentifierFormat()
  270. {
  271. this.DisplayName = "C/C++ Identifiers (VsLol)";
  272. this.ForegroundColor = Colors.Silver;
  273. this.ForegroundOpacity = 1.0;
  274. this.IsBold = false;
  275. CopyStyleColor(PredefinedClassificationTypeNames.Identifier);
  276. }
  277. }
  278. } /* namespace lol */