示例#1
0
        private void TextBox_StyleNeeded(object sender, ScintillaNET.StyleNeededEventArgs e)
        {
            ScintillaNET.Scintilla scintilla = ((ScintillaNET.Scintilla)sender);

            var startPos = scintilla.GetEndStyled();
            var endPos   = e.Position;

            scintilla.StartStyling(100);
            scintilla.SetStyling(100, 1);
            scintilla.SetStyling(100, 2);
        }
示例#2
0
        public override void Highlight(ScintillaNET.Scintilla sci, int start, int end, AppSettings s)
        {
            bool startsWithNL = sci.GetCharAt(start) == '\n' || (start + 1 < sci.TextLength && sci.GetCharAt(start) == '\r' && sci.GetCharAt(start + 1) == '\n');
            bool endsWithNL   = sci.GetCharAt(end) == '\n' || (end - 1 >= 0 && sci.GetCharAt(end - 1) == '\n');

            // move back to start of line
            if (!startsWithNL)
            {
                for (int i = 0; i < MAX_BACKTRACE && start > 0; i++, start--)
                {
                    if (start > 0 && sci.GetCharAt(start - 1) == '\n')
                    {
                        break;
                    }
                }
            }

            // move forward to end of line
            if (!endsWithNL)
            {
                for (int i = 0; i < MAX_FORWARDTRACE && end < sci.TextLength; i++, end++)
                {
                    if (end >= sci.TextLength || sci.GetCharAt(end) == '\n')
                    {
                        break;
                    }
                }
            }

            var text = sci.GetTextRange(start, end - start);

            if (s.LinkMode != LinkHighlightMode.Disabled)
            {
                LinkHighlight(sci, start, text);
            }
            else
            {
                sci.StartStyling(start);
                sci.SetStyling(end - start, STYLE_DEFAULT);
            }
        }
示例#3
0
        private void HighlightLine(ScintillaNET.Scintilla sci, string text, AppSettings s)
        {
            if (text.TrimStart().StartsWith("#") && text.TrimStart().TrimStart('#').StartsWith(" "))
            {
                sci.SetStyling(text.Length, STYLE_MD_HEADER);
                return;
            }

            if (text.StartsWith("    "))
            {
                sci.SetStyling(text.Length, STYLE_MD_CODE);
                return;
            }

            var  position    = 0;
            var  mode        = ParserMode.None;
            bool isLineStart = true;

            for (int i = 0; i < text.Length; i++)
            {
                var enumNumLen = 0;

                var isDoubleStar = (i + 2 < text.Length) && text[i] == '*' && text[i + 1] == '*';
                var isSingleStar = !isDoubleStar && (i + 1 < text.Length) && text[i] == '*';
                var isBacktick   = text[i] == '`';
                var isOpenBrace  = text[i] == '[';
                var isEnumStar   = isLineStart && (i + 1 < text.Length) && text[i] == '*' && (text[i + 1] == ' ' || text[i + 1] == '\t');
                var isEnumDash   = isLineStart && (i + 1 < text.Length) && text[i] == '-' && (text[i + 1] == ' ' || text[i + 1] == '\t');
                var isEnumPlus   = isLineStart && (i + 1 < text.Length) && text[i] == '+' && (text[i + 1] == ' ' || text[i + 1] == '\t');
                var isEnumNumber = isLineStart && ParseMarkdownEnumNumber(text.Substring(i), out enumNumLen);

                if (text[i] != ' ' && text[i] != '\t')
                {
                    isLineStart = false;
                }

                if (mode == ParserMode.None)
                {
                    if (isDoubleStar)
                    {
                        sci.SetStyling(i - position, STYLE_MD_DEFAULT);
                        position += (i - position);
                        mode      = ParserMode.Bold;
                        continue;
                    }
                    else if (isSingleStar)
                    {
                        sci.SetStyling(i - position, STYLE_MD_DEFAULT);
                        position += (i - position);
                        mode      = ParserMode.Italic;
                        continue;
                    }
                    else if (isBacktick)
                    {
                        sci.SetStyling(i - position, STYLE_MD_DEFAULT);
                        position += (i - position);
                        mode      = ParserMode.CodeTick;
                        continue;
                    }
                    else if (isOpenBrace)
                    {
                        var found = ParseMarkdownLink(text.Substring(i), s.LinkMode, out var len1, out var len2, out var len3);
                        if (found)
                        {
                            sci.SetStyling(i - position, STYLE_MD_DEFAULT);
                            position += (i - position);

                            sci.SetStyling(len1, STYLE_MD_URL);
                            if (len2 > 0)
                            {
                                sci.SetStyling(len2, STYLE_MD_CLICKURL);
                            }
                            sci.SetStyling(len3, STYLE_MD_URL);

                            position += len1 + len2 + len3;
                            i         = position;
                            continue;
                        }
                    }
                    else if (isEnumStar || isEnumDash || isEnumPlus)
                    {
                        sci.SetStyling(i - position, STYLE_MD_DEFAULT);
                        position += (i - position);

                        sci.SetStyling(2, STYLE_MD_LIST);
                        position += 2;
                        i         = position - 1;

                        mode = ParserMode.None;
                        continue;
                    }
                    else if (isEnumNumber)
                    {
                        sci.SetStyling(i - position, STYLE_MD_DEFAULT);
                        position += (i - position);

                        sci.SetStyling(enumNumLen, STYLE_MD_LIST);
                        position += enumNumLen;
                        i         = position - 1;
                        continue;
                    }
                }
                else if (mode == ParserMode.Bold)
                {
                    if (isDoubleStar)
                    {
                        i++;
                        sci.SetStyling(i - position + 1, STYLE_MD_BOLD);
                        position += (i - position + 1);
                        mode      = ParserMode.None;
                        continue;
                    }
                }
                else if (mode == ParserMode.Italic)
                {
                    if (isSingleStar)
                    {
                        sci.SetStyling(i - position + 1, STYLE_MD_ITALIC);
                        position += (i - position + 1);
                        mode      = ParserMode.None;
                        continue;
                    }
                }
                else if (mode == ParserMode.CodeTick)
                {
                    if (isBacktick)
                    {
                        sci.SetStyling(i - position + 1, STYLE_MD_CODE);
                        position += (i - position + 1);
                        mode      = ParserMode.None;
                        continue;
                    }
                }
            }

            sci.SetStyling(text.Length - position, STYLE_MD_DEFAULT);
        }
示例#4
0
 private void HighlightFencedLine(ScintillaNET.Scintilla sci, string text)
 {
     sci.SetStyling(text.Length, STYLE_MD_CODE);
 }