File: qsourcecolorizer.h

package info (click to toggle)
kdevelop3 4%3A3.2.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 58,220 kB
  • ctags: 33,997
  • sloc: cpp: 278,404; ansic: 48,238; sh: 23,721; tcl: 19,882; perl: 5,498; makefile: 4,717; ruby: 1,610; python: 1,549; awk: 1,484; xml: 563; java: 359; php: 20; asm: 14; ada: 5; pascal: 4; fortran: 4; haskell: 2; sql: 1
file content (304 lines) | stat: -rw-r--r-- 8,415 bytes parent folder | download | duplicates (2)
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
/*
 *  This file is part of Klint
 *  Copyright (C) 2001 Roberto Raggi (roberto@kdevelop.org)
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 */

#ifndef qsourcecolorizer_h
#define qsourcecolorizer_h

#include <private/qrichtext_p.h>
#include <qintdict.h>
#include <qptrlist.h>
#include <qmap.h>
#include <qregexp.h>
#include <qpair.h>
#include <kdebug.h>

#define DECLARE_FORMAT_ITEM(type, id, f, c)\
{\
    QFont font = f; \
    QColor color = c; \
    font = config->readFontEntry( QString("Font ") + id, &font ); \
    color = config->readColorEntry( QString("Color ") + id, &color ); \
    m_formats.insert( type, qMakePair(QString(id), new QTextFormat(font, color)) ); \
}

#define UPDATE_FORMAT_ITEM(type, f, c)\
    m_formats[ type ].second->setFont( f ); \
    m_formats[ type ].second->setColor( c );

#define STORE_FORMAT_ITEM(type)\
{\
    QString id = m_formats[ type ].first; \
    QTextFormat* fmt = m_formats[ type ].second; \
    config->writeEntry( QString("Font ") + id, fmt->font() ); \
    config->writeEntry( QString("Color ") + id, fmt->color() ); \
}

class QEditor;

class HLItem{
public:
    HLItem( int state=0, int context=0 )
        : m_state( state ), m_context( context ) {}
    virtual ~HLItem() {}

    virtual int attr() const { return m_state; }
    virtual int context() const { return m_context; }

    virtual int checkHL( const QChar* buffer, int pos, int length, int*, int* ) = 0;

private:
    int m_state;
    int m_context;
};

class NumberHLItem: public HLItem{
public:
    NumberHLItem( int state, int context )
        : HLItem( state, context ) {}

    int checkHL( const QChar* buffer, int pos, int length, int*, int* ){
	//kdDebug(9032) << "NumberHLItem::checkHLItem" << endl;
	while( pos<length && buffer[pos].isNumber() ){
	    ++pos;
	}
	return pos;
    }
};

class WhiteSpacesHLItem: public HLItem{
public:
    WhiteSpacesHLItem( int state, int context )
        : HLItem( state, context ) {}

    int checkHL( const QChar* buffer, int pos, int length, int*, int* ){
	//kdDebug(9032) << "WhiteSpacesHLItem::checkHLItem" << endl;
	while( (pos<length) && buffer[pos].isSpace() ){
	    ++pos;
	}
	return pos;
    }
};

class KeywordsHLItem: public HLItem{
public:
    KeywordsHLItem( const char** keywords, int state, int ide_state, int context, bool finalize = true, bool ignoreCase = false )
        : HLItem( state, context ), m_ok(false), m_state(state), m_ide_state(ide_state), m_finalize(finalize), m_ignoreCase(ignoreCase) {
            int i = 1;
	    if ( ignoreCase ) {
		while( *keywords )
		    m_keywords.insert( QString(*keywords++).lower(), i++ );
	    } else {
		while( *keywords )
		    m_keywords.insert( QString(*keywords++), i++ );
            }
    }
	
    KeywordsHLItem( const QMap<QString, int> keywords, int state, int ide_state, int context, bool finalize = true, bool ignoreCase = false )
	: HLItem( state, context ), m_ok(false), m_state(state), m_ide_state(ide_state), m_finalize(finalize), m_ignoreCase(ignoreCase) {
	    m_keywords = keywords;
    }
    
    int attr() const { return m_ok ? m_state : m_ide_state; }

    int checkHL( const QChar* buffer, int pos, int length, int*, int* ){
	//kdDebug(9032) << "KeywordsHLItem::checkHLItem" << endl;
	
	int start_pos = pos;
	
	while( (pos<length) && (buffer[pos].isLetterOrNumber() || buffer[pos] == '_') )
	    ++pos;
	
	if( start_pos != pos ) {
	    if ( m_ignoreCase ) {
	    	m_ok = m_keywords.contains( QString(buffer+start_pos, pos-start_pos).lower() );
	    } else {
		m_ok = m_keywords.contains( QString(buffer+start_pos, pos-start_pos) );
	    }
	}
	
	return ( m_ok || m_finalize ) ? pos : start_pos;
    }

private:
    QMap<QString, int> m_keywords;
    bool m_ok;
    int m_state;
    int m_ide_state;
    bool m_finalize; //!< setting finalize to false allows to have another KeywordsHLItem in HLItemCollection after this one
    bool m_ignoreCase;
};

