File indexing completed on 2024-05-12 05:51:36

0001 /*
0002     SPDX-FileCopyrightText: 2019 Mark Nauwelaerts <mark.nauwelaerts@gmail.com>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "../lspclientserver.h"
0008 #include <iostream>
0009 
0010 #include <QCoreApplication>
0011 #include <QEventLoop>
0012 #include <QFile>
0013 #include <QJsonObject>
0014 #include <QTextStream>
0015 
0016 int main(int argc, char **argv)
0017 {
0018     if (argc < 5) {
0019         return -1;
0020     }
0021 
0022     LSPClientServer lsp(QString::fromLatin1(argv[1]).split(QLatin1Char(' ')), QUrl(QString::fromLatin1(argv[2])));
0023 
0024     QCoreApplication app(argc, argv);
0025     QEventLoop q;
0026 
0027     auto state_h = [&lsp, &q]() {
0028         if (lsp.state() == LSPClientServer::State::Running) {
0029             q.quit();
0030         }
0031     };
0032     auto conn = QObject::connect(&lsp, &LSPClientServer::stateChanged, state_h);
0033     lsp.start(true);
0034     q.exec();
0035     QObject::disconnect(conn);
0036 
0037     auto diagnostics_h = [](const LSPPublishDiagnosticsParams &diag) {
0038         std::cout << "diagnostics  " << diag.uri.toLocalFile().toUtf8().toStdString() << " count: " << diag.diagnostics.length();
0039     };
0040 
0041     QObject::connect(&lsp, &LSPClientServer::publishDiagnostics, diagnostics_h);
0042 
0043     auto document = QUrl(QString::fromLatin1(argv[3]));
0044 
0045     QFile file(document.toLocalFile());
0046     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0047         return -1;
0048     }
0049     QTextStream in(&file);
0050     QString content = in.readAll();
0051     lsp.didOpen(document, 0, QString(), content);
0052 
0053     auto ds_h = [&q](const std::list<LSPSymbolInformation> &syms) {
0054         std::cout << "symbol count: " << syms.size() << std::endl;
0055         q.quit();
0056     };
0057     lsp.documentSymbols(document, &app, ds_h);
0058     q.exec();
0059 
0060     auto position = QString::fromLatin1(argv[4]).split(QLatin1Char(' '));
0061     auto def_h = [&q](const QList<LSPLocation> &defs) {
0062         std::cout << "definition count: " << defs.length() << std::endl;
0063         q.quit();
0064     };
0065     lsp.documentDefinition(document, {position[0].toInt(), position[1].toInt()}, &app, def_h);
0066     q.exec();
0067 
0068     auto comp_h = [&q](const QList<LSPCompletionItem> &completions) {
0069         std::cout << "completion count: " << completions.length() << std::endl;
0070         q.quit();
0071     };
0072     lsp.documentCompletion(document, {position[0].toInt(), position[1].toInt()}, &app, comp_h);
0073     q.exec();
0074 
0075     auto sig_h = [&q](const LSPSignatureHelp &help) {
0076         std::cout << "signature help count: " << help.signatures.length() << std::endl;
0077         q.quit();
0078     };
0079     lsp.signatureHelp(document, {position[0].toInt(), position[1].toInt()}, &app, sig_h);
0080     q.exec();
0081 
0082     auto hover_h = [&q](const LSPHover &hover) {
0083         for (auto &element : hover.contents) {
0084             std::cout << "hover: " << element.value.toStdString() << std::endl;
0085         }
0086         q.quit();
0087     };
0088     lsp.documentHover(document, {position[0].toInt(), position[1].toInt()}, &app, hover_h);
0089     q.exec();
0090 
0091     auto ref_h = [&q](const QList<LSPLocation> &refs) {
0092         std::cout << "refs: " << refs.length() << std::endl;
0093         q.quit();
0094     };
0095     lsp.documentReferences(document, {position[0].toInt(), position[1].toInt()}, true, &app, ref_h);
0096     q.exec();
0097 
0098     auto hl_h = [&q](const QList<LSPDocumentHighlight> &hls) {
0099         std::cout << "highlights: " << hls.length() << std::endl;
0100         q.quit();
0101     };
0102     lsp.documentHighlight(document, {position[0].toInt(), position[1].toInt()}, &app, hl_h);
0103     q.exec();
0104 
0105     auto fmt_h = [&q](const QList<LSPTextEdit> &edits) {
0106         std::cout << "edits: " << edits.length() << std::endl;
0107         q.quit();
0108     };
0109     lsp.documentFormatting(document, {2, true, QJsonObject()}, &app, fmt_h);
0110     q.exec();
0111 
0112     // lsp.didOpen(document, 0, QStringLiteral("blah"));
0113     lsp.didChange(document, 1, QStringLiteral("foo"));
0114     lsp.didClose(document);
0115 }