aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/lsp/lsptypes.cpp
blob: 751fc4e3d6b65685b395b33130e870b9e4413dbe (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "lsptypes.h"

#include "languageserverprotocoltr.h"
#include "lsputils.h"
#include "textutils.h"

#include <QFile>
#include <QHash>
#include <QJsonArray>
#include <QMap>
#include <QRegularExpression>
#include <QVector>

namespace lsp {

std::optional<DiagnosticSeverity> Diagnostic::severity() const
{
    if (auto val = optionalValue<int>(severityKey))
        return std::make_optional(static_cast<DiagnosticSeverity>(*val));
    return std::nullopt;
}

std::optional<Diagnostic::Code> Diagnostic::code() const
{
    QJsonValue codeValue = value(codeKey);
    auto it = find(codeKey);
    if (codeValue.isUndefined())
        return std::nullopt;
    QJsonValue::Type type = it.value().type();
    if (type != QJsonValue::String && type != QJsonValue::Double)
        return std::make_optional(Code(QString()));
    return std::make_optional(codeValue.isDouble() ? Code(codeValue.toInt())
                                                   : Code(codeValue.toString()));
}

void Diagnostic::setCode(const Diagnostic::Code &code)
{
    insertVariant<int, QString>(codeKey, code);
}

std::optional<WorkspaceEdit::Changes> WorkspaceEdit::changes() const
{
    auto it = find(changesKey);
    if (it == end())
        return std::nullopt;
    const QJsonObject &changesObject = it.value().toObject();
    Changes changesResult;
    for (const QString &key : changesObject.keys())
        changesResult[DocumentUri::fromProtocol(key)] = LanguageClientArray<TextEdit>(changesObject.value(key)).toList();
    return std::make_optional(changesResult);
}

void WorkspaceEdit::setChanges(const Changes &changes)
{
    QJsonObject changesObject;
    const auto end = changes.end();
    for (auto it = changes.begin(); it != end; ++it) {
        QJsonArray edits;
        for (const TextEdit &edit : it.value())
            edits.append(QJsonValue(edit));
        changesObject.insert(QJsonValue(it.key()).toString(), edits);
    }
    insert(changesKey, changesObject);
}

WorkSpaceFolder::WorkSpaceFolder(const DocumentUri &uri, const QString &name)
{
    setUri(uri);
    setName(name);
}

MarkupOrString::MarkupOrString(const std::variant<QString, MarkupContent> &val)
    : std::variant<QString, MarkupContent>(val)
{ }

MarkupOrString::MarkupOrString(const QString &val)
    : std::variant<QString, MarkupContent>(val)
{ }

MarkupOrString::MarkupOrString(const MarkupContent &val)
    : std::variant<QString, MarkupContent>(val)
{ }

MarkupOrString::MarkupOrString(const QJsonValue &val)
{
    if (val.isString()) {
        emplace<QString>(val.toString());
    } else {
        MarkupContent markupContent(val.toObject());
        if (markupContent.isValid())
            emplace<MarkupContent>(MarkupContent(val.toObject()));
    }
}

QJsonValue MarkupOrString::toJson() const
{
    if (std::holds_alternative<QString>(*this))
        return std::get<QString>(*this);
    if (std::holds_alternative<MarkupContent>(*this))
        return QJsonValue(std::get<MarkupContent>(*this));
    return {};
}

QMap<QString, QString> languageIds()
{
    static const QMap<QString, QString> languages({
            {"Windows Bat","bat"                        },
            {"BibTeX","bibtex"                          },
            {"Clojure","clojure"                        },
            {"Coffeescript","coffeescript"              },
            {"C","c"                                    },
            {"C++","cpp"                                },
            {"C#","csharp"                              },
            {"CSS","css"                                },
            {"Diff","diff"                              },
            {"Dockerfile","dockerfile"                  },
            {"F#","fsharp"                              },
            {"Git commit","git-commit"                  },
            {"Git rebase","git-rebase"                  },
            {"Go","go"                                  },
            {"Groovy","groovy"                          },
            {"Handlebars","handlebars"                  },
            {"HTML","html"                              },
            {"Ini","ini"                                },
            {"Java","java"                              },
            {"JavaScript","javascript"                  },
            {"JSON","json"                              },
            {"LaTeX","latex"                            },
            {"Less","less"                              },
            {"Lua","lua"                                },
            {"Makefile","makefile"                      },
            {"Markdown","markdown"                      },
            {"Objective-C","objective-c"                },
            {"Objective-C++","objective-cpp"            },
            {"Perl6","perl6"                            },
            {"Perl","perl"                              },
            {"PHP","php"                                },
            {"Powershell","powershell"                  },
            {"Pug","jade"                               },
            {"Python","python"                          },
            {"R","r"                                    },
            {"Razor (cshtml)","razor"                   },
            {"Ruby","ruby"                              },
            {"Rust","rust"                              },
            {"Scss (syntax using curly brackets)","scss"},
            {"Sass (indented syntax)","sass"            },
            {"ShaderLab","shaderlab"                    },
            {"Shell Script (Bash)","shellscript"        },
            {"SQL","sql"                                },
            {"Swift","swift"                            },
            {"TypeScript","typescript"                  },
            {"TeX","tex"                                },
            {"Visual Basic","vb"                        },
            {"XML","xml"                                },
            {"XSL","xsl"                                },
            {"YAML","yaml"                              }
    });
    return languages;
}

bool TextDocumentItem::isValid() const
{
    return contains(uriKey) && contains(languageIdKey) && contains(versionKey) && contains(textKey);
}

TextDocumentPositionParams::TextDocumentPositionParams()
    : TextDocumentPositionParams(TextDocumentIdentifier(), Position())
{

}

TextDocumentPositionParams::TextDocumentPositionParams(
        const TextDocumentIdentifier &document, const Position &position)
{
    setTextDocument(document);
    setPosition(position);
}

Position::Position(int line, int character)
{
    setLine(line);
    setCharacter(character);
}

Range::Range(const Position &start, const Position &end)
{
    setStart(start);
    setEnd(end);
}

bool Range::contains(const Range &other) const
{
    if (start() > other.start())
        return false;
    if (end() < other.end())
        return false;
    return true;
}

bool Range::overlaps(const Range &range) const
{
    return !isLeftOf(range) && !range.isLeftOf(*this);
}

QString expressionForGlob(QString globPattern)
{
    const QString anySubDir("qtc_anysubdir_id");
    globPattern.replace("**/", anySubDir);
    QString regexp = QRegularExpression::wildcardToRegularExpression(globPattern);
    regexp.replace(anySubDir,"(.*[/\\\\])*");
    regexp.replace("\\{", "(");
    regexp.replace("\\}", ")");
    regexp.replace(",", "|");
    return regexp;
}

bool DocumentFilter::applies(const Utils::FilePath &fileName) const
{
    if (std::optional<QString> _pattern = pattern()) {
        QRegularExpression::PatternOption option = QRegularExpression::NoPatternOption;
        if (fileName.caseSensitivity() == Qt::CaseInsensitive)
            option = QRegularExpression::CaseInsensitiveOption;
        const QRegularExpression regexp(expressionForGlob(*_pattern), option);
        if (regexp.isValid() && regexp.match(fileName.toString()).hasMatch())
            return true;
    }
    // return false when any of the filter didn't match but return true when no filter was defined
    return !contains(schemeKey) && !contains(languageKey) && !contains(patternKey);
}

Utils::Link Location::toLink(const DocumentUri::PathMapper &mapToHostPath) const
{
    if (!isValid())
        return Utils::Link();

    return Utils::Link(uri().toFilePath(mapToHostPath),
                       range().start().line() + 1,
                       range().start().character());
}

// Ensure %xx like %20 are really decoded using fromPercentEncoding
// Else, a path with spaces would keep its %20 which would cause failure
// to open the file by the text editor. This is the cases with compilers in
// C:\Programs Files on Windows.
DocumentUri::DocumentUri(const QString &other)
    : QUrl(QUrl::fromPercentEncoding(other.toUtf8()))
{ }

DocumentUri::DocumentUri(const Utils::FilePath &other, const PathMapper &mapToServerPath)
    : QUrl(QUrl::fromLocalFile(mapToServerPath(other).path()))
{ }

Utils::FilePath DocumentUri::toFilePath(const PathMapper &mapToHostPath) const
{
    if (isLocalFile()) {
        const Utils::FilePath serverPath = Utils::FilePath::fromUserInput(toLocalFile());
        QBS_ASSERT(mapToHostPath, return serverPath);
        return mapToHostPath(serverPath);
    }
    return {};
}

DocumentUri DocumentUri::fromProtocol(const QString &uri)
{
    return DocumentUri(uri);
}

DocumentUri DocumentUri::fromFilePath(const Utils::FilePath &file, const PathMapper &mapToServerPath)
{
    return DocumentUri(file, mapToServerPath);
}

MarkupKind::MarkupKind(const QJsonValue &value)
{
    m_value = value.toString() == "markdown" ? markdown : plaintext;
}

MarkupKind::operator QJsonValue() const
{
    switch (m_value) {
    case MarkupKind::markdown:
        return "markdown";
    case MarkupKind::plaintext:
        return "plaintext";
    }
    return {};
}

DocumentChange::DocumentChange(const QJsonValue &value)
{
    const QString kind = value["kind"].toString();
    if (kind == "create")
        emplace<CreateFileOperation>(value);
    else if (kind == "rename")
        emplace<RenameFileOperation>(value);
    else if (kind == "delete")
        emplace<DeleteFileOperation>(value);
    else
        emplace<TextDocumentEdit>(value);
}

using DocumentChangeBase = std::variant<TextDocumentEdit, CreateFileOperation, RenameFileOperation, DeleteFileOperation>;

bool DocumentChange::isValid() const
{
    return std::visit([](const auto &v) { return v.isValid(); }, DocumentChangeBase(*this));
}

DocumentChange::operator const QJsonValue () const
{
    return std::visit([](const auto &v) { return QJsonValue(v); }, DocumentChangeBase(*this));
}

CreateFileOperation::CreateFileOperation()
{
    insert(kindKey, "create");
}

QString CreateFileOperation::message(const DocumentUri::PathMapper &mapToHostPath) const
{
    return Tr::tr("Create %1").arg(uri().toFilePath(mapToHostPath).toUserOutput());
}

bool CreateFileOperation::isValid() const
{
    return contains(uriKey) && value(kindKey) == "create";
}

RenameFileOperation::RenameFileOperation()
{
    insert(kindKey, "rename");
}

QString RenameFileOperation::message(const DocumentUri::PathMapper &mapToHostPath) const
{
    return Tr::tr("Rename %1 to %2")
        .arg(oldUri().toFilePath(mapToHostPath).toUserOutput(),
             newUri().toFilePath(mapToHostPath).toUserOutput());
}

bool RenameFileOperation::isValid() const
{
    return contains(oldUriKey) && contains(newUriKey) && value(kindKey) == "rename";
}

DeleteFileOperation::DeleteFileOperation()
{
    insert(kindKey, "delete");
}

QString DeleteFileOperation::message(const DocumentUri::PathMapper &mapToHostPath) const
{
    return Tr::tr("Delete %1").arg(uri().toFilePath(mapToHostPath).toUserOutput());
}

bool DeleteFileOperation::isValid() const
{
    return contains(uriKey) && value(kindKey) == "delete";
}

} // namespace lsp