Example #1
0
bool AbbreviationPlugin::InsertExpansion(const wxString& abbreviation)
{
    // get the active editor
    IEditor *editor = m_mgr->GetActiveEditor();

    if (!editor || !abbreviation)
        return false;

    // hide the completion box
    if (editor->IsCompletionBoxShown()) {
        editor->HideCompletionBox();
    }

    // search for abbreviation that matches str
    // prepate list of abbreviations
    AbbreviationJSONEntry jsonData;
    if ( !m_config.ReadItem(&jsonData) ) {
        // merge the data from the old configuration
        AbbreviationEntry data;
        m_mgr->GetConfigTool()->ReadObject(wxT("AbbreviationsData"), &data);
    
        jsonData.SetAutoInsert( data.GetAutoInsert() );
        jsonData.SetEntries( data.GetEntries() );
        m_config.WriteItem( &jsonData );
    }

    // search for the old item
    const JSONElement::wxStringMap_t& entries = jsonData.GetEntries();
    JSONElement::wxStringMap_t::const_iterator iter = entries.find(abbreviation);

    if (iter != entries.end()) {

        wxString text = iter->second;
        int selStart = editor->WordStartPos(editor->GetCurrentPosition(), true);
        int selEnd   = editor->WordEndPos(editor->GetCurrentPosition(), true);
        int curPos   = editor->GetCurrentPosition();
        int typedWordLen = curPos - selStart;    

        if (typedWordLen < 0) {
            typedWordLen = 0;
        }

        // format the text to insert
        bool appendEol(false);
        if (text.EndsWith(wxT("\r")) || text.EndsWith(wxT("\n"))) {
            appendEol = true;
        }            

        text = editor->FormatTextKeepIndent(text, selStart, Format_Text_Save_Empty_Lines);    

        // remove the first line indenation that might have been placed by CL
        text.Trim(false).Trim();

        if (appendEol) {
            wxString eol;
            switch (editor->GetEOL()) {
            case 1:
                eol = wxT("\r");
                break;
            case 0:
                eol = wxT("\r\n");
                break;
            case 2:
                eol = wxT("\n");
                break;
            }
            text << eol;
        }

        //--------------------------------------------
        // replace any place holders
        //--------------------------------------------
        wxString project;
        text = MacroManager::Instance()->Expand(text, m_mgr, editor->GetProjectName());
        
        // locate the caret
        int where = text.Find(wxT("|"));
        if (where == wxNOT_FOUND) {
            where = text.length();
        }

        // remove the pipe (|) character
        text.Replace(wxT("|"), wxT(""));

        if (selEnd - selStart >= 0) {
            editor->SelectText(selStart, selEnd - selStart);
            editor->ReplaceSelection(text);
            editor->SetCaretAt(curPos + where - typedWordLen);
        }
        return true;
    } else
        return false;
}