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);
            }
        }