Beispiel #1
0
        public string GetExpression(int pos, bool onlyWord, ChameleonEditor editor, bool forCC)
        {
            bool cont  = true;
            int  depth = 0;

            ChameleonEditor ctrl = (editor == null) ? m_editor : editor;


            int  position  = pos;
            int  at        = position;
            bool prevGt    = false;
            bool prevColon = false;

            while (cont && depth >= 0)
            {
                char ch = ctrl.PreviousChar(pos, ref at, true);
                position = at;
                //Eof?
                if (ch == 0)
                {
                    at = 0;
                    break;
                }

                //Comment?
                int style = ctrl.Styles.GetStyleAt(pos);
                if (IsCommentOrString(pos))
                {
                    continue;
                }

                switch (ch)
                {
                case ';':
                {
                    // don't include this token
                    at        = ctrl.NativeInterface.PositionAfter(at);
                    cont      = false;
                    prevColon = false;
                    break;
                }

                case '-':
                {
                    if (prevGt)
                    {
                        prevGt = false;
                        //if previous char was '>', we found an arrow so reduce the depth
                        //which was increased
                        depth--;
                    }
                    else
                    {
                        if (depth <= 0)
                        {
                            //don't include this token
                            at   = ctrl.NativeInterface.PositionAfter(at);
                            cont = false;
                        }
                    }
                    prevColon = false;
                    break;
                }

                case ' ':
                case '\n':
                case '\v':
                case '\t':
                case '\r':
                {
                    prevGt    = false;
                    prevColon = false;
                    if (depth <= 0)
                    {
                        cont = false;
                        break;
                    }
                    break;
                }

                case '{':
                case '=':
                {
                    prevGt    = false;
                    prevColon = false;
                    cont      = false;
                    break;
                }

                case '(':
                case '[':
                {
                    depth--;
                    prevGt    = false;
                    prevColon = false;
                    if (depth < 0)
                    {
                        //don't include this token
                        at   = ctrl.NativeInterface.PositionAfter(at);
                        cont = false;
                    }
                    break;
                }

                case ',':
                case '*':
                case '&':
                case '!':
                case '~':
                case '+':
                case '^':
                case '|':
                case '%':
                case '?':
                {
                    prevGt    = false;
                    prevColon = false;
                    if (depth <= 0)
                    {
                        //don't include this token
                        at   = ctrl.NativeInterface.PositionAfter(at);
                        cont = false;
                    }
                    break;
                }

                case '>':
                {
                    prevGt    = true;
                    prevColon = false;
                    depth++;
                    break;
                }

                case '<':
                {
                    prevGt    = false;
                    prevColon = false;
                    depth--;
                    if (depth < 0)
                    {
                        //don't include this token
                        at   = ctrl.NativeInterface.PositionAfter(at);
                        cont = false;
                    }
                    break;
                }

                case ')':
                case ']':
                {
                    prevGt    = false;
                    prevColon = false;
                    depth++;
                    break;
                }

                default:
                {
                    prevGt    = false;
                    prevColon = false;
                    break;
                }
                }
            }

            if (at < 0)
            {
                at = 0;
            }
            string expr = new Range(at, pos, ctrl).Text;

            if (!forCC)
            {
                // If we do not require the expression for CodeCompletion
                // return the un-touched buffer
                return(expr);
            }

            //remove comments from it
            CPPScannerWrapper sc = new CPPScannerWrapper();

            sc.SetText(expr);
            string expression = "";
            int    type       = 0;

            for (type = sc.Lex(); type != 0;)
            {
                string token = sc.TokenText();
                expression += token + " ";
            }

            return(expression);
        }
