private void ApplyStyleColor(ScriptStyleType type, Color color)
 {
     foreach (KeyValuePair <int, ScriptStyleType> group in styletranslation)
     {
         if (group.Value == type)
         {
             scriptedit.Styles[group.Key].ForeColor = color;
         }
     }
 }
Example #2
0
        // This gathers information about the current caret position
        protected void UpdatePositionInfo()
        {
            int bracketlevel = 0;                               // bracket level counting
            int argindex     = 0;                               // function argument counting
            int pos          = scriptedit.CurrentPosition;

            // Get the text
            string scripttext = scriptedit.Text;

            // Reset position info
            curfunctionname     = "";
            curargumentindex    = 0;
            curfunctionstartpos = 0;

            // Determine lowest backtrack position
            int limitpos = Math.Max(0, scriptedit.CurrentPosition - MAX_BACKTRACK_LENGTH);

            // We can only do this when we have function syntax information
            if ((scriptconfig.ArgumentDelimiter.Length == 0) || (scriptconfig.FunctionClose.Length == 0) ||
                (scriptconfig.FunctionOpen.Length == 0) || (scriptconfig.Terminator.Length == 0))
            {
                return;
            }

            // Get int versions of the function syntax informantion
            int argumentdelimiter = scriptconfig.ArgumentDelimiter[0];
            int functionclose     = scriptconfig.FunctionClose[0];
            int functionopen      = scriptconfig.FunctionOpen[0];
            int terminator        = scriptconfig.Terminator[0];

            // Continue backtracking until we reached the limitpos
            while (pos >= limitpos)
            {
                // Backtrack 1 character
                pos--;

                // Get the style and character at this position
                ScriptStyleType curstyle = scriptcontrol.GetScriptStyle(scriptedit.GetStyleAt(pos));
                int             curchar  = scriptedit.GetCharAt(pos);

                // Then meeting ) then increase bracket level
                // When meeting ( then decrease bracket level
                // When bracket level goes -1, then the next word should be the function name
                // Only when at bracket level 0, count the comma's for argument index

                // TODO:
                // Original code checked for scope character here and breaks if found

                // Check if in plain text or keyword
                if ((curstyle == ScriptStyleType.PlainText) || (curstyle == ScriptStyleType.Keyword))
                {
                    // Closing bracket
                    if (curchar == functionclose)
                    {
                        bracketlevel++;
                    }
                    // Opening bracket
                    else if (curchar == functionopen)
                    {
                        bracketlevel--;

                        // Out of the brackets?
                        if (bracketlevel < 0)
                        {
                            // Skip any whitespace before this bracket
                            do
                            {
                                // Backtrack 1 character
                                curchar = scriptedit.GetCharAt(--pos);
                            }while((pos >= limitpos) && ((curchar == ' ') || (curchar == '\t') ||
                                                         (curchar == '\r') || (curchar == '\n')));

                            // NOTE: We may need to set onlyWordCharacters argument in the
                            // following calls to false to get any argument delimiter included,
                            // but this may also cause a valid keyword to be combined with other
                            // surrounding characters that do not belong to the keyword.

                            // Find the word before this bracket
                            int    wordstart = scriptedit.WordStartPosition(pos, true);
                            int    wordend   = scriptedit.WordEndPosition(pos, true);
                            string word      = scripttext.Substring(wordstart, wordend - wordstart);
                            if (word.Length > 0)
                            {
                                // Check if this is an argument delimiter
                                // I can't remember why I did this, but I'll probably stumble
                                // upon the problem if this doesn't work right (see note above)
                                if (word[0] == argumentdelimiter)
                                {
                                    // We are now in the parent function
                                    bracketlevel++;
                                    argindex = 0;
                                }
                                // Now check if this is a keyword
                                else if (scriptconfig.IsKeyword(word))
                                {
                                    // Found it!
                                    curfunctionname     = scriptconfig.GetKeywordCase(word);
                                    curargumentindex    = argindex;
                                    curfunctionstartpos = wordstart;
                                    break;
                                }
                                else
                                {
                                    // Don't know this word
                                    break;
                                }
                            }
                        }
                    }
                    // Argument delimiter
                    else if (curchar == argumentdelimiter)
                    {
                        // Only count these at brackt level 0
                        if (bracketlevel == 0)
                        {
                            argindex++;
                        }
                    }
                    // Terminator
                    else if (curchar == terminator)
                    {
                        // Can't find anything, break now
                        break;
                    }
                }
            }
        }