-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormatFortranIndentPlugin.cpp
692 lines (587 loc) · 20.2 KB
/
FormatFortranIndentPlugin.cpp
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/***************************************************************
* Name: FormatFortranIndentPlugin.cpp
* Purpose: Code::Blocks Plugin to Format Source Code Indent
* in Fortran 95, Fortran 90 Source Code (free source form)
* Author: YWX ([email protected])
* Created: 2011-12-22
* Copyright: (c) YWX <[email protected]>
* License: GNU General Public License, version 3
**************************************************************/
#include <sdk.h> // Code::Blocks SDK
#include <configurationpanel.h>
#include "FormatFortranIndentPlugin.h"
#include "CFortranIndentConfigDlg.h"
#include "CMyFortranIndentConfig.h"
#include <wx/progdlg.h>
#include "cbstyledtextctrl.h"
#include <configmanager.h>
#include <editormanager.h>
#include <projectmanager.h>
#include <logmanager.h>
#include <cbproject.h>
#include <cbeditor.h>
#include <wx/dynarray.h>
WX_DEFINE_ARRAY_INT(int, intArray);
namespace
{
const int idCodeFortranIndentFile = wxNewId();
const int idCodeFortranIndentActiveFile = wxNewId();
const int idCodeFortranIndentProject = wxNewId();
}
BEGIN_EVENT_TABLE( FormatFortranIndentPlugin, cbPlugin )
EVT_MENU( idCodeFortranIndentActiveFile, FormatFortranIndentPlugin::OnFormatActiveFile )
EVT_MENU( idCodeFortranIndentProject, FormatFortranIndentPlugin::OnFormatProject )
END_EVENT_TABLE()
// Register the plugin with Code::Blocks.
// We are using an anonymous namespace so we don't litter the global one.
namespace
{
PluginRegistrant<FormatFortranIndentPlugin> reg(_T("FormatFortranIndentPlugin"));
}
// constructor
FormatFortranIndentPlugin::FormatFortranIndentPlugin()
{
// Make sure our resources are available.
// In the generated boilerplate code we have no resources but when
// we add some, it will be nice that this code is in place already ;)
if(!Manager::LoadResource(_T("FormatFortranIndentPlugin.zip")))
{
NotifyMissingFile(_T("FormatFortranIndentPlugin.zip"));
}
}
// destructor
FormatFortranIndentPlugin::~FormatFortranIndentPlugin()
{
}
void FormatFortranIndentPlugin::OnAttach()
{
// do whatever initialization you need for your plugin
// NOTE: after this function, the inherited member variable
// m_IsAttached will be TRUE...
// You should check for it in other functions, because if it
// is FALSE, it means that the application did *not* "load"
// (see: does not need) this plugin...
}
void FormatFortranIndentPlugin::OnRelease(bool appShutDown)
{
// do de-initialization for your plugin
// if appShutDown is true, the plugin is unloaded because Code::Blocks is being shut down,
// which means you must not use any of the SDK Managers
// NOTE: after this function, the inherited member variable
// m_IsAttached will be FALSE...
}
int FormatFortranIndentPlugin::Configure()
{
//create and display the configuration dialog for your plugin
//cbConfigurationDialog dlg(Manager::Get()->GetAppWindow(), wxID_ANY, _("Your dialog title"));
//cbConfigurationPanel* panel = GetConfigurationPanel(&dlg);
//if (panel)
//{
// dlg.AttachConfigurationPanel(panel);
// PlaceWindow(&dlg);
// return dlg.ShowModal() == wxID_OK ? 0 : -1;
//}
//return -1;
return 0;
}
cbConfigurationPanel* FormatFortranIndentPlugin::GetConfigurationPanel(wxWindow* parent)
{
CFortranIndentConfigDlg* dlg = new CFortranIndentConfigDlg(parent);
// deleted by the caller
return dlg;
}
void FormatFortranIndentPlugin::BuildMenu(wxMenuBar* menuBar)
{
//The application is offering its menubar for your plugin,
//to add any menu items you want...
if (!IsAttached())
return;
//Append any items you need in the menu...
/// Append to Menu Fortran
int pos = menuBar->FindMenu(_("&Fortran"));
if( pos != wxNOT_FOUND )
{
wxMenu* m_FortranMenu = menuBar->GetMenu(pos);
m_FortranMenu->Append(idCodeFortranIndentActiveFile, _("Format Fortran Indent"));
}
else
{
//Manager::Get()->GetLogManager()->Log( _("Could not find Fortran Menu!") );
/// Append to Menu Tools
pos = menuBar->FindMenu(_("&Tools"));
if( pos != wxNOT_FOUND )
{
/// Create Fortran Menu
wxMenu* m_FortranMenu = new wxMenu(); // pointer to the Fortran menu
menuBar->Insert(pos, m_FortranMenu, _("&Fortran"));
/// Append to Menu Fortran
m_FortranMenu->Append(idCodeFortranIndentActiveFile, _("Format Fortran Indent"));
}
else
{
Manager::Get()->GetLogManager()->Log( _("Could not find Tools Menu!") );
}
}
/// Append to Menu Plugins
pos = menuBar->FindMenu(_("P&lugins"));
if( pos != wxNOT_FOUND )
{
wxMenu* m_PluginsMenu = menuBar->GetMenu(pos);
m_PluginsMenu->Prepend(idCodeFortranIndentActiveFile, _("Format Fortran Indent"));
}
else
{
Manager::Get()->GetLogManager()->Log( _("Could not find Plugins Menu!") );
}
//NOTE: Be careful in here... The application's menubar is at your disposal.
//NotImplemented(_T("FormatFortranIndentPlugin::BuildMenu()"));
}
void FormatFortranIndentPlugin::BuildModuleMenu( const ModuleType type, wxMenu* menu, const FileTreeData* data )
{
if ( !menu || !IsAttached() )
return;
switch ( type )
{
case mtEditorManager:
menu->AppendSeparator();
menu->Append( idCodeFortranIndentActiveFile, _( "Format Fortran Indent" ), _( "Format the selected Fortran source code Indent (selected line) in the current file" ) );
break;
case mtProjectManager:
if ( data ) switch ( data->GetKind() )
{
case FileTreeData::ftdkProject:
menu->AppendSeparator();
menu->Append( idCodeFortranIndentProject, _( "Format Fortran Indent this project" ), _( "Format the Fortran source code Indent in this project" ) );
menu->AppendSeparator();
break;
case FileTreeData::ftdkFile:
menu->AppendSeparator();
menu->Append( idCodeFortranIndentProject, _( "Format Fortran Indent this file" ), _( "Format the Fortran source code Indent in this file" ) );
break;
default:
// Do nothing.
break;
}
break;
default:
break;
}
}
bool FormatFortranIndentPlugin::BuildToolBar(wxToolBar* toolBar)
{
//The application is offering its toolbar for your plugin,
//to add any toolbar items you want...
//Append any items you need on the toolbar...
NotImplemented(_T("FormatFortranIndentPlugin::BuildToolBar()"));
// return true if you add toolbar items
return false;
}
///
bool FormatFortranIndentPlugin::isFortranFilename( const wxFileName fname )
{
wxString fExt;
fExt = fname.GetExt().Lower();
if( wxT("f90") == fExt || wxT("f95") == fExt || wxT("f03") == fExt )
return true;
if( wxT("for") == fExt || wxT("f") == fExt || wxT("fi") == fExt || wxT("fd") == fExt )
return true;
return false;
}
bool FormatFortranIndentPlugin::isFortranFilename( const wxString & filename )
{
bool isFortran = false;
wxFileName fname;
fname.Assign(filename);
isFortran = isFortranFilename( fname );
return isFortran;
}
void FormatFortranIndentPlugin::OnFormatProject( wxCommandEvent& /*event*/ )
{
ProjectManager* manager = Manager::Get()->GetProjectManager();
wxTreeCtrl *tree = manager->GetUI().GetTree();
if ( !tree )
return;
wxTreeItemId treeItem = tree->GetSelection();
if ( !treeItem.IsOk() )
return;
const FileTreeData *data = static_cast<FileTreeData*>( tree->GetItemData( treeItem ) );
if ( !data )
return;
switch ( data->GetKind() )
{
case FileTreeData::ftdkProject:
{
cbProject* prj = data->GetProject();
wxProgressDialog progressDlg(_("Please wait"), _("Formatting..."), prj->GetFilesCount(), 0, wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_SMOOTH );
progressDlg.Show();
for ( int i = 0; i < prj->GetFilesCount(); ++i )
{
ProjectFile* pf = prj->GetFile( i );
wxString filename = pf->file.GetFullPath();
FileType fileType = FileTypeOf( filename );
if ( fileType == ftSource || fileType == ftHeader )
{
if( isFortranFilename( pf->file ) )
{
FormatFile( filename );
if ( false == progressDlg.Update( i, wxString(_("Formatting ")) + pf->relativeFilename ) )
break;
}
}
}
}
break;
case FileTreeData::ftdkFile:
{
ProjectFile* f = data->GetProject()->GetFile( data->GetFileIndex() );
if ( f )
if( isFortranFilename( f->file ) )
FormatFile( f->file.GetFullPath() );
}
break;
default:
break;
}
}
void FormatFortranIndentPlugin::OnFormatActiveFile( wxCommandEvent& /*event*/ )
{
Execute();
}
int FormatFortranIndentPlugin::Execute()
{
// do your magic ;)
//NotImplemented(_T("FormatFortranIndentPlugin::Execute()"));
if( !IsAttached() )
return -1;
cbEditor *ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed)
return 0;
cbStyledTextCtrl* control = ed->GetControl();
if( ( ! isFortranFilename( ed->GetFilename() ) ) && ( wxSCI_LEX_FORTRAN != control->GetLexer() ) )
{
if( cbMessageBox( wxT("Are you sure \n") + ed->GetFilename() +
wxT("\nis a Fortran Free Format Source File?\nContinue to Format the Indent?"), _("Error Message"),
wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT ) == wxID_NO )
return 0;
}
FormatEditor( ed );
Manager::Get()->GetLogManager()->Log( _("Format Fortran Indent Plugin!") );
return 0;
//return -1;
}
void FormatFortranIndentPlugin::FormatFile( const wxString &filename )
{
cbEditor* ed = Manager::Get()->GetEditorManager()->IsBuiltinOpen( filename );
if ( ed ) // File is already open
FormatEditor( ed );
else
{
// File is not open. We must open it.
ed = Manager::Get()->GetEditorManager()->Open( filename );
if ( ed )
{
bool changed = FormatEditor( ed );
if ( !changed ) // We opened a file and it didn't change. Close it.
Manager::Get()->GetEditorManager()->Close( filename );
}
}
}
bool FormatFortranIndentPlugin::FormatEditor( cbEditor *ed )
{
cbStyledTextCtrl* control = ed->GetControl();
if (control->GetReadOnly())
{
cbMessageBox(_("The file is read-only!"), _("Error"), wxICON_ERROR);
return false;
}
wxString eolChars;
switch (control->GetEOLMode())
{
case wxSCI_EOL_CRLF:
eolChars = _T("\r\n");
break;
case wxSCI_EOL_CR:
eolChars = _T("\r");
break;
case wxSCI_EOL_LF:
eolChars = _T("\n");
break;
}
// load settings
CMyFortranIndentConfig myFortranIndentConfig;
ConfigManager *cfg = Manager::Get()->GetConfigManager(_T("fortran_indent"));
wxASSERT( 0 != cfg );
if( cfg->Read( _T("is_SameAsEditor"), & myFortranIndentConfig.isSameAsEditor ) )
{
cfg->Read( _T("is_UseTab"), & myFortranIndentConfig.isUseTab );
cfg->Read( _T("i_TabWidth"), & myFortranIndentConfig.iTabWidth );
cfg->Read( _T("is_KeepBlankLineOnly"), & myFortranIndentConfig.isKeepBlankLineOnly );
cfg->Read( _T("is_TrimLineFromRight"), & myFortranIndentConfig.isTrimLineFromRight );
cfg->Read( _T("i_PreprocessorType"), & myFortranIndentConfig.iPreprocessorType );
}
bool isOnlyBlankLine = myFortranIndentConfig.isKeepBlankLineOnly;
bool isTrimLineFromRight = myFortranIndentConfig.isTrimLineFromRight;
wxString indentStr;
if( myFortranIndentConfig.isSameAsEditor )
{
if( control->GetUseTabs() )
indentStr = wxT("\t");
else
indentStr = wxString(wxT(' '), control->GetTabWidth() );
}
else
{
if( myFortranIndentConfig.isUseTab )
indentStr = wxT("\t");
else
indentStr = wxString(wxT(' '), myFortranIndentConfig.iTabWidth );
}
ed->UnfoldAll();
bool onlySelected = false;
wxString edText;
const int pos_cur = control->GetCurrentPos();
int pos_selStart = control->GetSelectionStart();
int pos_selEnd = control->GetSelectionEnd();
int indexLineStart = 0, indexLineEnd = 0;
int nLines = control->GetLineCount();
if( pos_selStart != pos_selEnd )
{
onlySelected = true;
indexLineStart = control->LineFromPosition( pos_selStart );
control->GotoLine( indexLineStart );
pos_selStart = control->GetCurrentPos();
indexLineEnd = control->LineFromPosition( pos_selEnd );
if( indexLineEnd == nLines )
{
control->GotoLine( indexLineEnd );
control->LineEnd();
}
else
{
control->GotoLine( indexLineEnd + 1 );
}
pos_selEnd = control->GetCurrentPos();
control->SetSelectionStart( pos_selStart );
control->SetSelectionEnd( pos_selEnd );
edText = control->GetTextRange( pos_selStart, pos_selEnd );
}
else
edText = control->GetText();
int indentNum = 0;
if( onlySelected )
{
nLines = indexLineEnd + 1;
int indentW = ed->GetLineIndentInSpaces( indexLineStart );
int tabW = control->GetTabWidth();
indentNum = indentW / tabW;
if( (indentW % tabW) > 0 )
{
++indentNum;
}
}
else
{
indexLineStart = 0;
indexLineEnd = nLines - 1;
}
wxString formattedText;
bool isCur = true;
bool isCaseBegin = false;
IsMultiLines isMultiLines;
wxString tmpLine;
wxString tmpMultiLines;
/// WX_DEFINE_ARRAY_INT(int, intArray);
intArray new_bookmark_array;
intArray ed_breakpoints_array;
wxSetCursor(*wxHOURGLASS_CURSOR);
///formattedText
for( int i = indexLineStart; i < nLines; ++i )
{
if (ed->HasBookmark(i))
{
new_bookmark_array.Add( i );
}
if (ed->HasBreakpoint(i))
{
ed_breakpoints_array.Add( i );
}
isCur = true;
tmpLine = control->GetLine( i );
if( myWxFortranIndent.getIsHasPreprocessor( tmpLine, myFortranIndentConfig.iPreprocessorType ) )
{
if( isTrimLineFromRight )
{
tmpLine.Trim(); //trim from right
if( i < indexLineEnd )
{
tmpLine += eolChars;
}
}
formattedText += tmpLine;
continue;
}
tmpLine = tmpLine.Trim(false); // trim from left
if( myWxFortranIndent.getIsHasLineContinuation( tmpLine ) )
{
wxString tempLine;
tempLine = tmpLine; // trim from left
if( false == isMultiLines.isHaveMultiLines )
{
isMultiLines.isHaveMultiLines = true;
isMultiLines.iFirstLineNo = i;
tmpMultiLines.Empty();
}
myWxFortranIndent.delLineContinuation( tempLine );
if( tempLine.Len() > 0 )
{
if( wxT('&') == tempLine[0] )
{
tempLine = tempLine.Mid( 1 );
}
}
myWxFortranIndent.delComment( tempLine );
tmpMultiLines += tempLine;
continue ;
}
if( true == isMultiLines.isHaveMultiLines )
{
isMultiLines.iEndLineNo = i;
wxString tempLine;
tempLine = tmpLine; // trim from left
if( tempLine.Len() > 0 )
{
if( wxT('&') == tempLine[0] )
{
tempLine = tempLine.Mid( 1 );
}
}
tmpMultiLines += tempLine;
myWxFortranIndent.getFortranIndentLine( tmpMultiLines, indentNum, isCur, isCaseBegin );
for( int j = isMultiLines.iFirstLineNo; j <= isMultiLines.iEndLineNo; ++j )
{
tempLine = control->GetLine( j ).Trim(false); // trim from left
if( 0 == tempLine.Len() && j < indexLineEnd )
{
if( isOnlyBlankLine )
{
formattedText += eolChars;
continue;
}
else
{
tempLine += eolChars;
}
}
else
{
if( isTrimLineFromRight )
{
tempLine.Trim(); //trim from right
if( j < indexLineEnd )
{
tempLine += eolChars;
}
}
}
int tmpN = indentNum;
if( j == isMultiLines.iFirstLineNo )
{
if( false == isCur )
{
tmpN -= 1;
}
}
else
{
tmpN += 1;
}
for( int k = 0; k < tmpN; ++k )
{
formattedText += indentStr;
}
formattedText += tempLine;
}
isMultiLines.reset();
}
else
{
if( 0 == tmpLine.Len() && i < indexLineEnd )
{
if( isOnlyBlankLine )
{
formattedText += eolChars;
continue;
}
else
{
tmpLine += eolChars;
}
}
else
{
if( isTrimLineFromRight )
{
tmpLine.Trim(); //trim from right
if( i < indexLineEnd )
{
tmpLine += eolChars;
}
}
myWxFortranIndent.getFortranIndentLine( tmpLine, indentNum, isCur, isCaseBegin );
}
int tmpN = indentNum;
if( false == isCur )
{
tmpN = tmpN - 1;
}
for( int k = 0; k < tmpN; ++k )
{
formattedText += indentStr;
}
formattedText += tmpLine;
}
}
bool changed = myWxFortranIndent.BuffersDiffer( formattedText, edText, eolChars, !( isOnlyBlankLine || isTrimLineFromRight ) );
if ( changed )
{
// clear Bookmark and Breakpoint in change
size_t iCount = 0;
iCount = new_bookmark_array.GetCount();
for( size_t i = 0; i < iCount; ++i )
{
ed->ToggleBookmark( new_bookmark_array[i] ); // new_bookmark_array.Item(i)
}
iCount = 0;
iCount = ed_breakpoints_array.GetCount();
for( size_t i = 0; i < iCount; ++i )
{
ed->ToggleBreakpoint( ed_breakpoints_array[i] ); // ed_breakpoints_array.Item(i)
}
if (onlySelected)
{
control->ReplaceSelection(formattedText);
}
else
{
control->SetText(formattedText);
}
// set Bookmark and Breakpoint in change
iCount = 0;
iCount = new_bookmark_array.GetCount();
for( size_t i = 0; i < iCount; ++i )
{
ed->ToggleBookmark( new_bookmark_array[i] ); // new_bookmark_array.Item(i)
}
iCount = 0;
iCount = ed_breakpoints_array.GetCount();
for( size_t i = 0; i < iCount; ++i )
{
ed->ToggleBreakpoint( ed_breakpoints_array[i] ); // ed_breakpoints_array.Item(i)
}
control->GotoPos(pos_cur);
}
wxSetCursor(wxNullCursor);
new_bookmark_array.Clear();
ed_breakpoints_array.Clear();
return changed;
}