Example #1
0
        /// <summary>
        /// Relex the first word directly before the current position.
        /// </summary>
        /// <param name="editor"></param>
        /// <param name="pos"></param>
        protected void RelexPreviousWord(CodeEditor editor, int pos)
        {
            // get previous word and start position
            var start = editor.WordStartPosition(pos, false);
            var word = editor.GetWordFromPosition(start);

            // find out if the previous word is a keyword
            for (int i = 0; i < keywords.Length; i++)
            {
                // reset the style of the word if it is a keyword
                if (keywords[i]?[word].Any(x => x.word == word) ?? false)
                {
                    editor.StartStyling(start);
                    editor.SetStyling(pos - start, StateToStyle(i));
                }
            }
        }
Example #2
0
        private int ProcessBraceState(CodeEditor editor, Region c, int endPos)
        {
            if (c[-1] == '{')
            {
                // get header string from block position
                var typePos = FindLastStyleOf(editor, StateToStyle((int)State.Type), c.Pos);
                var typeName = typePos >= 0 ? editor.GetWordFromPosition(typePos) : null;

                // find lexer that can lex this code block
                var lex = lexer.Where(x => x.IsLexerForType(typeName)).FirstOr(null);

                // if no lexer found, skip this code block
                if (lex == null)
                {
                    // go to matching brace
                    int start = c.Pos;
                    for (int n = 0; c.Pos < endPos && n >= 0; c.Pos++)
                    {
                        if (c.c == '{') n++;
                        if (c.c == '}') n--;
                    }
                    // make code block invalid
                    editor.SetStyling(--c.Pos - start, StateToStyle((int)BaseState.Default));
                    return (int)(c.c == '}' ? BaseState.Braces : BaseState.Default);
                }

                // re-lex code block
                c.Pos = lex.Style(editor, c.Pos, endPos);
            }
            return ProcessDefaultState(editor, c);
        }
Example #3
0
        public virtual int Style(CodeEditor editor, int pos, int endPos)
        {
            editor.StartStyling(pos);
            var textLen = editor.TextLength;

            // instantiate region class
            var c = new Region(editor, pos);

            // continue processing from the last state
            for (var state = GetPrevState(c); c.Pos < textLen; c.Pos++)
            {
                // process current state
                var lastStyle = c.GetStyleAt(0);
                state = ProcessState(editor, state, c, textLen);
                var newStyle = StateToStyle(state);

                // 
                if (c.Pos >= endPos && (c.Pos >= textLen || (newStyle > 0 && newStyle == lastStyle)))
                    break;

                // the lexer can no longer handle the code
                // go to the parent lexer and lex the code there
                if (state < 0)
                    return parentLexer?.Style(editor, c.Pos, endPos) ?? endPos;

                // style the current character
                editor.SetStyling(1, newStyle);
            }

            return c.Pos;
        }