2017-02-07 18:28:20 +08:00
|
|
|
//===--- Protocol.cpp - Language Server Protocol Implementation -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the serialization code for the LSP structs.
|
|
|
|
// FIXME: This is extremely repetetive and ugly. Is there a better way?
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Protocol.h"
|
2017-09-20 15:24:15 +08:00
|
|
|
#include "Logger.h"
|
2017-09-18 23:02:59 +08:00
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
2017-09-18 23:02:59 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2017-04-07 19:03:26 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-08-02 17:08:39 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-09-20 15:24:15 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2017-02-07 18:28:20 +08:00
|
|
|
using namespace clang::clangd;
|
|
|
|
|
2017-09-18 23:02:59 +08:00
|
|
|
namespace {
|
2017-09-20 15:24:15 +08:00
|
|
|
void logIgnoredField(llvm::StringRef KeyValue, clangd::Logger &Logger) {
|
|
|
|
Logger.log(llvm::formatv("Ignored unknown field \"{0}\"\n", KeyValue));
|
2017-09-18 23:02:59 +08:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-04-07 19:03:26 +08:00
|
|
|
URI URI::fromUri(llvm::StringRef uri) {
|
|
|
|
URI Result;
|
|
|
|
Result.uri = uri;
|
|
|
|
uri.consume_front("file://");
|
2017-04-21 23:51:18 +08:00
|
|
|
// Also trim authority-less URIs
|
|
|
|
uri.consume_front("file:");
|
2017-04-07 19:03:26 +08:00
|
|
|
// For Windows paths e.g. /X:
|
|
|
|
if (uri.size() > 2 && uri[0] == '/' && uri[2] == ':')
|
|
|
|
uri.consume_front("/");
|
|
|
|
// Make sure that file paths are in native separators
|
|
|
|
Result.file = llvm::sys::path::convert_to_slash(uri);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
URI URI::fromFile(llvm::StringRef file) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
URI Result;
|
|
|
|
Result.file = file;
|
|
|
|
Result.uri = "file://";
|
|
|
|
// For Windows paths e.g. X:
|
|
|
|
if (file.size() > 1 && file[1] == ':')
|
|
|
|
Result.uri += "/";
|
|
|
|
// Make sure that uri paths are with posix separators
|
|
|
|
Result.uri += path::convert_to_slash(file, path::Style::posix);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
URI URI::parse(llvm::yaml::ScalarNode *Param) {
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
return URI::fromUri(Param->getValue(Storage));
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr URI::unparse(const URI &U) { return U.uri; }
|
2017-04-07 19:03:26 +08:00
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::Optional<TextDocumentIdentifier>
|
2017-09-18 23:02:59 +08:00
|
|
|
TextDocumentIdentifier::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
TextDocumentIdentifier Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
if (KeyValue == "uri") {
|
2017-04-07 19:03:26 +08:00
|
|
|
Result.uri = URI::parse(Value);
|
2017-02-07 18:28:20 +08:00
|
|
|
} else if (KeyValue == "version") {
|
|
|
|
// FIXME: parse version, but only for VersionedTextDocumentIdentifiers.
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-09-18 23:02:59 +08:00
|
|
|
llvm::Optional<Position> Position::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
Position Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "line") {
|
|
|
|
long long Val;
|
|
|
|
if (llvm::getAsSignedInteger(Value->getValue(Storage), 0, Val))
|
|
|
|
return llvm::None;
|
|
|
|
Result.line = Val;
|
|
|
|
} else if (KeyValue == "character") {
|
|
|
|
long long Val;
|
|
|
|
if (llvm::getAsSignedInteger(Value->getValue(Storage), 0, Val))
|
|
|
|
return llvm::None;
|
|
|
|
Result.character = Val;
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr Position::unparse(const Position &P) {
|
|
|
|
return json::obj{
|
|
|
|
{"line", P.line},
|
|
|
|
{"character", P.character},
|
|
|
|
};
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-09-18 23:02:59 +08:00
|
|
|
llvm::Optional<Range> Range::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
Range Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "start") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Position::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.start = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "end") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Position::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.end = std::move(*Parsed);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr Range::unparse(const Range &P) {
|
|
|
|
return json::obj{
|
|
|
|
{"start", P.start},
|
|
|
|
{"end", P.end},
|
|
|
|
};
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr Location::unparse(const Location &P) {
|
|
|
|
return json::obj{
|
|
|
|
{"uri", P.uri},
|
|
|
|
{"range", P.range},
|
|
|
|
};
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::Optional<TextDocumentItem>
|
2017-10-10 22:21:04 +08:00
|
|
|
TextDocumentItem::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
TextDocumentItem Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "uri") {
|
2017-04-07 19:03:26 +08:00
|
|
|
Result.uri = URI::parse(Value);
|
2017-02-07 18:28:20 +08:00
|
|
|
} else if (KeyValue == "languageId") {
|
|
|
|
Result.languageId = Value->getValue(Storage);
|
|
|
|
} else if (KeyValue == "version") {
|
|
|
|
long long Val;
|
|
|
|
if (llvm::getAsSignedInteger(Value->getValue(Storage), 0, Val))
|
|
|
|
return llvm::None;
|
|
|
|
Result.version = Val;
|
|
|
|
} else if (KeyValue == "text") {
|
|
|
|
Result.text = Value->getValue(Storage);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-09-18 23:02:59 +08:00
|
|
|
llvm::Optional<Metadata> Metadata::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-07-06 16:44:54 +08:00
|
|
|
Metadata Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "extraFlags") {
|
|
|
|
auto *Seq = dyn_cast<llvm::yaml::SequenceNode>(Value);
|
|
|
|
if (!Seq)
|
|
|
|
return llvm::None;
|
|
|
|
for (auto &Item : *Seq) {
|
|
|
|
auto *Node = dyn_cast<llvm::yaml::ScalarNode>(&Item);
|
|
|
|
if (!Node)
|
|
|
|
return llvm::None;
|
|
|
|
Result.extraFlags.push_back(Node->getValue(Storage));
|
|
|
|
}
|
2017-09-18 23:02:59 +08:00
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-07-06 16:44:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-09-18 23:02:59 +08:00
|
|
|
llvm::Optional<TextEdit> TextEdit::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
TextEdit Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "range") {
|
|
|
|
auto *Map = dyn_cast<llvm::yaml::MappingNode>(Value);
|
|
|
|
if (!Map)
|
|
|
|
return llvm::None;
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Range::parse(Map, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.range = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "newText") {
|
|
|
|
auto *Node = dyn_cast<llvm::yaml::ScalarNode>(Value);
|
|
|
|
if (!Node)
|
|
|
|
return llvm::None;
|
|
|
|
Result.newText = Node->getValue(Storage);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr TextEdit::unparse(const TextEdit &P) {
|
|
|
|
return json::obj{
|
|
|
|
{"range", P.range},
|
|
|
|
{"newText", P.newText},
|
|
|
|
};
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
}
|
|
|
|
|
2017-09-27 23:31:17 +08:00
|
|
|
namespace {
|
|
|
|
TraceLevel getTraceLevel(llvm::StringRef TraceLevelStr,
|
|
|
|
clangd::Logger &Logger) {
|
|
|
|
if (TraceLevelStr == "off")
|
|
|
|
return TraceLevel::Off;
|
|
|
|
else if (TraceLevelStr == "messages")
|
|
|
|
return TraceLevel::Messages;
|
|
|
|
else if (TraceLevelStr == "verbose")
|
|
|
|
return TraceLevel::Verbose;
|
|
|
|
|
|
|
|
Logger.log(llvm::formatv("Unknown trace level \"{0}\"\n", TraceLevelStr));
|
|
|
|
return TraceLevel::Off;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
llvm::Optional<InitializeParams>
|
|
|
|
InitializeParams::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
2017-10-12 21:29:58 +08:00
|
|
|
// If we don't understand the params, proceed with default parameters.
|
|
|
|
auto ParseFailure = [&] {
|
|
|
|
Logger.log("Failed to decode InitializeParams\n");
|
|
|
|
return InitializeParams();
|
|
|
|
};
|
2017-09-27 23:31:17 +08:00
|
|
|
InitializeParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
2017-10-12 21:29:58 +08:00
|
|
|
return ParseFailure();
|
2017-09-27 23:31:17 +08:00
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (KeyValue == "processId") {
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
2017-10-12 21:29:58 +08:00
|
|
|
return ParseFailure();
|
2017-09-27 23:31:17 +08:00
|
|
|
long long Val;
|
|
|
|
if (llvm::getAsSignedInteger(Value->getValue(KeyStorage), 0, Val))
|
2017-10-12 21:29:58 +08:00
|
|
|
return ParseFailure();
|
2017-09-27 23:31:17 +08:00
|
|
|
Result.processId = Val;
|
|
|
|
} else if (KeyValue == "rootPath") {
|
|
|
|
Result.rootPath = Value->getValue(KeyStorage);
|
|
|
|
} else if (KeyValue == "rootUri") {
|
|
|
|
Result.rootUri = URI::parse(Value);
|
|
|
|
} else if (KeyValue == "initializationOptions") {
|
|
|
|
// Not used
|
|
|
|
} else if (KeyValue == "capabilities") {
|
|
|
|
// Not used
|
|
|
|
} else if (KeyValue == "trace") {
|
|
|
|
Result.trace = getTraceLevel(Value->getValue(KeyStorage), Logger);
|
|
|
|
} else {
|
|
|
|
logIgnoredField(KeyValue, Logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::Optional<DidOpenTextDocumentParams>
|
2017-09-18 23:02:59 +08:00
|
|
|
DidOpenTextDocumentParams::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
DidOpenTextDocumentParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "textDocument") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentItem::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
2017-07-06 16:44:54 +08:00
|
|
|
} else if (KeyValue == "metadata") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Metadata::parse(Value, Logger);
|
2017-07-06 16:44:54 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.metadata = std::move(*Parsed);
|
2017-04-10 21:31:39 +08:00
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-04-10 21:31:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<DidCloseTextDocumentParams>
|
2017-09-18 23:02:59 +08:00
|
|
|
DidCloseTextDocumentParams::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-04-10 21:31:39 +08:00
|
|
|
DidCloseTextDocumentParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
|
|
|
|
if (KeyValue == "textDocument") {
|
|
|
|
auto *Map = dyn_cast<llvm::yaml::MappingNode>(Value);
|
|
|
|
if (!Map)
|
|
|
|
return llvm::None;
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Map, Logger);
|
2017-04-10 21:31:39 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
2017-02-07 18:28:20 +08:00
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<DidChangeTextDocumentParams>
|
2017-09-18 23:02:59 +08:00
|
|
|
DidChangeTextDocumentParams::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
DidChangeTextDocumentParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "textDocument") {
|
|
|
|
auto *Map = dyn_cast<llvm::yaml::MappingNode>(Value);
|
|
|
|
if (!Map)
|
|
|
|
return llvm::None;
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Map, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "contentChanges") {
|
|
|
|
auto *Seq = dyn_cast<llvm::yaml::SequenceNode>(Value);
|
|
|
|
if (!Seq)
|
|
|
|
return llvm::None;
|
|
|
|
for (auto &Item : *Seq) {
|
|
|
|
auto *I = dyn_cast<llvm::yaml::MappingNode>(&Item);
|
|
|
|
if (!I)
|
|
|
|
return llvm::None;
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentContentChangeEvent::parse(I, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.contentChanges.push_back(std::move(*Parsed));
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-10-03 02:00:37 +08:00
|
|
|
llvm::Optional<FileEvent> FileEvent::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
|
|
|
llvm::Optional<FileEvent> Result = FileEvent();
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
// We have to consume the whole MappingNode because it doesn't support
|
|
|
|
// skipping and we want to be able to parse further valid events.
|
|
|
|
if (!Result)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString) {
|
|
|
|
Result.reset();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value) {
|
|
|
|
Result.reset();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "uri") {
|
|
|
|
Result->uri = URI::parse(Value);
|
|
|
|
} else if (KeyValue == "type") {
|
|
|
|
long long Val;
|
|
|
|
if (llvm::getAsSignedInteger(Value->getValue(Storage), 0, Val)) {
|
|
|
|
Result.reset();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Result->type = static_cast<FileChangeType>(Val);
|
|
|
|
if (Result->type < FileChangeType::Created ||
|
|
|
|
Result->type > FileChangeType::Deleted)
|
|
|
|
Result.reset();
|
|
|
|
} else {
|
|
|
|
logIgnoredField(KeyValue, Logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<DidChangeWatchedFilesParams>
|
|
|
|
DidChangeWatchedFilesParams::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
|
|
|
DidChangeWatchedFilesParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "changes") {
|
|
|
|
auto *Seq = dyn_cast<llvm::yaml::SequenceNode>(Value);
|
|
|
|
if (!Seq)
|
|
|
|
return llvm::None;
|
|
|
|
for (auto &Item : *Seq) {
|
|
|
|
auto *I = dyn_cast<llvm::yaml::MappingNode>(&Item);
|
|
|
|
if (!I)
|
|
|
|
return llvm::None;
|
|
|
|
auto Parsed = FileEvent::parse(I, Logger);
|
|
|
|
if (Parsed)
|
|
|
|
Result.changes.push_back(std::move(*Parsed));
|
|
|
|
else
|
|
|
|
Logger.log("Failed to decode a FileEvent.\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logIgnoredField(KeyValue, Logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::Optional<TextDocumentContentChangeEvent>
|
2017-09-18 23:02:59 +08:00
|
|
|
TextDocumentContentChangeEvent::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
TextDocumentContentChangeEvent Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "text") {
|
|
|
|
Result.text = Value->getValue(Storage);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<FormattingOptions>
|
2017-10-10 22:21:04 +08:00
|
|
|
FormattingOptions::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
FormattingOptions Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "tabSize") {
|
|
|
|
long long Val;
|
|
|
|
if (llvm::getAsSignedInteger(Value->getValue(Storage), 0, Val))
|
|
|
|
return llvm::None;
|
|
|
|
Result.tabSize = Val;
|
|
|
|
} else if (KeyValue == "insertSpaces") {
|
|
|
|
long long Val;
|
|
|
|
StringRef Str = Value->getValue(Storage);
|
|
|
|
if (llvm::getAsSignedInteger(Str, 0, Val)) {
|
|
|
|
if (Str == "true")
|
|
|
|
Val = 1;
|
|
|
|
else if (Str == "false")
|
|
|
|
Val = 0;
|
|
|
|
else
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
Result.insertSpaces = Val;
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr FormattingOptions::unparse(const FormattingOptions &P) {
|
|
|
|
return json::obj{
|
|
|
|
{"tabSize", P.tabSize},
|
|
|
|
{"insertSpaces", P.insertSpaces},
|
|
|
|
};
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<DocumentRangeFormattingParams>
|
2017-09-18 23:02:59 +08:00
|
|
|
DocumentRangeFormattingParams::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
DocumentRangeFormattingParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "textDocument") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "range") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Range::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.range = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "options") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = FormattingOptions::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.options = std::move(*Parsed);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
2017-02-16 18:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<DocumentOnTypeFormattingParams>
|
2017-09-18 23:02:59 +08:00
|
|
|
DocumentOnTypeFormattingParams::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-16 18:49:46 +08:00
|
|
|
DocumentOnTypeFormattingParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
|
|
|
|
if (KeyValue == "ch") {
|
|
|
|
auto *ScalarValue =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!ScalarValue)
|
|
|
|
return llvm::None;
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
Result.ch = ScalarValue->getValue(Storage);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
if (KeyValue == "textDocument") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Value, Logger);
|
2017-02-16 18:49:46 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "position") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Position::parse(Value, Logger);
|
2017-02-16 18:49:46 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.position = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "options") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = FormattingOptions::parse(Value, Logger);
|
2017-02-16 18:49:46 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.options = std::move(*Parsed);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-16 18:49:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<DocumentFormattingParams>
|
2017-09-18 23:02:59 +08:00
|
|
|
DocumentFormattingParams::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-02-07 18:28:20 +08:00
|
|
|
DocumentFormattingParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "textDocument") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "options") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = FormattingOptions::parse(Value, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.options = std::move(*Parsed);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
2017-03-02 00:16:29 +08:00
|
|
|
|
2017-09-18 23:02:59 +08:00
|
|
|
llvm::Optional<Diagnostic> Diagnostic::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-03-02 00:16:29 +08:00
|
|
|
Diagnostic Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "range") {
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Range::parse(Value, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.range = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "severity") {
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
long long Val;
|
|
|
|
if (llvm::getAsSignedInteger(Value->getValue(Storage), 0, Val))
|
|
|
|
return llvm::None;
|
|
|
|
Result.severity = Val;
|
2017-09-18 23:02:59 +08:00
|
|
|
} else if (KeyValue == "code") {
|
|
|
|
// Not currently used
|
|
|
|
} else if (KeyValue == "source") {
|
|
|
|
// Not currently used
|
2017-03-02 00:16:29 +08:00
|
|
|
} else if (KeyValue == "message") {
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
Result.message = Value->getValue(Storage);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<CodeActionContext>
|
2017-10-10 22:21:04 +08:00
|
|
|
CodeActionContext::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
2017-03-02 00:16:29 +08:00
|
|
|
CodeActionContext Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "diagnostics") {
|
|
|
|
auto *Seq = dyn_cast<llvm::yaml::SequenceNode>(Value);
|
|
|
|
if (!Seq)
|
|
|
|
return llvm::None;
|
|
|
|
for (auto &Item : *Seq) {
|
|
|
|
auto *I = dyn_cast<llvm::yaml::MappingNode>(&Item);
|
|
|
|
if (!I)
|
|
|
|
return llvm::None;
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Diagnostic::parse(I, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.diagnostics.push_back(std::move(*Parsed));
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<CodeActionParams>
|
2017-10-10 22:21:04 +08:00
|
|
|
CodeActionParams::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
2017-03-02 00:16:29 +08:00
|
|
|
CodeActionParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "textDocument") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Value, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "range") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Range::parse(Value, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.range = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "context") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = CodeActionContext::parse(Value, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.context = std::move(*Parsed);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-03-02 00:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
2017-04-04 17:46:39 +08:00
|
|
|
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
llvm::Optional<std::map<std::string, std::vector<TextEdit>>>
|
|
|
|
parseWorkspaceEditChange(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
|
|
|
std::map<std::string, std::vector<TextEdit>> Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
if (Result.count(KeyValue)) {
|
|
|
|
logIgnoredField(KeyValue, Logger);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::SequenceNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
for (auto &Item : *Value) {
|
|
|
|
auto *ItemValue = dyn_cast_or_null<llvm::yaml::MappingNode>(&Item);
|
|
|
|
if (!ItemValue)
|
|
|
|
return llvm::None;
|
|
|
|
auto Parsed = TextEdit::parse(ItemValue, Logger);
|
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
Result[KeyValue].push_back(*Parsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<WorkspaceEdit>
|
|
|
|
WorkspaceEdit::parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger) {
|
|
|
|
WorkspaceEdit Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "changes") {
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return llvm::None;
|
|
|
|
auto Parsed = parseWorkspaceEditChange(Value, Logger);
|
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.changes = std::move(*Parsed);
|
|
|
|
} else {
|
|
|
|
logIgnoredField(KeyValue, Logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
|
|
|
|
"clangd.applyFix";
|
|
|
|
|
|
|
|
llvm::Optional<ExecuteCommandParams>
|
|
|
|
ExecuteCommandParams::parse(llvm::yaml::MappingNode *Params,
|
|
|
|
clangd::Logger &Logger) {
|
|
|
|
ExecuteCommandParams Result;
|
|
|
|
// Depending on which "command" we parse, we will use this function to parse
|
|
|
|
// the command "arguments".
|
|
|
|
std::function<bool(llvm::yaml::MappingNode * Params)> ArgParser = nullptr;
|
|
|
|
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
|
|
|
|
// Note that "commands" has to be parsed before "arguments" for this to
|
|
|
|
// work properly.
|
|
|
|
if (KeyValue == "command") {
|
|
|
|
auto *ScalarValue =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getValue());
|
|
|
|
if (!ScalarValue)
|
|
|
|
return llvm::None;
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
Result.command = ScalarValue->getValue(Storage);
|
|
|
|
if (Result.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
|
|
|
|
ArgParser = [&Result, &Logger](llvm::yaml::MappingNode *Params) {
|
|
|
|
auto WE = WorkspaceEdit::parse(Params, Logger);
|
|
|
|
if (WE)
|
|
|
|
Result.workspaceEdit = WE;
|
|
|
|
return WE.hasValue();
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
} else if (KeyValue == "arguments") {
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
auto *Seq = dyn_cast<llvm::yaml::SequenceNode>(Value);
|
|
|
|
if (!Seq)
|
|
|
|
return llvm::None;
|
|
|
|
for (auto &Item : *Seq) {
|
|
|
|
auto *ItemValue = dyn_cast_or_null<llvm::yaml::MappingNode>(&Item);
|
|
|
|
if (!ItemValue || !ArgParser)
|
|
|
|
return llvm::None;
|
|
|
|
if (!ArgParser(ItemValue))
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logIgnoredField(KeyValue, Logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Result.command.empty())
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr WorkspaceEdit::unparse(const WorkspaceEdit &WE) {
|
|
|
|
if (!WE.changes)
|
|
|
|
return json::obj{};
|
|
|
|
json::obj FileChanges;
|
|
|
|
for (auto &Change : *WE.changes)
|
|
|
|
FileChanges[Change.first] = json::ary(Change.second);
|
|
|
|
return json::obj{{"changes", std::move(FileChanges)}};
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
ApplyWorkspaceEditParams::unparse(const ApplyWorkspaceEditParams &Params) {
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
return json::obj{{"edit", Params.edit}};
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 17:46:39 +08:00
|
|
|
llvm::Optional<TextDocumentPositionParams>
|
2017-09-18 23:02:59 +08:00
|
|
|
TextDocumentPositionParams::parse(llvm::yaml::MappingNode *Params,
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger) {
|
2017-04-04 17:46:39 +08:00
|
|
|
TextDocumentPositionParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
2017-04-21 23:51:23 +08:00
|
|
|
continue;
|
2017-04-04 17:46:39 +08:00
|
|
|
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
if (KeyValue == "textDocument") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Value, Logger);
|
2017-04-04 17:46:39 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "position") {
|
2017-09-20 15:24:15 +08:00
|
|
|
auto Parsed = Position::parse(Value, Logger);
|
2017-04-04 17:46:39 +08:00
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.position = std::move(*Parsed);
|
|
|
|
} else {
|
2017-09-20 15:24:15 +08:00
|
|
|
logIgnoredField(KeyValue, Logger);
|
2017-04-04 17:46:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr CompletionItem::unparse(const CompletionItem &CI) {
|
2017-04-04 17:46:39 +08:00
|
|
|
assert(!CI.label.empty() && "completion item label is required");
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::obj Result{{"label", CI.label}};
|
2017-04-04 17:46:39 +08:00
|
|
|
if (CI.kind != CompletionItemKind::Missing)
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["kind"] = static_cast<int>(CI.kind);
|
2017-04-04 17:46:39 +08:00
|
|
|
if (!CI.detail.empty())
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["detail"] = CI.detail;
|
2017-04-04 17:46:39 +08:00
|
|
|
if (!CI.documentation.empty())
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["documentation"] = CI.documentation;
|
2017-04-04 17:46:39 +08:00
|
|
|
if (!CI.sortText.empty())
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["sortText"] = CI.sortText;
|
2017-04-04 17:46:39 +08:00
|
|
|
if (!CI.filterText.empty())
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["filterText"] = CI.filterText;
|
2017-04-04 17:46:39 +08:00
|
|
|
if (!CI.insertText.empty())
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["insertText"] = CI.insertText;
|
|
|
|
if (CI.insertTextFormat != InsertTextFormat::Missing)
|
|
|
|
Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
|
2017-04-04 17:46:39 +08:00
|
|
|
if (CI.textEdit)
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["textEdit"] = *CI.textEdit;
|
|
|
|
if (!CI.additionalTextEdits.empty())
|
|
|
|
Result["additionalTextEdits"] = json::ary(CI.additionalTextEdits);
|
|
|
|
return std::move(Result);
|
2017-04-04 17:46:39 +08:00
|
|
|
}
|
2017-10-06 19:54:17 +08:00
|
|
|
|
2017-11-08 15:44:12 +08:00
|
|
|
bool clangd::operator<(const CompletionItem &L, const CompletionItem &R) {
|
|
|
|
return (L.sortText.empty() ? L.label : L.sortText) <
|
|
|
|
(R.sortText.empty() ? R.label : R.sortText);
|
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr ParameterInformation::unparse(const ParameterInformation &PI) {
|
2017-10-06 19:54:17 +08:00
|
|
|
assert(!PI.label.empty() && "parameter information label is required");
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::obj Result{{"label", PI.label}};
|
2017-10-06 19:54:17 +08:00
|
|
|
if (!PI.documentation.empty())
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["documentation"] = PI.documentation;
|
|
|
|
return std::move(Result);
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr SignatureInformation::unparse(const SignatureInformation &SI) {
|
2017-10-06 19:54:17 +08:00
|
|
|
assert(!SI.label.empty() && "signature information label is required");
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::obj Result{
|
|
|
|
{"label", SI.label},
|
|
|
|
{"parameters", json::ary(SI.parameters)},
|
|
|
|
};
|
2017-10-06 19:54:17 +08:00
|
|
|
if (!SI.documentation.empty())
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
Result["documentation"] = SI.documentation;
|
|
|
|
return std::move(Result);
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
|
|
|
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
json::Expr SignatureHelp::unparse(const SignatureHelp &SH) {
|
2017-10-06 19:54:17 +08:00
|
|
|
assert(SH.activeSignature >= 0 &&
|
|
|
|
"Unexpected negative value for number of active signatures.");
|
|
|
|
assert(SH.activeParameter >= 0 &&
|
|
|
|
"Unexpected negative value for active parameter index");
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
return json::obj{
|
|
|
|
{"activeSignature", SH.activeSignature},
|
|
|
|
{"activeParameter", SH.activeParameter},
|
|
|
|
{"signatures", json::ary(SH.signatures)},
|
|
|
|
};
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
2017-11-09 19:30:04 +08:00
|
|
|
|
|
|
|
llvm::Optional<RenameParams>
|
|
|
|
RenameParams::parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger) {
|
|
|
|
RenameParams Result;
|
|
|
|
for (auto &NextKeyValue : *Params) {
|
|
|
|
auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
|
|
|
if (!KeyString)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
|
|
|
|
if (KeyValue == "textDocument") {
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
continue;
|
|
|
|
auto *Map = dyn_cast<llvm::yaml::MappingNode>(Value);
|
|
|
|
if (!Map)
|
|
|
|
return llvm::None;
|
|
|
|
auto Parsed = TextDocumentIdentifier::parse(Map, Logger);
|
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.textDocument = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "position") {
|
|
|
|
auto *Value =
|
|
|
|
dyn_cast_or_null<llvm::yaml::MappingNode>(NextKeyValue.getValue());
|
|
|
|
if (!Value)
|
|
|
|
continue;
|
|
|
|
auto Parsed = Position::parse(Value, Logger);
|
|
|
|
if (!Parsed)
|
|
|
|
return llvm::None;
|
|
|
|
Result.position = std::move(*Parsed);
|
|
|
|
} else if (KeyValue == "newName") {
|
|
|
|
auto *Value = NextKeyValue.getValue();
|
|
|
|
if (!Value)
|
|
|
|
continue;
|
|
|
|
auto *Node = dyn_cast<llvm::yaml::ScalarNode>(Value);
|
|
|
|
if (!Node)
|
|
|
|
return llvm::None;
|
|
|
|
llvm::SmallString<10> Storage;
|
|
|
|
Result.newName = Node->getValue(Storage);
|
|
|
|
} else {
|
|
|
|
logIgnoredField(KeyValue, Logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|