nixd
Loading...
Searching...
No Matches
URI.h
Go to the documentation of this file.
1#pragma once
2
3#include <llvm/ADT/StringRef.h>
4#include <llvm/Support/Error.h>
5#include <llvm/Support/Registry.h>
6
7namespace lspserver {
8
9/// A URI describes the location of a source file.
10/// In the simplest case, this is a "file" URI that directly encodes the
11/// absolute path to a file. More abstract cases are possible: a shared index
12/// service might expose repo:// URIs that are relative to the source control
13/// root.
14///
15/// Clangd handles URIs of the form <scheme>:[//<authority>]<body>. It doesn't
16/// further split the authority or body into constituent parts (e.g. query
17/// strings is included in the body).
18class URI {
19public:
20 URI(llvm::StringRef Scheme, llvm::StringRef Authority, llvm::StringRef Body);
21
22 /// Returns decoded scheme e.g. "https"
23 llvm::StringRef scheme() const { return Scheme; }
24 /// Returns decoded authority e.g. "reviews.lvm.org"
25 llvm::StringRef authority() const { return Authority; }
26 /// Returns decoded body e.g. "/D41946"
27 llvm::StringRef body() const { return Body; }
28
29 /// Returns a string URI with all components percent-encoded.
30 std::string toString() const;
31
32 /// Creates a URI for a file in the given scheme. \p Scheme must be
33 /// registered. The URI is percent-encoded.
34 static llvm::Expected<URI> create(llvm::StringRef AbsolutePath,
35 llvm::StringRef Scheme);
36
37 // Similar to above except this picks a registered scheme that works. If none
38 // works, this falls back to "file" scheme.
39 static URI create(llvm::StringRef AbsolutePath);
40
41 /// This creates a file:// URI for \p AbsolutePath. The path must be absolute.
42 static URI createFile(llvm::StringRef AbsolutePath);
43
44 /// Parse a URI string "<scheme>:[//<authority>/]<path>". Percent-encoded
45 /// characters in the URI will be decoded.
46 static llvm::Expected<URI> parse(llvm::StringRef Uri);
47
48 /// Resolves the absolute path of \p U. If there is no matching scheme, or the
49 /// URI is invalid in the scheme, this returns an error.
50 ///
51 /// \p HintPath A related path, such as the current file or working directory,
52 /// which can help disambiguate when the same file exists in many workspaces.
53 static llvm::Expected<std::string> resolve(const URI &U,
54 llvm::StringRef HintPath = "");
55
56 /// Same as above, in addition it parses the \p FileURI using URI::parse.
57 static llvm::Expected<std::string> resolve(llvm::StringRef FileURI,
58 llvm::StringRef HintPath = "");
59
60 /// Resolves \p AbsPath into a canonical path of its URI, by converting
61 /// \p AbsPath to URI and resolving the URI to get th canonical path.
62 /// This ensures that paths with the same URI are resolved into consistent
63 /// file path.
64 static llvm::Expected<std::string> resolvePath(llvm::StringRef AbsPath,
65 llvm::StringRef HintPath = "");
66
67 /// Gets the preferred spelling of this file for #include, if there is one,
68 /// e.g. <system_header.h>, "path/to/x.h".
69 ///
70 /// This allows URI schemas to provide their customized include paths.
71 ///
72 /// Returns an empty string if normal include-shortening based on the absolute
73 /// path should be used.
74 /// Fails if the URI is not valid in the schema.
75 static llvm::Expected<std::string> includeSpelling(const URI &U);
76
77 friend bool operator==(const URI &LHS, const URI &RHS) {
78 return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) ==
79 std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
80 }
81
82 friend bool operator<(const URI &LHS, const URI &RHS) {
83 return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) <
84 std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
85 }
86
87private:
88 URI() = default;
89
90 std::string Scheme;
91 std::string Authority;
92 std::string Body;
93};
94
95/// URIScheme is an extension point for teaching clangd to recognize a custom
96/// URI scheme. This is expected to be implemented and exposed via the
97/// URISchemeRegistry.
98class URIScheme {
99public:
100 virtual ~URIScheme() = default;
101
102 /// Returns the absolute path of the file corresponding to the URI
103 /// authority+body in the file system. See URI::resolve for semantics of
104 /// \p HintPath.
105 virtual llvm::Expected<std::string>
106 getAbsolutePath(llvm::StringRef Authority, llvm::StringRef Body,
107 llvm::StringRef HintPath) const = 0;
108
109 virtual llvm::Expected<URI>
110 uriFromAbsolutePath(llvm::StringRef AbsolutePath) const = 0;
111
112 /// Returns the include path of the file (e.g. <path>, "path"), which can be
113 /// #included directly. See URI::includeSpelling for details.
114 virtual llvm::Expected<std::string> getIncludeSpelling(const URI &U) const {
115 return ""; // no customized include path for this scheme.
116 }
117};
118
119/// By default, a "file" scheme is supported where URI paths are always absolute
120/// in the file system.
121typedef llvm::Registry<URIScheme> URISchemeRegistry;
122
123} // namespace lspserver
virtual ~URIScheme()=default
virtual llvm::Expected< std::string > getAbsolutePath(llvm::StringRef Authority, llvm::StringRef Body, llvm::StringRef HintPath) const =0
virtual llvm::Expected< URI > uriFromAbsolutePath(llvm::StringRef AbsolutePath) const =0
virtual llvm::Expected< std::string > getIncludeSpelling(const URI &U) const
Definition URI.h:114
friend bool operator<(const URI &LHS, const URI &RHS)
Definition URI.h:82
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Definition URI.cpp:242
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Definition URI.cpp:174
static llvm::Expected< URI > create(llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
Definition URI.cpp:206
llvm::StringRef authority() const
Returns decoded authority e.g. "reviews.lvm.org".
Definition URI.h:25
llvm::StringRef body() const
Returns decoded body e.g. "/D41946".
Definition URI.h:27
llvm::StringRef scheme() const
Returns decoded scheme e.g. "https".
Definition URI.h:23
std::string toString() const
Returns a string URI with all components percent-encoded.
Definition URI.cpp:158
static llvm::Expected< std::string > includeSpelling(const URI &U)
Definition URI.cpp:270
friend bool operator==(const URI &LHS, const URI &RHS)
Definition URI.h:77
static URI createFile(llvm::StringRef AbsolutePath)
This creates a file:// URI for AbsolutePath. The path must be absolute.
Definition URI.cpp:235
static llvm::Expected< std::string > resolvePath(llvm::StringRef AbsPath, llvm::StringRef HintPath="")
Definition URI.cpp:250
Whether current platform treats paths case insensitively.
Definition Connection.h:11
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)
llvm::Registry< URIScheme > URISchemeRegistry
Definition URI.h:121