-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBlock.cpp
57 lines (49 loc) · 1.47 KB
/
TextBlock.cpp
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
#include "TextBlock.h"
#include <QTextStream>
#include <QFile>
#include <QRegularExpression>
#include <QFileInfo>
#include <QDebug>
TextBlock::TextBlock(const QString& content,
const QString& filePath,
int lineNumber,
const QString& keyword)
: _content(content),
_filePath(filePath),
_lineNumber(lineNumber),
_keyword(keyword)
{}
QString TextBlock::getPackageName() const
{
QFile file(_filePath);
if(file.open(QFile::ReadOnly))
{
QTextStream is(&file);
QString content = is.readAll();
QRegularExpression re("package\\s+(?<name>[\\w|\\.]+);");
QRegularExpressionMatch match = re.match(content);
if(match.hasMatch())
return match.captured("name");
}
return "default";
}
QString TextBlock::getClassName() const
{
QFile file(_filePath);
if(file.open(QFile::ReadOnly))
{
QTextStream is(&file);
QString content = is.readAll();
QRegularExpression re("public\\s+\\w+?\\s+class\\s+(?<name>\\w+)");
QRegularExpressionMatch match = re.match(content);
if(match.hasMatch())
return match.captured("name");
}
return QFileInfo(_filePath).fileName();
}
QString TextBlock::getCompleteClassName() const
{
QString packageName = getPackageName();
QString className = getClassName();
return packageName + "." + className;
}