Patch #1125 2006-06-12 18:10

kkez

Replace in files
Download
1125-Replace_in_fil.patch (54.1 KB)
Category
Plugin::FeatureAdd
Status
Accepted
Close date
2006-06-14 11:11
Assigned to
 
Index: src/sdk/confirmreplacedlg.cpp
===================================================================
--- src/sdk/confirmreplacedlg.cpp    (revision 2551)
+++ src/sdk/confirmreplacedlg.cpp    (working copy)
@@ -34,15 +34,20 @@
 #include "confirmreplacedlg.h"
 
 BEGIN_EVENT_TABLE(ConfirmReplaceDlg, wxDialog)
-    EVT_BUTTON(XRCID("btnYes"),     ConfirmReplaceDlg::OnYes)
-    EVT_BUTTON(XRCID("btnNo"),         ConfirmReplaceDlg::OnNo)
-    EVT_BUTTON(XRCID("btnAll"),     ConfirmReplaceDlg::OnAll)
-    EVT_BUTTON(XRCID("btnCancel"),    ConfirmReplaceDlg::OnCancel)
+    EVT_BUTTON(XRCID("btnYes"),         ConfirmReplaceDlg::OnYes)
+    EVT_BUTTON(XRCID("btnNo"),             ConfirmReplaceDlg::OnNo)
+    EVT_BUTTON(XRCID("btnAllInFile"),   ConfirmReplaceDlg::OnAllInFile)
+    EVT_BUTTON(XRCID("btnSkipFile"),    ConfirmReplaceDlg::OnSkipFile)
+    EVT_BUTTON(XRCID("btnAll"),         ConfirmReplaceDlg::OnAll)
+    EVT_BUTTON(XRCID("btnCancel"),        ConfirmReplaceDlg::OnCancel)
 END_EVENT_TABLE()
 
-ConfirmReplaceDlg::ConfirmReplaceDlg(wxWindow* parent, const wxString& label)
+ConfirmReplaceDlg::ConfirmReplaceDlg(wxWindow* parent, bool replaceInFiles, const wxString& label)
 {
-    wxXmlResource::Get()->LoadDialog(this, parent, _T("dlgConfirmReplace"));
+    if (replaceInFiles)
+        wxXmlResource::Get()->LoadDialog(this, parent, _T("dlgConfirmReplaceMultiple"));
+    else
+        wxXmlResource::Get()->LoadDialog(this, parent, _T("dlgConfirmReplace"));
     XRCCTRL(*this, "lblMessage", wxStaticText)->SetLabel(label);
 }
 
@@ -60,6 +65,16 @@
     EndModal(crNo);
 }
 
