aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/haskell/ghcmod.cpp
blob: f51841b70c29fe182b7d9549bb42ad68a103451e (plain)
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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "ghcmod.h"

#include <utils/environment.h>

#include <QFileInfo>
#include <QFutureWatcher>
#include <QLoggingCategory>
#include <QMutexLocker>
#include <QProcess>
#include <QRegularExpression>
#include <QTime>
#include <QTimer>

Q_LOGGING_CATEGORY(ghcModLog, "qtc.haskell.ghcmod")
Q_LOGGING_CATEGORY(asyncGhcModLog, "qtc.haskell.ghcmod.async")

// TODO do not hardcode
const char STACK_EXE[] = "/usr/local/bin/stack";
const int kTimeoutMS = 10 * 1000;

using namespace Utils;

namespace Haskell {
namespace Internal {

GhcMod::GhcMod(const Utils::FileName &path)
    : m_path(path)
{
}

GhcMod::~GhcMod()
{
    shutdown();
}

FileName GhcMod::basePath() const
{
    return m_path;
}

static QString toUnicode(QByteArray data)
{
    // clean zero bytes which let QString think that the string ends there
    data.replace('\x0', QByteArray());
    return QString::fromUtf8(data);
}

Utils::optional<SymbolInfo> GhcMod::findSymbol(const FileName &filePath, const QString &symbol)
{
    return parseFindSymbol(runFindSymbol(filePath, symbol));
}

Utils::optional<QString> GhcMod::typeInfo(const FileName &filePath, int line, int col)
{
    return parseTypeInfo(runTypeInfo(filePath, line, col));
}

bool GhcMod::ensureStarted()
{
    if (m_process)
        return true;
    log("starting");
    Environment env = Environment::systemEnvironment();
    env.prependOrSetPath(QFileInfo(STACK_EXE).absolutePath()); // for ghc-mod finding stack back
    m_process.reset(new QProcess);
    m_process->setWorkingDirectory(m_path.toString());
    m_process->setEnvironment(env.toStringList());
    m_process->start(STACK_EXE, {"exec", "ghc-mod", "--", "legacy-interactive"});
    if (!m_process->waitForStarted(kTimeoutMS)) {
        log("failed to start");
        return false;
    }
    log("started");
    m_process->setReadChannel(QProcess::StandardOutput);
    return true;
}

void GhcMod::shutdown()
{
    if (!m_process)
        return;
    log("shutting down");
    m_process->write("\n");
    m_process->closeWriteChannel();
    m_process->waitForFinished(300);
    m_process.reset();
}

void GhcMod::log(const QString &message)
{
    qCDebug(ghcModLog) << "ghcmod for" << m_path.toString() << ":" << qPrintable(message);
}

Utils::optional<QByteArray> GhcMod::runQuery(const QString &query)
{
    if (!ensureStarted())
        return Utils::nullopt;
    log("query \"" + query + "\"");
    m_process->write(query.toUtf8() + "\n");
    bool ok = false;
    QByteArray response;
    QTime readTime;
    readTime.start();
    while (!ok && readTime.elapsed() < kTimeoutMS) {
        m_process->waitForReadyRead(kTimeoutMS - readTime.elapsed() + 10);
        response += m_process->read(2048);
        ok = response.endsWith("OK\n") || response.endsWith("OK\r\n");
    }
    if (ghcModLog().isDebugEnabled())
        qCDebug(ghcModLog) << "response" << QTextCodec::codecForLocale()->toUnicode(response);
    if (!ok) {
        log("failed");
        m_process.reset();
        return Utils::nullopt;
    }
    log("success");
    // convert to unix line endings
    response.replace("\r\n", "\n");
    response.chop(3); // cut off "OK\n"
    return response;
}

Utils::optional<QByteArray> GhcMod::runFindSymbol(const FileName &filePath, const QString &symbol)
{
    return runQuery(QString("info %1 %2").arg(filePath.toString()) // TODO toNative? quoting?
                    .arg(symbol));
}

Utils::optional<QByteArray> GhcMod::runTypeInfo(const FileName &filePath, int line, int col)
{
    return runQuery(QString("type %1 %2 %3").arg(filePath.toString()) // TODO toNative? quoting?
                    .arg(line)
                    .arg(col + 1));
}

Utils::optional<SymbolInfo> GhcMod::parseFindSymbol(const Utils::optional<QByteArray> &response)
{
    QRegularExpression infoRegEx("^\\s*(.*?)\\s+--\\sDefined ((at (.+?)(:(\\d+):(\\d+))?)|(in ‘(.+)’.*))$");
    if (!response)
        return Utils::nullopt;
    SymbolInfo info;
    bool hasFileOrModule = false;
    const QString str = toUnicode(QByteArray(response.value()).replace('\x0', '\n'));
    for (const QString &line : str.split('\n')) {
        if (hasFileOrModule) {
            info.additionalInfo += line;
        } else {
            QRegularExpressionMatch result = infoRegEx.match(line);
            if (result.hasMatch()) {
                hasFileOrModule = true;
                info.definition += result.captured(1);
                if (result.lastCapturedIndex() == 7) { // Defined at <file:line:col>
                    info.file = FileName::fromString(result.captured(4));
                    bool ok;
                    int num = result.captured(6).toInt(&ok);
                    if (ok)
                        info.line = num;
                    num = result.captured(7).toInt(&ok);
                    if (ok)
                        info.col = num;
                } else if (result.lastCapturedIndex() == 9) { // Defined in <module>
                    info.module = result.captured(9);
                }
            } else {
                info.definition += line;
            }
        }
    }
    if (hasFileOrModule)
        return info;
    return Utils::nullopt;
}

Utils::optional<QString> GhcMod::parseTypeInfo(const Utils::optional<QByteArray> &response)
{
    QRegularExpression typeRegEx("^\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+\"(.*)\"$",
                                 QRegularExpression::MultilineOption);
    if (!response)
        return Utils::nullopt;
    QRegularExpressionMatch result = typeRegEx.match(toUnicode(response.value()));
    if (result.hasMatch())
        return result.captured(1);
    return Utils::nullopt;
}

AsyncGhcMod::AsyncGhcMod(const FileName &path)
    : m_ghcmod(path)
{
    qCDebug(asyncGhcModLog) << "starting thread for" << m_ghcmod.basePath().toString();
    moveToThread(&m_thread);
    m_thread.start();
}

AsyncGhcMod::~AsyncGhcMod()
{
    qCDebug(asyncGhcModLog) << "stopping thread for" << m_ghcmod.basePath().toString();
    m_mutex.lock();
    for (Operation &op : m_queue)
        op.fi.cancel();
    m_queue.clear();
    m_mutex.unlock();
    m_thread.quit();
    m_thread.wait();
}

FileName AsyncGhcMod::basePath() const
{
    return m_ghcmod.basePath();
}

template <typename Result>
QFuture<Result> createFuture(AsyncGhcMod::Operation op,
                             const std::function<Result(const Utils::optional<QByteArray>&)> &postOp)
{
    auto fi = new QFutureInterface<Result>;
    fi->reportStarted();

    auto opWatcher = new QFutureWatcher<Utils::optional<QByteArray>>();
    QObject::connect(opWatcher, &QFutureWatcherBase::canceled, [fi] { fi->cancel(); });
    QObject::connect(opWatcher, &QFutureWatcherBase::finished, opWatcher, &QObject::deleteLater);
    QObject::connect(opWatcher, &QFutureWatcherBase::finished, [fi] {
        fi->reportFinished();
        delete fi;
    });
    QObject::connect(opWatcher, &QFutureWatcherBase::resultReadyAt,
                     [fi, opWatcher, postOp](int index) {
                         fi->reportResult(postOp(opWatcher->future().resultAt(index)));
                     });
    opWatcher->setFuture(op.fi.future());

    auto fiWatcher = new QFutureWatcher<Result>();
    QObject::connect(fiWatcher, &QFutureWatcherBase::canceled, [op] { op.fi.cancel(); });
    QObject::connect(fiWatcher, &QFutureWatcherBase::finished, fiWatcher, &QObject::deleteLater);
    fiWatcher->setFuture(fi->future());

    return fi->future();
}

QFuture<Utils::optional<SymbolInfo>> AsyncGhcMod::findSymbol(const FileName &filePath,
                                                             const QString &symbol)
{
    QMutexLocker lock(&m_mutex);
    Operation op([this, filePath, symbol] { return m_ghcmod.runFindSymbol(filePath, symbol); });
    m_queue.append(op);
    QTimer::singleShot(0, this, [this] { reduceQueue(); });
    return createFuture<Utils::optional<SymbolInfo>>(op, &GhcMod::parseFindSymbol);
}

QFuture<Utils::optional<QString>> AsyncGhcMod::typeInfo(const FileName &filePath, int line, int col)
{
    QMutexLocker lock(&m_mutex);
    Operation op([this, filePath, line, col] { return m_ghcmod.runTypeInfo(filePath, line, col); });
    m_queue.append(op);
    QTimer::singleShot(0, this, [this] { reduceQueue(); });
    return createFuture<Utils::optional<QString>>(op, &GhcMod::parseTypeInfo);
}

void AsyncGhcMod::reduceQueue()
{
    const auto takeFirst = [this]() {
        Operation op;
        m_mutex.lock();
        if (!m_queue.isEmpty())
            op = m_queue.takeFirst();
        m_mutex.unlock();
        return op;
    };

    Operation op;
    while ((op = takeFirst()).op) {
        if (!op.fi.isCanceled()) {
            Utils::optional<QByteArray> result = op.op();
            op.fi.reportResult(result);
        }
        op.fi.reportFinished();
    }
}

AsyncGhcMod::Operation::Operation(const std::function<Utils::optional<QByteArray>()> &op)
    : op(op)
{
    fi.reportStarted();
}

} // Internal
} // Haskell