Exemplo n.º 1
0
 void CmdEnterDataOrCommand()
 {
     CmdEnd();
     if (RuntimeConsoleBase.IsCompleteSourceCode(lineBuffer.ToString()))
     {
         done = true;
     }
     else
     {
         CmdDataChar('\n');
     }
 }
Exemplo n.º 2
0
        public static string ReplReadImp()
        {
            var top       = Console.CursorTop;
            var left      = Console.CursorLeft;
            var len       = 0;
            var pos       = 0;
            var buffer    = new StringBuilder();
            var col       = 0;
            var row       = 0;
            var topChoice = 0;

            Action <char> writeChar = (char ch) =>
            {
                var r1 = Console.CursorTop;
                var c1 = Console.CursorLeft;
                Console.Write(ch);
                var r2 = Console.CursorTop;
                var c2 = Console.CursorLeft;
                if (r1 == Console.BufferHeight - 1)
                {
                    // on last row
                    if (c1 > c2 || ch == '\n')
                    {
                        // scrolled
                        --top;
                        --topChoice;
                        --row;
                    }
                }
            };

            Action Paint = () =>
            {
                //
                // update display
                //

                Console.SetCursorPosition(left, top);
                row = top;
                col = left;

                for (var i = 0; i < len; ++i)
                {
                    writeChar(buffer[i]);

                    if (i + 1 == pos)
                    {
                        row = Console.CursorTop;
                        col = Console.CursorLeft;
                    }
                }

                writeChar(' ');
            };

            Action Erase = () =>
            {
                var start = Console.CursorLeft + Console.CursorTop * Console.BufferWidth;
                var end   = (Console.WindowTop + Console.WindowHeight) * Console.BufferWidth - 1;
                if (start < end)
                {
                    var blanks = new String(' ', end - start);
                    Console.Write(blanks);
                }
            };

            while (true)
            {
                Paint();
                Erase();

                //
                // get next key
                //

                Console.SetCursorPosition(col, row);
                var keyInfo = Console.ReadKey(true);
                var key     = keyInfo.Key;
                var mod     = keyInfo.Modifiers;
                var ch      = keyInfo.KeyChar;

                switch (key)
                {
                case ConsoleKey.Backspace:
                {
                    if (pos > 0)
                    {
                        --pos;
                        --len;
                        buffer.Remove(pos, 1);
                    }
                    break;
                }

                case ConsoleKey.Delete:
                {
                    if (pos < len)
                    {
                        --len;
                        buffer.Remove(pos, 1);
                    }
                    break;
                }

                case ConsoleKey.Enter:
                {
                    var s = buffer.ToString();
                    if (mod != ConsoleModifiers.Control && IsCompleteSourceCode(s))
                    {
                        return(s);
                    }
                    else
                    {
                        writeChar('\n');
                        buffer.Append('\n');
                        ++pos;
                        ++len;
                    }
                    break;
                }

                case ConsoleKey.Home:
                {
                    pos = 0;
                    break;
                }

                case ConsoleKey.End:
                {
                    pos = len;
                    break;
                }

                case ConsoleKey.LeftArrow:
                {
                    if (pos > 0)
                    {
                        --pos;
                    }
                    break;
                }

                case ConsoleKey.RightArrow:
                {
                    if (pos < len)
                    {
                        ++pos;
                    }
                    break;
                }

                case ConsoleKey.UpArrow:
                {
                    if (History != null)
                    {
                        var s = History.Previous();
                        buffer = new StringBuilder(s);
                        pos    = len = buffer.Length;
                    }
                    break;
                }

                case ConsoleKey.DownArrow:
                {
                    if (History != null)
                    {
                        var s = History.Next();
                        buffer = new StringBuilder(s);
                        pos    = len = buffer.Length;
                    }
                    break;
                }

                case ConsoleKey.Escape:
                {
                    buffer.Clear();
                    pos = len = buffer.Length;
                    Paint();
                    Erase();
                    Console.SetCursorPosition(col, row);
                    return(null);
                }

                case ConsoleKey.Tab:
                {
                    var line        = buffer.ToString();
                    var loc         = Runtime.LocateLeftWord(line, pos);
                    var searchTerm  = line.Substring(loc.Begin, loc.Span);
                    var completions = RuntimeConsoleBase.GetCompletions(searchTerm);
                    var posOrig     = pos;
                    var index       = 0;
                    var leftChoice  = 0;
                    var done        = false;
                    pos = len;
                    while (!done)
                    {
                        Paint();
                        writeChar('\n');
                        for (var i = 0; i < completions.Count; ++i)
                        {
                            foreach (var ch3 in completions[i])
                            {
                                writeChar(ch3);
                            }
                            if (i == index)
                            {
                                topChoice  = Console.CursorTop;
                                leftChoice = Console.CursorLeft;
                            }
                            writeChar(' ');
                            writeChar(' ');
                        }
                        Erase();
                        Console.SetCursorPosition(leftChoice, topChoice);
                        var keyInfo2 = Console.ReadKey(true);
                        var key2     = keyInfo2.Key;
                        if (key2 == ConsoleKey.DownArrow || key2 == ConsoleKey.Enter || (key2 == ConsoleKey.Tab && completions.Count == 1))
                        {
                            line = line.Remove(loc.Begin, loc.Span).Insert(loc.Begin, completions[index]);
                            pos  = loc.Begin + completions[index].Length;
                            if (pos == line.Length || !char.IsWhiteSpace(line, pos))
                            {
                                line = line.Insert(pos, " ");
                            }
                            ++pos;
                            buffer = new StringBuilder(line);
                            len    = buffer.Length;
                            done   = true;
                        }
                        else if (key2 == ConsoleKey.LeftArrow || (key2 == ConsoleKey.Tab && (keyInfo2.Modifiers & ConsoleModifiers.Shift) != 0))
                        {
                            // Stay positive.
                            index = (index + completions.Count - 1) % completions.Count;
                        }
                        else if (key2 == ConsoleKey.RightArrow || key2 == ConsoleKey.Tab)
                        {
                            index = (index + 1) % completions.Count;
                        }
                        else if (key2 == ConsoleKey.UpArrow || key2 == ConsoleKey.Escape)
                        {
                            pos  = posOrig;
                            done = true;
                        }
                    }
                    break;
                }

                default:
                {
                    if (mod == ConsoleModifiers.Control)
                    {
                        switch (key)
                        {
                        case ConsoleKey.V:
                            var text = Runtime.GetClipboardData();
                            foreach (var ch2 in text)
                            {
                                var ch3 = (ch2 == '\n' || ch2 >= ' ') ? ch2 : ' ';
                                buffer.Insert(pos, ch3);
                                ++pos;
                                ++len;
                            }
                            break;

                        case ConsoleKey.C:
                            var text2 = buffer.ToString();
                            Runtime.SetClipboardData(text2);
                            break;

                        case ConsoleKey.U:
                            buffer.Remove(0, pos);
                            pos = 0;
                            len = buffer.Length;
                            break;

                        case ConsoleKey.K:
                            buffer.Remove(pos, buffer.Length - pos);
                            len = buffer.Length;
                            break;

                        case ConsoleKey.W:
                            var line = buffer.ToString();
                            var loc  = Runtime.LocateLeftWord(line, pos);
                            buffer.Remove(loc.Begin, pos - loc.Begin);
                            pos = loc.Begin;
                            len = buffer.Length;
                            break;
                        }
                    }
                    else if (ch >= ' ')
                    {
                        buffer.Insert(pos, ch);
                        ++pos;
                        ++len;
                    }
                    break;
                }
                }
            }
        }
