// // The EditLoop: One loop for each key pressed! // private string EditLoop(IQueryCompiledError queryCompiledError) { while (true) { // // Fill m_UndoRedoStates // m_UndoRedoStates.Append(m_CurrentState); // // Re-write the query each time a key is pressed // m_ConsoleWriter.Rewrite(m_CurrentState); // Eventually show errors if (queryCompiledError != null) { // Show errors once... m_ConsoleWriter.ShowQueryCompiledErrors(m_CurrentState, queryCompiledError); // ... and then set queryCompiledError to null to avoid showing them anymore queryCompiledError = null; } // // ReadKey! // var consoleKeyInfo = Console.ReadKey(); // // Handle special console key // switch (consoleKeyInfo.Key) { case ConsoleKey.F5: // F5 --> Run the query!; Console.CursorVisible = false; m_ConsoleWriter.Rewrite(m_CurrentState); // <-- need to rewrite the query, else the char where the cursor was is hidden! m_ConsoleWriter.SetCursorPosition(0, m_CurrentState.Lines.Count); Console.CursorLeft = 0; Console.CursorVisible = true; m_AlreadyEditedQueries.AppendButDontClearNext(m_CurrentState.QueryString); return(m_CurrentState.QueryString); case ConsoleKey.Escape: return(null); case ConsoleKey.PageUp: m_ConsoleWriter.ClearQueryEdit(m_CurrentState); m_CurrentState = new State(m_AlreadyEditedQueries.GetPrevious(), 0, 0, Location.Null); m_UndoRedoStates.Clear(m_CurrentState); continue; case ConsoleKey.PageDown: m_ConsoleWriter.ClearQueryEdit(m_CurrentState); m_CurrentState = new State(m_AlreadyEditedQueries.GetNext(), 0, 0, Location.Null); m_UndoRedoStates.Clear(m_CurrentState); continue; } // // Handle the key inputed with the appropriate keyHandler // foreach (var keyHandler in m_KeyHandlers) { if (!keyHandler.IsHandlerFor(consoleKeyInfo)) { continue; } m_CurrentState = keyHandler.Handle(consoleKeyInfo, m_CurrentState); break; } continue; } }
//----------------------------------------------------------------- // // Show query compilation error // //----------------------------------------------------------------- internal void ShowQueryCompiledErrors(State state, IQueryCompiledError queryCompiledError) { Debug.Assert(state != null); Debug.Assert(queryCompiledError != null); // // Write errors descriptions // Console.ForegroundColor = ConsoleColor.Red; Console.BackgroundColor = ConsoleColor.Black; var lines = state.Lines; Console.CursorTop = m_QueryEditionTop + lines.Count; Console.CursorLeft = 0; var nbErrors = queryCompiledError.Errors.Count; Console.WriteLine(nbErrors + " compilation error" + (nbErrors > 1 ? "s" : "")); foreach (var error in queryCompiledError.Errors) { Console.WriteLine(" " + error.Description); } var actualY = Console.CursorTop - m_QueryEditionTop; m_MaxNbLines = actualY > m_MaxNbLines ? actualY : m_MaxNbLines; // // Highlight errors on query edited! // Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.White; var queryString = state.QueryString; var consoleWidth = Console.WindowWidth; foreach (var error in queryCompiledError.Errors) { var startPos = error.SubStringStartPos; var length = error.SubStringLength; Debug.Assert(startPos + length <= queryString.Length); var location = new Location(lines, startPos); length = length > AvailableWidth ? AvailableWidth : length; var errorString = queryString.Substring(startPos, length); // An error can spawn several lines! errorString = errorString.Replace(LINE_FEED, "\n"); var errorLines = errorString.Split(new [] { '\n' }); var x = PROMPTER.Length + location.X; for (var y = 0; y < errorLines.Length; y++) { Console.CursorTop = m_QueryEditionTop + location.Y + y; var errorLine = errorLines[y]; if (x + errorLine.Length < consoleWidth) { Console.CursorLeft = x; Console.Write(errorLine); } x = PROMPTER.Length; } Console.CursorTop = m_QueryEditionTop; Console.CursorLeft = PROMPTER.Length; } Console.BackgroundColor = BACK_COLOR; Console.ForegroundColor = FORE_COLOR; SetCursorPosition(state.CursorX, state.CursorY); }
internal string ShowCompilatioErrorsAndThenGetQueryString(IQueryCompiledError queryCompiledError) { Debug.Assert(queryCompiledError != null); Debug.Assert(m_CurrentState != null); return(EditLoop(queryCompiledError)); }