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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
//===--- Protocol.h - Language Server Protocol Implementation ---*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains structs based on the LSP specification at
// https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
//
// This is not meant to be a complete implementation, new interfaces are added
// when they're needed.
//
// Each struct has a parse and unparse function, that converts back and forth
// between the struct and a JSON representation.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H
#include "JSONExpr.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/YAMLParser.h"
#include <string>
#include <vector>
namespace clang {
namespace clangd {
class Logger;
struct URI {
std::string uri;
std::string file;
static URI fromUri(llvm::StringRef uri);
static URI fromFile(llvm::StringRef file);
static URI parse(llvm::yaml::ScalarNode *Param);
static json::Expr unparse(const URI &U);
friend bool operator==(const URI &LHS, const URI &RHS) {
return LHS.uri == RHS.uri;
}
friend bool operator!=(const URI &LHS, const URI &RHS) {
return !(LHS == RHS);
}
friend bool operator<(const URI &LHS, const URI &RHS) {
return LHS.uri < RHS.uri;
}
};
struct TextDocumentIdentifier {
/// The text document's URI.
URI uri;
static llvm::Optional<TextDocumentIdentifier>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct Position {
/// Line position in a document (zero-based).
int line;
/// Character offset on a line in a document (zero-based).
int character;
friend bool operator==(const Position &LHS, const Position &RHS) {
return std::tie(LHS.line, LHS.character) ==
std::tie(RHS.line, RHS.character);
}
friend bool operator<(const Position &LHS, const Position &RHS) {
return std::tie(LHS.line, LHS.character) <
std::tie(RHS.line, RHS.character);
}
static llvm::Optional<Position> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
static json::Expr unparse(const Position &P);
};
struct Range {
/// The range's start position.
Position start;
/// The range's end position.
Position end;
friend bool operator==(const Range &LHS, const Range &RHS) {
return std::tie(LHS.start, LHS.end) == std::tie(RHS.start, RHS.end);
}
friend bool operator<(const Range &LHS, const Range &RHS) {
return std::tie(LHS.start, LHS.end) < std::tie(RHS.start, RHS.end);
}
static llvm::Optional<Range> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
static json::Expr unparse(const Range &P);
};
struct Location {
/// The text document's URI.
URI uri;
Range range;
friend bool operator==(const Location &LHS, const Location &RHS) {
return LHS.uri == RHS.uri && LHS.range == RHS.range;
}
friend bool operator!=(const Location &LHS, const Location &RHS) {
return !(LHS == RHS);
}
friend bool operator<(const Location &LHS, const Location &RHS) {
return std::tie(LHS.uri, LHS.range) < std::tie(RHS.uri, RHS.range);
}
static json::Expr unparse(const Location &P);
};
struct Metadata {
std::vector<std::string> extraFlags;
static llvm::Optional<Metadata> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
};
struct TextEdit {
/// The range of the text document to be manipulated. To insert
/// text into a document create a range where start === end.
Range range;
/// The string to be inserted. For delete operations use an
/// empty string.
std::string newText;
static llvm::Optional<TextEdit> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
static json::Expr unparse(const TextEdit &P);
};
struct TextDocumentItem {
/// The text document's URI.
URI uri;
/// The text document's language identifier.
std::string languageId;
/// The version number of this document (it will strictly increase after each
int version;
/// The content of the opened text document.
std::string text;
static llvm::Optional<TextDocumentItem> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
};
enum class TraceLevel {
Off = 0,
Messages = 1,
Verbose = 2,
};
struct NoParams {
static llvm::Optional<NoParams> parse(llvm::yaml::MappingNode *Params,
Logger &Logger) {
return NoParams{};
}
};
using ShutdownParams = NoParams;
using ExitParams = NoParams;
struct InitializeParams {
/// The process Id of the parent process that started
/// the server. Is null if the process has not been started by another
/// process. If the parent process is not alive then the server should exit
/// (see exit notification) its process.
llvm::Optional<int> processId;
/// The rootPath of the workspace. Is null
/// if no folder is open.
///
/// @deprecated in favour of rootUri.
llvm::Optional<std::string> rootPath;
/// The rootUri of the workspace. Is null if no
/// folder is open. If both `rootPath` and `rootUri` are set
/// `rootUri` wins.
llvm::Optional<URI> rootUri;
// User provided initialization options.
// initializationOptions?: any;
/// The capabilities provided by the client (editor or tool)
/// Note: Not currently used by clangd
// ClientCapabilities capabilities;
/// The initial trace setting. If omitted trace is disabled ('off').
llvm::Optional<TraceLevel> trace;
static llvm::Optional<InitializeParams> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
};
struct DidOpenTextDocumentParams {
/// The document that was opened.
TextDocumentItem textDocument;
/// Extension storing per-file metadata, such as compilation flags.
llvm::Optional<Metadata> metadata;
static llvm::Optional<DidOpenTextDocumentParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct DidCloseTextDocumentParams {
/// The document that was closed.
TextDocumentIdentifier textDocument;
static llvm::Optional<DidCloseTextDocumentParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct TextDocumentContentChangeEvent {
/// The new text of the document.
std::string text;
static llvm::Optional<TextDocumentContentChangeEvent>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct DidChangeTextDocumentParams {
/// The document that did change. The version number points
/// to the version after all provided content changes have
/// been applied.
TextDocumentIdentifier textDocument;
/// The actual content changes.
std::vector<TextDocumentContentChangeEvent> contentChanges;
static llvm::Optional<DidChangeTextDocumentParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
enum class FileChangeType {
/// The file got created.
Created = 1,
/// The file got changed.
Changed = 2,
/// The file got deleted.
Deleted = 3
};
struct FileEvent {
/// The file's URI.
URI uri;
/// The change type.
FileChangeType type;
static llvm::Optional<FileEvent> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
};
struct DidChangeWatchedFilesParams {
/// The actual file events.
std::vector<FileEvent> changes;
static llvm::Optional<DidChangeWatchedFilesParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct FormattingOptions {
/// Size of a tab in spaces.
int tabSize;
/// Prefer spaces over tabs.
bool insertSpaces;
static llvm::Optional<FormattingOptions>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
static json::Expr unparse(const FormattingOptions &P);
};
struct DocumentRangeFormattingParams {
/// The document to format.
TextDocumentIdentifier textDocument;
/// The range to format
Range range;
/// The format options
FormattingOptions options;
static llvm::Optional<DocumentRangeFormattingParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct DocumentOnTypeFormattingParams {
/// The document to format.
TextDocumentIdentifier textDocument;
/// The position at which this request was sent.
Position position;
/// The character that has been typed.
std::string ch;
/// The format options.
FormattingOptions options;
static llvm::Optional<DocumentOnTypeFormattingParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct DocumentFormattingParams {
/// The document to format.
TextDocumentIdentifier textDocument;
/// The format options
FormattingOptions options;
static llvm::Optional<DocumentFormattingParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct Diagnostic {
/// The range at which the message applies.
Range range;
/// The diagnostic's severity. Can be omitted. If omitted it is up to the
/// client to interpret diagnostics as error, warning, info or hint.
int severity;
/// The diagnostic's code. Can be omitted.
/// Note: Not currently used by clangd
// std::string code;
/// A human-readable string describing the source of this
/// diagnostic, e.g. 'typescript' or 'super lint'.
/// Note: Not currently used by clangd
// std::string source;
/// The diagnostic's message.
std::string message;
friend bool operator==(const Diagnostic &LHS, const Diagnostic &RHS) {
return std::tie(LHS.range, LHS.severity, LHS.message) ==
std::tie(RHS.range, RHS.severity, RHS.message);
}
friend bool operator<(const Diagnostic &LHS, const Diagnostic &RHS) {
return std::tie(LHS.range, LHS.severity, LHS.message) <
std::tie(RHS.range, RHS.severity, RHS.message);
}
static llvm::Optional<Diagnostic> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
};
struct CodeActionContext {
/// An array of diagnostics.
std::vector<Diagnostic> diagnostics;
static llvm::Optional<CodeActionContext>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct CodeActionParams {
/// The document in which the command was invoked.
TextDocumentIdentifier textDocument;
/// The range for which the command was invoked.
Range range;
/// Context carrying additional information.
CodeActionContext context;
static llvm::Optional<CodeActionParams> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
};
struct WorkspaceEdit {
/// Holds changes to existing resources.
llvm::Optional<std::map<std::string, std::vector<TextEdit>>> changes;
/// Note: "documentChanges" is not currently used because currently there is
/// no support for versioned edits.
static llvm::Optional<WorkspaceEdit> parse(llvm::yaml::MappingNode *Params,
clangd::Logger &Logger);
static json::Expr unparse(const WorkspaceEdit &WE);
};
/// Exact commands are not specified in the protocol so we define the
/// ones supported by Clangd here. The protocol specifies the command arguments
/// to be "any[]" but to make this safer and more manageable, each command we
/// handle maps to a certain llvm::Optional of some struct to contain its
/// arguments. Different commands could reuse the same llvm::Optional as
/// arguments but a command that needs different arguments would simply add a
/// new llvm::Optional and not use any other ones. In practice this means only
/// one argument type will be parsed and set.
struct ExecuteCommandParams {
// Command to apply fix-its. Uses WorkspaceEdit as argument.
const static std::string CLANGD_APPLY_FIX_COMMAND;
/// The command identifier, e.g. CLANGD_APPLY_FIX_COMMAND
std::string command;
// Arguments
llvm::Optional<WorkspaceEdit> workspaceEdit;
static llvm::Optional<ExecuteCommandParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
struct ApplyWorkspaceEditParams {
WorkspaceEdit edit;
static json::Expr unparse(const ApplyWorkspaceEditParams &Params);
};
struct TextDocumentPositionParams {
/// The text document.
TextDocumentIdentifier textDocument;
/// The position inside the text document.
Position position;
static llvm::Optional<TextDocumentPositionParams>
parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger);
};
/// The kind of a completion entry.
enum class CompletionItemKind {
Missing = 0,
Text = 1,
Method = 2,
Function = 3,
Constructor = 4,
Field = 5,
Variable = 6,
Class = 7,
Interface = 8,
Module = 9,
Property = 10,
Unit = 11,
Value = 12,
Enum = 13,
Keyword = 14,
Snippet = 15,
Color = 16,
File = 17,
Reference = 18,
};
/// Defines whether the insert text in a completion item should be interpreted
/// as plain text or a snippet.
enum class InsertTextFormat {
Missing = 0,
/// The primary text to be inserted is treated as a plain string.
PlainText = 1,
/// The primary text to be inserted is treated as a snippet.
///
/// A snippet can define tab stops and placeholders with `$1`, `$2`
/// and `${3:foo}`. `$0` defines the final tab stop, it defaults to the end
/// of the snippet. Placeholders with equal identifiers are linked, that is
/// typing in one will update others too.
///
/// See also:
/// https//github.com/Microsoft/vscode/blob/master/src/vs/editor/contrib/snippet/common/snippet.md
Snippet = 2,
};
struct CompletionItem {
/// The label of this completion item. By default also the text that is
/// inserted when selecting this completion.
std::string label;
/// The kind of this completion item. Based of the kind an icon is chosen by
/// the editor.
CompletionItemKind kind = CompletionItemKind::Missing;
/// A human-readable string with additional information about this item, like
/// type or symbol information.
std::string detail;
/// A human-readable string that represents a doc-comment.
std::string documentation;
/// A string that should be used when comparing this item with other items.
/// When `falsy` the label is used.
std::string sortText;
/// A string that should be used when filtering a set of completion items.
/// When `falsy` the label is used.
std::string filterText;
/// A string that should be inserted to a document when selecting this
/// completion. When `falsy` the label is used.
std::string insertText;
/// The format of the insert text. The format applies to both the `insertText`
/// property and the `newText` property of a provided `textEdit`.
InsertTextFormat insertTextFormat = InsertTextFormat::Missing;
/// An edit which is applied to a document when selecting this completion.
/// When an edit is provided `insertText` is ignored.
///
/// Note: The range of the edit must be a single line range and it must
/// contain the position at which completion has been requested.
llvm::Optional<TextEdit> textEdit;
/// An optional array of additional text edits that are applied when selecting
/// this completion. Edits must not overlap with the main edit nor with
/// themselves.
std::vector<TextEdit> additionalTextEdits;
// TODO(krasimir): The following optional fields defined by the language
// server protocol are unsupported:
//
// command?: Command - An optional command that is executed *after* inserting
// this completion.
//
// data?: any - A data entry field that is preserved on a completion item
// between a completion and a completion resolve request.
static json::Expr unparse(const CompletionItem &P);
};
/// A single parameter of a particular signature.
struct ParameterInformation {
/// The label of this parameter. Mandatory.
std::string label;
/// The documentation of this parameter. Optional.
std::string documentation;
static json::Expr unparse(const ParameterInformation &);
};
/// Represents the signature of something callable.
struct SignatureInformation {
/// The label of this signature. Mandatory.
std::string label;
/// The documentation of this signature. Optional.
std::string documentation;
/// The parameters of this signature.
std::vector<ParameterInformation> parameters;
static json::Expr unparse(const SignatureInformation &);
};
/// Represents the signature of a callable.
struct SignatureHelp {
/// The resulting signatures.
std::vector<SignatureInformation> signatures;
/// The active signature.
int activeSignature = 0;
/// The active parameter of the active signature.
int activeParameter = 0;
static json::Expr unparse(const SignatureHelp &);
};
} // namespace clangd
} // namespace clang
#endif
|