|
| 1 | +//===-- GoToTargetsRequestHandler.cpp |
| 2 | +//--------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "DAP.h" |
| 11 | + |
| 12 | +#include "JSONUtils.h" |
| 13 | + |
| 14 | +#include <lldb/API/SBStream.h> |
| 15 | + |
| 16 | +namespace lldb_dap { |
| 17 | + |
| 18 | +// "GotoTargetsRequest": { |
| 19 | +// "allOf": [ { "$ref": "#/definitions/Request" }, { |
| 20 | +// "type": "object", |
| 21 | +// "description": "This request retrieves the possible goto targets for the |
| 22 | +// specified source location.\nThese targets can be used in the `goto` |
| 23 | +// request.\nClients should only call this request if the corresponding |
| 24 | +// capability `supportsGotoTargetsRequest` is true.", "properties": { |
| 25 | +// "command": { |
| 26 | +// "type": "string", |
| 27 | +// "enum": [ "gotoTargets" ] |
| 28 | +// }, |
| 29 | +// "arguments": { |
| 30 | +// "$ref": "#/definitions/GotoTargetsArguments" |
| 31 | +// } |
| 32 | +// }, |
| 33 | +// "required": [ "command", "arguments" ] |
| 34 | +// }] |
| 35 | +// }, |
| 36 | +// "GotoTargetsArguments": { |
| 37 | +// "type": "object", |
| 38 | +// "description": "Arguments for `gotoTargets` request.", |
| 39 | +// "properties": { |
| 40 | +// "source": { |
| 41 | +// "$ref": "#/definitions/Source", |
| 42 | +// "description": "The source location for which the goto targets are |
| 43 | +// determined." |
| 44 | +// }, |
| 45 | +// "line": { |
| 46 | +// "type": "integer", |
| 47 | +// "description": "The line location for which the goto targets are |
| 48 | +// determined." |
| 49 | +// }, |
| 50 | +// "column": { |
| 51 | +// "type": "integer", |
| 52 | +// "description": "The position within `line` for which the goto targets |
| 53 | +// are determined. It is measured in UTF-16 code units and the client |
| 54 | +// capability `columnsStartAt1` determines whether it is 0- or 1-based." |
| 55 | +// } |
| 56 | +// }, |
| 57 | +// "required": [ "source", "line" ] |
| 58 | +// }, |
| 59 | +// "GotoTargetsResponse": { |
| 60 | +// "allOf": [ { "$ref": "#/definitions/Response" }, { |
| 61 | +// "type": "object", |
| 62 | +// "description": "Response to `gotoTargets` request.", |
| 63 | +// "properties": { |
| 64 | +// "body": { |
| 65 | +// "type": "object", |
| 66 | +// "properties": { |
| 67 | +// "targets": { |
| 68 | +// "type": "array", |
| 69 | +// "items": { |
| 70 | +// "$ref": "#/definitions/GotoTarget" |
| 71 | +// }, |
| 72 | +// "description": "The possible goto targets of the specified |
| 73 | +// location." |
| 74 | +// } |
| 75 | +// }, |
| 76 | +// "required": [ "targets" ] |
| 77 | +// } |
| 78 | +// }, |
| 79 | +// "required": [ "body" ] |
| 80 | +// }] |
| 81 | +// }, |
| 82 | +void GoToTargetsRequestHandler::operator()( |
| 83 | + const llvm::json::Object &request) const { |
| 84 | + llvm::json::Object response; |
| 85 | + FillResponse(request, response); |
| 86 | + const auto *arguments = request.getObject("arguments"); |
| 87 | + const auto *source = arguments->getObject("source"); |
| 88 | + const std::string path = GetString(source, "path").str(); |
| 89 | + |
| 90 | + const auto goto_line = GetInteger<uint64_t>(arguments, "line").value_or(0u); |
| 91 | + const auto goto_column = |
| 92 | + GetInteger<uint64_t>(arguments, "column").value_or(0u); |
| 93 | + |
| 94 | + lldb::SBLineEntry line_entry{}; |
| 95 | + const lldb::SBFileSpec file_spec(path.c_str(), true); |
| 96 | + line_entry.SetFileSpec(file_spec); |
| 97 | + line_entry.SetLine(goto_line); |
| 98 | + line_entry.SetColumn(goto_column); |
| 99 | + |
| 100 | + const auto target_id = dap.goto_id_map.InsertLineEntry(line_entry); |
| 101 | + llvm::json::Array response_targets; |
| 102 | + const auto target_line = line_entry.GetLine(); |
| 103 | + const auto target_column = line_entry.GetColumn(); |
| 104 | + auto target = llvm::json::Object(); |
| 105 | + target.try_emplace("id", target_id); |
| 106 | + |
| 107 | + lldb::SBStream stream; |
| 108 | + line_entry.GetDescription(stream); |
| 109 | + target.try_emplace("label", |
| 110 | + llvm::StringRef(stream.GetData(), stream.GetSize())); |
| 111 | + target.try_emplace("column", target_column); |
| 112 | + target.try_emplace("line", target_line); |
| 113 | + |
| 114 | + response_targets.push_back(std::move(target)); |
| 115 | + llvm::json::Object body; |
| 116 | + body.try_emplace("targets", std::move(response_targets)); |
| 117 | + response.try_emplace("body", std::move(body)); |
| 118 | + dap.SendJSON(llvm::json::Value(std::move(response))); |
| 119 | +} |
| 120 | +} // namespace lldb_dap |
0 commit comments