File: cscopetab.cpp

package info (click to toggle)
codelite 14.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (259 lines) | stat: -rw-r--r-- 8,585 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : cscopetab.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "bitmap_loader.h"
#include "cscopedbbuilderthread.h"
#include "cscopetab.h"
#include "csscopeconfdata.h"
#include "drawingutils.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "fileextmanager.h"
#include "globals.h"
#include "imanager.h"
#include "plugin.h"
#include "workspace.h"
#include <set>
#include <wx/app.h>
#include <wx/imaglist.h>
#include <wx/log.h>
#include <wx/treectrl.h>
#include <clFileSystemWorkspace.hpp>

CscopeTab::CscopeTab(wxWindow* parent, IManager* mgr)
    : CscopeTabBase(parent)
    , m_table(NULL)
    , m_mgr(mgr)
{
    m_styler.Reset(new clFindResultsStyler(m_stc));

    CScopeConfData data;
    m_mgr->GetConfigTool()->ReadObject(wxT("CscopeSettings"), &data);

    const wxString SearchScope[] = { wxTRANSLATE("Entire Workspace"), wxTRANSLATE("Active Project") };
    m_stringManager.AddStrings(sizeof(SearchScope) / sizeof(wxString), SearchScope, data.GetScanScope(),
                               m_choiceSearchScope);

    wxFont defFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
    m_font = wxFont(defFont.GetPointSize(), wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);

    m_checkBoxUpdateDb->SetValue(data.GetRebuildOption());
    m_checkBoxRevertedIndex->SetValue(data.GetBuildRevertedIndexOption());
    SetMessage(_("Ready"), 0);

    Clear(); // To make the Clear button UpdateUI work initially
    EventNotifier::Get()->Connect(wxEVT_CL_THEME_CHANGED, wxCommandEventHandler(CscopeTab::OnThemeChanged), NULL, this);
}

CscopeTab::~CscopeTab()
{
    EventNotifier::Get()->Disconnect(wxEVT_CL_THEME_CHANGED, wxCommandEventHandler(CscopeTab::OnThemeChanged), NULL,
                                     this);
}

void CscopeTab::Clear()
{
    FreeTable();
    m_stc->SetEditable(true);
    m_stc->ClearAll();
    m_stc->SetEditable(false);
    m_matchesInStc.clear();
}

void CscopeTab::BuildTable(CScopeResultTable_t* table)
{
    CHECK_PTR_RET(table);
    // Free the old table
    FreeTable();

    m_table = table;
    ClearText();
    m_matchesInStc.clear();
    m_styler->SetStyles(m_stc);

    wxStringSet_t insertedItems;
    CScopeResultTable_t::iterator iter = m_table->begin();
    for(; iter != m_table->end(); ++iter) {
        wxString file = iter->first;

        // Add line for the file
        AddFile(file);

        // Add the entries for this file
        CScopeEntryDataVec_t* vec = iter->second;
        for(size_t i = 0; i < vec->size(); ++i) {
            CscopeEntryData entry = vec->at(i);
            // Dont insert duplicate entries to the match view
            wxString display_string;
            display_string << _("Line: ") << entry.GetLine() << wxT(", ") << entry.GetScope() << wxT(", ")
                           << entry.GetPattern();
            if(insertedItems.count(display_string) == 0) {
                insertedItems.insert(display_string);
                int lineno = m_stc->GetLineCount() - 1; // STC line number *before* we add the result
                AddMatch(entry.GetLine(), entry.GetPattern());
                m_matchesInStc.insert(std::make_pair(lineno, entry));
            }
        }
    }
    FreeTable();
}

void CscopeTab::FreeTable()
{
    if(m_table) {
        CScopeResultTable_t::iterator iter = m_table->begin();
        for(; iter != m_table->end(); iter++) {
            // delete the vector
            delete iter->second;
        }
        m_table->clear();
        wxDELETE(m_table);
    }
}

void CscopeTab::SetMessage(const wxString& msg, int percent)
{
    if(m_mgr->GetStatusBar()) { m_mgr->GetStatusBar()->SetMessage(msg, 3); }
    m_gauge->SetValue(percent);
}

void CscopeTab::OnClearResults(wxCommandEvent& e)
{
    wxUnusedVar(e);
    SetMessage(_("Ready"), 0);
    Clear();
}

