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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
#include "BuildTab.hpp"
#include "ColoursAndFontsManager.h"
#include "StringUtils.h"
#include "build_settings_config.h"
#include "clAnsiEscapeCodeHandler.hpp"
#include "clStrings.h"
#include "clTerminalViewCtrl.hpp"
#include "editor_config.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "frame.h"
#include "globals.h"
#include "mainbook.h"
#include "manager.h"
#include "shell_command.h"
#include <wx/app.h>
#include <wx/msgdlg.h>
#include <wx/sizer.h>
#include <wx/tokenzr.h>
namespace
{
struct LineClientData {
wxString project_name;
Compiler::PatternMatch match_pattern;
wxString message;
wxString toolchain;
};
} // namespace
BuildTab::BuildTab(wxWindow* parent)
: wxPanel(parent, wxID_ANY)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
GetSizer()->Fit(this);
m_view = new clTerminalViewCtrl(this);
GetSizer()->Add(m_view, 1, wxEXPAND);
// Event handling
EventNotifier::Get()->Bind(wxEVT_BUILD_PROCESS_STARTED, &BuildTab::OnBuildStarted, this);
EventNotifier::Get()->Bind(wxEVT_BUILD_PROCESS_ADDLINE, &BuildTab::OnBuildAddLine, this);
EventNotifier::Get()->Bind(wxEVT_BUILD_PROCESS_ENDED, &BuildTab::OnBuildEnded, this);
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, [this](clWorkspaceEvent& e) {
e.Skip();
Cleanup();
});
m_view->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &BuildTab::OnLineActivated, this);
m_view->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &BuildTab::OnLineActivated, this);
m_view->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &BuildTab::OnContextMenu, this);
wxTheApp->Bind(wxEVT_MENU, &BuildTab::OnNextBuildError, this, XRCID("next_build_error"));
wxTheApp->Bind(wxEVT_UPDATE_UI, &BuildTab::OnNextBuildErrorUI, this, XRCID("next_build_error"));
}
BuildTab::~BuildTab()
{
wxTheApp->Unbind(wxEVT_MENU, &BuildTab::OnNextBuildError, this, XRCID("next_build_error"));
wxTheApp->Unbind(wxEVT_UPDATE_UI, &BuildTab::OnNextBuildErrorUI, this, XRCID("next_build_error"));
}
void BuildTab::OnBuildStarted(clBuildEvent& e)
{
e.Skip();
m_buildInProgress = true;
// clear all build markers
IEditor::List_t all_editors;
clGetManager()->GetAllEditors(all_editors);
for(auto editor : all_editors) {
editor->DelAllCompilerMarkers();
}
// ensure that the BUILD_IN is visible
ManagerST::Get()->ShowOutputPane(BUILD_WIN, true);
m_buffer.clear();
if(e.IsCleanLog()) {
ClearView();
}
// read the build tab settings
EditorConfigST::Get()->ReadObject(wxT("BuildTabSettings"), &m_buildTabSettings);
// clean the last used compiler
m_activeCompiler.Reset(nullptr);
m_error_count = m_warn_count = 0;
// get the toolchain from the event and attempt to load the compiler
const wxString& toolchain = e.GetToolchain();
if(!toolchain.empty() && BuildSettingsConfigST::Get()->IsCompilerExist(toolchain)) {
m_activeCompiler = BuildSettingsConfigST::Get()->GetCompiler(toolchain);
clDEBUG() << "Active compiler is set to:" << m_activeCompiler->GetName() << endl;
}
// notify the plugins that the build had started
clBuildEvent build_started_event(wxEVT_BUILD_STARTED);
build_started_event.SetProjectName(e.GetProjectName());
build_started_event.SetConfigurationName(e.GetConfigurationName());
EventNotifier::Get()->AddPendingEvent(build_started_event);
// start stop watch
m_sw.Start();
}
void BuildTab::OnBuildAddLine(clBuildEvent& e)
{
e.Skip();
m_buffer << e.GetString();
ProcessBuffer();
}
void BuildTab::OnBuildEnded(clBuildEvent& e)
{
e.Skip();
m_buildInProgress = false;
ProcessBuffer(true);
wxString text = CreateSummaryLine();
m_buffer.swap(text);
ProcessBuffer(true);
if(m_buildTabSettings.GetScrollTo() == BuildTabSettingsData::SCROLL_TO_FIRST_ERROR) {
SelectFirstErrorOrWarning(0);
}
// notify the plugins that the build has ended
clBuildEvent build_ended_event(wxEVT_BUILD_ENDED);
build_ended_event.SetErrorCount(m_error_count);
build_ended_event.SetWarningCount(m_warn_count);
EventNotifier::Get()->AddPendingEvent(build_ended_event);
m_currentProjectName.clear();
}
void BuildTab::ProcessBuffer(bool last_line)
{
// process completed lines
m_view->Begin();
auto lines = ::wxStringTokenize(m_buffer, "\n", wxTOKEN_RET_DELIMS);
wxString remainder;
while(!lines.empty()) {
auto& line = lines[0];
if(!last_line && !line.EndsWith("\n")) {
// not a complete line
remainder.swap(line);
break;
}
line.Trim();
// easy path: check for common makefile messages
if(line.Lower().Contains("entering directory") || line.Lower().Contains("leaving directory")) {
line = WrapLineInColour(line, eAsciiColours::GRAY);
m_view->AppendItem(line);
} else if(line.Lower().Contains("building project")) {
ProcessBuildingProjectLine(line);
line = WrapLineInColour(line, eAsciiColours::NORMAL_TEXT, true);
m_view->AppendItem(line);
} else {
std::unique_ptr<LineClientData> m(new LineClientData);
m->message = line;
// remove the terminal ascii colouring escape code
wxString modified_line;
StringUtils::StripTerminalColouring(line, modified_line);
bool lineHasColours = (line.length() != modified_line.length());
if(!m_activeCompiler || !m_activeCompiler->Matches(modified_line, &m->match_pattern)) {
m.reset();
} else {
switch(m->match_pattern.sev) {
case Compiler::kSevError:
m_error_count++;
break;
case Compiler::kSevWarning:
m_warn_count++;
break;
default:
break;
}
}
// if this line matches a pattern (error or warning) AND
// this colour has no colour associated with it (using ANSI escape)
// add some
if(!lineHasColours && m.get()) {
line = WrapLineInColour(line, m->match_pattern.sev == Compiler::kSevError ? eAsciiColours::RED
: eAsciiColours::YELLOW);
}
// Associate the match info with the line in the view
// this will be used later when selecting lines
// Note: its OK to pass null here
if(m) {
// set the line project name
m->toolchain = m_activeCompiler->GetName();
m->project_name = GetCurrentProjectName();
}
m_view->AppendItem(line, wxNOT_FOUND, wxNOT_FOUND, (wxUIntPtr)m.release());
}
lines.erase(lines.begin());
}
if(last_line) {
m_buffer.clear();
} else {
m_buffer.swap(remainder);
}
m_view->Commit();
m_view->ScrollToBottom();
}
void BuildTab::Cleanup()
{
m_buildInProgress = false;
m_buffer.clear();
ClearView();
m_activeCompiler.Reset(nullptr);
m_error_count = 0;
m_warn_count = 0;
m_buildInterrupted = false;
m_currentProjectName.clear();
}
void BuildTab::AppendLine(const wxString& text)
{
m_buffer << text;
ProcessBuffer();
}
void BuildTab::ClearView()
{
m_view->DeleteAllItems([](wxUIntPtr d) {
if(d) {
LineClientData* p = reinterpret_cast<LineClientData*>(d);
wxDELETE(p);
}
});
m_buffer.clear();
}
wxString BuildTab::WrapLineInColour(const wxString& line, eAsciiColours colour, bool fold_font) const
{
wxString text;
clAnsiEscapeCodeColourBuilder text_builder(&text);
bool is_light = m_view->GetColours().IsLightTheme();
text_builder.SetTheme(is_light ? eAsciiTheme::LIGHT : eAsciiTheme::DARK).Add(line, colour, fold_font);
return text;
}
void BuildTab::OnLineActivated(wxDataViewEvent& e)
{
LOG_IF_TRACE { clDEBUG1() << "Build line double clicked" << endl; }
auto cd = reinterpret_cast<LineClientData*>(m_view->GetItemData(e.GetItem()));
CHECK_PTR_RET(cd);
// let the plugins a first chance in handling this line
if(!cd->match_pattern.file_path.empty()) {
clBuildEvent eventErrorClicked(wxEVT_BUILD_OUTPUT_HOTSPOT_CLICKED);
eventErrorClicked.SetFileName(cd->match_pattern.file_path);
eventErrorClicked.SetLineNumber(cd->match_pattern.line_number);
eventErrorClicked.SetProjectName(cd->project_name);
clDEBUG1() << "Letting plugins process it first (" << cd->match_pattern.file_path << ":"
<< cd->match_pattern.line_number << ")" << endl;
if(EventNotifier::Get()->ProcessEvent(eventErrorClicked)) {
return;
}
wxFileName fn(cd->match_pattern.file_path);
if(fn.FileExists()) {
// if we resolved it now, open the file there is no point in searching this file
// in m_buildInfoPerFile since the key on this map is kept as full name
int line_number = cd->match_pattern.line_number;
line_number -= 1;
int column = cd->match_pattern.column != wxNOT_FOUND ? cd->match_pattern.column - 1 : wxNOT_FOUND;
auto cb = [=](IEditor* editor) {
editor->GetCtrl()->ClearSelections();
// compilers report line numbers starting from `1`
// our editor sees line numbers starting from `0`
editor->CenterLine(line_number, column);
if(cd->match_pattern.sev == Compiler::kSevError) {
editor->SetErrorMarker(line_number, cd->message);
} else {
editor->SetWarningMarker(line_number, cd->message);
}
editor->SetActive();
};
clGetManager()->OpenFileAndAsyncExecute(fn.GetFullPath(), std::move(cb));
}
}
}
void BuildTab::OnContextMenu(wxDataViewEvent& e)
{
wxMenu menu;
if(e.GetItem().IsOk()) {
menu.Append(XRCID("copy-current-lines"), _("Copy"));
menu.Enable(XRCID("copy-current-lines"), !m_buildInProgress);
menu.Append(XRCID("copy-all-lines"), _("Copy all"));
menu.Enable(XRCID("copy-all-lines"), !m_buildInProgress);
menu.AppendSeparator();
}
menu.Append(wxID_SAVEAS);
menu.Enable(wxID_SAVEAS, !m_buildInProgress);
menu.AppendSeparator();
menu.Append(wxID_CLEAR);
menu.Enable(wxID_CLEAR, !m_buildInProgress);
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
Cleanup();
},
wxID_CLEAR);
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
CallAfter(&BuildTab::SaveBuildLog);
},
wxID_SAVEAS);
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
CallAfter(&BuildTab::CopySelections);
},
XRCID("copy-current-lines"));
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
CallAfter(&BuildTab::CopyAll);
},
XRCID("copy-all-lines"));
m_view->PopupMenu(&menu);
}
void BuildTab::SaveBuildLog()
{
// get the content
wxString content;
content.reserve(16 * 1024); // 16K
for(size_t i = 0; i < m_view->GetItemCount(); ++i) {
wxString str;
StringUtils::StripTerminalColouring(m_view->GetItemText(m_view->RowToItem(i)), str);
content << str << "\n";
}
wxString path = ::wxFileSelector();
if(path.empty()) {
return;
}
if(wxFileName::FileExists(path)) {
if(::wxMessageBox(_("A file with the same name already exists, continue?"), "CodeLite",
wxICON_WARNING | wxYES_NO | wxCANCEL | wxCANCEL_DEFAULT) != wxYES) {
return;
}
}
FileUtils::WriteFileContent(path, content);
}
void BuildTab::CopySelections()
{
wxDataViewItemArray items;
if(m_view->GetSelections(items) == 0) {
return;
}
wxString content;
for(size_t i = 0; i < items.size(); ++i) {
wxString str;
StringUtils::StripTerminalColouring(m_view->GetItemText(items[i]), str);
content << str << "\n";
}
::CopyToClipboard(content);
}
void BuildTab::CopyAll()
{
if(m_view->IsEmpty()) {
return;
}
wxString content;
content.reserve(16 * 1024); // reserve 16K
for(size_t i = 0; i < m_view->GetItemCount(); ++i) {
wxString str;
StringUtils::StripTerminalColouring(m_view->GetItemText(m_view->RowToItem(i)), str);
content << str << "\n";
}
::CopyToClipboard(content);
}
wxString BuildTab::CreateSummaryLine()
{
long elapsed = m_sw.Time() / 1000;
wxString total_time;
if(elapsed > 5) {
long sec = elapsed % 60;
long hours = elapsed / 3600;
long minutes = (elapsed % 3600) / 60;
total_time << wxString::Format(wxT(", %s: %02ld:%02ld:%02ld %s"), _("total time"), hours, minutes, sec,
_("seconds"));
}
wxString text;
if(m_buildInterrupted) {
// build was cancelled by the user
text << _("(Build cancelled by the user)\n");
text = WrapLineInColour(text, eAsciiColours::YELLOW);
m_buildInterrupted = false;
} else {
// at this point, m_buffer is empty
text << "\n";
if(m_error_count) {
text = _("==== build ended with ");
text << WrapLineInColour(_("errors"), eAsciiColours::RED, true);
} else if(m_warn_count) {
text = _("=== build ended with ");
text << WrapLineInColour(_("warnings"), eAsciiColours::YELLOW, true);
} else {
text = _("=== build completed ");
text << WrapLineInColour(_("successfully"), eAsciiColours::GREEN, true);
}
text << " (" << m_error_count << _(" errors, ") << m_warn_count << _(" warnings)");
if(!total_time.empty()) {
text << total_time;
}
text << " ===";
}
return text;
}
void BuildTab::ProcessBuildingProjectLine(const wxString& line)
{
// extract the project name from the line
// an example line:
// ----------Building project:[ CodeLiteIDE - Win_x64_Release ] (Single File Build)----------
wxString s = line.AfterFirst('[');
s = s.BeforeLast(']');
s = s.BeforeLast('-');
s.Trim().Trim(false);
// keep the current project name
m_currentProjectName.swap(s);
}
size_t BuildTab::GetNextLineWithErrorOrWarning(size_t from, bool errors_only) const
{
if(m_error_count == 0 && (m_warn_count == 0 || errors_only)) {
return wxString::npos;
}
if(from >= m_view->GetItemCount()) {
return wxString::npos;
}
for(size_t i = from; i < m_view->GetItemCount(); ++i) {
wxUIntPtr p = m_view->GetItemData(m_view->RowToItem(i));
if(p) {
LineClientData* cd = reinterpret_cast<LineClientData*>(p);
if(errors_only && cd->match_pattern.sev == Compiler::kSevError) {
return i;
} else if(!errors_only && (cd->match_pattern.sev == Compiler::kSevWarning ||
cd->match_pattern.sev == Compiler::kSevError)) {
return i;
}
}
}
return wxString::npos;
}
void BuildTab::OnNextBuildError(wxCommandEvent& event)
{
wxUnusedVar(event);
// get the next line to select
size_t from = m_view->GetSelectedRow();
if(from == wxString::npos) {
from = 0;
} else {
++from;
}
SelectFirstErrorOrWarning(from);
}
void BuildTab::OnNextBuildErrorUI(wxUpdateUIEvent& event) { event.Enable(m_warn_count || m_error_count); }
void BuildTab::SelectFirstErrorOrWarning(size_t from)
{
size_t line_to_select = GetNextLineWithErrorOrWarning(from, m_buildTabSettings.IsSkipWarnings());
if(line_to_select != wxString::npos) {
m_view->UnselectAll();
m_view->SetFirstVisibleRow(line_to_select);
m_view->SelectRow(line_to_select);
}
}
|