-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcodeeditor.h
More file actions
422 lines (320 loc) · 10.8 KB
/
codeeditor.h
File metadata and controls
422 lines (320 loc) · 10.8 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
Ted, a simple text editor/IDE.
Copyright 2012, Blitz Research Ltd.
See LICENSE.TXT for licensing terms.
*/
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include "std.h"
#include "quickhelp.h"
#include "codeanalyzer.h"
class CodeDocument;
class CodeEditor;
class Highlighter;
class Prefs;
class BlockData;
class QuickHelp;
class ListWidgetComplete;
class LineNumberArea;
class ListWidgetCompleteItem;
class ItemWithData;
class ExtraSelection;
class SelItem;
enum Highlighting {
HlCommon, HlCaretRow, HlError, HlCaretRowCentered
};
//***** CodeEditor *****
class CodeEditor : public QPlainTextEdit {
Q_OBJECT
public:
CodeEditor( QWidget *parent=0 );
~CodeEditor();
bool aucompIsVisible();
//return true if successful and path updated
bool open( const QString &path );
bool save( const QString &path );
void rename( const QString &path );
const QString &path(){ return _path; }
const QString &fileName(){ return _fileName; }
int modified(){ return _modified; }
QString fileType(){ return _fileType; }
bool isTxt(){ return _txt; }
bool isCode(){ return _code; }
bool isMonkey(){ return _monkey; }
void gotoLine( int line );
void highlightLine(int line , Highlighting kind = HlCommon);
void highlightCurrentLine();
bool findNext( const QString &findText,bool cased,bool wrap,bool backward=false );
bool replace( const QString &findText,const QString &replaceText,bool cased );
int replaceAll( const QString &findText,const QString &replaceText,bool cased,bool wrap );
QString identAtCursor(bool fullWord=true);
Highlighter *highlighter(){ return _highlighter; }
void updateCodeViews(QTreeView *tree, QListView *view);
void aucompProcess(CodeItem *item , bool forInheritance=false);
void aucompShowList(bool process=true , bool inheritance=false);
void storeBlock( int num=-1 );
QString cursorRowCol();
void cursorLineChanged();
void lineNumberAreaPaintEvent(QPaintEvent *event);
void pressOnLineNumber(QMouseEvent *e);
void moveOnLineNumber(QMouseEvent *e);
void commentUncommentBlock();
void bookmarkToggle();
void bookmarkNext();
void bookmarkPrev();
void bookmarkFind( int dir, int start=-1 );
void analyzeCode();
void updateSourceNavigationByCurrentScope();
void fillSourceListWidget(CodeItem *item, QStandardItem *si);
void fillCodeTree();
QTextBlock foldBlock(QTextBlock block);
QTextBlock unfoldBlock(QTextBlock block);
void foldAll();
void unfoldAll();
void adjustDocumentSize();
void goBack();
void goForward();
void gotoPos(int blockNum, int posInBlock);
void autoformatAll();
void lowerUpperCase(bool lower);
//
bool canFindUsages();
QString findUsages(QTreeWidget *tree);
void replaceInRange(int from, int to, const QString &text);
void showDialogAddProperty();
public slots:
void undo();
void redo();
void onTextChanged();
void onCursorPositionChanged();
void onPrefsChanged( const QString &name );
void onCodeTreeViewClicked( const QModelIndex &index );
void onSourceListViewClicked( const QModelIndex &index );
void onPaste(const QString &text);//replace quotes by ~q when paste in "{here}"
void onThemeChanged();
private slots:
void onCompleteProcess(QListWidgetItem *item=0);
void onCompleteChangeItem(QListWidgetItem *current, QListWidgetItem *previous);
void onCompleteFocusOut();
void onUpdateLineNumberArea(const QRect &, int);
void onShowAutocompleteList();
signals:
void keyEscapePressed();
void showCode( const QString &file, int line);
void statusBarChanged( const QString &text );
void keyPressed(QKeyEvent *event);
void openCodeFile( const QString &file, const QString &path, const int &lineNumber );
protected:
void resizeEvent(QResizeEvent *e);
void keyPressEvent( QKeyEvent *e );
void keyReleaseEvent( QKeyEvent *e );
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
void leaveEvent(QEvent *) {
QGuiApplication::restoreOverrideCursor();
}
private:
CodeItem *_scopeUnderCursor;
ExtraSelection *_selection;
Highlighter *_highlighter;
//
QString _path;
QString _fileType, _fileName;
bool _txt, _code, _monkey;
int _modified;
ListWidgetComplete *_lcomp;//autocomplete list
bool _lcompInheritance, _lcompProcess, _lcompFillClassesOnly;
QString _lcompTargetIdentType;
int _lcompCursorPosition;
CodeScope _scope;
QTextBlock _storedBlock;
bool _blockChangeCursorMethod;
int _storedBlockNumber;
LineNumberArea *_lineNumberArea;
bool _showLineNumbers;
int _prevCursorPos, _prevTextLen, _prevTextChangedPos;
QList<int*> _editPosList;
int _editPosIndex;
bool _isHighlightLine, _isHighlightWordUnderCursor;
bool _useAutoBrackets;
bool _addVoidForMethods, _capitalizeKeywords;
int _charsCountForCompletion;
friend class Highlighter;
void capitalizeKeywords(const QTextBlock &block, bool checkCursorPos);
void adjustShowLineNumbers();
void showToolTip(QPoint pos, QString s, bool nowrap=true);
void storeCurrentEditPosition(const QTextCursor &cursor);
bool checkFor_New(const QString &text);
int getBlockScreenPositionY(const QTextBlock &block);
int indexOfClosedBracket(const QString &text, const QChar &sourceBracket, int findFrom);
int indexOfOpenedBracket(const QString &text, const QChar &sourceBracket, int findFrom);
};
//***** Highlighter *****
class Highlighter : public QSyntaxHighlighter{
Q_OBJECT
public:
Highlighter( CodeEditor *editor );
~Highlighter();
CodeEditor *editor(){ return _editor; }
enum Formats {
FormatDefault, FormatIdentifier,
FormatUserClass, FormatUserClassVar, FormatUserDecl,
FormatMonkeyClass,
FormatKeyword, FormatParam, FormatNumber, FormatString, FormatComment
};
void setEnabled( bool value ) { _enabled = value; }
bool isEnabled() { return _enabled; }
public slots:
void onPrefsChanged( const QString &name );
protected:
void highlightBlock( const QString &text );
private:
CodeEditor *_editor;
QColor _backgroundColor;
QColor _defaultColor;
QColor _numbersColor;
QColor _stringsColor;
QColor _identifiersColor;
QColor _keywordsColor;
QColor _monkeywordsColor;
QColor _userwordsColor, _userwordsVarColor, _userwordsDeclColor, _paramsColor;
QColor _commentsColor;
QColor _highlightColor;
void setTextFormat(int start, int end, Formats format, bool italic=false);
bool _enabled;
friend class BlockData;
};
//***** BlockData *****
class BlockData : public QTextBlockUserData {
public:
BlockData( const QTextBlock &block );
~BlockData();
QTextBlock block(){ return _block; }
QTextBlock blockEnd(){ return _blockEnd; }
void setBlockEnd(QTextBlock block){ _blockEnd = block; }
bool isBookmarked(){ return _marked; }
void setBookmark(bool mark) { _marked = mark; }
void toggleBookmark() { _marked = !_marked; }
int modified(){ return _modified; }
void setModified(bool value) {
if(value) {
_modified += 2;
}
else {
_modified = (_modified>0 ? 1 : 0);
}
}
static BlockData* data(const QTextBlock &block, bool create=false);
static void flush(QTextBlock &block);
bool isFoldable() { return _foldable; }
void setFoldable(bool value) { _foldable = value; }
bool isFolded(){ return _folded; }
void setFolded(bool value) { _folded = value; }
void toggleFold() { _folded = !_folded; }
CodeItem* code(){ return _code; }
void setCode(CodeItem *code){ _code = code; }
void addItem(CodeItem *item){ _items.append(item); }
CodeItem *item(QString &ident);
QList<CodeItem*> items() { return _items; }
int foldType;
private:
QTextBlock _block, _blockEnd;
bool _marked, _folded, _foldable;
int _modified;
CodeItem *_code;
QList<CodeItem*> _items;
};
//***** CodeTreeItem *****
class ItemWithData : public QStandardItem{
public:
ItemWithData() : _data(0), _code(0){
setEditable( false );
}
ItemWithData(const QIcon &icon, const QString &text) : QStandardItem(icon,text), _data(0), _code(0){
setEditable( false );
}
void setData( BlockData *data ){
_data = data;
}
void setCode( CodeItem *code ){
_code = code;
}
BlockData* data(){
return _data;
}
CodeItem* code(){
return _code;
}
private:
BlockData *_data;
CodeItem *_code;
};
//----------------------------------------------------------------------------------
class LineNumberArea : public QWidget {
public:
LineNumberArea(CodeEditor *editor, int width) : QWidget(editor),_wdth(width) {
_codeEditor = editor;
}
int sizeHint() { return _wdth; }
int maxwidth() { return _wdth; }
void setWidth(int value) { _wdth = value; }
void pressed(int left, int right) { _pressedPosLeft = left; _pressedPosRight = right; }
int pressedLeft() { return _pressedPosLeft; }
int pressedRight() { return _pressedPosRight; }
protected:
void paintEvent(QPaintEvent *event) {
_codeEditor->lineNumberAreaPaintEvent(event);
}
void mousePressEvent(QMouseEvent *e) {
_codeEditor->pressOnLineNumber(e);
}
void mouseMoveEvent(QMouseEvent *e) {
_codeEditor->moveOnLineNumber(e);
}
void enterEvent(QEvent *) {
QGuiApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor));
}
void leaveEvent(QEvent *) {
QGuiApplication::restoreOverrideCursor();
}
private:
CodeEditor *_codeEditor;
int _wdth;
int _pressedPosLeft, _pressedPosRight;
};
/* ExtraSelection */
class ExtraSelection {
public:
ExtraSelection(CodeEditor *editor);
~ExtraSelection();
void resetAll();
void resetToolTip();
void resetWords();
void resetCaretRow();
void appendCaretRow();
void appendCaretRow(QTextCursor &cursor, Highlighting kind);
void appendToolTip();
void appendWords(QList<SelItem*> list);
void setLastWord(QString word, int scroll);
QList<QTextEdit::ExtraSelection> sels();
QString lastSelWord(){return _lastSelWord;}
int lastSelScrollPos(){return _lastSelScrollPos;}
SelItem* caretRowSel(){return _caretRowSel;}
SelItem* toolTipSel(){return _toolTipSel;}
SelItem* wordSel(){return _wordSel;}
void readPrefs();
private:
QColor _commonColor, _wordColor, _caretColor, _errorColor, _toolTipColor;
CodeEditor *_editor;
QList<SelItem*> _items;
QList<QTextEdit::ExtraSelection> _sels;
SelItem *_caretRowSel, *_toolTipSel, *_wordSel;
QString _lastSelWord;
int _lastSelScrollPos;
void adjust(bool isDirty);
};
class SelItem {
public:
QTextEdit::ExtraSelection selection;
};
#endif // CODEEDITOR_H