void PHPEditorContextMenu::OnInsertDoxyComment(wxCommandEvent& e)
{
    IEditor* editor = m_manager->GetActiveEditor();
    if(editor) {
        PHPEntityBase::Ptr_t entry =
            PHPCodeCompletion::Instance()->GetPHPEntityAtPos(editor, editor->GetCurrentPosition());
        if(entry) {
            wxStyledTextCtrl* ctrl = editor->GetCtrl();
            ctrl->BeginUndoAction();
            wxString comment = entry->FormatPhpDoc();

            // Create the whitespace buffer
            int lineStartPos = ctrl->PositionFromLine(ctrl->GetCurrentLine());
            int lineEndPos = lineStartPos + ctrl->LineLength(ctrl->GetCurrentLine());

            // Collect all whitespace from the begining of the line until the first non whitespace
            // character we find
            wxString whitespace;
            for(int i = lineStartPos; lineStartPos < lineEndPos; ++i) {
                if(ctrl->GetCharAt(i) == ' ' || ctrl->GetCharAt(i) == '\t') {
                    whitespace << (wxChar)ctrl->GetCharAt(i);
                } else {
                    break;
                }
            }

            // Prepare the comment block
            wxArrayString lines = ::wxStringTokenize(comment, "\n", wxTOKEN_STRTOK);
            for(size_t i = 0; i < lines.size(); ++i) {
                lines.Item(i).Prepend(whitespace);
            }

            // Glue the lines back together
            wxString doxyBlock = ::wxJoin(lines, '\n');
            doxyBlock << "\n";

            // Insert the text
            ctrl->InsertText(lineStartPos, doxyBlock);

            // Try to place the caret after the @brief
            wxRegEx reBrief("[@\\]brief[ \t]*");
            if(reBrief.IsValid() && reBrief.Matches(doxyBlock)) {
                wxString match = reBrief.GetMatch(doxyBlock);
                // Get the index
                int where = doxyBlock.Find(match);
                if(where != wxNOT_FOUND) {
                    where += match.length();
                    int caretPos = lineStartPos + where;
                    editor->SetCaretAt(caretPos);
                    // Remove the @brief as its non standard in the PHP world
                    editor->GetCtrl()->DeleteRange(caretPos - match.length(), match.length());
                }
            }
            editor->GetCtrl()->EndUndoAction();
        }
    }
}
Exemplo n.º 2
0
void PHPCodeCompletion::OnInsertDoxyBlock(clCodeCompletionEvent& e)
{
    e.Skip();

    // Do we have a workspace open?
    CHECK_COND_RET(PHPWorkspace::Get()->IsOpen());

    // Sanity
    IEditor* editor = dynamic_cast<IEditor*>(e.GetEditor());
    CHECK_PTR_RET(editor);

    // Is this a PHP editor?
    CHECK_COND_RET(IsPHPFile(editor));

    // Get the text from the caret current position
    // until the end of file
    wxString unsavedBuffer = editor->GetTextRange(editor->GetCurrentPosition(), editor->GetLength());
    unsavedBuffer.Trim().Trim(false);
    PHPSourceFile source("<?php " + unsavedBuffer);
    source.SetParseFunctionBody(false);
    source.Parse();

    PHPEntityBase::Ptr_t ns = source.Namespace();
    if(ns) {
        const PHPEntityBase::List_t& children = ns->GetChildren();
        for(PHPEntityBase::List_t::const_iterator iter = children.begin(); iter != children.end(); ++iter) {
            PHPEntityBase::Ptr_t match = *iter;
            if(match->GetLine() == 0 && match->Is(kEntityTypeFunction)) {
                e.Skip(false); // notify codelite to stop processing this event
                wxString phpdoc = match->FormatPhpDoc();
                phpdoc.Trim();
                e.SetTooltip(phpdoc);
            }
        }
    }
}