Beispiel #2
0
        public void AutoIndent(char ch)
        {
            if (m_editor.Indentation.SmartIndentType == ScintillaNet.SmartIndent.CPP2)
            {
                return;
            }

            int curpos = m_editor.CurrentPos;

            if (IsComment(curpos) && ch == '\n')
            {
                // TODO wanted?  needed?
                //AutoAddComment();
                SimpleIndent(ch);
                return;
            }

            INativeScintilla ns = m_editor.NativeInterface;

            if (IsCommentOrString(curpos))
            {
                SimpleIndent(ch);

                return;
            }

            int line = ns.LineFromPosition(curpos);

            if (ch == '\n')
            {
                int    prevpos  = -1;
                int    foundPos = -1;
                string word;

                char prevChar = m_editor.PreviousChar(curpos, ref prevpos, false);
                word = m_editor.PreviousWord(curpos, ref foundPos);

                // user hit ENTER after 'else'
                if (word == "else")
                {
                    int prevLine   = ns.LineFromPosition(prevpos);
                    int prevIndent = ns.GetLineIndentation(prevLine);
                    ns.SetLineIndentation(line, prevIndent);
                    m_editor.SetCaretAt(ns.GetLineIndentPosition(line));
                    ns.ChooseCaretX();                     // set new column as "current" column
                    return;
                }

                // User typed 'ENTER' immediately after closing brace ')'
                if (prevpos != -1 && ch == ')')
                {
                    int openBracePos           = -1;
                    int posWordBeforeOpenBrace = -1;

                    if (m_editor.MatchBraceBack(')', prevpos, ref openBracePos))
                    {
                        m_editor.PreviousChar(openBracePos, ref posWordBeforeOpenBrace, false);
                        if (posWordBeforeOpenBrace != -1)
                        {
                            word = m_editor.PreviousWord(posWordBeforeOpenBrace, ref foundPos);

                            // c++ expression with single line and should be treated separatly
                            if (word == "if" || word == "while" || word == "for")
                            {
                                int prevLine = ns.LineFromPosition(prevpos);
                                ns.SetLineIndentation(line, ns.GetIndent() + ns.GetLineIndentation(prevLine));
                                m_editor.SetCaretAt(ns.GetLineIndentPosition(line));
                                ns.ChooseCaretX();                                 // set new column as "current" column
                                return;
                            }
                        }
                    }
                }

                // User typed 'ENTER' immediately after colons ':'
                if (prevpos != -1 && ch == ':')
                {
                    int posWordBeforeColons = -1;

                    m_editor.PreviousChar(prevpos, ref posWordBeforeColons, false);
                    if (posWordBeforeColons != -1)
                    {
                        word = m_editor.PreviousWord(posWordBeforeColons, ref foundPos);
                        int prevLine = ns.LineFromPosition(posWordBeforeColons);

                        // If we found one of the following keywords, un-indent their line by (foldLevel - 1)*indentSize
                        if (word == "public" || word == "private" || word == "protected")
                        {
                            SimpleIndent(ch);

                            // Indent this line according to the block indentation level

                            int foldLevel = (int)(ns.GetFoldLevel(prevLine) & (int)FoldLevel.NumberMask) - (int)FoldLevel.Base;
                            if (foldLevel > 0)
                            {
                                ns.SetLineIndentation(prevLine, ((foldLevel - 1) * ns.GetIndent()));
                                ns.ChooseCaretX();
                            }
                            return;
                        }
                    }
                }

                // use the previous line indentation level
                if (prevpos == -1 || ch != '{' || IsCommentOrString(prevpos))
                {
                    SimpleIndent(ch);
                    return;
                }

                // Open brace? increase indent size
                int prevLineNum = ns.LineFromPosition(prevpos);
                int indent2     = ns.GetIndent();
                int lineIndent  = ns.GetLineIndentation(prevLineNum);
                ns.SetLineIndentation(line, indent2 + lineIndent);                //ns.GetIndent() + ns.GetLineIndentation(prevLineNum));
                m_editor.SetCaretAt(ns.GetLineIndentPosition(line));
            }
            else if (ch == '}')
            {
                int matchPos = -1;
                if (!m_editor.MatchBraceBack('}', ns.PositionBefore(curpos), ref matchPos))
                {
                    return;
                }
                int secondLine = ns.LineFromPosition(matchPos);
                if (secondLine == line)
                {
                    return;
                }
                ns.SetLineIndentation(line, ns.GetLineIndentation(secondLine));
            }
            else if (ch == '{')
            {
                string lineString = m_editor.Lines[line].Text;
                lineString.Trim();

                int  matchPos     = -1;
                char previousChar = m_editor.PreviousChar(ns.PositionBefore(curpos), ref matchPos, false);

                if (previousChar != '{' && lineString == "{")
                {
                    // indent this line according to the previous line
                    int currLine = ns.LineFromPosition(m_editor.CurrentPos);
                    ns.SetLineIndentation(line, ns.GetLineIndentation(currLine - 1));
                    ns.ChooseCaretX();
                }
            }

            // set new column as "current" column
            ns.ChooseCaretX();
        }