1 /*
2 * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3 * http://www.gnu.org/licenses/gpl-3.0.html
4 *
5 * $Revision: 9264 $
6 * $Id: tidycmt.cpp 9264 2013-08-17 09:35:46Z mortenmacfly $
7 * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/plugins/tidycmt/tidycmt.cpp $
8 */
9
10 #include "sdk.h"
11 #ifndef CB_PRECOMP
12 #include <wx/intl.h>
13 #include <wx/string.h>
14 #include "cbeditor.h"
15 #include "manager.h"
16 #endif
17 #include "tidycmt.h"
18 #include "cbstyledtextctrl.h"
19
20 #define SCI_SETUNDOCOLLECTION 2012
21
22 const unsigned int len = 112;
23
24 namespace
25 {
26 PluginRegistrant<TidyCmt> reg(_T("tidycmt"));
27 }
28
OnAttach()29 void TidyCmt::OnAttach()
30 {
31 Manager::Get()->RegisterEventSink(cbEVT_EDITOR_BEFORE_SAVE, new cbEventFunctor<TidyCmt, CodeBlocksEvent>(this, &TidyCmt::OnSave));
32 }
33
OnSave(CodeBlocksEvent & event)34 void TidyCmt::OnSave(CodeBlocksEvent& event)
35 {
36 cbEditor* ed = (cbEditor*) event.GetEditor();
37 cbStyledTextCtrl* ctrl = ed->GetControl();
38
39 unsigned int n = ctrl->GetLineCount();
40
41 int pos = ctrl->GetCurrentPos();
42 ctrl->SendMsg(SCI_SETUNDOCOLLECTION, 0, 0);
43
44 for(unsigned int i = 0; i < n; ++i)
45 {
46 int a = ctrl->GetLineIndentPosition(i);
47 int b = ctrl->GetLineEndPosition(i);
48
49 wxString s = ctrl->GetTextRange(a,b);
50
51 if(s.StartsWith(_T("//--")))
52 {
53 unsigned int from = s.find_first_not_of(_T("/- \t\r\n"));
54 unsigned int to = s.find_last_not_of(_T("/- \t\r\n")) + 1;
55 s = s.Mid(from, to - from);
56
57 unsigned int pad = len - s.length() - 8 - ctrl->GetLineIndentation(i);
58 s = _T("//---- ") + s + _T(' ') + wxString(_T('-'), pad);
59
60 ctrl->SetTargetStart(a);
61 ctrl->SetTargetEnd(b);
62 ctrl->ReplaceTarget(s);
63 }
64 if(s.StartsWith(_T("/*--")) && s.EndsWith(_T("*/")))
65 {
66 s.RemoveLast().RemoveLast();
67 unsigned int from = s.find_first_not_of(_T("/*- \t\r\n"));
68 unsigned int to = s.find_last_not_of(_T("/*- \t\r\n")) + 1;
69 s = s.Mid(from, to - from);
70 unsigned int pad = len - s.length() - 10 - ctrl->GetLineIndentation(i);
71 s = _T("/*---- ") + s + _T(' ') + wxString(_T('-'), pad) + _T("*/");
72
73 ctrl->SetTargetStart(a);
74 ctrl->SetTargetEnd(b);
75 ctrl->ReplaceTarget(s);
76 }
77 }
78 ctrl->SendMsg(SCI_SETUNDOCOLLECTION, 1, 0);
79 ctrl->SetCurrentPos(pos);
80 }
81