Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

388 wiersze
12 KiB

  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. };
  99. private static string[] m_csharp_types =
  100. {
  101. "var|string",
  102. "out|ref|internal|sealed|public|private|protected|override"
  103. };
  104. private static string[] m_lolfx_types =
  105. {
  106. "attribute|varying|uniform|in|out",
  107. "int|uint",
  108. "(|[dui])(vec|mat)[234]"
  109. };
  110. private static string[] m_all_constants =
  111. {
  112. "true|false"
  113. };
  114. private static string[] m_cpp_constants =
  115. {
  116. "NULL|nullptr",
  117. "EXIT_SUCCESS|EXIT_FAILURE",
  118. "M_(E|LOG(2|10)E|LN2|LN10|PI|PI_[24]|[12]_PI|2_SQRTPI|SQRT(2|1_2))",
  119. "SIG(HUP|INT|QUIT|ILL|TRAP|ABRT|FPE|KILL|USR1|SEGV|USR2|PIPE|ALRM)",
  120. "SIG(TERM|CHLD|CONT|STOP|TSTP|TTIN|TTOU)"
  121. };
  122. private static string[] m_lol_constants =
  123. {
  124. "(F|D|LD)_(PI|PI_[234]|[12]_PI|SQRT(2|3|1_2))",
  125. };
  126. private static string[] m_csharp_constants =
  127. {
  128. "null",
  129. };
  130. private static string[] m_lolfx_constants =
  131. {
  132. "gl_Position|gl_FragColor",
  133. };
  134. private static string[] m_all_normal =
  135. {
  136. };
  137. private static string[] m_cpp_normal =
  138. {
  139. "interface|delegate|event|finally",
  140. "gcnew|generic|initonly|property|sealed",
  141. };
  142. internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
  143. IClassifier classifier,
  144. IContentType type,
  145. bool islolengine)
  146. {
  147. m_classifier = classifier;
  148. m_inprogress = false;
  149. m_islolengine = islolengine;
  150. m_types_type = registry.GetClassificationType("LolAnyType");
  151. m_normal_type = registry.GetClassificationType("LolAnyIdentifier");
  152. m_constant_type = registry.GetClassificationType("LolAnyConstant");
  153. List<string> types_list = m_all_types.ToList();
  154. List<string> constants_list = m_all_constants.ToList();
  155. List<string> normals_list = m_all_normal.ToList();
  156. if (type.IsOfType("c/c++"))
  157. {
  158. types_list = types_list.Concat(m_cpp_types).ToList();
  159. constants_list = constants_list.Concat(m_cpp_constants).ToList();
  160. normals_list = normals_list.Concat(m_cpp_normal).ToList();
  161. }
  162. if (type.IsOfType("csharp"))
  163. {
  164. types_list = types_list.Concat(m_csharp_types).ToList();
  165. constants_list = constants_list.Concat(m_csharp_constants).ToList();
  166. }
  167. if (type.IsOfType("lolfx"))
  168. {
  169. types_list = types_list.Concat(m_lolfx_types).ToList();
  170. constants_list = constants_list.Concat(m_lolfx_constants).ToList();
  171. }
  172. if (m_islolengine)
  173. {
  174. types_list = types_list.Concat(m_lol_types).ToList();
  175. constants_list = constants_list.Concat(m_lol_constants).ToList();
  176. }
  177. m_types_regex =
  178. new Regex("^(" + String.Join("|", types_list.ToArray()) + ")$");
  179. m_constant_regex =
  180. new Regex("^(" + String.Join("|", constants_list.ToArray()) + ")$");
  181. m_normal_regex =
  182. new Regex("^(" + String.Join("|", normals_list.ToArray()) + ")$");
  183. }
  184. public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
  185. {
  186. List<ClassificationSpan> ret = new List<ClassificationSpan>();
  187. if (m_inprogress)
  188. {
  189. /* For some reason we can get called recursively when parsing a
  190. * string for the IntelliSense drop-down menu. There is information
  191. * on how to deal with it properly on the following SO page:
  192. * http://stackoverflow.com/q/3155598/111461
  193. * The crash can be reproduced by simply typing "vec2(" and waiting
  194. * for IntelliSense to spawn the menu. */
  195. ret.Add(new ClassificationSpan(span, m_constant_type));
  196. return ret;
  197. }
  198. m_inprogress = true;
  199. foreach (ClassificationSpan cs in m_classifier.GetClassificationSpans(span))
  200. {
  201. string cs_class = cs.ClassificationType.Classification.ToLower();
  202. /* Only apply our rules if we found a keyword or an identifier */
  203. if (cs_class == "keyword" || cs_class == "identifier")
  204. {
  205. if (m_types_regex.IsMatch(cs.Span.GetText()))
  206. {
  207. ret.Add(new ClassificationSpan(cs.Span, m_types_type));
  208. continue;
  209. }
  210. if (m_constant_regex.IsMatch(cs.Span.GetText()))
  211. {
  212. ret.Add(new ClassificationSpan(cs.Span, m_constant_type));
  213. continue;
  214. }
  215. if (m_normal_regex.IsMatch(cs.Span.GetText()))
  216. {
  217. ret.Add(new ClassificationSpan(cs.Span, m_normal_type));
  218. continue;
  219. }
  220. }
  221. ret.Add(cs);
  222. }
  223. m_inprogress = false;
  224. return ret;
  225. }
  226. public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
  227. }
  228. internal class LolGenericFormat : ClassificationFormatDefinition
  229. {
  230. static IClassificationTypeRegistryService m_type_registry;
  231. static IClassificationFormatMapService m_format_map;
  232. public static void SetRegistry(IClassificationTypeRegistryService type_registry,
  233. IClassificationFormatMapService format_map)
  234. {
  235. m_type_registry = type_registry;
  236. m_format_map = format_map;
  237. }
  238. protected void CopyStyleColor(string category)
  239. {
  240. if (m_type_registry == null || m_format_map == null)
  241. return;
  242. var map = m_format_map.GetClassificationFormatMap("Text Editor");
  243. if (map == null)
  244. return;
  245. //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" , "User Types", "User Types (Type parameters)", "User Types (Value types)"};
  246. var type = m_type_registry.GetClassificationType(category);
  247. if (type == null)
  248. return;
  249. var prop = map.GetExplicitTextProperties(type);
  250. if (prop == null)
  251. return;
  252. var c1 = prop.ForegroundBrush as SolidColorBrush;
  253. if (c1 != null && c1.Color != Colors.Transparent)
  254. {
  255. this.ForegroundColor = c1.Color;
  256. this.ForegroundOpacity = 1.0;
  257. }
  258. var c2 = prop.BackgroundBrush as SolidColorBrush;
  259. if (c2 != null && c2.Color != Colors.Transparent)
  260. {
  261. this.BackgroundColor = c2.Color;
  262. this.BackgroundOpacity = 1.0;
  263. }
  264. }
  265. }
  266. internal static class LolClassifierClassificationDefinition
  267. {
  268. [Export(typeof(ClassificationTypeDefinition))]
  269. [Name(LolCppTypeFormat.m_name)]
  270. internal static ClassificationTypeDefinition LolCustomClassType = null;
  271. [Export(typeof(ClassificationTypeDefinition))]
  272. [Name(LolCppConstantFormat.m_name)]
  273. internal static ClassificationTypeDefinition LolCustomConstantType = null;
  274. [Export(typeof(ClassificationTypeDefinition))]
  275. [Name(LolCppIdentifierFormat.m_name)]
  276. internal static ClassificationTypeDefinition LolCustomIdentifierType = null;
  277. }
  278. [Export(typeof(EditorFormatDefinition))]
  279. [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
  280. [Name(LolCppTypeFormat.m_name)]
  281. [UserVisible(true)]
  282. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  283. internal sealed class LolCppTypeFormat : LolGenericFormat
  284. {
  285. public const string m_name = "LolAnyType";
  286. public LolCppTypeFormat()
  287. {
  288. this.DisplayName = "C/C++ Types and Qualifiers (VsLol)";
  289. this.ForegroundColor = Colors.Lime;
  290. this.ForegroundOpacity = 1.0;
  291. this.IsBold = true;
  292. //CopyStyleColor("User Types");
  293. }
  294. }
  295. [Export(typeof(EditorFormatDefinition))]
  296. [ClassificationType(ClassificationTypeNames = LolCppConstantFormat.m_name)]
  297. [Name(LolCppConstantFormat.m_name)]
  298. [UserVisible(true)]
  299. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  300. internal sealed class LolCppConstantFormat : LolGenericFormat
  301. {
  302. public const string m_name = "LolAnyConstant";
  303. public LolCppConstantFormat()
  304. {
  305. this.DisplayName = "C/C++ Constants (VsLol)";
  306. this.ForegroundColor = Colors.Magenta;
  307. this.ForegroundOpacity = 1.0;
  308. this.IsBold = true;
  309. //CopyStyleColor("User Types");
  310. }
  311. }
  312. [Export(typeof(EditorFormatDefinition))]
  313. [ClassificationType(ClassificationTypeNames = LolCppIdentifierFormat.m_name)]
  314. [Name(LolCppIdentifierFormat.m_name)]
  315. [UserVisible(true)]
  316. [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
  317. internal sealed class LolCppIdentifierFormat : LolGenericFormat
  318. {
  319. public const string m_name = "LolAnyIdentifier";
  320. public LolCppIdentifierFormat()
  321. {
  322. this.DisplayName = "C/C++ Identifiers (VsLol)";
  323. this.ForegroundColor = Colors.Silver;
  324. this.ForegroundOpacity = 1.0;
  325. this.IsBold = false;
  326. CopyStyleColor(PredefinedClassificationTypeNames.Identifier);
  327. }
  328. }
  329. } /* namespace lol */