11 #include "llvm/ADT/None.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/Support/Errno.h" 14 #include "llvm/Support/Error.h" 15 #include "llvm/Support/Path.h" 25 if (!S.startswith(
"file://"))
29 llvm::consumeError(Uri.takeError());
32 for (
const auto &Mapping : Mappings) {
39 llvm::StringRef Body = Uri->body();
40 if (Body.consume_front(From) && (Body.empty() || Body.front() ==
'/')) {
41 std::string MappedBody = (To + Body).str();
42 return URI(Uri->scheme(), Uri->authority(), MappedBody.c_str())
54 llvm::json::Object *Obj = V.getAsObject();
55 llvm::json::Object MappedObj;
57 for (
auto &KV : *Obj) {
58 if (llvm::Optional<std::string> MappedKey =
60 MappedObj.try_emplace(std::move(*MappedKey), std::move(KV.second));
62 MappedObj.try_emplace(std::move(KV.first), std::move(KV.second));
65 *Obj = std::move(MappedObj);
70 for (llvm::json::Value &Val : *V.getAsArray())
73 if (llvm::Optional<std::string> Mapped =
75 V = std::move(*Mapped);
83 PathMappingMessageHandler(MessageHandler &Handler,
85 : WrappedHandler(Handler), Mappings(Mappings) {}
87 bool onNotify(llvm::StringRef
Method, llvm::json::Value Params)
override {
89 return WrappedHandler.onNotify(Method, std::move(Params));
92 bool onCall(llvm::StringRef Method, llvm::json::Value Params,
93 llvm::json::Value ID)
override {
95 return WrappedHandler.onCall(Method, std::move(Params), std::move(ID));
98 bool onReply(llvm::json::Value ID,
99 llvm::Expected<llvm::json::Value> Result)
override {
103 return WrappedHandler.onReply(std::move(ID), std::move(Result));
113 class PathMappingTransport :
public Transport {
115 PathMappingTransport(std::unique_ptr<Transport> Transp,
PathMappings Mappings)
116 : WrappedTransport(std::move(Transp)), Mappings(std::move(Mappings)) {}
118 void notify(llvm::StringRef
Method, llvm::json::Value Params)
override {
120 WrappedTransport->notify(Method, std::move(Params));
123 void call(llvm::StringRef Method, llvm::json::Value Params,
124 llvm::json::Value ID)
override {
126 WrappedTransport->call(Method, std::move(Params), std::move(ID));
129 void reply(llvm::json::Value ID,
130 llvm::Expected<llvm::json::Value> Result)
override {
134 WrappedTransport->reply(std::move(ID), std::move(Result));
137 llvm::Error loop(MessageHandler &Handler)
override {
138 PathMappingMessageHandler WrappedHandler(Handler, Mappings);
139 return WrappedTransport->loop(WrappedHandler);
143 std::unique_ptr<Transport> WrappedTransport;
149 llvm::Expected<std::string> parsePath(llvm::StringRef
Path) {
150 namespace path = llvm::sys::path;
151 if (path::is_absolute(Path, path::Style::posix)) {
153 }
else if (path::is_absolute(Path, path::Style::windows)) {
154 std::string Converted = path::convert_to_slash(Path, path::Style::windows);
155 if (Converted.front() !=
'/')
156 Converted =
"/" + Converted;
159 return llvm::createStringError(llvm::inconvertibleErrorCode(),
160 "Path not absolute: " + Path);
169 llvm::Expected<PathMappings>
171 llvm::StringRef ClientPath, ServerPath, PathPair, Rest = RawPathMappings;
173 while (!Rest.empty()) {
174 std::tie(PathPair, Rest) = Rest.split(
",");
175 std::tie(ClientPath, ServerPath) = PathPair.split(
"=");
176 if (ClientPath.empty() || ServerPath.empty())
177 return llvm::createStringError(llvm::inconvertibleErrorCode(),
178 "Not a valid path mapping pair: " +
180 llvm::Expected<std::string> ParsedClientPath = parsePath(ClientPath);
181 if (!ParsedClientPath)
182 return ParsedClientPath.takeError();
183 llvm::Expected<std::string> ParsedServerPath = parsePath(ServerPath);
184 if (!ParsedServerPath)
185 return ParsedServerPath.takeError();
186 ParsedMappings.push_back(
187 {std::move(*ParsedClientPath), std::move(*ParsedServerPath)});
189 return ParsedMappings;
192 std::unique_ptr<Transport>
195 return std::make_unique<PathMappingTransport>(std::move(Transp), Mappings);
PathMappings are a collection of paired client and server paths.
std::vector< PathMapping > PathMappings
Documents should not be synced at all.
void applyPathMappings(llvm::json::Value &V, PathMapping::Direction Dir, const PathMappings &Mappings)
Applies the Mappings to all the file:// URIs in Params.
static const char * toString(OffsetEncoding OE)
std::string Path
A typedef to represent a file path.
llvm::Expected< PathMappings > parsePathMappings(llvm::StringRef RawPathMappings)
Parse the command line RawPathMappings (e.g.
std::unique_ptr< Transport > createPathMappingTransport(std::unique_ptr< Transport > Transp, PathMappings Mappings)
Creates a wrapping transport over Transp that applies the Mappings to all inbound and outbound LSP me...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A URI describes the location of a source file.
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Parse a URI string "<scheme>:[//<authority>/]<path>".
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
llvm::Optional< std::string > doPathMapping(llvm::StringRef S, PathMapping::Direction Dir, const PathMappings &Mappings)
Returns a modified S with the first matching path in Mappings substituted, if applicable.