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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// file name : findusagetab.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 "findusagetab.h"
#include "ColoursAndFontsManager.h"
#include "clStrings.h"
#include "clSystemSettings.h"
#include "cl_editor.h"
#include "ctags_manager.h"
#include "editor_config.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "fileutils.h"
#include "findresultstab.h"
#include "frame.h"
#include "macros.h"
#include "plugin.h"
#include <map>
#include <wx/ffile.h>
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
#include <wx/textbuf.h>
#include <wx/textfile.h>
#include <wx/tokenzr.h>
#include <wx/xrc/xmlres.h>
struct FindUsageItemData : public wxTreeItemData {
const LSP::Location* location = nullptr;
FindUsageItemData(const LSP::Location* loc)
: location(loc)
{
}
};
FindUsageTab::FindUsageTab(wxWindow* parent)
: wxPanel(parent, wxID_ANY)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
m_ctrl = new clThemedTreeCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_ROW_LINES);
m_ctrl->SetRendererType(eRendererType::RENDERER_DIRECT2D);
m_ctrl->SetTreeStyle(wxTR_HIDE_ROOT | wxTR_ROW_LINES);
m_ctrl->AddRoot("Hidden Root");
m_ctrl->Bind(wxEVT_TREE_ITEM_ACTIVATED, &FindUsageTab::OnItemActivated, this);
m_ctrl->Bind(wxEVT_TREE_ITEM_EXPANDING, &FindUsageTab::OnItemExpanding, this);
m_ctrl->Bind(wxEVT_TREE_SEL_CHANGED, &FindUsageTab::OnItemActivated, this);
// show context menu for the control
m_ctrl->Bind(wxEVT_CONTEXT_MENU, [this](wxContextMenuEvent& event) {
wxMenu menu;
menu.Append(wxID_CLEAR);
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
Clear();
},
wxID_CLEAR);
m_ctrl->PopupMenu(&menu);
});
GetSizer()->Add(m_ctrl, 1, wxEXPAND);
GetSizer()->Layout();
UpdateStyle();
// bind events
EventNotifier::Get()->Bind(wxEVT_SYS_COLOURS_CHANGED, &FindUsageTab::OnThemeChanged, this);
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &FindUsageTab::OnWorkspaceClosed, this);
EventNotifier::Get()->Bind(wxEVT_LSP_REFERENCES, &FindUsageTab::OnReferences, this);
EventNotifier::Get()->Bind(wxEVT_LSP_REFERENCES_INPROGRESS, &FindUsageTab::OnReferencesInProgress, this);
}
FindUsageTab::~FindUsageTab()
{
EventNotifier::Get()->Unbind(wxEVT_SYS_COLOURS_CHANGED, &FindUsageTab::OnThemeChanged, this);
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &FindUsageTab::OnWorkspaceClosed, this);
EventNotifier::Get()->Unbind(wxEVT_LSP_REFERENCES, &FindUsageTab::OnReferences, this);
EventNotifier::Get()->Unbind(wxEVT_LSP_REFERENCES_INPROGRESS, &FindUsageTab::OnReferencesInProgress, this);
m_ctrl->Unbind(wxEVT_TREE_SEL_CHANGED, &FindUsageTab::OnItemActivated, this);
m_ctrl->Unbind(wxEVT_TREE_ITEM_ACTIVATED, &FindUsageTab::OnItemActivated, this);
m_ctrl->Unbind(wxEVT_TREE_ITEM_EXPANDING, &FindUsageTab::OnItemExpanding, this);
}
void FindUsageTab::Clear()
{
m_locations.clear();
m_ctrl->DeleteAllItems();
m_ctrl->AddRoot("Hidden Root");
}
void FindUsageTab::OnClearAll(wxCommandEvent& e)
{
wxUnusedVar(e);
Clear();
}
void FindUsageTab::OnClearAllUI(wxUpdateUIEvent& e)
{
wxTreeItemId root = m_ctrl->GetRootItem();
e.Enable(root.IsOk() && m_ctrl->GetChildrenCount(root, false) != 0);
}
void FindUsageTab::InitialiseView(const std::vector<LSP::Location>& locations)
{
Clear();
UpdateStyle();
m_locations = locations;
// sort by file / location
std::map<wxString, std::vector<const LSP::Location*>> sorted_entries;
for(const LSP::Location& location : m_locations) {
if(sorted_entries.count(location.GetPath()) == 0) {
sorted_entries.insert({ location.GetPath(), {} });
}
std::vector<const LSP::Location*>& file_matches = sorted_entries[location.GetPath()];
file_matches.push_back(&location);
}
// add the entries to the view
for(const auto& vt : sorted_entries) {
const wxString& filename = vt.first;
const std::vector<const LSP::Location*>& locations_vec = vt.second;
DoAddFileEntries(filename, locations_vec);
}
// expand the root, to show the file name
m_ctrl->Expand(m_ctrl->GetRootItem());
}
void FindUsageTab::OnThemeChanged(clCommandEvent& e)
{
e.Skip();
UpdateStyle();
}
void FindUsageTab::UpdateStyle()
{
// construct colours based on the current lexer
auto lexer = ColoursAndFontsManager::Get().GetLexer("c++");
m_ctrl->SetDefaultFont(lexer->GetFontForStyle(0, this));
clColours lexer_colours;
lexer_colours.FromLexer(lexer);
m_ctrl->SetColours(lexer_colours);
m_headerColour = lexer->GetProperty(wxSTC_C_GLOBALCLASS).GetFgColour();
wxColour match_colour_fg = lexer->GetProperty(wxSTC_C_WORD2).GetFgColour();
auto& colours = m_ctrl->GetColours();
colours.SetMatchedItemBgText(wxNullColour);
colours.SetMatchedItemText(match_colour_fg);
m_ctrl->Refresh();
}
void FindUsageTab::OnWorkspaceClosed(clWorkspaceEvent& event)
{
event.Skip();
Clear();
}
void FindUsageTab::DoAddFileEntries(const wxString& filename, const std::vector<const LSP::Location*>& matches)
{
wxTreeItemId child = m_ctrl->AppendItem(m_ctrl->GetRootItem(), filename);
if(m_headerColour.IsOk()) {
m_ctrl->SetItemTextColour(child, m_headerColour);
}
// append dummy child
for(const LSP::Location* location : matches) {
m_ctrl->AppendItem(child, "dummy_child", wxNOT_FOUND, wxNOT_FOUND, new FindUsageItemData(location));
}
}
void FindUsageTab::OnReferences(LSPEvent& event)
{
event.Skip();
InitialiseView(event.GetLocations());
// ensure that this view is shown
clGetManager()->ShowOutputPane(SHOW_USAGE);
}
void FindUsageTab::OnReferencesInProgress(LSPEvent& event)
{
event.Skip();
Clear();
}
void FindUsageTab::OnItemActivated(wxTreeEvent& event)
{
event.Skip();
CHECK_ITEM_RET(event.GetItem());
FindUsageItemData* item_data = static_cast<FindUsageItemData*>(m_ctrl->GetItemData(event.GetItem()));
if(!item_data) {
// header entry
DoExpandItem(event.GetItem());
if(!m_ctrl->IsExpanded(event.GetItem())) {
m_ctrl->Expand(event.GetItem());
}
return;
}
// Open the file
wxFileName fn(item_data->location->GetPath());
// prepare the "after-file-is-loaded" callback
auto range = item_data->location->GetRange();
auto callback = [=](IEditor* editor) {
editor->GetCtrl()->ClearSelections();
editor->SelectRange(range);
};
if(fn.FileExists()) {
clGetManager()->OpenFileAndAsyncExecute(fn.GetFullPath(), std::move(callback));
} else {
// the file does not exist
clCommandEvent open_file_event{ wxEVT_OPEN_FILE };
open_file_event.SetFileName(item_data->location->GetPath());
if(!EventNotifier::Get()->ProcessEvent(open_file_event)) {
::wxMessageBox(_("Failed to open file: ") + item_data->location->GetPath(), "CodeLite",
wxOK | wxOK_DEFAULT | wxICON_ERROR);
event.Veto();
return;
}
clGetManager()->OpenFileAndAsyncExecute(open_file_event.GetFileName(), std::move(callback));
}
}
void FindUsageTab::OnItemExpanding(wxTreeEvent& event)
{
event.Skip();
wxBusyCursor bc;
if(!DoExpandItem(event.GetItem())) {
event.Veto();
}
}
bool FindUsageTab::DoExpandItem(const wxTreeItemId& item)
{
wxBusyCursor bc;
CHECK_ITEM_RET_FALSE(item);
wxTreeItemIdValue cookie;
wxTreeItemId first_child = m_ctrl->GetFirstChild(item, cookie);
if(!first_child) {
return true;
}
// if the child text is not "dummy_child" -> return
if(m_ctrl->GetItemText(first_child) != "dummy_child") {
return true;
}
wxString content;
wxString filepath = m_ctrl->GetItemText(item);
if(!wxFileName::Exists(filepath)) {
// not a local file, request for file download
clCommandEvent event_download{ wxEVT_DOWNLOAD_FILE };
event_download.SetFileName(filepath);
clDEBUG() << "Sending event wxEVT_DOWNLOAD_FILE" << endl;
if(!EventNotifier::Get()->ProcessEvent(event_download)) {
::wxMessageBox(_("Failed to download file: ") + filepath, "CodeLite", wxOK | wxOK_DEFAULT | wxICON_ERROR);
return false;
}
filepath = event_download.GetFileName();
}
// load the file into wxTextBuffer
wxTextFile text_buffer(filepath);
if(!text_buffer.Open()) {
::wxMessageBox(_("Failed to open file: ") + filepath, "CodeLite", wxOK | wxOK_DEFAULT | wxICON_ERROR);
return false;
}
// go over the entries and set the actual line
wxTreeItemIdValue cookie2;
wxTreeItemId child = m_ctrl->GetFirstChild(item, cookie2);
while(child.IsOk()) {
FindUsageItemData* item_data = static_cast<FindUsageItemData*>(m_ctrl->GetItemData(child));
// incase this was a remote file, update the filepath
const_cast<LSP::Location*>(item_data->location)->SetPath(filepath);
size_t line_number = item_data->location->GetRange().GetStart().GetLine();
if(line_number < text_buffer.GetLineCount()) {
// update the text
m_ctrl->SetItemText(child, text_buffer.GetLine(line_number));
// update the highlight information
const auto& start = item_data->location->GetRange().GetStart();
const auto& end = item_data->location->GetRange().GetEnd();
if(start.GetLine() == end.GetLine()) {
m_ctrl->SetItemHighlightInfo(child, start.GetCharacter(), end.GetCharacter() - start.GetCharacter());
m_ctrl->HighlightText(child, true);
}
}
child = m_ctrl->GetNextChild(item, cookie2);
}
return true;
}
|