File: generic_context.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (164 lines) | stat: -rw-r--r-- 6,446 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : generic_context.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "generic_context.h"
#include "editor_config.h"
#include "cl_editor.h"
#include "clEditorWordCharsLocker.h"
#include "clEditorColouriseLocker.h"
#include "file_logger.h"

ContextGeneric::ContextGeneric(LEditor* container, const wxString& name)
    : ContextBase(container)
{
    SetName(name);
    ApplySettings();
}

ContextGeneric::~ContextGeneric() {}

ContextBase* ContextGeneric::NewInstance(LEditor* container) { return new ContextGeneric(container, GetName()); }

void ContextGeneric::ApplySettings()
{
    LexerConf::Ptr_t lexPtr;
    if(EditorConfigST::Get()->IsOk()) {
        lexPtr = EditorConfigST::Get()->GetLexer(GetName());
    }
    LEditor& rCtrl = GetCtrl();
    if(lexPtr) {
        rCtrl.SetLexer(lexPtr->GetLexerId());
        for(int i = 0; i <= 4; ++i) {
            wxString keyWords = lexPtr->GetKeyWords(i);
            keyWords.Replace(wxT("\n"), wxT(" "));
            keyWords.Replace(wxT("\r"), wxT(" "));
            rCtrl.SetKeyWords(i, keyWords);
        }

        if(lexPtr->GetName() == "css") {
            // set the word characters for the CSS lexer
            GetCtrl().SetWordChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-@");
        }
    } else {
        rCtrl.SetLexer(wxSTC_LEX_NULL);
    }
    DoApplySettings(lexPtr);
}

void ContextGeneric::ProcessIdleActions()
{
    LEditor& ctrl = GetCtrl();
    if((ctrl.GetLexerId() == wxSTC_LEX_XML) || (ctrl.GetLexerId() == wxSTC_LEX_PHPSCRIPT) ||
       (ctrl.GetLexerId() == wxSTC_LEX_HTML)) {
        // XML lexer, highlight XML tags
        // Update the word chars
        clEditorXmlHelper xmlHelper(ctrl.GetCtrl());
        clEditorColouriseLocker colouriseLocker(ctrl.GetCtrl());

        // Highlight matching tags
        int startPos, endPos;
        wxString word = xmlHelper.GetXmlTagAt(ctrl.GetCurrentPosition(), startPos, endPos);
        if(word.IsEmpty()) {
            ctrl.SetIndicatorCurrent(MARKER_CONTEXT_WORD_HIGHLIGHT);
            ctrl.IndicatorClearRange(0, ctrl.GetLength());
            return;
        }

        static wxRegEx reOpenHtmlTag("<([a-z_\\-0-9]+)", wxRE_ADVANCED | wxRE_ICASE);
        static wxRegEx reCloseHtmlTag("</([a-z_\\-0-9]+)>", wxRE_ADVANCED | wxRE_ICASE);

        wxString searchWhat;
        wxString closeTag;

        if(reOpenHtmlTag.Matches(word)) {
            searchWhat = reOpenHtmlTag.GetMatch(word, 1);
            closeTag << "</" << searchWhat << ">";
            wxRegEx reOpenTag("<" + searchWhat + "[>]?", wxRE_ADVANCED | wxRE_ICASE);

            int pos = endPos;
            int depth = 0;
            int where = FindNext(searchWhat, pos, true);

            while(where != wxNOT_FOUND) {
                int startPos2, endPos2;
                word = xmlHelper.GetXmlTagAt(where, startPos2, endPos2);
                if((closeTag == word) && (depth == 0)) {
                    // We got the closing brace
                    ctrl.SetIndicatorCurrent(MARKER_CONTEXT_WORD_HIGHLIGHT);
                    ctrl.IndicatorClearRange(0, ctrl.GetLength());

                    // Set the new markers
                    ctrl.IndicatorFillRange(startPos, endPos - startPos);
                    ctrl.IndicatorFillRange(startPos2, endPos2 - startPos2);
                    return;

                } else if(closeTag == word) {
                    --depth;
                } else if(reOpenTag.Matches(word)) {
                    depth++;
                }
                where = FindNext(searchWhat, pos, true);
            }

        } else if(reCloseHtmlTag.Matches(word)) {
            searchWhat = reCloseHtmlTag.GetMatch(word, 1);
            closeTag << "</" << searchWhat << ">";
            
            wxString reString = "<" + searchWhat + "[>]?";
            wxRegEx reOpenTag(reString, wxRE_DEFAULT | wxRE_ICASE);
            if(!reOpenTag.IsValid()) {
                clDEBUG() << "Invalid regex:" << reString << clEndl;
            }
            
            int pos = startPos;
            int depth = 0;
            int where = FindPrev(searchWhat, pos, true);

            while(where != wxNOT_FOUND) {
                int startPos2, endPos2;
                word = xmlHelper.GetXmlTagAt(where, startPos2, endPos2);
                if(reOpenTag.Matches(word) && (depth == 0)) {
                    // We got the closing brace
                    ctrl.SetIndicatorCurrent(MARKER_CONTEXT_WORD_HIGHLIGHT);
                    ctrl.IndicatorClearRange(0, ctrl.GetLength());

                    // Set the new markers
                    ctrl.IndicatorFillRange(startPos, endPos - startPos);
                    ctrl.IndicatorFillRange(startPos2, endPos2 - startPos2);
                    return;

                } else if(closeTag == word) {
                    ++depth;
                } else if(reOpenTag.Matches(word)) {
                    --depth;
                }
                where = FindPrev(searchWhat, pos, true);
            }
        }

        // Clear the current selection
        ctrl.SetIndicatorCurrent(MARKER_CONTEXT_WORD_HIGHLIGHT);
        ctrl.IndicatorClearRange(0, ctrl.GetLength());
    }
}