Exemplo n.º 3
0
 protected override void OnFormClosed(FormClosedEventArgs e)
 {
     base.OnFormClosed(e);
     RuntimeConsoleBase.Quit();
 }
Exemplo n.º 4
0
        internal void CmdCodeCompletion()
        {
            if (!CodeCompletion)
            {
                return;
            }
            var line        = lineBuffer.ToString();
            var loc         = Runtime.LocateLeftWord(line, lineIndex);
            var searchTerm  = line.Substring(loc.Begin, loc.Span);
            var completions = RuntimeConsoleBase.GetCompletions(searchTerm);
            var index       = 0;

            savedPos = CursorPos;

            while (true)
            {
                CursorPos = savedPos + charsWritten;
                WriteLine();
                for (var i = 0; i < completions.Count; ++i)
                {
                    if (i == index)
                    {
                        Highlight = true;
                    }
                    Write(completions[i]);
                    if (i == index)
                    {
                        Highlight = false;
                    }
                    Write(' ');
                }
                ClearToBot();
                var info2 = ReadKey(false);
                var key2  = info2.KeyData;
                if (key2 == Keys.Down || key2 == Keys.Enter || (key2 == Keys.Tab && completions.Count == 1))
                {
                    CursorPos = savedPos + charsWritten;
                    ClearToBot();
                    CursorPos = savedPos;
                    line      = line.Remove(loc.Begin, loc.Span).Insert(loc.Begin, completions[index]);
                    lineIndex = loc.Begin + completions[index].Length;
                    if (lineIndex == line.Length || !char.IsWhiteSpace(line, lineIndex))
                    {
                        line = line.Insert(lineIndex, " ");
                    }
                    ++lineIndex;
                    lineBuffer = new StringBuilder(line);
                    break;
                }
                else if (key2 == Keys.Left || key2 == (Keys.Tab | Keys.Shift))
                {
                    // Stay positive.
                    index = (index + completions.Count - 1) % completions.Count;
                }
                else if (key2 == Keys.Right || key2 == Keys.Tab)
                {
                    index = (index + 1) % completions.Count;
                }
                else if (key2 == Keys.Up || key2 == Keys.Escape || key2 == RuntimeRepl.PseudoKeyForResizeEvent)
                {
                    CursorPos = savedPos + charsWritten;
                    ClearToBot();
                    CursorPos = savedPos;
                    break;
                }
            }
        }