Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

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