void CscopeTab::OnClearResultsUI(wxUpdateUIEvent& e)
{
    CHECK_CL_SHUTDOWN();
    e.Enable(!m_stc->IsEmpty());
}

void CscopeTab::OnChangeSearchScope(wxCommandEvent& e)
{
    CScopeConfData data;
    m_mgr->GetConfigTool()->ReadObject(wxT("CscopeSettings"), &data);
    // update the settings
    data.SetScanScope(m_stringManager.GetStringSelection());
    data.SetRebuildDbOption(m_checkBoxUpdateDb->IsChecked());
    data.SetBuildRevertedIndexOption(m_checkBoxRevertedIndex->IsChecked());
    // store the object
    m_mgr->GetConfigTool()->WriteObject(wxT("CscopeSettings"), &data);
}

void CscopeTab::OnCreateDB(wxCommandEvent& e)
{
    // There's no easy way afaict to reach the class Cscope direct, so...
    e.SetId(XRCID("cscope_create_db"));
    e.SetEventType(wxEVT_COMMAND_MENU_SELECTED);
    wxPostEvent(m_mgr->GetTheApp(), e);
}

void CscopeTab::OnWorkspaceOpenUI(wxUpdateUIEvent& e)
{
    CHECK_CL_SHUTDOWN();
    e.Enable(IsWorkspaceOpen());
}

void CscopeTab::OnThemeChanged(wxCommandEvent& e)
{
    e.Skip();
    m_styler->SetStyles(m_stc);
}

void CscopeTab::ClearText()
{
    m_stc->SetEditable(true);
    m_stc->ClearAll();
    m_stc->SetEditable(false);
}

void CscopeTab::AddMatch(int line, const wxString& pattern)
{
    m_stc->SetEditable(true);
    wxString linenum = wxString::Format(wxT(" %5d: "), line);
    m_stc->AppendText(linenum + pattern + "\n");
    m_stc->SetEditable(false);
}

void CscopeTab::AddFile(const wxString& filename)
{
    m_stc->SetEditable(true);
    m_stc->AppendText(filename + "\n");
    m_stc->SetEditable(false);
}

void CscopeTab::OnHotspotClicked(wxStyledTextEvent& e)
{
    if(!IsWorkspaceOpen()) { return; }
    
    int clickedLine;
    int style = m_styler->HitTest(e, clickedLine);
    if(style == clFindResultsStyler::LEX_FIF_FILE || style == clFindResultsStyler::LEX_FIF_HEADER) {
        // Toggle
        m_stc->ToggleFold(clickedLine);
    } else {
        // Open the match
        std::map<int, CscopeEntryData>::const_iterator iter = m_matchesInStc.find(clickedLine);
        if(iter != m_matchesInStc.end()) {
            wxString wsp_path = GetWorkingDirectory();
            wxFileName fn(iter->second.GetFile());
            if(!fn.MakeAbsolute(wsp_path)) {
                clLogMessage(wxT("CScope: failed to convert file to absolute path"));
                return;
            }
            m_mgr->OpenFile(fn.GetFullPath(), "", iter->second.GetLine() - 1);

            // In theory this isn't needed as it happened in OpenFile()
            // In practice there's a timing issue: if the file needs to be loaded,
            // the CenterLine() call arrives too soon. So repeat it here, delayed.
            CallAfter(&CscopeTab::CenterEditorLine, iter->second.GetLine() - 1);
        }
    }
}

void CscopeTab::CenterEditorLine(int lineno)
{
    IEditor* editor = m_mgr->GetActiveEditor();
    if(editor) { editor->CenterLine(lineno); }
}

wxString CscopeTab::GetWorkingDirectory() const
{
    if(!IsWorkspaceOpen()) { return wxEmptyString; }

    if(clFileSystemWorkspace::Get().IsOpen()) {
        wxFileName fn = clFileSystemWorkspace::Get().GetFileName();
        fn.AppendDir(".codelite");
        return fn.GetPath();
    } else {
        return clCxxWorkspaceST::Get()->GetPrivateFolder();
    }
}

bool CscopeTab::IsWorkspaceOpen() const
{
    return clFileSystemWorkspace::Get().IsOpen() || clCxxWorkspaceST::Get()->IsOpen();
}