예제 #1
0
파일: snipwiz.cpp 프로젝트: eranif/codelite
//------------------------------------------------------------
void SnipWiz::OnMenuExpandSwitch(wxCommandEvent& e)
{
    IEditor* editor = GetEditor();
    if(!editor) return;

    bool isSelection = false;
    wxString var = editor->GetSelection();
    if(!var.IsEmpty()) isSelection = true;
    var = ::wxGetTextFromUser(_("Enter identifier name"), _("switch(...)"), var);
    if(var.IsEmpty()) return;
    long count = ::wxGetNumberFromUser(_("Enter number of cases"), _("Cases:"), _("switch(...)"), 1, 1, 20);
    if(count < 1) return;

    int curEol = editor->GetEOL();
    int curPos = editor->GetCurrentPosition();
    wxString tabs = GetTabs(editor, curPos);

    wxString output = wxString::Format(wxT("switch( %s )%s%s{%s"), var.c_str(), eol[curEol].c_str(), tabs.c_str(),
                                       eol[curEol].c_str());
    for(long i = 0; i < count; i++)
        output += wxString::Format(wxT("%scase :%s%sbreak;%s"), tabs.c_str(), eol[curEol].c_str(), tabs.c_str(),
                                   eol[curEol].c_str());

    output += tabs.c_str();
    output += wxT("}");
    if(isSelection)
        editor->ReplaceSelection(output);
    else
        editor->InsertText(curPos, output);
}
예제 #2
0
파일: snipwiz.cpp 프로젝트: eranif/codelite
//------------------------------------------------------------
void SnipWiz::OnMenuSnippets(wxCommandEvent& e)
{
    IEditor* editor = GetEditor();
    if(!editor) return;

    bool crtl = ::wxGetKeyState(WXK_CONTROL);
    bool sourceIsMenu(false);

    wxMenu* m = dynamic_cast<wxMenu*>(e.GetEventObject());
    if(m) { sourceIsMenu = true; }

    if(e.GetId() >= IDM_ADDSTART && e.GetId() < (IDM_ADDSTART + (int)m_snippets.GetCount())) {
        wxString key = m_snippets.Item(e.GetId() - IDM_ADDSTART);
        wxString srText = m_StringDb.GetSnippetString(key);
        wxString selection = editor->GetSelection();

        // replace template eols with current
        int curEol = editor->GetEOL();
        if(srText.Find(eol[2]) != wxNOT_FOUND) srText.Replace(eol[2], eol[curEol].c_str());

        // Replace any escaped carets/selection strings
        srText.Replace(USER_ESC_CARET, TMP_ESC_CARET_STR);
        srText.Replace(USER_ESC_SELECTION, TMP_ESC_SELECTION_STR);
        srText.Replace(CARET, REAL_CARET_STR);
        srText.Replace(SELECTION, REAL_SELECTION_STR);

        // selection ?
        if(srText.Find(REAL_SELECTION_STR) != wxNOT_FOUND) srText.Replace(REAL_SELECTION_STR, selection.c_str());

        // restore the escaped selection, this time without the escaping backslash
        srText.Replace(TMP_ESC_SELECTION_STR, SELECTION);

        // restore the escaped caret, this time without the escaping backslash
        srText.Replace(TMP_ESC_CARET_STR, CARET);

        // if the user pressed control while clicking
        if(crtl && sourceIsMenu) {
            m_clipboard = srText;
            // remove caret mark if there
            srText.Replace(REAL_CARET_STR, wxT(""));

            // copy text to clipboard
            if(wxTheClipboard->Open()) {
                wxTheClipboard->SetData(new wxTextDataObject(srText));
                wxTheClipboard->Close();
            }
        } else {
            // otherwise insert text

            // format the text for insertion
            wxString output = FormatOutput(editor, srText);

            int curPos = editor->GetCurrentPosition();
            if(selection.Len() != 0) { curPos = editor->GetSelectionStart(); }

            // get caret position
            long cursorPos = output.Find(REAL_CARET_STR);
            if(cursorPos != wxNOT_FOUND) output.Remove(cursorPos, wxStrlen(REAL_CARET_STR));
            editor->ReplaceSelection(output);
            // set caret
            if(cursorPos != wxNOT_FOUND)
                editor->SetCaretAt(curPos + cursorPos);
            else
                editor->SetCaretAt(curPos + output.Len());
        }
    }
}
예제 #3
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;
}