|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 | +#include "ClientLauncher.h" |
| 10 | +#include "llvm/ADT/StringExtras.h" |
| 11 | +#include "llvm/ADT/StringSwitch.h" |
| 12 | +#include "llvm/Support/FormatVariadic.h" |
| 13 | + |
| 14 | +using namespace lldb_dap; |
| 15 | + |
| 16 | +std::optional<ClientLauncher::Client> |
| 17 | +ClientLauncher::GetClientFrom(llvm::StringRef str) { |
| 18 | + return llvm::StringSwitch<std::optional<ClientLauncher::Client>>(str.lower()) |
| 19 | + .Case("vscode", ClientLauncher::VSCode) |
| 20 | + .Default(std::nullopt); |
| 21 | +} |
| 22 | + |
| 23 | +std::unique_ptr<ClientLauncher> |
| 24 | +ClientLauncher::GetLauncher(ClientLauncher::Client client) { |
| 25 | + switch (client) { |
| 26 | + case ClientLauncher::VSCode: |
| 27 | + return std::make_unique<VSCodeLauncher>(); |
| 28 | + } |
| 29 | + return nullptr; |
| 30 | +} |
| 31 | + |
| 32 | +static std::string URLEncode(llvm::StringRef in) { |
| 33 | + std::string out; |
| 34 | + llvm::raw_string_ostream os(out); |
| 35 | + for (char c : in) { |
| 36 | + if (std::isalnum(c) || llvm::StringRef("-_.~").contains(c)) |
| 37 | + os << c; |
| 38 | + else |
| 39 | + os << '%' << llvm::utohexstr(c, false, 2); |
| 40 | + } |
| 41 | + return os.str(); |
| 42 | +} |
| 43 | + |
| 44 | +llvm::Error VSCodeLauncher::Launch(const std::vector<llvm::StringRef> args) { |
| 45 | + std::vector<std::string> encoded_launch_args; |
| 46 | + for (llvm::StringRef arg : args) |
| 47 | + encoded_launch_args.push_back(URLEncode(arg)); |
| 48 | + |
| 49 | + const std::string args_str = llvm::join(args, "&args="); |
| 50 | + const std::string launch_url = llvm::formatv( |
| 51 | + "vscode://llvm-vs-code-extensions.lldb-dap/start?program={0}", args_str); |
| 52 | + const std::string command = |
| 53 | + llvm::formatv("code --open-url {0}", launch_url).str(); |
| 54 | + |
| 55 | + std::system(command.c_str()); |
| 56 | + return llvm::Error::success(); |
| 57 | +} |
0 commit comments