//==============================================================================
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;
}
void CodeEditorComponent::paste()
{
    newTransaction();
    const String clip (SystemClipboard::getTextFromClipboard());

    if (clip.isNotEmpty())
        insertTextAtCaret (clip);

    newTransaction();
}
void CodeEditorComponent::insertTabAtCaret()
{
    if (CharacterFunctions::isWhitespace (caretPos.getCharacter())
         && caretPos.getLineNumber() == caretPos.movedBy (1).getLineNumber())
    {
        moveCaretTo (document.findWordBreakAfter (caretPos), false);
    }

    if (useSpacesForTabs)
    {
        const int caretCol = indexToColumn (caretPos.getLineNumber(), caretPos.getIndexInLine());
        const int spacesNeeded = spacesPerTab - (caretCol % spacesPerTab);
        insertTextAtCaret (String::repeatedString (T(" "), spacesNeeded));
    }
    else
    {
        insertTextAtCaret (T("\t"));
    }
}
//==============================================================================
bool CodeEditorComponent::keyPressed (const KeyPress& key)
{
    if (! TextEditorKeyMapper<CodeEditorComponent>::invokeKeyFunction (*this, key))
    {
        if (key == KeyPress::tabKey || key.getTextCharacter() == '\t')      handleTabKey();
        else if (key == KeyPress::returnKey)                                handleReturnKey();
        else if (key == KeyPress::escapeKey)                                handleEscapeKey();
        else if (key == KeyPress ('[', ModifierKeys::commandModifier, 0))   unindentSelection();
        else if (key == KeyPress (']', ModifierKeys::commandModifier, 0))   indentSelection();
        else if (key.getTextCharacter() >= ' ')                             insertTextAtCaret (String::charToString (key.getTextCharacter()));
        else                                                                return false;
    }

    pimpl->handleUpdateNowIfNeeded();
    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::cut()
{
    insertTextAtCaret (String::empty);
}
void CodeEditorComponent::handleReturnKey()
{
    insertTextAtCaret (document.getNewLineCharacters());
}