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
|
// Copyright (C) Filippo Cucchetto <filippocucchetto@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "nimsuggest.h"
namespace Nim {
namespace Suggest {
NimSuggest::NimSuggest(QObject *parent)
: QObject(parent)
{
connect(&m_server, &NimSuggestServer::started, this, &NimSuggest::onServerStarted);
connect(&m_server, &NimSuggestServer::done, this, &NimSuggest::onServerDone);
connect(&m_client, &NimSuggestClient::disconnected, this, &NimSuggest::onClientDisconnected);
connect(&m_client, &NimSuggestClient::connected, this, &NimSuggest::onClientConnected);
}
QString NimSuggest::projectFile() const
{
return m_projectFile;
}
void NimSuggest::setProjectFile(const QString &file)
{
if (m_projectFile == file)
return;
m_projectFile = file;
emit projectFileChanged(file);
restart();
}
QString NimSuggest::executablePath() const
{
return m_executablePath;
}
void NimSuggest::setExecutablePath(const QString &path)
{
if (m_executablePath == path)
return;
m_executablePath = path;
emit executablePathChanged(path);
restart();
}
bool NimSuggest::isReady() const
{
return m_ready;
}
std::shared_ptr<NimSuggestClientRequest> NimSuggest::sug(const QString &filename, int line, int column,
const QString &dirtyFilename)
{
return m_ready ? m_client.sug(filename, line, column, dirtyFilename) : nullptr;
}
std::shared_ptr<NimSuggestClientRequest> NimSuggest::def(const QString &filename, int line, int column, const QString &dirtyFilename)
{
return m_ready ? m_client.def(filename, line, column, dirtyFilename) : nullptr;
}
void NimSuggest::restart()
{
disconnectClient();
setClientReady(false);
stopServer();
setServerReady(false);
startServer();
}
void NimSuggest::setReady(bool ready)
{
if (m_ready == ready)
return;
m_ready = ready;
emit readyChanged(ready);
}
void NimSuggest::setServerReady(bool ready)
{
if (m_serverReady == ready)
return;
m_serverReady = ready;
setReady(m_clientReady && m_serverReady);
}
void NimSuggest::setClientReady(bool ready)
{
if (m_clientReady == ready)
return;
m_clientReady = ready;
setReady(m_clientReady && m_serverReady);
}
void NimSuggest::connectClient()
{
m_client.connectToServer(m_server.port());
}
void NimSuggest::disconnectClient()
{
m_client.disconnectFromServer();
}
void NimSuggest::stopServer()
{
m_server.stop();
}
void NimSuggest::startServer()
{
if (!m_projectFile.isEmpty() && !m_executablePath.isEmpty())
m_server.start(m_executablePath, m_projectFile);
}
void NimSuggest::onServerStarted()
{
setServerReady(true);
connectClient();
}
void NimSuggest::onServerDone()
{
setServerReady(false);
disconnectClient();
restart();
}
void NimSuggest::onClientConnected()
{
setClientReady(true);
}
void NimSuggest::onClientDisconnected()
{
setClientReady(false);
if (isServerReady())
connectClient();
}
} // namespace Suggest
} // namespace Nim
|