clang-tools 19.0.0git
PathMapping.h
Go to the documentation of this file.
1//===--- PathMapping.h - apply path mappings to LSP messages -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHMAPPING_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHMAPPING_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/Support/Error.h"
14#include "llvm/Support/JSON.h"
15#include "llvm/Support/raw_ostream.h"
16#include <memory>
17#include <optional>
18#include <string>
19#include <vector>
20
21namespace clang {
22namespace clangd {
23
24class Transport;
25
26/// PathMappings are a collection of paired client and server paths.
27/// These pairs are used to alter file:// URIs appearing in inbound and outbound
28/// LSP messages, as the client's environment may have source files or
29/// dependencies at different locations than the server. Therefore, both
30/// paths are stored as they appear in file URI bodies, e.g. /usr/include or
31/// /C:/config
32///
33/// For example, if the mappings were {{"/home/user", "/workarea"}}, then
34/// a client-to-server LSP message would have file:///home/user/foo.cpp
35/// remapped to file:///workarea/foo.cpp, and the same would happen for replies
36/// (in the opposite order).
38 std::string ClientPath;
39 std::string ServerPath;
41};
42using PathMappings = std::vector<PathMapping>;
43
44llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const PathMapping &M);
45
46/// Parse the command line \p RawPathMappings (e.g. "/client=/server") into
47/// pairs. Returns an error if the mappings are malformed, i.e. not absolute or
48/// not a proper pair.
49llvm::Expected<PathMappings> parsePathMappings(llvm::StringRef RawPathMappings);
50
51/// Returns a modified \p S with the first matching path in \p Mappings
52/// substituted, if applicable
53std::optional<std::string> doPathMapping(llvm::StringRef S,
55 const PathMappings &Mappings);
56
57/// Applies the \p Mappings to all the file:// URIs in \p Params.
58/// NOTE: The first matching mapping will be applied, otherwise \p Params will
59/// be untouched.
60void applyPathMappings(llvm::json::Value &Params, PathMapping::Direction Dir,
61 const PathMappings &Mappings);
62
63/// Creates a wrapping transport over \p Transp that applies the \p Mappings to
64/// all inbound and outbound LSP messages. All calls are then delegated to the
65/// regular transport (e.g. XPC, JSON).
66std::unique_ptr<Transport>
67createPathMappingTransport(std::unique_ptr<Transport> Transp,
68 PathMappings Mappings);
69
70} // namespace clangd
71} // namespace clang
72
73#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHMAPPING_H
const google::protobuf::Message & M
Definition: Server.cpp:309
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
llvm::Expected< PathMappings > parsePathMappings(llvm::StringRef RawPathMappings)
Parse the command line RawPathMappings (e.g.
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
void applyPathMappings(llvm::json::Value &V, PathMapping::Direction Dir, const PathMappings &Mappings)
Applies the Mappings to all the file:// URIs in Params.
Definition: PathMapping.cpp:48
std::vector< PathMapping > PathMappings
Definition: PathMapping.h:42
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...
std::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.
Definition: PathMapping.cpp:20
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
PathMappings are a collection of paired client and server paths.
Definition: PathMapping.h:37