//==============================================================================
bool CodeEditorComponent::keyPressed (const KeyPress& key)
{
    if (! TextEditorKeyMapper<CodeEditorComponent>::invokeKeyFunction (*this, key))
    {
        if (key == KeyPress::tabKey || key.getTextCharacter() == '\t')
        {
            insertTabAtCaret();
        }
        else if (key == KeyPress::returnKey)
        {
            newTransaction();
            insertTextAtCaret (document.getNewLineCharacters());
        }
        else if (key.isKeyCode (KeyPress::escapeKey))
        {
            newTransaction();
        }
        else if (key.getTextCharacter() >= ' ')
        {
            insertTextAtCaret (String::charToString (key.getTextCharacter()));
        }
        else
        {
            return false;
        }
    }

    return true;
}
//==============================================================================
bool CodeEditorComponent::keyPressed (const KeyPress& key)
{
    const bool moveInWholeWordSteps = key.getModifiers().isCtrlDown() || key.getModifiers().isAltDown();
    const bool shiftDown = key.getModifiers().isShiftDown();

    if (key.isKeyCode (KeyPress::leftKey))
    {
        cursorLeft (moveInWholeWordSteps, shiftDown);
    }
    else if (key.isKeyCode (KeyPress::rightKey))
    {
        cursorRight (moveInWholeWordSteps, shiftDown);
    }
    else if (key.isKeyCode (KeyPress::upKey))
    {
        if (key.getModifiers().isCtrlDown() && ! shiftDown)
            scrollDown();
#if JUCE_MAC
        else if (key.getModifiers().isCommandDown())
            goToStartOfDocument (shiftDown);
#endif
        else
            cursorUp (shiftDown);
    }
    else if (key.isKeyCode (KeyPress::downKey))
    {
        if (key.getModifiers().isCtrlDown() && ! shiftDown)
            scrollUp();
#if JUCE_MAC
        else if (key.getModifiers().isCommandDown())
            goToEndOfDocument (shiftDown);
#endif
        else
            cursorDown (shiftDown);
    }
    else if (key.isKeyCode (KeyPress::pageDownKey))
    {
        pageDown (shiftDown);
    }
    else if (key.isKeyCode (KeyPress::pageUpKey))
    {
        pageUp (shiftDown);
    }
    else if (key.isKeyCode (KeyPress::homeKey))
    {
        if (moveInWholeWordSteps)
            goToStartOfDocument (shiftDown);
        else
            goToStartOfLine (shiftDown);
    }
    else if (key.isKeyCode (KeyPress::endKey))
    {
        if (moveInWholeWordSteps)
            goToEndOfDocument (shiftDown);
        else
            goToEndOfLine (shiftDown);
    }
    else if (key.isKeyCode (KeyPress::backspaceKey))
    {
        backspace (moveInWholeWordSteps);
    }
    else if (key.isKeyCode (KeyPress::deleteKey))
    {
        deleteForward (moveInWholeWordSteps);
    }
    else if (key == KeyPress (T('c'), ModifierKeys::commandModifier, 0))
    {
        copy();
    }
    else if (key == KeyPress (T('x'), ModifierKeys::commandModifier, 0))
    {
        copyThenCut();
    }
    else if (key == KeyPress (T('v'), ModifierKeys::commandModifier, 0))
    {
        paste();
    }
    else if (key == KeyPress (T('z'), ModifierKeys::commandModifier, 0))
    {
        undo();
    }
    else if (key == KeyPress (T('y'), ModifierKeys::commandModifier, 0)
              || key == KeyPress (T('z'), ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
    {
        redo();
    }
    else if (key == KeyPress (T('a'), ModifierKeys::commandModifier, 0))
    {
        selectAll();
    }
    else if (key == KeyPress::tabKey || key.getTextCharacter() == '\t')
    {
        insertTabAtCaret();
    }
    else if (key == KeyPress::returnKey)
    {
        newTransaction();
        insertTextAtCaret (document.getNewLineCharacters());
    }
    else if (key.isKeyCode (KeyPress::escapeKey))
    {
        newTransaction();
    }
    else if (key.getTextCharacter() >= ' ')
    {
        insertTextAtCaret (String::charToString (key.getTextCharacter()));
    }
    else
    {
        return false;
    }

    return true;
}
void CodeEditorComponent::handleTabKey()
{
    insertTabAtCaret();
}