~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/SmartIndent/SmartIndentFortran.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "SmartIndentFortran.h"
 
2
 
 
3
#include <sdk.h> // Code::Blocks SDK
 
4
 
 
5
#ifndef CB_PRECOMP
 
6
    #include <cbeditor.h>
 
7
    #include <configmanager.h>
 
8
    #include <editormanager.h>
 
9
    #include <editorcolourset.h>
 
10
    #include <manager.h>
 
11
#endif
 
12
 
 
13
#include <cbstyledtextctrl.h>
 
14
 
 
15
// Register the plugin with Code::Blocks.
 
16
// We are using an anonymous namespace so we don't litter the global one.
 
17
namespace
 
18
{
 
19
    PluginRegistrant<SmartIndentFortran> reg(wxT("SmartIndentFortran"));
 
20
}
 
21
 
 
22
void SmartIndentFortran::OnEditorHook(cbEditor* ed, wxScintillaEvent& event) const
 
23
{
 
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
 
27
 
 
28
    if (!ed)
 
29
        return;
 
30
 
 
31
    if ( !SmartIndentEnabled() )
 
32
        return;
 
33
 
 
34
    wxEventType type = event.GetEventType();
 
35
    if ( type != wxEVT_SCI_CHARADDED )
 
36
        return;
 
37
 
 
38
    cbStyledTextCtrl* stc = ed->GetControl();
 
39
    if (!stc)
 
40
        return;
 
41
 
 
42
    wxString langname = Manager::Get()->GetEditorManager()->GetColourSet()->GetLanguageName(ed->GetLanguage());
 
43
    if ( langname != wxT("Fortran") && langname != wxT("Fortran77") ) return;
 
44
 
 
45
    ed->AutoIndentDone(); // we are responsible.
 
46
 
 
47
    const int pos = stc->GetCurrentPos();
 
48
    wxChar ch = event.GetKey();
 
49
 
 
50
    if ( (ch == _T('\n')) || ( (stc->GetEOLMode() == wxSCI_EOL_CR) && (ch == _T('\r')) ) )
 
51
    {
 
52
        const bool autoIndent = AutoIndentEnabled();
 
53
 
 
54
        stc->BeginUndoAction();
 
55
 
 
56
        int currLine = stc->LineFromPosition(pos);
 
57
 
 
58
        // auto indent
 
59
        if (autoIndent && currLine > 0)
 
60
        {
 
61
            wxString indent = ed->GetLineIndentString(currLine - 1);
 
62
            stc->InsertText(pos, indent);
 
63
            stc->GotoPos(pos + indent.Length());
 
64
            stc->ChooseCaretX();
 
65
        }
 
66
 
 
67
        // smart indent
 
68
        bool smartIndent = SmartIndentEnabled();
 
69
        if (smartIndent && currLine > 0)
 
70
        {
 
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"))
 
78
                || text == _T("else")
 
79
                || text == _T("elseif")
 
80
                || text == _T("enum")
 
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")
 
87
                || text == _T("case")
 
88
                || text == _T("associate")
 
89
                || text == _T("block")
 
90
                || text == _T("critical")
 
91
                || text == _T("do")
 
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:")) ) )
 
100
            {
 
101
                stc->Tab();
 
102
            }
 
103
        }
 
104
 
 
105
        stc->EndUndoAction();
 
106
    }
 
107
 
 
108
    bool braceCompleted = false;
 
109
    if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
 
110
        braceCompleted = stc->DoSelectionBraceCompletion(ch);
 
111
    if (!braceCompleted && BraceCompletionEnabled())
 
112
        stc->DoBraceCompletion(ch);
 
113
}