1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "analysekeeper.h" |
6 | |
7 | #include "common/common.h" |
8 | |
9 | #include <QStandardItem> |
10 | |
11 | namespace { |
12 | static WordCountAnalyse *wordcounter{nullptr}; |
13 | static ActionAnalyseArgs analyseArgs; |
14 | static AnalysedData analyseData; |
15 | } |
16 | |
17 | AnalyseKeeper::AnalyseKeeper() |
18 | { |
19 | |
20 | } |
21 | |
22 | AnalyseKeeper *AnalyseKeeper::instance() |
23 | { |
24 | static AnalyseKeeper ins; |
25 | return &ins; |
26 | } |
27 | |
28 | void AnalyseKeeper::doAnalyse(const ActionAnalyseArgs &args) |
29 | { |
30 | analyseArgs = args; |
31 | if (wordcounter) { |
32 | QObject::disconnect(wordcounter, &WordCountAnalyse::analyseDone, |
33 | this, &AnalyseKeeper::doAnalyseDone); |
34 | wordcounter->kill(); |
35 | wordcounter->waitForFinished(); |
36 | delete wordcounter; |
37 | wordcounter = nullptr; |
38 | } |
39 | wordcounter = new WordCountAnalyse(); |
40 | wordcounter->setArgs(args); |
41 | wordcounter->WordCountAnalyse::start(); |
42 | QObject::connect(wordcounter, &WordCountAnalyse::analyseDone, |
43 | this, &AnalyseKeeper::doAnalyseDone); |
44 | } |
45 | |
46 | void AnalyseKeeper::doAnalyseDone(bool result) |
47 | { |
48 | if (!result) { |
49 | ActionAnalyseArgs args = wordcounter->args(); |
50 | ContextDialog::ok(QDialog::tr( |
51 | "Error Do User Action Anlyse\n" |
52 | "workspace: %0\n" |
53 | "language: %1\n" |
54 | "storage: %2\n" ) |
55 | .arg(args.workspace) |
56 | .arg(args.language) |
57 | .arg(args.storage)); |
58 | } |
59 | |
60 | QFile wordcountFile(wordcounter->args().storage + QDir::separator() + ".wordcount" ); |
61 | QTextStream ts(&wordcountFile); |
62 | if (!wordcountFile.open(QIODevice::ReadWrite)) { |
63 | qDebug("fail to open wordcountFile" ); |
64 | } |
65 | |
66 | int totalLineNum = 0; |
67 | while (!ts.atEnd()) { |
68 | ts.readLine(); |
69 | totalLineNum++; |
70 | } |
71 | ts.seek(0); |
72 | |
73 | analyseData.rules.resize(4); |
74 | analyseData.rules[0] = "UpperCamel" ; |
75 | analyseData.rules[1] = "LowerCamel" ; |
76 | analyseData.rules[2] = "Snake" ; |
77 | analyseData.rules[3] = "Others" ; |
78 | |
79 | analyseData.tokenMaps.resize(totalLineNum); |
80 | int lineNum = 0; |
81 | while (!ts.atEnd() && lineNum < totalLineNum) { |
82 | |
83 | QStringList line = ts.readLine().split(" " , QString::SkipEmptyParts); |
84 | analyseData.tokenMaps[lineNum].semanticTokenType = line.at(0).toStdString(); |
85 | analyseData.tokenMaps[lineNum].semanticTokenModifier = line.at(1).toStdString(); |
86 | analyseData.tokenMaps[lineNum].result.resize(4); |
87 | analyseData.tokenMaps[lineNum].result[0] = line.at(2).toFloat(); |
88 | std::cerr << analyseData.tokenMaps[lineNum].result[0]; |
89 | analyseData.tokenMaps[lineNum].result[1] = line.at(3).toFloat(); |
90 | analyseData.tokenMaps[lineNum].result[2] = line.at(4).toFloat(); |
91 | analyseData.tokenMaps[lineNum].result[3] = line.at(5).toFloat(); |
92 | lineNum++; |
93 | } |
94 | |
95 | wordcounter->kill(); |
96 | delete wordcounter; |
97 | wordcounter = nullptr; |
98 | |
99 | actionanalyse.analyseDone(analyseArgs.workspace, analyseArgs.language, |
100 | analyseArgs.storage, analyseData); |
101 | } |
102 | |