+void ConfirmReplaceDlg::OnAllInFile(wxCommandEvent& event)
+{
+    EndModal(crAllInFile);
+}
+
+void ConfirmReplaceDlg::OnSkipFile(wxCommandEvent& event)
+{
+    EndModal(crSkipFile);
+}
+
 void ConfirmReplaceDlg::OnAll(wxCommandEvent& event)
 {
     EndModal(crAll);
Index: src/sdk/confirmreplacedlg.h
===================================================================
--- src/sdk/confirmreplacedlg.h    (revision 2551)
+++ src/sdk/confirmreplacedlg.h    (working copy)
@@ -10,6 +10,8 @@
 {
     crYes = 0,
     crNo,
+    crAllInFile,
+    crSkipFile,
     crAll,
     crCancel
 };
@@ -17,10 +19,13 @@
 class ConfirmReplaceDlg : public wxDialog
 {
     public:
-        ConfirmReplaceDlg(wxWindow* parent, const wxString& label = _("Replace this occurence?"));
+        ConfirmReplaceDlg(wxWindow* parent, bool replaceInFiles = false, 
+            const wxString& label = _("Replace this occurrence?"));
         ~ConfirmReplaceDlg();
         void OnYes(wxCommandEvent& event);
         void OnNo(wxCommandEvent& event);
+        void OnAllInFile(wxCommandEvent& event);
+        void OnSkipFile(wxCommandEvent& event);
         void OnAll(wxCommandEvent& event);
         void OnCancel(wxCommandEvent& event);
         void CalcPosition(cbStyledTextCtrl* ed);
Index: src/sdk/editormanager.cpp
===================================================================
--- src/sdk/editormanager.cpp    (revision 2551)
+++ src/sdk/editormanager.cpp    (working copy)
@@ -951,7 +951,7 @@
                 wxString msg;
                 msg.Printf(_("File %s is modified outside the IDE...\nDo you want to reload it (you will lose any unsaved work)?"),
                            ed->GetFilename().c_str());
-                ConfirmReplaceDlg dlg(Manager::Get()->GetAppWindow(), msg);
+                ConfirmReplaceDlg dlg(Manager::Get()->GetAppWindow(), false, msg);
                 dlg.SetTitle(_("Reload file?"));
                 PlaceWindow(&dlg);
                 ret = dlg.ShowModal();
@@ -1161,7 +1161,7 @@
     if (!replace)
         dlg = new FindDlg(Manager::Get()->GetAppWindow(), phraseAtCursor, hasSelection, !ed, explicitly_find_in_files);
     else
-        dlg = new ReplaceDlg(Manager::Get()->GetAppWindow(), phraseAtCursor, hasSelection);
+        dlg = new ReplaceDlg(Manager::Get()->GetAppWindow(), phraseAtCursor, hasSelection, !ed, explicitly_find_in_files);
 
     PlaceWindow(dlg);
     if (dlg->ShowModal() == wxID_CANCEL)
@@ -1205,23 +1205,26 @@
     if (!replace)
     {
         if (m_LastFindReplaceData->findInFiles)
-        {
-            int ans = FindInFiles(m_LastFindReplaceData);
-            // FindInFiles() is done, default back to Find in Editor
-            m_LastFindReplaceData->findInFiles = false;
-            return ans;
-        }
+            return FindInFiles(m_LastFindReplaceData);
         else
             return Find(control, m_LastFindReplaceData);
     }
     else
     {
         m_LastFindReplaceData->initialreplacing = true;
-        return Replace(control, m_LastFindReplaceData);
+        
+        if (m_LastFindReplaceData->findInFiles)
+            return ReplaceInFiles(m_LastFindReplaceData);
+        else
+            return Replace(control, m_LastFindReplaceData);
     }
+    
+    //Default back to find or replace in Editor
+    if(m_LastFindReplaceData->findInFiles)
+        m_LastFindReplaceData->findInFiles = false;
 }
 
-v
download for full patch...
kkez 2006-06-12 18:26

It adds and modify:

1) search menu item "Replace in files" under "Replace"

2) another confirmation dialog with more option ("replace all occurrence in a file" and "skip a file"), so another .xrc and another class under confirmreplacedlg.cpp/.h

3) the .xrc of the replace dialog, modified with notebook and "replace in files" panel; modified replacedlg.cpp/.h to handle both panels

4) the "replace in files" function

5) added the CodeBlocks.cbp file too since i've added a .xrc file.

6) fixed a little typo in the confirm_replace.xrc (occurence)

kkez 2006-06-13 08:52

This patch was breaking the "Find in files" feature, because i was setting the data->start variable to 0 instead of the return value of GetCurrentPos(). But this shouldn't happen, because the "Find in files" search should search in the entire scope and don't do any wrap around.

Both Replace and Find does the wrap around, but FindInFiles shouldn't use the Find function since it wraps around, but use its own algoritm, just like i did in the ReplaceInFiles function.

By now i just added a bool to the CalculateFindReplaceStartEnd() function to set data->start to 0 when ReplaceInFiles, or GetCurrentPos() in any other case.

I've also splitted the patch to make it more readable. The CodeBlocks.cbp path now it's in this file:

The other part is the one i'm submitting now.

kkez 2006-06-13 08:54

Uh, forgot to add the patch part 1 link: http://www.winapizone.net/replaceinfiles.part1.patch

kkez 2006-06-13 09:45

I've now removed the ConfirmReplaceMultipleDlg class and merged the new buttons events with the existing ConfirmReplaceDlg class. Sorry for all these updates :)

kkez 2006-06-13 10:38

Another update :)

I forgot to add the new xrc file and the "reload file" dialog now uses the correct "confirm replace" dialog. I don't quite understand the implicit conversion between the wxString "File has changed from the last..." and the bool in the ConfirmReplace constructor. Luckily i've spotted it immediately :)

kkez 2006-06-13 16:08

Now i close the file when no replacements took place only if i opened the file in the editor (because it wasn't already open), and i replaced the loop to check if the file is already open with IsOpen. Now this patch should really be ok.

mandrav 2006-06-14 11:11

Thanks for the patch.

I still had to fix/improve some things but it's nice that you provided the base code :)