/// <summary> /// Registers the built-in highlighting definitions on first time request for a definition /// or when the application changes its WPF Theme (eg. from 'Light' to 'Dark') to load the /// appropriate highlighting resource when queried for it. /// </summary> /// <param name="hlm"></param> /// <param name="theme"></param> internal static void RegisterBuiltInHighlightings( DefaultHighlightingManager hlm, IHLTheme theme) { // This registration was already performed for this highlighting theme if (theme.IsBuiltInThemesRegistered == true) { return; } hlm.RegisterHighlighting(theme, "XmlDoc", null, "XmlDoc.xshd"); hlm.RegisterHighlighting(theme, "C#", new[] { ".cs" }, "CSharp-Mode.xshd"); hlm.RegisterHighlighting(theme, "Gcode", new[] { ".nc" }, "Gcode.xshd"); hlm.RegisterHighlighting(theme, "GRazor", new[] { ".grazor" }, "GRazor-Mode.xshd"); hlm.RegisterHighlighting(theme, "JavaScript", new[] { ".js" }, "JavaScript-Mode.xshd"); hlm.RegisterHighlighting(theme, "HTML", new[] { ".htm", ".html" }, "HTML-Mode.xshd"); hlm.RegisterHighlighting(theme, "ASP/XHTML", new[] { ".asp", ".aspx", ".asax", ".asmx", ".ascx", ".master" }, "ASPX.xshd"); hlm.RegisterHighlighting(theme, "Boo", new[] { ".boo" }, "Boo.xshd"); hlm.RegisterHighlighting(theme, "Coco", new[] { ".atg" }, "Coco-Mode.xshd"); hlm.RegisterHighlighting(theme, "CSS", new[] { ".css" }, "CSS-Mode.xshd"); hlm.RegisterHighlighting(theme, "C++", new[] { ".c", ".h", ".cc", ".cpp", ".hpp" }, "CPP-Mode.xshd"); hlm.RegisterHighlighting(theme, "Java", new[] { ".java" }, "Java-Mode.xshd"); hlm.RegisterHighlighting(theme, "Patch", new[] { ".patch", ".diff" }, "Patch-Mode.xshd"); hlm.RegisterHighlighting(theme, "PowerShell", new[] { ".ps1", ".psm1", ".psd1" }, "PowerShell.xshd"); hlm.RegisterHighlighting(theme, "PHP", new[] { ".php" }, "PHP-Mode.xshd"); hlm.RegisterHighlighting(theme, "Python", new[] { ".py", ".pyw" }, "Python-Mode.xshd"); hlm.RegisterHighlighting(theme, "TeX", new[] { ".tex" }, "Tex-Mode.xshd"); hlm.RegisterHighlighting(theme, "TSQL", new[] { ".sql" }, "TSQL-Mode.xshd"); hlm.RegisterHighlighting(theme, "VB", new[] { ".vb" }, "VB-Mode.xshd"); hlm.RegisterHighlighting(theme, "XML", (".xml;.xsl;.xslt;.xsd;.manifest;.config;.addin;" + ".xshd;.wxs;.wxi;.wxl;.proj;.csproj;.vbproj;.ilproj;" + ".booproj;.build;.xfrm;.targets;.xaml;.xpt;" + ".xft;.map;.wsdl;.disco;.ps1xml;.nuspec").Split(';'), "XML-Mode.xshd"); hlm.RegisterHighlighting(theme, "MarkDown", new[] { ".md" }, "MarkDown-Mode.xshd"); // Additional Highlightings hlm.RegisterHighlighting(theme, "ActionScript3", new[] { ".as" }, "AS3.xshd"); hlm.RegisterHighlighting(theme, "BAT", new[] { ".bat", ".dos" }, "DOSBATCH.xshd"); hlm.RegisterHighlighting(theme, "F#", new[] { ".fs" }, "FSharp-Mode.xshd"); hlm.RegisterHighlighting(theme, "HLSL", new[] { ".fx" }, "HLSL.xshd"); hlm.RegisterHighlighting(theme, "INI", new[] { ".cfg", ".conf", ".ini", ".iss" }, "INI.xshd"); hlm.RegisterHighlighting(theme, "LOG", new[] { ".log" }, "Log.xshd"); hlm.RegisterHighlighting(theme, "Pascal", new[] { ".pas" }, "Pascal.xshd"); hlm.RegisterHighlighting(theme, "PLSQL", new[] { ".plsql" }, "PLSQL.xshd"); hlm.RegisterHighlighting(theme, "Ruby", new[] { ".rb" }, "Ruby.xshd"); hlm.RegisterHighlighting(theme, "Scheme", new[] { ".sls", ".sps", ".ss", ".scm" }, "scheme.xshd"); hlm.RegisterHighlighting(theme, "Squirrel", new[] { ".nut" }, "squirrel.xshd"); hlm.RegisterHighlighting(theme, "TXT", new[] { ".txt" }, "TXT.xshd"); hlm.RegisterHighlighting(theme, "VTL", new[] { ".vtl", ".vm" }, "vtl.xshd"); theme.IsBuiltInThemesRegistered = true; }
/// <summary> /// Adds another highlighting theme into the current collection of highlighting themes. /// </summary> /// <param name="key"></param> /// <param name="theme"></param> public void ThemedHighlightingAdd(string key, IHLTheme theme) { lock (lockObj) { _ThemedHighlightings.Add(key, theme); } }
Func <IHighlightingDefinition> LoadHighlighting(IHLTheme theme, string name, string resourceName) { Func <IHighlightingDefinition> func = delegate { ICSharpCode.AvalonEdit.Highlighting.Xshd.XshdSyntaxDefinition xshd; using (Stream s = HLResources.OpenStream(GetPrefix(CurrentTheme.HLBaseKey), resourceName)) { using (XmlTextReader reader = new XmlTextReader(s)) { // in release builds, skip validating the built-in highlightings xshd = HighlightingLoader.LoadXshd(reader, true); } } var hlTheme = theme.HlTheme; SyntaxDefinition themedHighlights = null; if (hlTheme != null) { themedHighlights = hlTheme.GetNamedSyntaxDefinition(name); } return(HighlightingLoader.Load(themedHighlights, xshd, this)); }; return(func); }
/// <summary> /// Registering a built-in highlighting including highlighting themes (if any). /// </summary> /// <param name="theme"></param> /// <param name="name"></param> /// <param name="extensions"></param> /// <param name="resourceName"></param> internal void RegisterHighlighting(IHLTheme theme, string name, string[] extensions, string resourceName) { try { #if DEBUG // don't use lazy-loading in debug builds, show errors immediately ICSharpCode.AvalonEdit.Highlighting.Xshd.XshdSyntaxDefinition xshd; using (Stream s = HLResources.OpenStream(GetPrefix(CurrentTheme.HLBaseKey), resourceName)) { using (XmlTextReader reader = new XmlTextReader(s)) { xshd = HighlightingLoader.LoadXshd(reader, false); } } Debug.Assert(name == xshd.Name); if (extensions != null) { Debug.Assert(System.Linq.Enumerable.SequenceEqual(extensions, xshd.Extensions)); } else { Debug.Assert(xshd.Extensions.Count == 0); } var hlTheme = theme.HlTheme; SyntaxDefinition themedHighlights = null; if (hlTheme != null) { themedHighlights = hlTheme.GetNamedSyntaxDefinition(name); } // round-trip xshd: // string resourceFileName = Path.Combine(Path.GetTempPath(), resourceName); // using (XmlTextWriter writer = new XmlTextWriter(resourceFileName, System.Text.Encoding.UTF8)) { // writer.Formatting = Formatting.Indented; // new Xshd.SaveXshdVisitor(writer).WriteDefinition(xshd); // } // using (FileStream fs = File.Create(resourceFileName + ".bin")) { // new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(fs, xshd); // } // using (FileStream fs = File.Create(resourceFileName + ".compiled")) { // new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(fs, Xshd.HighlightingLoader.Load(xshd, this)); // } base.RegisterHighlighting(name, extensions, HighlightingLoader.Load(themedHighlights, xshd, this)); #else base.RegisterHighlighting(name, extensions, LoadHighlighting(theme, name, resourceName)); #endif } catch (HighlightingDefinitionInvalidException ex) { throw new InvalidOperationException("The built-in highlighting '" + name + "' is invalid.", ex); } }
public static IHighlightingDefinition GetHighlighterByLanguageName(IHLTheme theme, string langName) { // at first, try the real name - but that does only work if the case is exactly matching IHighlightingDefinition def = theme.GetDefinition(langName); if (def != null) { return(def); } langName = langName.ToLower().Replace("-", "").Replace("_", ""); // then try it by extension def = theme.GetDefinitionByExtension("." + langName.Replace(".", "")); if (def != null) { return(def); } string customName = ""; switch (langName) { case "xmldoc": customName = "XmlDoc"; break; case "c#": case "csharp": customName = "C#"; break; case "javascript": customName = "JavaScript"; break; case "xhtml": case "asp": customName = "ASP/XHTML"; break; case "boo": customName = "Boo"; break; case "coco": customName = "coco"; break; case "css": customName = "css"; break; case "cpp": case "c++": customName = "C++"; break; case "ps": case "powershell": customName = "PowerShell"; break; case "python": customName = "Python"; break; case "tex": customName = "TeX"; break; case "tsql": case "mssql": customName = "TSQL"; break; case "vb": case "visualbasic": customName = "VB"; break; // additional ones case "actionscript": case "as3": case "actionscript3": customName = "ActionScript3"; break; case "f#": case "fsharp": customName = "F#"; break; case "hlsl": customName = "HLSL"; break; case "pascal": case "pasc": customName = "Pascal"; break; case "ruby": case "rb": customName = "Ruby"; break; case "scheme": customName = "Scheme"; break; case "squirrel": customName = "Squirrel"; break; } return(theme.GetDefinition(customName)); }