1
#include "SmartIndentFortran.h"
3
#include <sdk.h> // Code::Blocks SDK
7
#include <configmanager.h>
8
#include <editormanager.h>
9
#include <editorcolourset.h>
13
#include <cbstyledtextctrl.h>
15
// Register the plugin with Code::Blocks.
16
// We are using an anonymous namespace so we don't litter the global one.
19
PluginRegistrant<SmartIndentFortran> reg(wxT("SmartIndentFortran"));
22
void SmartIndentFortran::OnEditorHook(cbEditor* ed, wxScintillaEvent& event) const
24
// check if smart indent is enabled
25
// check the event type and the currently set language
26
// if it is not a CharAdded event or the language is not Fortran return
31
if ( !SmartIndentEnabled() )
34
wxEventType type = event.GetEventType();
35
if ( type != wxEVT_SCI_CHARADDED )
38
cbStyledTextCtrl* stc = ed->GetControl();
42
wxString langname = Manager::Get()->GetEditorManager()->GetColourSet()->GetLanguageName(ed->GetLanguage());
43
if ( langname != wxT("Fortran") && langname != wxT("Fortran77") ) return;
45
ed->AutoIndentDone(); // we are responsible.
47
const int pos = stc->GetCurrentPos();
48
wxChar ch = event.GetKey();
50
if ( (ch == _T('\n')) || ( (stc->GetEOLMode() == wxSCI_EOL_CR) && (ch == _T('\r')) ) )
52
const bool autoIndent = AutoIndentEnabled();
54
stc->BeginUndoAction();
56
int currLine = stc->LineFromPosition(pos);
59
if (autoIndent && currLine > 0)
61
wxString indent = ed->GetLineIndentString(currLine - 1);
62
stc->InsertText(pos, indent);
63
stc->GotoPos(pos + indent.Length());
68
bool smartIndent = SmartIndentEnabled();
69
if (smartIndent && currLine > 0)
71
int start = stc->GetLineIndentPosition(currLine - 1);
72
int endt = stc->WordEndPosition(start, true);
73
wxString text = stc->GetTextRange(start, endt).Lower();
74
wxString lineText = stc->GetLine(currLine - 1).BeforeFirst('!').Lower();
75
wxString lastText = lineText.AfterLast(')').Trim().Trim(false);
76
wxString secText = lineText.Trim(false).Mid(text.Length()).Trim(false);
77
if ( (text == _T("if") && lastText == _T("then"))
79
|| text == _T("elseif")
81
||(text == _T("where") && lastText.IsEmpty())
82
|| text == _T("elsewhere")
83
|| text == _T("block")
84
|| text == _T("blockdata")
85
||(text == _T("forall") && lastText.IsEmpty())
86
|| text == _T("while")
88
|| text == _T("associate")
89
|| text == _T("block")
90
|| text == _T("critical")
92
||(text == _T("type") && !secText.StartsWith(_T("(")))
93
|| text == _T("program")
94
|| text == _T("function")
95
|| text == _T("subroutine")
96
|| text == _T("interface")
97
||( text == _T("module")
98
&& !secText.StartsWith(_T("procedure "))
99
&& !secText.StartsWith(_T("procedure:")) ) )
105
stc->EndUndoAction();
108
bool braceCompleted = false;
109
if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
110
braceCompleted = stc->DoSelectionBraceCompletion(ch);
111
if (!braceCompleted && BraceCompletionEnabled())
112
stc->DoBraceCompletion(ch);