1 #include "SmartIndentLua.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<SmartIndentLua> reg(wxT("SmartIndentLua"));
20 }
21 
OnEditorHook(cbEditor * ed,wxScintillaEvent & event) const22 void SmartIndentLua::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 Lua 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("Lua") )
44         return;
45 
46     ed->AutoIndentDone(); // we are responsible.
47 
48     // if a newline was added
49     const int pos = stc->GetCurrentPos();
50     int currLine = stc->LineFromPosition(pos);
51 
52     if (currLine == 0)
53         return;
54 
55     wxChar ch = event.GetKey();
56 
57     // indent
58     if ( (ch == wxT('\n')) || ( (stc->GetEOLMode() == wxSCI_EOL_CR) && (ch == wxT('\r')) ) )
59     {
60         if (AutoIndentEnabled())
61         {
62             stc->BeginUndoAction();
63             wxString indent = ed->GetLineIndentString(currLine - 1);
64             BraceIndent(stc, indent);
65             stc->InsertText(pos, indent);
66             stc->GotoPos(pos + indent.Length());
67             stc->ChooseCaretX();
68             stc->EndUndoAction();
69         }
70     }
71 
72     bool braceCompleted = false;
73     if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
74         braceCompleted = stc->DoSelectionBraceCompletion(ch);
75     if (!braceCompleted && BraceCompletionEnabled())
76         stc->DoBraceCompletion(ch);
77 }
78 
BraceIndent(cbStyledTextCtrl * stc,wxString & indent) const79 bool SmartIndentLua::BraceIndent(cbStyledTextCtrl *stc, wxString &indent)const
80 {
81     if ( BraceSmartIndentEnabled() )
82     {
83         int style = wxSCI_LUA_STRING;
84 
85         int brace_position = GetFirstBraceInLine(stc, style);
86         return Indent(stc, indent, brace_position);
87     }
88     return false;
89 }
90