Exemplo n.º 1
0
void ClangDiagnostics::OnDiagnostics( ClangEvent& event )
{
    ClDiagnosticLevel diagLv = dlFull; // TODO
    bool update = false;
    EditorManager* edMgr = Manager::Get()->GetEditorManager();
    cbEditor* ed = edMgr->GetBuiltinActiveEditor();
    if (!ed)
    {
        std::cout<<"No editor..."<<std::endl;
        return;
    }
    if( event.GetTranslationUnitId() != GetCurrentTranslationUnitId() )
    {
        // Switched translation unit before event delivered
        return;
    }
    if( (diagLv == dlFull)&&(event.GetLocation().line != 0)&&(event.GetLocation().column != 0) )
    {
        update = true;
    }
    const std::vector<ClDiagnostic>& diagnostics = event.GetDiagnosticResults();
    cbStyledTextCtrl* stc = ed->GetControl();
    int firstVisibleLine = stc->GetFirstVisibleLine();
    if ((diagLv == dlFull)&&(!update) )
        stc->AnnotationClearAll();
    const int warningIndicator = 0; // predefined
    const int errorIndicator = 15; // hopefully we do not clash with someone else...
    stc->SetIndicatorCurrent(warningIndicator);
    if ( !update )
        stc->IndicatorClearRange(0, stc->GetLength());
    stc->IndicatorSetStyle(errorIndicator, wxSCI_INDIC_SQUIGGLE);
    stc->IndicatorSetForeground(errorIndicator, *wxRED);
    stc->SetIndicatorCurrent(errorIndicator);
    if ( !update )
        stc->IndicatorClearRange(0, stc->GetLength());
    const wxString& filename = ed->GetFilename();
    if ( (diagLv == dlFull)&&(update) )
    {
        int line = event.GetLocation().line-1;
        stc->AnnotationClearLine(line);
    }
    for ( std::vector<ClDiagnostic>::const_iterator dgItr = diagnostics.begin();
            dgItr != diagnostics.end(); ++dgItr )
    {
        //Manager::Get()->GetLogManager()->Log(dgItr->file + wxT(" ") + dgItr->message + F(wxT(" %d, %d"), dgItr->range.first, dgItr->range.second));
        if (dgItr->file != filename)
            continue;
        if (diagLv == dlFull)
        {
            if( (!update) || ((int)dgItr->line == (int)event.GetLocation().line) )
            {
                wxString str = stc->AnnotationGetText(dgItr->line - 1);
                if (!str.IsEmpty())
                    str += wxT('\n');
                stc->AnnotationSetText(dgItr->line - 1, str + dgItr->message);
                stc->AnnotationSetStyle(dgItr->line - 1, 50);
            }
        }
        int pos = stc->PositionFromLine(dgItr->line - 1) + dgItr->range.first - 1;
        int range = dgItr->range.second - dgItr->range.first;
        if (range == 0)
        {
            range = stc->WordEndPosition(pos, true) - pos;
            if (range == 0)
            {
                pos = stc->WordStartPosition(pos, true);
                range = stc->WordEndPosition(pos, true) - pos;
            }
        }
        if (dgItr->severity == sError)
            stc->SetIndicatorCurrent(errorIndicator);
        else if (  dgItr != diagnostics.begin()
                && dgItr->line == (dgItr - 1)->line
                && dgItr->range.first <= (dgItr - 1)->range.second )
        {
            continue; // do not overwrite the last indicator
        }
        else
            stc->SetIndicatorCurrent(warningIndicator);
        stc->IndicatorFillRange(pos, range);
    }
    if ( diagLv == dlFull )
    {
        stc->AnnotationSetVisible(wxSCI_ANNOTATION_BOXED);
        stc->ScrollToLine(firstVisibleLine);
    }
}
Exemplo n.º 2
0
void ClangDiagnostics::OnDiagnosticsUpdated(ClangEvent& event)
{
    event.Skip();
    if (!IsAttached())
        return;

    ClDiagnosticLevel diagLv = dlFull; // TODO
    bool update = false;
    EditorManager* edMgr = Manager::Get()->GetEditorManager();
    cbEditor* ed = edMgr->GetBuiltinActiveEditor();
    if (!ed)
        return;

    if (event.GetTranslationUnitId() != GetCurrentTranslationUnitId())
    {
        CCLogger::Get()->DebugLog( wxT("OnDiagnostics: tu ID mismatch") );
        // Switched translation unit before event delivered
        return;
    }

    const std::vector<ClDiagnostic>& diagnostics = event.GetDiagnosticResults();
    if ( (diagLv == dlFull)&&(event.GetLocation().line != 0) && (event.GetLocation().column != 0) )
    {
        CCLogger::Get()->DebugLog( wxT("OnDiagnostics: Doing partial update") );
        update = true;
    }
    else
    {
        CCLogger::Get()->DebugLog( wxT("OnDiagnostics: Doing full update") );
        m_Diagnostics = diagnostics;
    }

    cbStyledTextCtrl* stc = ed->GetControl();

    int firstVisibleLine = stc->GetFirstVisibleLine();

    const int warningIndicator = 0; // predefined
    const int errorIndicator = 15; // hopefully we do not clash with someone else...
    stc->SetIndicatorCurrent(warningIndicator);
    if (!update)
        stc->IndicatorClearRange(0, stc->GetLength());
    stc->IndicatorSetStyle(errorIndicator, wxSCI_INDIC_SQUIGGLE);
    stc->IndicatorSetForeground(errorIndicator, *wxRED);
    stc->SetIndicatorCurrent(errorIndicator);
    if (!update)
        stc->IndicatorClearRange(0, stc->GetLength());

    const wxString& filename = ed->GetFilename();
    if (!m_bShowInline)
        stc->AnnotationClearAll();
    else if ((diagLv == dlFull) && update)
    {
        int line = event.GetLocation().line-1;
        stc->AnnotationClearLine(line);
    }
    else
        stc->AnnotationClearAll();

    int lastLine = 0;
    for (std::vector<ClDiagnostic>::const_iterator dgItr = diagnostics.begin();
         dgItr != diagnostics.end(); ++dgItr)
    {
        Manager::Get()->GetLogManager()->Log(dgItr->file + wxT(" ") + dgItr->message + F(wxT(" %d, %d"), dgItr->range.first, dgItr->range.second));
        if (dgItr->file != filename)
        {
            CCLogger::Get()->Log(wxT("WARNING: Filename mismatch in diagnostics !!"));
            continue;
        }
        if (update)
        {
            m_Diagnostics.push_back( *dgItr );
        }
        if (diagLv == dlFull)
        {
            if (update && (lastLine != (dgItr->line - 1)))
            {
                stc->AnnotationClearLine(dgItr->line - 1);
            }
            if (m_bShowInline)
            {
                wxString str = stc->AnnotationGetText(dgItr->line - 1);
                if (!str.IsEmpty())
                    str += wxT('\n');
                if (!str.Contains(dgItr->message))
                {
                    switch (dgItr->severity)
                    {
                    case sWarning:
                        if (m_bShowWarning)
                        {
                            stc->AnnotationSetText(dgItr->line - 1, str + dgItr->message);
                            stc->AnnotationSetStyle(dgItr->line - 1, 51);
                        }
                        break;
                    case sError:
                        if (m_bShowError)
                        {
                            stc->AnnotationSetText(dgItr->line - 1, str + dgItr->message);
                            stc->AnnotationSetStyle(dgItr->line - 1, 52);
                        }
                        break;
                    case sNote:
                        break;
                    }
                }
            }
        }
        int pos = stc->PositionFromLine(dgItr->line - 1) + dgItr->range.first - 1;
        int range = dgItr->range.second - dgItr->range.first;
        if (range == 0)
        {
            range = stc->WordEndPosition(pos, true) - pos;
            if (range == 0)
            {
                pos = stc->WordStartPosition(pos, true);
                range = stc->WordEndPosition(pos, true) - pos;
            }
        }
        if (dgItr->severity == sError)
            stc->SetIndicatorCurrent(errorIndicator);
        else if (   dgItr != diagnostics.begin()
                 && dgItr->line == (dgItr - 1)->line
                 && dgItr->range.first <= (dgItr - 1)->range.second )
        {
            continue; // do not overwrite the last indicator
        }
        else
            stc->SetIndicatorCurrent(warningIndicator);
        stc->IndicatorFillRange(pos, range);
        lastLine = dgItr->line - 1;
    }
    if (diagLv == dlFull)
    {
        stc->AnnotationSetVisible(wxSCI_ANNOTATION_BOXED);
        stc->ScrollLines(firstVisibleLine - stc->GetFirstVisibleLine());
    }
}