예제 #1
0
        /// <summary>
        /// Creates the lexer from XML file used by the Notepad++ software.
        /// </summary>
        /// <param name="scintilla">The <see cref="Scintilla"/> which lexer style to set.</param>
        /// <param name="lexerType">Type of the lexer.</param>
        /// <param name="fileName">A file name to get the lexer type from.</param>
        /// <param name="useGlobalOverride">A flag indicating whether the style "Global override" should be set for the lexer from the XML document.</param>
        /// <param name="font">A flag indicating whether to use the defined font name from the XML document or not.</param>
        /// <param name="useWhiteSpace">A flag indicating whether to color the white space symbol.</param>
        /// <param name="useSelectionColors">A flag indicating whether to color the selection.</param>
        /// <param name="useMarginColors">A flag indicating whether to color the margin.</param>
        /// <returns><c>true</c> if the operation was successful, <c>false</c> otherwise.</returns>
        public static bool CreateLexerFromFile(Scintilla scintilla, LexerType lexerType,
                                               string fileName, bool useGlobalOverride, bool font, bool useWhiteSpace, bool useSelectionColors,
                                               bool useMarginColors)
        {
            try
            {
                XDocument document = XDocument.Load(fileName);

                ScintillaNotepadPlusPlusStyles.SetGlobalDefaultStyles(document, scintilla, useGlobalOverride, font);

                ScintillaNotepadPlusPlusStyles.LoadScintillaStyleFromNotepadPlusXml(document, scintilla, useWhiteSpace,
                                                                                    useSelectionColors, useMarginColors);

                ScintillaNotepadPlusPlusStyles.LoadLexerStyleFromNotepadPlusXml(document, scintilla,
                                                                                lexerType); // TODO::Font?

                scintilla.Lexer = LexerTypeName.GetLexerByLexerType(lexerType);

                ScintillaKeyWords.SetKeywords(scintilla, lexerType);

                LexerFoldProperties.SetFoldProperties(scintilla, lexerType);

                ScintillaNotepadPlusPlusStyles.SetFolding(document, scintilla);

                System.Diagnostics.Debug.WriteLine(scintilla.DescribeKeywordSets());

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        /// <summary>
        /// Sets the folding of a <see cref="Scintilla"/> based on the "Fold" style defined in the XML document.
        /// </summary>
        /// <param name="document">The XML document to read the folding style from.</param>
        /// <param name="scintilla">The instance to a scintilla of which folding style to set.</param>
        /// <returns><c>true</c> if the operation was successful, <c>false</c> otherwise.</returns>
        public static bool SetFolding(XDocument document, Scintilla scintilla)
        {
            try
            {
                var foldStyle = // <WidgetStyle name="Fold"...
                                document.Descendants(XName.Get("WidgetStyle")).FirstOrDefault(f =>
                                                                                              f.Attribute("name") != null &&
                                                                                              f.Attribute("name").Value == "Fold");

                if (foldStyle == null)
                {
                    return(false);
                }

                var style = XmlStyleNotepadPlusPlusHelper.FromXElement(foldStyle);

                // the default colors as in the example.. (C)::https://github.com/jacobslusser/ScintillaNET/wiki/Automatic-Code-Folding

                // Instruct the lexer to calculate folding..
                LexerFoldProperties.FoldDefault(scintilla);

                // Configure a margin to display folding symbols
                scintilla.Margins[2].Type      = MarginType.Symbol;
                scintilla.Margins[2].Mask      = Marker.MaskFolders;
                scintilla.Margins[2].Sensitive = true;
                scintilla.Margins[2].Width     = 20;

                // Set colors for all folding markers
                for (int i = 25; i <= 31; i++)
                {
                    scintilla.Markers[i].SetForeColor(style.ColorBackground);
                    scintilla.Markers[i].SetBackColor(style.ColorForeground);
                }

                // Configure folding markers with respective symbols
                scintilla.Markers[Marker.Folder].Symbol        = MarkerSymbol.BoxPlus;
                scintilla.Markers[Marker.FolderOpen].Symbol    = MarkerSymbol.BoxMinus;
                scintilla.Markers[Marker.FolderEnd].Symbol     = MarkerSymbol.BoxPlusConnected;
                scintilla.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
                scintilla.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
                scintilla.Markers[Marker.FolderSub].Symbol     = MarkerSymbol.VLine;
                scintilla.Markers[Marker.FolderTail].Symbol    = MarkerSymbol.LCorner;

                // Enable automatic folding
                scintilla.AutomaticFold = (AutomaticFold.Show | AutomaticFold.Click | AutomaticFold.Change);

                return(true);
            }
            catch
            {
                return(false);
            }
        }