-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[lldb-dap] Add support for launching supported clients #165941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # RUN: lldb-dap --client vscode-url -- /path/to/foo | FileCheck %s | ||
| # CHECK: vscode://llvm-vs-code-extensions.lldb-dap/start?program=%2Fpath%2Fto%2Ffoo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "ClientLauncher.h" | ||
| #include "llvm/ADT/StringExtras.h" | ||
| #include "llvm/ADT/StringSwitch.h" | ||
| #include "llvm/Support/FormatVariadic.h" | ||
|
|
||
| using namespace lldb_dap; | ||
|
|
||
| std::optional<ClientLauncher::Client> | ||
| ClientLauncher::GetClientFrom(llvm::StringRef str) { | ||
| return llvm::StringSwitch<std::optional<ClientLauncher::Client>>(str.lower()) | ||
| .Case("vscode", ClientLauncher::VSCode) | ||
| .Case("vscode-url", ClientLauncher::VSCodeURL) | ||
| .Default(std::nullopt); | ||
| } | ||
|
|
||
| std::unique_ptr<ClientLauncher> | ||
| ClientLauncher::GetLauncher(ClientLauncher::Client client) { | ||
| switch (client) { | ||
| case ClientLauncher::VSCode: | ||
| return std::make_unique<VSCodeLauncher>(); | ||
| case ClientLauncher::VSCodeURL: | ||
| return std::make_unique<VSCodeURLPrinter>(); | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| std::string VSCodeLauncher::URLEncode(llvm::StringRef str) { | ||
| std::string out; | ||
| llvm::raw_string_ostream os(out); | ||
| for (char c : str) { | ||
| if (std::isalnum(c) || llvm::StringRef("-_.~").contains(c)) | ||
| os << c; | ||
| else | ||
| os << '%' << llvm::utohexstr(c, false, 2); | ||
| } | ||
| return os.str(); | ||
| } | ||
|
|
||
| std::string | ||
| VSCodeLauncher::GetLaunchURL(const std::vector<llvm::StringRef> args) const { | ||
| assert(!args.empty() && "empty launch args"); | ||
|
|
||
| std::vector<std::string> encoded_launch_args; | ||
| for (llvm::StringRef arg : args) | ||
| encoded_launch_args.push_back(URLEncode(arg)); | ||
|
|
||
| const std::string args_str = llvm::join(encoded_launch_args, "&args="); | ||
| return llvm::formatv( | ||
| "vscode://llvm-vs-code-extensions.lldb-dap/start?program={0}", | ||
| args_str) | ||
| .str(); | ||
| } | ||
|
|
||
| llvm::Error VSCodeLauncher::Launch(const std::vector<llvm::StringRef> args) { | ||
| const std::string launch_url = GetLaunchURL(args); | ||
| const std::string command = | ||
| llvm::formatv("code --open-url {0}", launch_url).str(); | ||
|
|
||
| std::system(command.c_str()); | ||
| return llvm::Error::success(); | ||
| } | ||
|
|
||
| llvm::Error VSCodeURLPrinter::Launch(const std::vector<llvm::StringRef> args) { | ||
| llvm::outs() << GetLaunchURL(args) << '\n'; | ||
| return llvm::Error::success(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLDB_TOOLS_LLDB_DAP_CLIENTLAUNCHER_H | ||
| #define LLDB_TOOLS_LLDB_DAP_CLIENTLAUNCHER_H | ||
|
|
||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/Support/Error.h" | ||
| #include <vector> | ||
|
|
||
| namespace lldb_dap { | ||
|
|
||
| class ClientLauncher { | ||
| public: | ||
| enum Client { | ||
| VSCode, | ||
| VSCodeURL, | ||
| }; | ||
|
|
||
| virtual ~ClientLauncher() = default; | ||
| virtual llvm::Error Launch(const std::vector<llvm::StringRef> args) = 0; | ||
|
|
||
| static std::optional<Client> GetClientFrom(llvm::StringRef str); | ||
| static std::unique_ptr<ClientLauncher> GetLauncher(Client client); | ||
| }; | ||
|
|
||
| class VSCodeLauncher : public ClientLauncher { | ||
| public: | ||
| using ClientLauncher::ClientLauncher; | ||
|
|
||
| llvm::Error Launch(const std::vector<llvm::StringRef> args) override; | ||
|
|
||
| std::string GetLaunchURL(const std::vector<llvm::StringRef> args) const; | ||
| static std::string URLEncode(llvm::StringRef str); | ||
| }; | ||
|
|
||
| class VSCodeURLPrinter : public VSCodeLauncher { | ||
| using VSCodeLauncher::VSCodeLauncher; | ||
|
|
||
| llvm::Error Launch(const std::vector<llvm::StringRef> args) override; | ||
| }; | ||
|
|
||
| } // namespace lldb_dap | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| add_lldb_unittest(DAPTests | ||
| ClientLauncherTest.cpp | ||
| DAPErrorTest.cpp | ||
| DAPTest.cpp | ||
| DAPTypesTest.cpp | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "ClientLauncher.h" | ||
| #include "llvm/ADT/StringRef.h" | ||
| #include "gtest/gtest.h" | ||
| #include <optional> | ||
|
|
||
| using namespace lldb_dap; | ||
| using namespace llvm; | ||
|
|
||
| TEST(ClientLauncherTest, GetClientFromVSCode) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also include a URLEncode test? Either directly or by checking with calls to |
||
| std::optional<ClientLauncher::Client> result = | ||
| ClientLauncher::GetClientFrom("vscode"); | ||
| ASSERT_TRUE(result.has_value()); | ||
| EXPECT_EQ(ClientLauncher::VSCode, result.value()); | ||
| } | ||
|
|
||
| TEST(ClientLauncherTest, GetClientFromVSCodeUpperCase) { | ||
| std::optional<ClientLauncher::Client> result = | ||
| ClientLauncher::GetClientFrom("VSCODE"); | ||
| ASSERT_TRUE(result.has_value()); | ||
| EXPECT_EQ(ClientLauncher::VSCode, result.value()); | ||
| } | ||
|
|
||
| TEST(ClientLauncherTest, GetClientFromVSCodeMixedCase) { | ||
| std::optional<ClientLauncher::Client> result = | ||
| ClientLauncher::GetClientFrom("VSCode"); | ||
| ASSERT_TRUE(result.has_value()); | ||
| EXPECT_EQ(ClientLauncher::VSCode, result.value()); | ||
| } | ||
|
|
||
| TEST(ClientLauncherTest, GetClientFromInvalidString) { | ||
| std::optional<ClientLauncher::Client> result = | ||
| ClientLauncher::GetClientFrom("invalid"); | ||
| EXPECT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(ClientLauncherTest, GetClientFromEmptyString) { | ||
| std::optional<ClientLauncher::Client> result = | ||
| ClientLauncher::GetClientFrom(""); | ||
| EXPECT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(ClientLauncherTest, URLEncode) { | ||
| EXPECT_EQ("", VSCodeLauncher::URLEncode("")); | ||
| EXPECT_EQ( | ||
| "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~", | ||
| VSCodeLauncher::URLEncode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST" | ||
| "UVWXYZ0123456789-_.~")); | ||
| EXPECT_EQ("hello%20world", VSCodeLauncher::URLEncode("hello world")); | ||
| EXPECT_EQ("hello%21%40%23%24", VSCodeLauncher::URLEncode("hello!@#$")); | ||
| EXPECT_EQ("%2Fpath%2Fto%2Ffile", VSCodeLauncher::URLEncode("/path/to/file")); | ||
| EXPECT_EQ("key%3Dvalue%26key2%3Dvalue2", | ||
| VSCodeLauncher::URLEncode("key=value&key2=value2")); | ||
| EXPECT_EQ("100%25complete", VSCodeLauncher::URLEncode("100%complete")); | ||
| EXPECT_EQ("file_name%20with%20spaces%20%26%20special%21.txt", | ||
| VSCodeLauncher::URLEncode("file_name with spaces & special!.txt")); | ||
| EXPECT_EQ("%00%01%02", | ||
| VSCodeLauncher::URLEncode(llvm::StringRef("\x00\x01\x02", 3))); | ||
| EXPECT_EQ("test-file_name.txt~", | ||
| VSCodeLauncher::URLEncode("test-file_name.txt~")); | ||
|
|
||
| // UTF-8 encoded characters should be percent-encoded byte by byte. | ||
| EXPECT_EQ("%C3%A9", VSCodeLauncher::URLEncode("é")); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be great to also have a "url" / "terminal" / "stdout" client which simply prints the URL to stdout. That would allow me to also use it with custom scripts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although 🤔 I guess that even that would already be VSCode specific... So I guess it would have to be "vscode-url" instead of just "url"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. This also made a good candidate for a Shell test.