1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "codeeditorreceiver.h"
6
7#include "common/common.h"
8#include "services/project/projectservice.h"
9#include "services/window/windowelement.h"
10
11#include "mainframe/texteditkeeper.h"
12
13CodeEditorReceiver::CodeEditorReceiver(QObject *parent)
14 : dpf::EventHandler (parent)
15 , dpf::AutoEventHandlerRegister<CodeEditorReceiver> ()
16{
17
18}
19
20dpf::EventHandler::Type CodeEditorReceiver::type()
21{
22 return dpf::EventHandler::Type::Sync;
23}
24
25QStringList CodeEditorReceiver::topics()
26{
27 return {editor.topic, actionanalyse.topic, project.topic};
28}
29
30void CodeEditorReceiver::eventProcess(const dpf::Event &event)
31{
32 if (event.data() == project.activedProject.name) {
33 QVariant proInfoVar = event.property(project.activedProject.pKeys[0]);
34 TextEditKeeper::saveProjectInfo(qvariant_cast<dpfservice::ProjectInfo>(proInfoVar));
35 } else if (event.data() == project.deletedProject.name) {
36 QVariant proInfoVar = event.property(project.deletedProject.pKeys[0]);
37 TextEditKeeper::removeProjectInfo(qvariant_cast<dpfservice::ProjectInfo>(proInfoVar));
38 } else if (event.data() == project.createdProject.name) {
39 QVariant proInfoVar = event.property(project.createdProject.pKeys[0]);
40 TextEditKeeper::saveProjectInfo(qvariant_cast<dpfservice::ProjectInfo>(proInfoVar));
41 }else if (event.data() == editor.cleanRunning.name) {
42 return EditorCallProxy::instance()->toRunClean();
43 } else if (event.data() == editor.openFile.name) {
44 navigation.doSwitch(dpfservice::MWNA_EDIT);
45 using namespace support_file;
46 QString filePath = event.property(editor.openFile.pKeys[0]).toString();
47 QString language = Language::idAlias(Language::id(filePath));
48 QString workspace = QDir::homePath();
49 editor.openFileWithKey(workspace, language, filePath);
50 } else if (event.data() == editor.openFileWithKey.name) {
51 navigation.doSwitch(dpfservice::MWNA_EDIT);
52 using namespace support_file;
53 QString workspace = event.property(editor.openFileWithKey.pKeys[0]).toString();
54 QString language = event.property(editor.openFileWithKey.pKeys[1]).toString();
55 QString filePath = event.property(editor.openFileWithKey.pKeys[2]).toString();
56 if (workspace.isEmpty())
57 workspace = QDir::homePath();
58 if (language.isEmpty())
59 language = Language::idAlias(Language::id(filePath));
60 newlsp::ProjectKey proKey;
61 proKey.language = language.toStdString();
62 proKey.workspace = workspace.toStdString();
63 EditorCallProxy::instance()->toOpenFileWithKey(proKey, filePath);
64 } else if (event.data() == editor.searchText.name) {
65 QString srcText = event.property(editor.searchText.pKeys[0]).toString();
66 int findType = event.property(editor.searchText.pKeys[1]).toInt();
67 return EditorCallProxy::instance()->toSearchText(srcText, findType);
68 } else if (event.data() == editor.replaceText.name) {
69 QString srcText = event.property(editor.replaceText.pKeys[0]).toString();
70 QString targetText = event.property(editor.replaceText.pKeys[1]).toString();
71 int replaceType = event.property(editor.replaceText.pKeys[2]).toInt();
72 return EditorCallProxy::instance()->toReplaceText(srcText, targetText, replaceType);
73 } else if (event.data() == editor.jumpToLine.name) {
74 QString workspace = TextEditKeeper::projectInfo().workspaceFolder();
75 if (workspace.isEmpty()) {
76 workspace = QDir::homePath();
77 }
78 QString filePath = event.property(editor.jumpToLine.pKeys[0]).toString();
79 using namespace support_file;
80 QString language = Language::idAlias(Language::id(filePath));
81 newlsp::ProjectKey proKey;
82 proKey.language = language.toStdString();
83 proKey.workspace = workspace.toStdString();
84 int line = event.property(editor.jumpToLine.pKeys[1]).toInt();
85 return EditorCallProxy::instance()->toJumpFileLineWithKey(proKey, filePath, line);
86 } else if (event.data() == actionanalyse.analyseDone.name) {
87 QString workspace = event.property(actionanalyse.analyseDone.pKeys[0]).toString();
88 QString language = event.property(actionanalyse.analyseDone.pKeys[1]).toString();
89 QString storage = event.property(actionanalyse.analyseDone.pKeys[2]).toString();
90 AnalysedData analyseData = qvariant_cast<AnalysedData>(event.property(actionanalyse.analyseDone.pKeys[3]));
91 TextEditKeeper::setAnalysedWorkspace(workspace);
92 TextEditKeeper::setAnalysedLanguage(language);
93 TextEditKeeper::setAnalysedStorage(storage);
94 TextEditKeeper::setAnalysedData(analyseData);
95 } else if (event.data() == actionanalyse.enabled.name) {
96 bool enabled = event.property(actionanalyse.enabled.pKeys[0]).toBool();
97 if (enabled) {
98 QString workspace = TextEditKeeper::getAnalysedWorkspace();
99 QString language = TextEditKeeper::getAnalysedLanguage();
100 QString storage = TextEditKeeper::getAnalysedStorage();;
101 if (workspace.isEmpty() || language.isEmpty() || storage.isEmpty()) {
102 dpfservice::ProjectInfo projectInfo = TextEditKeeper::projectInfo();
103 workspace = FileOperation::checkCreateDir(FileOperation::checkCreateDir(projectInfo.workspaceFolder(), ".unioncode"), "symbol");
104 language = projectInfo.language();
105 storage = workspace;
106 }
107 if (!workspace.isEmpty() && !language.isEmpty() && !storage.isEmpty())
108 actionanalyse.analyse(workspace, language, storage);
109 return;
110 } else {
111 TextEditKeeper::cleanAnalysedData();
112 EditorCallProxy::instance()->toCleanAllAnnotation(TextEditKeeper::userActionAnalyseTitle());
113 }
114 } else if (event.data() == editor.setAnnotation.name) {
115 QString filePath = event.property(editor.setAnnotation.pKeys[0]).toString();
116 int line = event.property(editor.setAnnotation.pKeys[1]).toInt();
117 QString title = event.property(editor.setAnnotation.pKeys[2]).toString();
118 AnnotationInfo annInfo = qvariant_cast<AnnotationInfo>(event.property(editor.setAnnotation.pKeys[3]));
119 EditorCallProxy::instance()->toSetAnnotation(filePath, line, title, annInfo);
120 } else if (event.data() == editor.cleanAnnotation.name) {
121 QString filePath = event.property(editor.cleanAnnotation.pKeys[0]).toString();
122 QString title = event.property(editor.cleanAnnotation.pKeys[1]).toString();
123 EditorCallProxy::instance()->toCleanAnnotation(filePath, title);
124 } else if (event.data() == editor.setLineBackground.name) {
125 QString filePath = event.property(editor.setLineBackground.pKeys[0]).toString();
126 int line = event.property(editor.setLineBackground.pKeys[1]).toInt();
127 QColor color = qvariant_cast<QColor>(event.property(editor.setLineBackground.pKeys[2]));
128 EditorCallProxy::instance()->toSetLineBackground(filePath, line, color);
129 } else if (event.data() == editor.delLineBackground.name) {
130 QString filePath = event.property(editor.delLineBackground.pKeys[0]).toString();
131 int line = event.property(editor.delLineBackground.pKeys[1]).toInt();
132 EditorCallProxy::instance()->toDelLineBackground(filePath, line);
133 } else if (event.data() == editor.cleanLineBackground.name) {
134 QString filePath = event.property(editor.cleanLineBackground.pKeys[0]).toString();
135 EditorCallProxy::instance()->toCleanLineBackground(filePath);
136 } else if (event.data() == editor.runningToLine.name) {
137 auto proInfo = TextEditKeeper::projectInfo();
138 QString workspace = proInfo.workspaceFolder();
139 if (workspace.isEmpty()) {
140 workspace = QDir::homePath();
141 }
142 QString language = proInfo.language();
143 QString filePath = event.property(editor.runningToLine.pKeys[0]).toString();
144 if (language.isEmpty()) {
145 using namespace support_file;
146 QString language = Language::idAlias(Language::id(filePath));
147 }
148 newlsp::ProjectKey proKey;
149 proKey.language = language.toStdString();
150 proKey.workspace = workspace.toStdString();
151 int line = event.property(editor.runningToLine.pKeys[1]).toInt();
152 EditorCallProxy::instance()->toRunFileLineWithKey(proKey, filePath, line);
153 } else if (event.data() == editor.jumpToLineWithKey.name){
154 QString workspace = event.property(editor.jumpToLineWithKey.pKeys[0]).toString();
155 QString language = event.property(editor.jumpToLineWithKey.pKeys[1]).toString();
156 newlsp::ProjectKey proKey;
157 proKey.language = language.toStdString();
158 proKey.workspace = workspace.toStdString();
159 QString filePath = event.property(editor.jumpToLineWithKey.pKeys[2]).toString();
160 int line = event.property(editor.jumpToLineWithKey.pKeys[3]).toInt();
161 EditorCallProxy::instance()->toJumpFileLineWithKey(proKey, filePath, line);
162 } else if (event.data() == editor.switchContext.name) {
163 QString titleName = event.property(editor.switchContext.pKeys[0]).toString();
164 EditorCallProxy::instance()->toSwitchContext(titleName);
165 } else if (event.data() == editor.switchWorkspace.name) {
166 QString titleName = event.property(editor.switchWorkspace.pKeys[0]).toString();
167 EditorCallProxy::instance()->toSwitchWorkspace(titleName);
168 } else if (event.data() == editor.setModifiedAutoReload.name) {
169 QString filePath = event.property(editor.setModifiedAutoReload.pKeys[0]).toString();
170 bool flag = event.property(editor.setModifiedAutoReload.pKeys[1]).toBool();
171 EditorCallProxy::instance()->toSetModifiedAutoReload(filePath, flag);
172 } else if (event.data() == editor.addDebugPoint.name) {
173 QString filePath = event.property(editor.addDebugPoint.pKeys[0]).toString();
174 int line = event.property(editor.addDebugPoint.pKeys[1]).toInt() - 1;
175 EditorCallProxy::instance()->toAddDebugPoint(filePath, line);
176 } else if (event.data() == editor.removeDebugPoint.name) {
177 QString filePath = event.property(editor.removeDebugPoint.pKeys[0]).toString();
178 int line = event.property(editor.removeDebugPoint.pKeys[1]).toInt() - 1;
179 EditorCallProxy::instance()->toRemoveDebugPoint(filePath, line);
180 }
181}
182
183EditorCallProxy::EditorCallProxy()
184{
185
186}
187
188EditorCallProxy *EditorCallProxy::instance()
189{
190 static EditorCallProxy proxy;
191 return &proxy;
192}
193