class StartsWithHLItem: public HLItem{
public:
    StartsWithHLItem( const QString& s, int state, int context )
        : HLItem( state, context ), m_text(s) {}
    
    int checkHL( const QChar* buffer, int pos, int length, int*, int* ){
	//kdDebug(9032) << "StartsWithHLItem::checkHLItem" << endl;    
	if( (length - pos) >= (int)m_text.length() && QString(buffer+pos, m_text.length()) == m_text )
	    return length;
	
	return pos;
    }
    
private:
    QString m_text;
};

class StringHLItem: public HLItem{
public:
    StringHLItem( const QString& text, int state, int context )
        : HLItem( state, context ), m_text(text) {}
    
    int checkHL( const QChar* buffer, int pos, int length, int*, int* ){
        //kdDebug(9032) << "StringHLItem::checkHLItem" << endl;    
	if( (length - pos) >= (int)m_text.length() && QString(buffer+pos, m_text.length()) == m_text )
	   return pos + m_text.length();
	   
	return pos;
    }

private:
    QString m_text;
};

class RegExpHLItem: public HLItem{
public:
    RegExpHLItem( QString pattern, int state, int context )
        : HLItem( state, context ), m_rx( pattern ) {}

    int checkHL( const QChar* buffer, int pos, int length, int*, int* ){
	//kdDebug(9032) << "RegExpHLItem::checkHLItem" << endl;
	QString s( buffer, length );
	
	int idx = m_rx.search(s, pos);
	if( idx == pos )
	    return pos + m_rx.matchedLength();
	
	return pos;
    }

private:
    QRegExp m_rx;
};

class HexHLItem: public HLItem{
public:
    HexHLItem( int state, int context )
        : HLItem( state, context ) {}
									           
    int checkHL( const QChar* buffer, int pos, int length, int*, int* ){
	if( (length-pos) > 2 ){
	    QString s( buffer+pos,2 );
	    if( s == "0x" || s == "0X" ){
		pos += 2;
		while( pos < length ){
		    const QChar& ch = buffer[ pos ];
		    if( ch.isNumber() || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
			++pos;
		    else
			break;	
		}	
		
	    }
	}
	return pos;
    }
};

class HLItemCollection: public HLItem{
public:
    HLItemCollection( int state=0, int context=0 ): HLItem( state, context )
    { m_items.setAutoDelete( TRUE ); }

    void appendChild( HLItem* item ) { m_items.append( item ); }

    int checkHL( const QChar* buffer, int pos, int length, int* state, int* next ){
	QPtrListIterator<HLItem> it( m_items );
	
	while( it.current() ){
	    HLItem* item = it.current();
	    
	    int npos = item->checkHL( buffer, pos, length, state, next );
		
	    if( npos > pos ){
	        pos = npos;
		if( state )
		    *state = item->attr();
		if( next )
		    *next = item->context();
		break;
	    }
	    ++it;
	}
	
	return pos;
    }

private:
    QPtrList<HLItem> m_items;
};

class QSourceColorizer: public QTextPreProcessor{
public:
    enum Type {
	Normal=0,
	PreProcessor,
	Keyword,
	BuiltInClass,
	Operator,
	Comment,
	Constant,
	String,
	
	Custom = 1000
    };

public:
    QSourceColorizer( QEditor* );
    virtual ~QSourceColorizer();

    QEditor* editor() const { return m_editor; }

    void insertHLItem( int, HLItemCollection* );

    void setSymbols( const QString&, const QString& );
    QString leftSymbols() const { return m_left; }
    QString rightSymbols() const { return m_right; }

    virtual QTextFormat* format( int key ) { return m_formats[ key ].second; }
    virtual QTextFormat* formatFromId( const QString& id );

    QStringList styleList() const;
    virtual void updateStyles( QMap<QString, QPair<QFont, QColor> >& values );
    virtual void process( QTextDocument*, QTextParagraph*, int, bool=FALSE );
    virtual int computeLevel( QTextParagraph*, int ) { return 0; }

protected:
    QEditor* m_editor;
    QMap<int, QPair<QString, QTextFormat*> > m_formats;
    QPtrList<HLItemCollection> m_items;
    QString m_left;
    QString m_right;
};


#endif