forked from engor/Jentos.Code
-
Notifications
You must be signed in to change notification settings - Fork 2
/
codeanalyzer.h
executable file
·350 lines (308 loc) · 12.4 KB
/
codeanalyzer.h
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
/*
Jentos IDE.
Copyright 2014, Evgeniy Goroshkin.
See LICENSE.TXT for licensing terms.
*/
#ifndef CODEANALYZER_H
#define CODEANALYZER_H
#include "std.h"
class CodeItem;
class ItemWithData;
class CodeScope;
class FileInfo;
class ImportedFiles;
class CodeAnalyzer : public QObject {
Q_OBJECT
public:
CodeAnalyzer(QObject *parent = 0);
static void init();
static void finalize();
static void loadTemplates( const QString &path );
static void loadKeywords( const QString &path );
static QString templateWord( const QString &key );
static QString keyword( const QString &key );
static void analyzeDir(const QString &path , const QStringList &exclude);
static bool analyzeFile(const QString &path , int kind=KIND_MONKEY);
static bool needCloseWithEnd( const QString &line );
static QIcon identIcon( const QString &ident );
//static void parseCodeItems(QString line , const QTextBlock &block);
static bool parse(QTextDocument *doc, const QString &path, int kind=0, QList<QTextBlock> *blocks=0); //parse all document
static void insertItem(QString ident, CodeItem *codeItem/*, const QString &path*/, int kind=0);
static bool autoFormat(QString &s , bool force=false);
static QString trimmedRight(const QString &s );
static QString clearSpaces( QString &s );
static CodeItem* itemKeyword(const QString &ident );
static CodeItem* itemMonkey(const QString &ident );
static CodeItem* itemUser(const QString &ident , bool withChildren=false);
static bool containsMonkey(const QString &ident );
static bool containsUser(const QString &ident );
static bool containsKeyword(const QString &ident );
static void clearMonkey();
static QString toolTip(const QString &ident, const QTextBlock &block);
static QString toolTip(CodeItem *itemAtBlock);
static void removeUserFile(const QString &path);
static void remove(CodeItem *item);
static void removeByPath(const QString &path);
//
static void fillListInheritance(const QTextBlock &block, QListWidget *l);
static void fillListFromCommon(QListWidget *l, const QString &ident , const QTextBlock &block);
static void fillTree();
static void fillListFromScope(QListWidget *l, const QString &ident, CodeScope scope );
static void allClasses(QString ident, bool addSelf, bool addBase, QList<CodeItem *> &list, CodeItem *item=0);
//
static void setShowVariables(bool value);
static void setSortByName(bool value);
static void setCurFilePath(const QString &filepath);
//
static QStringList extractParams(const QString &text);
static CodeItem* findInScope(const QTextBlock &block, int pos, QListWidget *l=0, bool findLastIdent=false, const QString &blockText="");
//static CodeItem* findInScope(const QString &ident, const QTextBlock &block, bool outsideScope=true);
static CodeItem* scopeAt(const QTextBlock &block, bool classOnly=false, bool checkCurFile=false);
static CodeItem* findScopeForIdent(const QString &ident, const QTextCursor &cursor, CodeScope &scope);
static CodeItem* remAt(const QTextBlock &block);
static CodeItem* foldAt(const QTextBlock &block);
static void identsList(const QString &text, int cursorPos, QStringList &list);
static CodeItem *isBlockHasClassOrFunc(const QTextBlock &block);
//
static void disable() { _disabled = true; }
static void enable() { _disabled = false; }
static bool isBetweenQuotes(QString text, int pos);
//
static QString tab() { return _tab; }
static void setTabSize(int size, bool useSpaces);
static const int KIND_USER = 0;
static const int KIND_MONKEY = 1;
static CodeAnalyzer* instance();
static bool isSortByName() { return _isSortByName; }
static bool isShowVariables() { return _isShowVariables; }
static bool isShowInherited() { return _isShowInherited; }
static bool isFillAucompWithInheritance() { return _isFillAucompWithInheritance; }
static void setViews(QTreeView *tree, QListView *list) {
if(!_treeView) {
_treeView = tree;
_listView = list;
tree->setModel( treeItemModel() );
list->setModel( listItemModel() );
}
}
static QTreeView* treeView() { return _treeView; }
static QListView* listView() { return _listView; }
static QStandardItemModel* treeItemModel();
static QStandardItemModel* listItemModel();
static QStandardItem* itemInTree( const QModelIndex &index );
static ItemWithData* itemInList( const QModelIndex &index );
static void linkCodeItemWithStandardItem(CodeItem *i, QStandardItem *si);
static CodeItem* getCodeItemFromStandardItem(QStandardItem *si);
static QString fullStandardItemPath(QStandardItem *si);
static QStandardItem* getStandardItem(QString path);
static QList<CodeItem*> listForRefreshIdentTypes() { return _listForRefreshIdentTypes; }
static void addToListForRefreshIdentTypes(CodeItem *i) { _listForRefreshIdentTypes.append(i); }
static void refreshIdentTypes();
static QList<int> listFoldTypes() { return _listFoldTypes; }
static void extractIdents(const QString &text, QStringList &list);
static void flushFileModified(const QString &path);
static QHash<QString,QTextDocument*> docs() {
return _docs;
}
static void storeCurFilePath() {
_storedFilePath = _curFilePath;
}
static void restoreCurFilePath() {
_curFilePath = _storedFilePath;
}
static void begin();
static void end();
private:
static QList<int> _listFoldTypes;
static QList<CodeItem*> _listForRefreshIdentTypes;
static QList<CodeItem*> _listUserItems;
static QHash<QString,CodeItem*> _codeItemLinks;
static QHash<QString,QStandardItem*> _standardItemLinks;
static QTreeView *_treeView;
static QListView* _listView;
static QHash<QString, QString>* mapTemplates();
static QMap<QString,CodeItem*>* mapMonkey();
static QHash<QString, CodeItem *> *mapUser();
static QHash<QString, CodeItem *> *mapKeywords();
static QHash<QString, CodeItem *> *mapRem();
static QHash<QString, CodeItem *>* mapFolds();
static QHash<QString, QTextDocument *> _docs;
static QHash<QString, ImportedFiles*> _imports;/*() {
static QHash<QString, QList<QString> > *h = 0;
if(h == 0)
h = new QHash<QString, QList<QString> >;
return h;
}*/
static QStringList _userFiles, _storedUserFiles;
static QHash<QString,FileInfo*>* userFilesModified() {
static QHash<QString,FileInfo*> *h = 0;
if(h == 0)
h = new QHash<QString,FileInfo*>;
return h;
}
static QString _curFilePath, _storedFilePath;
static bool _isShowVariables, _isSortByName, _isShowInherited, _isFillAucompWithInheritance;
static bool _disabled;
//
static QString _tab, _tabAsTab, _tabAsSpaces;
static bool _tabUseSpaces, _doAutoformat;
static int _tabSize;
public slots:
void onPrefsChanged( const QString &name );
};
//----------- CODE ITEM -----------------------------
class CodeItem : public QObject {
Q_OBJECT
public:
CodeItem(QString decl, QString line, int indent, QTextBlock &block, const QString &path, CodeItem *parent=0);
~CodeItem();
static void addUnique(QList<CodeItem*> &list, CodeItem *item);
static QString clearType(QString &s);
static bool equals(CodeItem *i1, CodeItem *i2);
static bool isListContains(QList<CodeItem*> &list, CodeItem *item);
static CodeItem* findTheSame(QList<CodeItem*> &list, CodeItem *item);
QString summary();
void setIdentType(const QString &type);
QString ident() { return _ident; }
QString decl() const { return _decl; }
QString descr() { return _descr; }
QString descrAsItem();
QString identForInsert() { return _identForInsert; }
QString identWithParamsBraces(int &cursorDelta);
QString identType() { return _identType; }
QString identTypeCleared();
QString filepath() { return _filepath; }
QString filename() { return _filename; }
QString module() { return _module; }
QTextBlock block() { return _block; }
QTextBlock blockEnd() { return _blockEnd; }
QList<CodeItem*> params() const { return _params; }
CodeItem* parent() const { return _parent; }
QList<CodeItem*> children() { return _children; }
QString toString() { return "("+_decl+") "+_descr; }
QString infoShort() { return "("+_decl+") "+_ident; }
void addChild(CodeItem *item);
void setParent(CodeItem *item){ _parent = item; }
int indent(){ return _indent; }
int blockNumber();
int blockEndNumber();
void setFoldable(bool value);
bool isFoldable(){ return _foldable; }
bool hasChildren(){ return !_children.isEmpty(); }
bool isClassOrInterface(){ return _isClass||_isInterface; }
bool isClass(){ return _isClass; }
bool isInterface(){ return _isInterface; }
bool isFunc(){ return _isFunc; }
bool isField();
bool isVar(){ return _isVar; }
bool isParam(){ return _isParam; }
bool isKeyword(){ return _isKeyword; }
bool isMonkey(){ return _isMonkey; }
bool isUser(){ return _isUser; }
void setIsMonkey(bool value){ _isMonkey = value; }
void setIsUser(bool value){ _isUser = value; }
CodeItem* child(const QString &ident, bool withBaseClasses=false);
void setBlockEnd(const QTextBlock &block);
void setItemWithData(const QString &name, ItemWithData *iwd);
ItemWithData* itemWithData(const QString &name){ return _itemsWithData.value(name,0); }
bool hasBaseClasses(){ return !_baseClasses.isEmpty(); }
QStringList baseClasses();
QStringList templWords(){ return _templWords; }
void setKind(int kind);
QString toolTip();
void setIsInherited() { _isInherited = true; }
bool isInherited() { return _isInherited; }
QString fullItemPath();
QString tempIdentType() { return _tempIdentType; }
bool isMultiLine() { return _blockNumber != _blockEndNumber; }
private:
QString _decl, _ident, _identType, _descr, _descrAsItem, _identForInsert, _filepath, _filename, _module;
QList<CodeItem*> _params;
CodeItem *_parent;
QTextBlock _block, _blockEnd;
QList<CodeItem*> _children;
int _indent, _blockNumber, _blockEndNumber;
bool _foldable;
bool _isClass, _isFunc, _isField, _isVar, _isKeyword, _isParam, _isInherited, _isInterface;
bool _isMonkey, _isUser;
QHash<QString,ItemWithData*> _itemsWithData;
QStringList _baseClasses, _templWords;
QString _tempIdentType;
};
class CodeScope {
public:
CodeScope() {
item = 0;
isSelf = isSuper = false;
ident = "";
}
~CodeScope(){}
void set(CodeItem *i, bool self=false, bool super=false) {
item = i;
isSelf = self;
isSuper = super;
}
void flush() {
item = 0;
isSelf = isSuper = false;
ident = toolTip = "";
}
CodeItem *item;
bool isSelf, isSuper;
QString ident, toolTip;
};
class FileInfo {
public:
FileInfo(const QString &path) {
this->path = path;
QFileInfo info(path);
modified = info.lastModified().toMSecsSinceEpoch();
}
QString path;
uint modified;
bool isModified() {
QFileInfo info(path);
uint time = info.lastModified().toMSecsSinceEpoch();
if(time != modified) {
modified = time;
return true;
}
return false;
}
};
class ImportedFiles {
public:
void append(const QString &path) {
if(!files.contains(path))
files.append(path);
}
QStringList files;
};
class UsagesResult {
public:
UsagesResult(QTreeWidgetItem *treeitem, const QString &path, const QString &ident, int block, int start, int end) {
this->path = path;
this->ident = ident;
blockNumber = block;
positionStart = start;
positionEnd = end;
}
static UsagesResult* add(QTreeWidgetItem *treeitem, const QString &path, const QString &ident, int block, int start, int end) {
UsagesResult *u = new UsagesResult(treeitem, path, ident, block, start, end);
usages.insert(treeitem,u);
return u;
}
static UsagesResult* item(QTreeWidgetItem *treeitem) {
return usages.value(treeitem);
}
static void clear() {
foreach (UsagesResult *i, usages.values()) {
delete i;
}
usages.clear();
}
QString path, ident;
int blockNumber, positionStart, positionEnd;
static QHash<QTreeWidgetItem*,UsagesResult*> usages;
};
#endif // CODEANALYZER_H