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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : replaceinfilespanel.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 <vector>
#include <wx/progdlg.h>
#include <wx/xrc/xmlres.h>
#include "ColoursAndFontsManager.h"
#include "clFileSystemEvent.h"
#include "clStrings.h"
#include "clThemeUpdater.h"
#include "clThemedButton.h"
#include "cl_command_event.h"
#include "cl_editor.h"
#include "codelite_events.h"
#include "editor_config.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "frame.h"
#include "globals.h"
#include "macros.h"
#include "manager.h"
#include "replaceinfilespanel.h"
#include <wx/dcgraph.h>
#include <wx/dcmemory.h>
#include <wx/renderer.h>
ReplaceInFilesPanel::ReplaceInFilesPanel(wxWindow* parent, int id, const wxString& name)
: FindResultsTab(parent, id, name)
{
clThemeUpdater::Get().RegisterWindow(this);
Bind(wxEVT_UPDATE_UI, &ReplaceInFilesPanel::OnHoldOpenUpdateUI, this, XRCID("hold_pane_open"));
wxBoxSizer* horzSizer = new wxBoxSizer(wxHORIZONTAL);
clThemedButton* unmark = new clThemedButton(this, wxID_ANY, _("&Unmark All"));
horzSizer->Add(unmark, 0, wxRIGHT | wxLEFT | wxALIGN_CENTER_VERTICAL, 5);
unmark->Bind(wxEVT_BUTTON, &ReplaceInFilesPanel::OnUnmarkAll, this);
unmark->Bind(wxEVT_UPDATE_UI, &ReplaceInFilesPanel::OnUnmarkAllUI, this);
clThemedButton* mark = new clThemedButton(this, wxID_ANY, _("Mark &All"));
horzSizer->Add(mark, 0, wxRIGHT | wxLEFT | wxALIGN_CENTER_VERTICAL, 5);
mark->Bind(wxEVT_BUTTON, &ReplaceInFilesPanel::OnMarkAll, this);
mark->Bind(wxEVT_UPDATE_UI, &ReplaceInFilesPanel::OnMarkAllUI, this);
m_replaceWithText = new wxStaticText(this, wxID_ANY, _("Replace With:"));
horzSizer->Add(m_replaceWithText, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxLEFT, 5);
m_replaceWithText->Bind(wxEVT_UPDATE_UI, &ReplaceInFilesPanel::OnReplaceWithComboUI, this);
m_replaceWith = new wxComboBox(this, wxID_ANY);
horzSizer->Add(m_replaceWith, 2, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxLEFT, 5);
m_replaceWith->Bind(wxEVT_UPDATE_UI, &ReplaceInFilesPanel::OnReplaceWithComboUI, this);
clThemedButton* repl = new clThemedButton(this, wxID_ANY, _("&Replace Marked"));
horzSizer->Add(repl, 0, wxRIGHT | wxLEFT | wxALIGN_CENTER_VERTICAL, 5);
repl->Bind(wxEVT_BUTTON, &ReplaceInFilesPanel::OnReplace, this);
repl->Bind(wxEVT_UPDATE_UI, &ReplaceInFilesPanel::OnReplaceUI, this);
m_progress = new wxGauge(this, wxID_ANY, 1, wxDefaultPosition, wxSize(-1, 15), wxGA_HORIZONTAL);
horzSizer->Add(m_progress, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxGA_SMOOTH, 5);
wxBoxSizer* vertSizer = new wxBoxSizer(wxVERTICAL);
vertSizer->Add(horzSizer, 0, wxEXPAND | wxTOP | wxBOTTOM);
// grab the base class scintilla and put our sizer in its place
wxSizer* mainSizer = m_vSizer;
mainSizer->Detach(m_sci);
vertSizer->Add(m_sci, 1, wxEXPAND | wxALL, 1);
m_tb->DeleteById(XRCID("repeat_output"));
m_tb->DeleteById(XRCID("recent_searches"));
m_tb->Realize();
#ifdef __WXMAC__
mainSizer->Insert(0, vertSizer, 1, wxEXPAND);
#else
mainSizer->Add(vertSizer, 1, wxEXPAND);
#endif
mainSizer->Layout();
}
ReplaceInFilesPanel::~ReplaceInFilesPanel() { clThemeUpdater::Get().UnRegisterWindow(this); }
void ReplaceInFilesPanel::OnSearchStart(wxCommandEvent& e)
{
e.Skip();
// set the "Replace With" field with the user value
SearchData* data = (SearchData*)e.GetClientData();
m_replaceWith->ChangeValue(data->GetReplaceWith());
FindResultsTab::OnSearchStart(e);
// Make sure that the Output view & the "Replace" tab
// are visible
clCommandEvent event(wxEVT_SHOW_OUTPUT_TAB);
event.SetSelected(true).SetString(REPLACE_IN_FILES);
EventNotifier::Get()->AddPendingEvent(event);
}
void ReplaceInFilesPanel::OnSearchMatch(wxCommandEvent& e)
{
e.Skip();
FindResultsTab::OnSearchMatch(e);
if(m_matchInfo.size() != 1 || !m_replaceWith->GetValue().IsEmpty()) return;
m_replaceWith->SetValue(m_matchInfo.begin()->second.GetFindWhat());
m_replaceWith->SetFocus();
}
void ReplaceInFilesPanel::OnSearchEnded(wxCommandEvent& e)
{
e.Skip();
SearchSummary* summary = (SearchSummary*)e.GetClientData();
CHECK_PTR_RET(summary);
// set the "Replace With" field with the user value
m_replaceWith->ChangeValue(summary->GetReplaceWith());
FindResultsTab::OnSearchEnded(e);
OnMarkAll(e);
// Set the focus to the "Replace With" field
m_replaceWith->CallAfter(&wxComboBox::SetFocus);
}
void ReplaceInFilesPanel::OnMarginClick(wxStyledTextEvent& e)
{
int line = m_sci->LineFromPosition(e.GetPosition());
if(m_matchInfo.find(line) == m_matchInfo.end()) {
FindResultsTab::OnMarginClick(e);
} else if(m_sci->MarkerGet(line) & 7 << 0x7) {
m_sci->MarkerDelete(line, 0x7);
} else {
m_sci->MarkerAdd(line, 0x7);
}
}
void ReplaceInFilesPanel::OnMarkAll(wxCommandEvent& e)
{
MatchInfo_t::const_iterator i = m_matchInfo.begin();
for(; i != m_matchInfo.end(); ++i) {
if(m_sci->MarkerGet(i->first) & 7 << 0x7) continue;
m_sci->MarkerAdd(i->first, 0x7);
}
}
void ReplaceInFilesPanel::OnMarkAllUI(wxUpdateUIEvent& e) { e.Enable((m_sci->GetLength() > 0) && !m_searchInProgress); }
void ReplaceInFilesPanel::OnUnmarkAll(wxCommandEvent& e) { m_sci->MarkerDeleteAll(0x7); }
void ReplaceInFilesPanel::OnUnmarkAllUI(wxUpdateUIEvent& e)
{
e.Enable((m_sci->GetLength() > 0) && !m_searchInProgress);
}
void ReplaceInFilesPanel::DoSaveResults(wxStyledTextCtrl* sci, std::map<int, SearchResult>::iterator begin,
std::map<int, SearchResult>::iterator end)
{
if(!sci || begin == end) return;
bool ok = true;
if(dynamic_cast<clEditor*>(sci) == NULL) {
// it's a temp editor, check if we have any changes to save
if(sci->GetModify() && !WriteFileWithBackup(begin->second.GetFileName(), sci->GetText(), false)) {
wxMessageBox(_("Failed to save file:\n") + begin->second.GetFileName(), _("CodeLite - Replace"),
wxICON_ERROR | wxOK);
clDEBUG() << "Replace: Failed to write file" << begin->second.GetFileName();
ok = false;
}
if(sci && ok) {
// Keep the modified file name
m_filesModified.Add(begin->second.GetFileName());
}
delete sci;
}
for(; begin != end; begin++) {
if((m_sci->MarkerGet(begin->first) & 7 << 0x7) == 1 << 0x7) { m_sci->MarkerAdd(begin->first, ok ? 0x9 : 0x8); }
}
}
wxStyledTextCtrl* ReplaceInFilesPanel::DoGetEditor(const wxString& fileName)
{
// look for open editor first
wxStyledTextCtrl* sci = clMainFrame::Get()->GetMainBook()->FindEditor(fileName);
if(sci) {
// FIXME: if editor is already modified, the found locations may not be accurate
return sci;
}
// not open for editing, so make our own temp editor
wxString content;
if(!ReadFileWithConversion(fileName, content)) {
wxMessageBox(_("Failed to open file:\n") + fileName, _("CodeLite - Replace"), wxICON_ERROR | wxOK);
clDEBUG() << "Replace: Failed to read file" << fileName;
return NULL;
}
sci = new wxStyledTextCtrl(this);
sci->Hide();
sci->SetText(content);
return sci;
}
void ReplaceInFilesPanel::OnReplace(wxCommandEvent& e)
{
m_filesModified.clear();
// FIX bug#2770561
int lineNumber(0);
clEditor* activeEditor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
if(activeEditor) { lineNumber = activeEditor->GetCurrentLine(); }
if(m_replaceWith->FindString(m_replaceWith->GetValue(), true) == wxNOT_FOUND) {
m_replaceWith->Append(m_replaceWith->GetValue());
}
// Step 1: apply selected replacements
wxStyledTextCtrl* sci = NULL; // file that is being altered by replacements
wxString lastFile; // track offsets of pending substitutions caused by previous substitutions
long lastLine = 0;
long delta = 0;
// remembers first entry in the file being updated
MatchInfo_t::iterator firstInFile = m_matchInfo.begin();
m_progress->SetRange(m_matchInfo.size());
// Disable the 'buffer limit' feature during replace
clMainFrame::Get()->GetMainBook()->SetUseBuffereLimit(false);
MatchInfo_t::iterator i = firstInFile;
for(; i != m_matchInfo.end(); ++i) {
m_progress->SetValue(m_progress->GetValue() + 1);
m_progress->Update();
if(i->second.GetFileName() != lastFile) {
// about to start a different file, save current results
DoSaveResults(sci, firstInFile, i);
firstInFile = i;
lastFile = i->second.GetFileName();
lastLine = 0;
sci = NULL;
}
if(i->second.GetLineNumber() == lastLine) {
// prior substitutions affected the location of this one
i->second.SetColumn(i->second.GetColumn() + delta);
} else {
delta = 0;
}
if((m_sci->MarkerGet(i->first) & 1 << 0x7) == 0)
// not selected for application
continue;
// extract originally matched text for safety check later
wxString text = i->second.GetPattern().Mid(i->second.GetColumn() - delta, i->second.GetLen());
if(text == m_replaceWith->GetValue()) continue; // no change needed
// need an editor for this file (try only once per file though)
if(!sci && lastLine == 0) {
sci = DoGetEditor(i->second.GetFileName());
lastLine = i->second.GetLineNumber();
}
if(!sci) {
// couldn't open file
m_sci->MarkerAdd(i->first, 0x8);
continue;
}
long pos = sci->PositionFromLine(i->second.GetLineNumber() - 1);
if(pos < 0) {
// invalid line number
m_sci->MarkerAdd(i->first, 0x8);
continue;
}
pos += i->second.GetColumn();
sci->SetSelection(pos, pos + i->second.GetLen());
if(sci->GetSelectedText() != text) {
// couldn't locate the original match (file may have been modified)
m_sci->MarkerAdd(i->first, 0x8);
continue;
}
sci->ReplaceSelection(m_replaceWith->GetValue());
delta += m_replaceWith->GetValue().Length() - i->second.GetLen();
lastLine = i->second.GetLineNumber();
i->second.SetPattern(m_sci->GetLine(i->first)); // includes prior updates to same line
i->second.SetLen(m_replaceWith->GetValue().Length());
}
m_progress->SetValue(0);
DoSaveResults(sci, firstInFile, m_matchInfo.end());
// Disable the 'buffer limit' feature during replace
clMainFrame::Get()->GetMainBook()->SetUseBuffereLimit(true);
// Step 2: Update the Replace pane
std::set<wxString> updatedEditors;
delta = 0; // offset from old line number to new
lastLine = 1; // points to the filename line
lastFile.Clear();
m_sci->MarkerDeleteAll(0x7);
m_sci->SetReadOnly(false);
std::vector<int> itemsToRemove;
i = m_matchInfo.begin();
for(; i != m_matchInfo.end(); i++) {
int line = i->first + delta;
if(i->second.GetFileName() != lastFile) {
if(lastLine == line - 2) {
// previous file's replacements are all done, so remove its filename line
m_sci->SetCurrentPos(m_sci->PositionFromLine(lastLine));
m_sci->LineDelete();
delta--;
line--;
} else {
lastLine = line - 1;
}
lastFile = i->second.GetFileName();
}
if(m_sci->MarkerGet(line) & 1 << 0x9) {
clEditor* editor = clMainFrame::Get()->GetMainBook()->FindEditor(lastFile);
if(editor && editor->GetModify()) { updatedEditors.insert(lastFile); }
// replacement done, remove from map
m_sci->MarkerDelete(line, 0x9);
m_sci->SetCurrentPos(m_sci->PositionFromLine(line));
m_sci->LineDelete();
itemsToRemove.push_back(i->first);
delta--;
} else if(line != i->first) {
// need to adjust line number
m_matchInfo[line] = i->second;
itemsToRemove.push_back(i->first);
}
}
// update the match info map
for(std::vector<int>::size_type i = 0; i < itemsToRemove.size(); i++) {
MatchInfo_t::iterator iter = m_matchInfo.find(itemsToRemove.at(i));
if(iter != m_matchInfo.end()) { m_matchInfo.erase(iter); }
}
m_sci->SetReadOnly(true);
m_sci->GotoLine(0);
if(m_matchInfo.empty()) { Clear(); }
// Step 3: Notify user of changes to already opened files, ask to save
std::vector<std::pair<wxFileName, bool>> filesToSave;
for(std::set<wxString>::iterator i = updatedEditors.begin(); i != updatedEditors.end(); i++) {
filesToSave.push_back(std::make_pair(wxFileName(*i), true));
}
if(!filesToSave.empty() && clMainFrame::Get()->GetMainBook()->UserSelectFiles(
filesToSave, _("Save Modified Files"),
_("Some files are modified.\nChoose the files you would like to save."), true)) {
for(size_t i = 0; i < filesToSave.size(); i++) {
if(filesToSave[i].second) {
clEditor* editor = clMainFrame::Get()->GetMainBook()->FindEditor(filesToSave[i].first.GetFullPath());
if(editor) { editor->SaveFile(); }
}
}
}
// FIX bug#2770561
if(activeEditor) {
clMainFrame::Get()->GetMainBook()->SelectPage(activeEditor);
// restore the line
activeEditor->GotoLine(lineNumber);
}
if(!m_filesModified.IsEmpty()) {
// Some files were modified directly on the file system, notify about it to the plugins
clFileSystemEvent event(wxEVT_FILES_MODIFIED_REPLACE_IN_FILES);
event.SetStrings(m_filesModified);
EventNotifier::Get()->AddPendingEvent(event);
m_filesModified.clear();
}
}
void ReplaceInFilesPanel::OnReplaceUI(wxUpdateUIEvent& e) { e.Enable((m_sci->GetLength() > 0) && !m_searchInProgress); }
void ReplaceInFilesPanel::OnReplaceWithComboUI(wxUpdateUIEvent& e)
{
e.Enable((m_sci->GetLength() > 0) && !m_searchInProgress);
}
void ReplaceInFilesPanel::OnHoldOpenUpdateUI(wxUpdateUIEvent& e)
{
int sel = clMainFrame::Get()->GetOutputPane()->GetNotebook()->GetSelection();
if(clMainFrame::Get()->GetOutputPane()->GetNotebook()->GetPage(sel) != this) { return; }
if(EditorConfigST::Get()->GetOptions()->GetHideOutpuPaneOnUserClick()) {
e.Enable(true);
e.Check(EditorConfigST::Get()->GetOptions()->GetHideOutputPaneNotIfReplace());
} else {
e.Enable(false);
e.Check(false);
}
}
void ReplaceInFilesPanel::OnMouseDClick(wxStyledTextEvent& e)
{
int clickedLine = wxNOT_FOUND;
m_styler->HitTest(m_sci, e, clickedLine);
// Did we clicked on a togglable line?
int toggleLine = m_styler->TestToggle(m_sci, e);
if(toggleLine != wxNOT_FOUND) {
m_sci->ToggleFold(toggleLine);
} else {
MatchInfo_t::const_iterator m = m_matchInfo.find(clickedLine);
if(m != m_matchInfo.end()) { DoOpenSearchResult(m->second, NULL, m->first); }
}
}
static void RenderCheckbox(wxWindow* win, wxDC& dc, const wxColour& bgColour, const wxColour& penColour,
const wxRect& _rect, bool checked)
{
wxRect rect = _rect;
dc.SetBrush(bgColour);
dc.SetPen(bgColour);
dc.DrawRectangle(rect);
if(!checked) { return; }
if(checked) {
wxRect innerRect = rect;
innerRect.Deflate(::clGetSize(4, win));
dc.SetPen(wxPen(penColour, ::clGetSize(2, win)));
wxPoint p1, p2, p3;
p1.x = innerRect.GetTopLeft().x;
p1.y = innerRect.GetTopLeft().y + (innerRect.GetHeight() / 2);
p2.x = innerRect.GetBottomLeft().x + (innerRect.GetWidth() / 3);
p2.y = innerRect.GetBottomLeft().y;
p3 = innerRect.GetTopRight();
dc.DrawLine(p1, p2);
dc.DrawLine(p2, p3);
}
}
void ReplaceInFilesPanel::SetStyles(wxStyledTextCtrl* sci)
{
FindResultsTab::SetStyles(sci);
LexerConf::Ptr_t lexer = ColoursAndFontsManager::Get().GetLexer("c++");
if(!lexer) { lexer = ColoursAndFontsManager::Get().GetLexer("text"); }
// render two bitmaps: checked and unchecked
int size = ::clGetSize(16, sci);
const StyleProperty& styleProperty = lexer->GetProperty(0);
wxColour bgColour = styleProperty.GetBgColour();
wxColour fgColour = lexer->IsDark() ? *wxYELLOW : *wxBLACK;
{
wxMemoryDC memDC;
m_bmpChecked = wxBitmap(size, size);
memDC.SelectObject(m_bmpChecked);
wxGCDC gcdc(memDC);
wxRect rect(0, 0, size, size);
RenderCheckbox(sci, gcdc, bgColour, fgColour, rect, true);
memDC.SelectObject(wxNullBitmap); // Apply the changes
}
#if wxCHECK_VERSION(3, 1, 0)
sci->SetMarginWidth(3, clGetSize(1, sci)); // separator margin
sci->SetMarginBackground(3, lexer->IsDark() ? bgColour.ChangeLightness(130) : bgColour.ChangeLightness(50));
#else
sci->SetMarginWidth(3, 0); // separator margin
#endif
sci->MarkerDefineBitmap(7, m_bmpChecked);
}
|