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

CppKeywordClassifier.cs 12 KiB

12 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // Lol Engine - VsLol add-in for Visual Studio
  3. //
  4. // Copyright: (c) 2010-2013 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 System.Diagnostics; /* For debugging purposes */
  17. using Microsoft.VisualStudio.Shell;
  18. using Microsoft.VisualStudio.Text;
  19. using Microsoft.VisualStudio.Text.Classification;
  20. using Microsoft.VisualStudio.Text.Formatting;
  21. using Microsoft.VisualStudio.Language.StandardClassification;
  22. using Microsoft.VisualStudio.Utilities;
  23. namespace lol
  24. {
  25. [Export(typeof(IClassifierProvider))]
  26. [ContentType("c/c++")]
  27. [ContentType("csharp")]
  28. [ContentType("lolfx")]
  29. internal class LolClassifierProvider : IClassifierProvider
  30. {
  31. [Import]
  32. internal IClassificationTypeRegistryService m_type_registry = null;
  33. [Import]
  34. internal IClassifierAggregatorService m_aggregator = null;
  35. [Import]
  36. internal IClassificationFormatMapService m_format_map = null;
  37. #if FALSE
  38. [Import]
  39. internal ITextDocumentFactoryService m_textdoc_factory = null;
  40. #endif
  41. [Import]
  42. internal SVsServiceProvider m_sp = null;
  43. internal static bool m_inprogress = false;
  44. public IClassifier GetClassifier(ITextBuffer buffer)
  45. {
  46. /* Avoid infinite recursion */
  47. if (m_inprogress)
  48. return null;
  49. #if FALSE
  50. if (m_textdoc_factory != null)
  51. {
  52. ITextDocument doc;
  53. m_textdoc_factory.TryGetTextDocument(buffer, out doc);
  54. /* print doc.FilePath */
  55. }
  56. #endif
  57. /* Try to guess whether this is a Lol Engine project */
  58. EnvDTE.DTE dte = m_sp.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
  59. bool islolengine = false;
  60. if (dte.Solution.FullName.Contains("Lol.sln"))
  61. islolengine = true;
  62. LolGenericFormat.SetRegistry(m_type_registry, m_format_map);
  63. try
  64. {
  65. m_inprogress = true;
  66. return buffer.Properties.GetOrCreateSingletonProperty<CppKeywordClassifier>(delegate { return new CppKeywordClassifier(m_type_registry, m_aggregator.GetClassifier(buffer), buffer.ContentType, islolengine); });
  67. }
  68. finally { m_inprogress = false; }
  69. }
  70. }
  71. class CppKeywordClassifier : IClassifier
  72. {
  73. private IClassifier m_classifier;
  74. private bool m_inprogress, m_islolengine;
  75. private IClassificationType m_types_type, m_constant_type, m_normal_type;
  76. private Regex m_types_regex, m_constant_regex, m_normal_regex;
  77. private static string[] m_all_types =
  78. {
  79. "void|bool|int|signed|unsigned|char|short|long|float|double",
  80. "class|struct|union|template|namespace|typename|typedef",
  81. "inline|restrict|export|explicit|mutable",
  82. "static|register|auto|volatile|extern|const"
  83. };
  84. private static string[] m_cpp_types =
  85. {
  86. "u?int(8|16|32|64|ptr|max)_t",
  87. "u?int_(least|fast)(8|16|32|64)_t",
  88. "(wchar|char16|char32|size|ssize|off|ptrdiff)_t",
  89. "(sig_atomic|fpos|clock|time|div|ldiv)_t",
  90. "va_list|jmp_buf|FILE|DIR",
  91. "__(int(8|16|32|64)|ptr(32|64)|m(64|128|128d|128i))",
  92. };
  93. private static string[] m_lol_types =
  94. {
  95. "ldouble|real|half",
  96. "(float|int)([234]|2x2|3x3|4x4)",
  97. "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)",
  98. "(|[dui])box[23]",
  99. };
  100. private static string[] m_csharp_types =
  101. {
  102. "var|string",
  103. "out|ref|internal|sealed|public|private|protected|override"
  104. };
  105. private static string[] m_lolfx_types =
  106. {
  107. "attribute|varying|uniform|in|out",
  108. "int|uint",
  109. "(|[dui])(vec|mat)[234]"
  110. };
  111. private static string[] m_all_constants =
  112. {
  113. "true|false"
  114. };
  115. private static string[] m_cpp_constants =
  116. {
  117. "NULL|nullptr",
  118. "EXIT_SUCCESS|EXIT_FAILURE",
  119. "M_(E|LOG(2|10)E|LN2|LN10|PI|PI_[24]|[12]_PI|2_SQRTPI|SQRT(2|1_2))",
  120. "SIG(HUP|INT|QUIT|ILL|TRAP|ABRT|FPE|KILL|USR1|SEGV|USR2|PIPE|ALRM)",
  121. "SIG(TERM|CHLD|CONT|STOP|TSTP|TTIN|TTOU)"
  122. };
  123. private static string[] m_lol_constants =
  124. {
  125. "(F|D|LD)_(PI|PI_[234]|[12]_PI|SQRT(2|3|1_2))",
  126. };
  127. private static string[] m_csharp_constants =
  128. {
  129. "null",
  130. };
  131. private static string[] m_lolfx_constants =
  132. {
  133. "gl_Position|gl_FragColor",
  134. };
  135. private static string[] m_all_normal =
  136. {
  137. };
  138. private static string[] m_cpp_normal =
  139. {
  140. "interface|delegate|event|finally",
  141. "gcnew|generic|initonly|property|sealed",
  142. };
  143. internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
  144. IClassifier classifier,
  145. IContentType type,
  146. bool islolengine)
  147. {
  148. m_classifier = classifier;
  149. m_inprogress = false;
  150. m_islolengine = islolengine;
  151. m_types_type = registry.GetClassificationType("LolAnyType");
  152. m_normal_type = registry.GetClassificationType("LolAnyIdentifier");
  153. m_constant_type = registry.GetClassificationType("LolAnyConstant");
  154. List<string> types_list = m_all_types.ToList();
  155. List<string> constants_list = m_all_constants.ToList();
  156. List<string> normals_list = m_all_normal.ToList();
  157. if (type.IsOfType("c/c++"))
  158. {
  159. types_list = types_list.Concat(m_cpp_types).ToList();
  160. constants_list = constants_list.Concat(m_cpp_constants).ToList();
  161. normals_list = normals_list.Concat(m_cpp_normal).ToList();
  162. }
  163. if (type.IsOfType("csharp"))
  164. {
  165. types_list = types_list.Concat(m_csharp_types).ToList();
  166. constants_list = constants_list.Concat(m_csharp_constants).ToList();
  167. }
  168. if (type.IsOfType("lolfx"))
  169. {
  170. types_list = types_list.Concat(m_lolfx_types).ToList();
  171. constants_list = constants_list.Concat(m_lolfx_constants).ToList();
  172. }
  173. if (m_islolengine)
  174. {
  175. types_list = types_list.Concat(m_lol_types).ToList();
  176. constants_list = constants_list.Concat(m_lol_constants).ToList();
  177. }
  178. m_types_regex =
  179. new Regex("^(" + String.Join("|", types_list.ToArray()) + ")$");
  180. m_constant_regex =
  181. new Regex("^(" + String.Join("|", constants_list.ToArray()) + ")$");
  182. m_normal_regex =
  183. new Regex("^(" + String.Join("|", normals_list.ToArray()) + ")$");
  184. }
  185. public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
  186. {
  187. List<ClassificationSpan> ret = new List<ClassificationSpan>();
  188. if (m_inprogress)
  189. {
  190. /* For some reason we can get called recursively when parsing a
  191. * string for the IntelliSense drop-down menu. There is information
  192. * on how to deal with it properly on the following SO page:
  193. * http://stackoverflow.com/q/3155598/111461
  194. * The crash can be reproduced by simply typing "vec2(" and waiting
  195. * for IntelliSense to spawn the menu. */
  196. ret.Add(new ClassificationSpan(span, m_constant_type));
  197. return ret;
  198. }
  199. m_inprogress = true;
  200. foreach (ClassificationSpan cs in m_classifier.GetClassificationSpans(span))
  201. {
  202. string cs_class = cs.ClassificationType.Classification.ToLower();
  203. /* Only apply our rules if we found a keyword or an identifier */
  204. if (cs_class == "keyword" || cs_class == "identifier")
  205. {
  206. if (m_types_regex.IsMatch(cs.Span.GetText()))
  207. {
  208. ret.Add(new ClassificationSpan(cs.Span, m_types_type));
  209. continue;
  210. }
  211. if (m_constant_regex.IsMatch(cs.Span.GetText()))
  212. {
  213. ret.Add(new ClassificationSpan(cs.Span, m_constant_type));
  214. continue;
  215. }
  216. if (m_normal_regex.IsMatch(cs.Span.GetText()))
  217. {
  218. ret.Add(new ClassificationSpan(cs.Span, m_normal_type));
  219. continue;
  220. }
  221. }
  222. ret.Add(cs);
  223. }
  224. m_inprogress = false;
  225. return ret;
  226. }
  227. public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
  228. }
  229. internal class LolGenericFormat : ClassificationFormatDefinition
  230. {
  231. static IClassificationTypeRegistryService m_type_registry;
  232. static IClassificationFormatMapService m_format_map;
  233. public static void SetRegistry(IClassificationTypeRegistryService type_registry,
  234. IClassificationFormatMapService format_map)
  235. {
  236. m_type_registry = type_registry;
  237. m_format_map = format_map;
  238. }
  239. protected void CopyStyleColor(string category)
  240. {
  241. if (m_type_registry == null || m_format_map == null)
  242. return;
  243. var map = m_format_map.GetClassificationFormatMap("Text Editor");
  244. if (map == null)
  245. return;
  246. //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" , "User Types", "User Types (Type parameters)", "User Types (Value types)"};
  247. var type = m_type_registry.GetClassificationType(category);
  248. if (type == null)
  249. return;
  250. var prop = map.GetExplicitTextProperties(type);
  251. if (prop == null)
  252. return;
  253. var c1 = prop.ForegroundBrush as SolidColorBrush;
  254. if (c1 != null && c1.Color != Colors.Transparent)
  255. {
  256. this.ForegroundColor = c1.Color;
  257. this.ForegroundOpacity = 1.0;
  258. }
  259. var c2 = prop.BackgroundBrush as SolidColorBrush;
  260. if (c2 != null && c2.Color != Colors.Transparent)
  261. {
  262. this.BackgroundColor = c2.Color;
  263. this.BackgroundOpacity = 1.0;
  264. }
  265. }
  266. }
  267. internal static class LolClassifierClassificationDefinition
  268. {
  269. [Export(typeof(ClassificationTypeDefinition))]
  270. [Name(LolCppTypeFormat.m_name)]
  271. internal static ClassificationTypeDefinition LolCustomClassType = null;
  272. [Export(typeof(ClassificationTypeDefinition))]
  273. [Name(LolCppConstantFormat.m_name)]
  274. internal static ClassificationTypeDefinition LolCustomConstantType = null;
  275. [Export(typeof(ClassificationTypeDefinition))]
  276. [Name(LolCppIdentifierFormat.m_name)]
  277. internal static ClassificationTypeDefinition LolCustomIdentifierType = null;
  278. }
  279. [Export(typeof(EditorFormatDefinition))]
  280. [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
  281. [Name(LolCppTypeFormat.m_name)]
  282. [UserVisible(true)]
  283. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  284. internal sealed class LolCppTypeFormat : LolGenericFormat
  285. {
  286. public const string m_name = "LolAnyType";
  287. public LolCppTypeFormat()
  288. {
  289. this.DisplayName = "C/C++ Types and Qualifiers (VsLol)";
  290. this.ForegroundColor = Colors.Lime;
  291. this.ForegroundOpacity = 1.0;
  292. this.IsBold = true;
  293. //CopyStyleColor("User Types");
  294. }
  295. }
  296. [Export(typeof(EditorFormatDefinition))]
  297. [ClassificationType(ClassificationTypeNames = LolCppConstantFormat.m_name)]
  298. [Name(LolCppConstantFormat.m_name)]
  299. [UserVisible(true)]
  300. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  301. internal sealed class LolCppConstantFormat : LolGenericFormat
  302. {
  303. public const string m_name = "LolAnyConstant";
  304. public LolCppConstantFormat()
  305. {
  306. this.DisplayName = "C/C++ Constants (VsLol)";
  307. this.ForegroundColor = Colors.Magenta;
  308. this.ForegroundOpacity = 1.0;
  309. this.IsBold = true;
  310. //CopyStyleColor("User Types");
  311. }
  312. }
  313. [Export(typeof(EditorFormatDefinition))]
  314. [ClassificationType(ClassificationTypeNames = LolCppIdentifierFormat.m_name)]
  315. [Name(LolCppIdentifierFormat.m_name)]
  316. [UserVisible(true)]
  317. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  318. internal sealed class LolCppIdentifierFormat : LolGenericFormat
  319. {
  320. public const string m_name = "LolAnyIdentifier";
  321. public LolCppIdentifierFormat()
  322. {
  323. this.DisplayName = "C/C++ Identifiers (VsLol)";
  324. this.ForegroundColor = Colors.Silver;
  325. this.ForegroundOpacity = 1.0;
  326. this.IsBold = false;
  327. CopyStyleColor(PredefinedClassificationTypeNames.Identifier);
  328. }
  329. }
  330. } /* namespace lol */