mirror of
https://github.com/open-goal/jak-project.git
synced 2024-10-20 11:26:18 -04:00
0e31a9c407
This change adds a few new features: - Decompiler automatically knows the type of `find-parent-method` use in jak 1 and jak2 when used in a method or virtual state handler. - Decompiler inserts a call to `call-parent-method` or `find-parent-state` - Removed most casts related to these functions There are still a few minor issues around this: - There are still some casts needed when using `post` methods, as `post` is just a `function`, and needs a cast to `(function none)` or similar. It didn't seem easy to change the type of `post`, so I'm not going to worry about it for this PR. It only shows up in like 3 places in jak 2. (and 0 in jak 1) - If "call the handler if it's not #f" logic should probably be another macro. Fixes #805
281 lines
11 KiB
C++
281 lines
11 KiB
C++
#pragma once
|
|
|
|
#include <optional>
|
|
#include <regex>
|
|
|
|
#include "goalc/compiler/docs/DocTypes.h"
|
|
#include "lsp/protocol/common_types.h"
|
|
#include "lsp/protocol/hover.h"
|
|
#include "lsp/state/data/mips_instructions.h"
|
|
#include "lsp/state/workspace.h"
|
|
|
|
bool is_number(const std::string& s) {
|
|
return !s.empty() && std::find_if(s.begin(), s.end(),
|
|
[](unsigned char c) { return !std::isdigit(c); }) == s.end();
|
|
}
|
|
|
|
std::vector<std::string> og_method_names = {"new", "delete", "print", "inspect", "length",
|
|
"asize-of", "copy", "relocate", "mem-usage"};
|
|
|
|
std::optional<LSPSpec::Hover> hover_handler_ir(Workspace& workspace,
|
|
const LSPSpec::TextDocumentPositionParams& params,
|
|
const WorkspaceIRFile& tracked_file) {
|
|
// See if it's an OpenGOAL symbol or a MIPS mnemonic
|
|
auto symbol_name = tracked_file.get_symbol_at_position(params.m_position);
|
|
auto token_at_pos = tracked_file.get_mips_instruction_at_position(params.m_position);
|
|
if (!symbol_name && !token_at_pos) {
|
|
return {};
|
|
}
|
|
|
|
LSPSpec::MarkupContent markup;
|
|
markup.m_kind = "markdown";
|
|
|
|
// TODO - try specifying the range so it highlights everything, ie. `c.lt.s`
|
|
// Prefer symbols
|
|
if (symbol_name) {
|
|
lg::debug("hover - symbol match - {}", symbol_name.value());
|
|
auto symbol_info = workspace.get_definition_info_from_all_types(symbol_name.value(),
|
|
tracked_file.m_all_types_uri);
|
|
if (symbol_info && symbol_info.value().docstring.has_value()) {
|
|
std::string docstring = symbol_info.value().docstring.value();
|
|
lg::debug("hover - symbol has docstring - {}", docstring);
|
|
// A docstring exists, print it!
|
|
// By convention, docstrings are assumed to be markdown, they support code-blocks everything
|
|
// the only thing extra we do, is replace [[<symbol>]] with links if available
|
|
std::unordered_map<std::string, std::string> symbol_replacements = {};
|
|
std::smatch match;
|
|
|
|
std::string::const_iterator searchStart(docstring.cbegin());
|
|
while (
|
|
std::regex_search(searchStart, docstring.cend(), match, std::regex("\\[{2}(.*)\\]{2}"))) {
|
|
// Have we already accounted for this symbol?
|
|
const auto& name = match[1].str();
|
|
if (symbol_replacements.count(name) != 0) {
|
|
continue;
|
|
}
|
|
// Get this symbols info
|
|
auto symbol_info =
|
|
workspace.get_definition_info_from_all_types(name, tracked_file.m_all_types_uri);
|
|
if (!symbol_info) {
|
|
symbol_replacements[name] = fmt::format("_{}_", name);
|
|
} else {
|
|
// Construct path
|
|
auto symbol_uri =
|
|
fmt::format("{}#L{}%2C{}", tracked_file.m_all_types_uri,
|
|
symbol_info.value().definition_info->line_idx_to_display + 1,
|
|
symbol_info.value().definition_info->pos_in_line);
|
|
symbol_replacements[name] = fmt::format("[{}]({})", name, symbol_uri);
|
|
}
|
|
searchStart = match.suffix().first;
|
|
}
|
|
// Replace all symbol occurences
|
|
for (const auto& [key, val] : symbol_replacements) {
|
|
docstring = std::regex_replace(docstring, std::regex("\\[{2}" + key + "\\]{2}"), val);
|
|
}
|
|
|
|
markup.m_value = docstring;
|
|
LSPSpec::Hover hover_resp;
|
|
hover_resp.m_contents = markup;
|
|
return hover_resp;
|
|
} else if (!token_at_pos) {
|
|
// Check if it's a number, and if so we'll do some numeric conversions
|
|
if (!is_number(symbol_name.value())) {
|
|
return {};
|
|
}
|
|
lg::debug("hover - numeric match - {}", symbol_name.value());
|
|
// Construct the body
|
|
std::string body = "";
|
|
uint32_t num = std::atoi(symbol_name.value().data());
|
|
// Assuming it comes in as Decimal
|
|
body += "| Base | Value |\n";
|
|
body += "|---------|-------|\n";
|
|
body += fmt::format("| Decimal | `{:d}` |\n", num);
|
|
body += fmt::format("| Hex | `{:X}` |\n", num);
|
|
// TODO - would be nice to format as groups of 4
|
|
body += fmt::format("| Binary | `{:b}` |\n", num);
|
|
if (num >= 16 && (num - 16) % 4 == 0) {
|
|
uint32_t method_id = (num - 16) / 4;
|
|
std::string method_name = "not built-in";
|
|
if (method_id <= 8) {
|
|
method_name = og_method_names.at(method_id);
|
|
}
|
|
body += fmt::format("| Method ID | `{}` - `{}` |\n", method_id, method_name);
|
|
}
|
|
body += fmt::format("| Octal | `{:o}` |\n", num);
|
|
|
|
markup.m_value = body;
|
|
LSPSpec::Hover hover_resp;
|
|
hover_resp.m_contents = markup;
|
|
return hover_resp;
|
|
}
|
|
}
|
|
|
|
// Otherwise, maybe it's a MIPS instruction
|
|
if (token_at_pos) {
|
|
lg::debug("hover - token match - {}", token_at_pos.value());
|
|
auto& token = token_at_pos.value();
|
|
std::transform(token.begin(), token.end(), token.begin(),
|
|
[](unsigned char c) { return std::tolower(c); });
|
|
// Find the instruction, there are some edge-cases here where they could be multiple
|
|
// TODO - havn't addressed `bc` and such instructions! Those need to be prefixed matched
|
|
std::vector<std::string> ee_instructions = {};
|
|
std::vector<std::string> vu_instructions = {};
|
|
for (const auto& instr : LSPData::MIPS_INSTRUCTION_LIST) {
|
|
auto mnemonic_lower = instr.mnemonic;
|
|
std::transform(mnemonic_lower.begin(), mnemonic_lower.end(), mnemonic_lower.begin(),
|
|
[](unsigned char c) { return std::tolower(c); });
|
|
if (mnemonic_lower == token) {
|
|
if (instr.type == "ee") {
|
|
ee_instructions.push_back(fmt::format("- _{}_\n\n", instr.description));
|
|
} else {
|
|
vu_instructions.push_back(fmt::format("- _{}_\n\n", instr.description));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Construct the body
|
|
std::string body = "";
|
|
if (!ee_instructions.empty()) {
|
|
body += "**EE Instructions**\n\n";
|
|
for (const auto& instr : ee_instructions) {
|
|
body += instr;
|
|
}
|
|
body += "___\n\n";
|
|
}
|
|
|
|
if (!vu_instructions.empty()) {
|
|
body += "**VU Instructions**\n\n";
|
|
for (const auto& instr : vu_instructions) {
|
|
body += instr;
|
|
}
|
|
body += "___\n\n";
|
|
}
|
|
|
|
markup.m_value = body;
|
|
LSPSpec::Hover hover_resp;
|
|
hover_resp.m_contents = markup;
|
|
return hover_resp;
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
std::string truncate_docstring(const std::string& docstring) {
|
|
std::string truncated = "";
|
|
const auto lines = str_util::split(docstring);
|
|
for (const auto& line : lines) {
|
|
const auto trimmed_line = str_util::ltrim(line);
|
|
if (str_util::starts_with(trimmed_line, "@")) {
|
|
break;
|
|
}
|
|
truncated += trimmed_line + "\n";
|
|
}
|
|
return truncated;
|
|
}
|
|
|
|
std::optional<json> hover_handler(Workspace& workspace, int /*id*/, json raw_params) {
|
|
auto params = raw_params.get<LSPSpec::TextDocumentPositionParams>();
|
|
auto file_type = workspace.determine_filetype_from_uri(params.m_textDocument.m_uri);
|
|
|
|
if (file_type == Workspace::FileType::OpenGOALIR) {
|
|
auto tracked_file = workspace.get_tracked_ir_file(params.m_textDocument.m_uri);
|
|
if (!tracked_file) {
|
|
return {};
|
|
}
|
|
return hover_handler_ir(workspace, params, tracked_file.value());
|
|
} else if (file_type == Workspace::FileType::OpenGOAL) {
|
|
auto tracked_file = workspace.get_tracked_og_file(params.m_textDocument.m_uri);
|
|
if (!tracked_file) {
|
|
return {};
|
|
}
|
|
// TODO - replace with AST usage instead of figuring out the symbol ourselves
|
|
const auto symbol = tracked_file->get_symbol_at_position(params.m_position);
|
|
if (!symbol) {
|
|
lg::debug("hover - no symbol");
|
|
return {};
|
|
}
|
|
// TODO - there is an issue with docstrings and overridden methods
|
|
const auto& symbol_info =
|
|
workspace.get_global_symbol_info(tracked_file.value(), symbol.value());
|
|
if (!symbol_info) {
|
|
lg::debug("hover - no symbol info - {}", symbol.value());
|
|
return {};
|
|
}
|
|
LSPSpec::MarkupContent markup;
|
|
markup.m_kind = "markdown";
|
|
|
|
const auto args =
|
|
Docs::get_args_from_docstring(symbol_info->args(), symbol_info->meta().docstring);
|
|
std::string signature = "";
|
|
bool takes_args = true;
|
|
if (symbol_info->kind() == SymbolInfo::Kind::FUNCTION) {
|
|
signature += "function ";
|
|
} else if (symbol_info->kind() == SymbolInfo::Kind::METHOD) {
|
|
signature += "method ";
|
|
} else if (symbol_info->kind() == SymbolInfo::Kind::MACRO) {
|
|
signature += "macro ";
|
|
} else {
|
|
takes_args = false;
|
|
}
|
|
// TODO - others useful, probably states?
|
|
signature += symbol.value();
|
|
if (takes_args) {
|
|
signature += "(";
|
|
for (int i = 0; i < (int)args.size(); i++) {
|
|
const auto& arg = args.at(i);
|
|
if (i == (int)args.size() - 1) {
|
|
signature += fmt::format("{}: {}", arg.name, arg.type);
|
|
} else {
|
|
signature += fmt::format("{}: {}, ", arg.name, arg.type);
|
|
}
|
|
}
|
|
signature += ")";
|
|
if (symbol_info->kind() == SymbolInfo::Kind::FUNCTION &&
|
|
workspace.get_symbol_typespec(tracked_file.value(), symbol.value())) {
|
|
signature +=
|
|
fmt::format(": {}", workspace.get_symbol_typespec(tracked_file.value(), symbol.value())
|
|
->last_arg()
|
|
.base_type());
|
|
} else if (symbol_info->kind() == SymbolInfo::Kind::METHOD) {
|
|
signature += fmt::format(": {}", symbol_info->method_info().type.last_arg().base_type());
|
|
}
|
|
} else if (workspace.get_symbol_typespec(tracked_file.value(), symbol.value())) {
|
|
signature += fmt::format(
|
|
": {}", workspace.get_symbol_typespec(tracked_file.value(), symbol.value())->base_type());
|
|
}
|
|
|
|
std::string body = fmt::format("```opengoal\n{}\n```\n\n", signature);
|
|
body += "___\n\n";
|
|
if (!symbol_info->meta().docstring.empty()) {
|
|
body += truncate_docstring(symbol_info->meta().docstring) + "\n\n";
|
|
}
|
|
|
|
// TODO - support @see/@returns/[[reference]]
|
|
for (const auto& arg : args) {
|
|
std::string param_line = "";
|
|
if (arg.is_mutated) {
|
|
param_line += fmt::format("*@param!* `{}: {}`", arg.name, arg.type);
|
|
} else if (arg.is_optional) {
|
|
param_line += fmt::format("*@param?* `{}: {}`", arg.name, arg.type);
|
|
} else if (arg.is_unused) {
|
|
param_line += fmt::format("*@param_* `{}: {}`", arg.name, arg.type);
|
|
} else {
|
|
param_line += fmt::format("*@param* `{}: {}`", arg.name, arg.type);
|
|
}
|
|
if (!arg.description.empty()) {
|
|
param_line += fmt::format(" - {}\n\n", arg.description);
|
|
} else {
|
|
param_line += "\n\n";
|
|
}
|
|
body += param_line;
|
|
}
|
|
|
|
markup.m_value = body;
|
|
LSPSpec::Hover hover_resp;
|
|
hover_resp.m_contents = markup;
|
|
return hover_resp;
|
|
}
|
|
|
|
return {};
|
|
}
|