Skip to content
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

LSP: Improve function default arg representation #101086

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
LSP: Improve function default arg representation
  • Loading branch information
HolonProduction committed Jan 3, 2025
commit 513b0582277e4a1207dd46efdd9ab80a9555eec7
29 changes: 15 additions & 14 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "gdscript_editor.h"

#include "gdscript.h"

#include "gdscript_analyzer.h"
#include "gdscript_parser.h"
#include "gdscript_tokenizer.h"
#include "gdscript_utility_functions.h"

Expand Down Expand Up @@ -736,7 +737,7 @@ static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg, co
return Variant::get_type_name(p_info.type);
}

static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) {
String make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation) {
String arghint;
if (!p_is_annotation) {
arghint += _get_visual_datatype(p_info.return_val, false) + " ";
Expand Down Expand Up @@ -784,7 +785,7 @@ static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool
return arghint;
}

static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx, bool p_just_args = false) {
String make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx, bool p_just_args) {
String arghint;

if (p_just_args) {
Expand Down Expand Up @@ -889,7 +890,7 @@ static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap<St
}

static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
r_arghint = _make_arguments_hint(p_annotation->info->info, p_argument, true);
r_arghint = make_arguments_hint(p_annotation->info->info, p_argument, true);
if (p_annotation->name == SNAME("@export_range")) {
if (p_argument == 3 || p_argument == 4 || p_argument == 5) {
// Slider hint.
Expand Down Expand Up @@ -2777,7 +2778,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
const GDScriptParser::ClassNode::Member &member = current->get_member("_init");

if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
r_arghint = base_type.class_type->get_datatype().to_string() + " new" + _make_arguments_hint(member.function, p_argidx, true);
r_arghint = base_type.class_type->get_datatype().to_string() + " new" + make_arguments_hint(member.function, p_argidx, true);
return;
}
}
Expand All @@ -2792,7 +2793,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_method);

if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
r_arghint = _make_arguments_hint(member.function, p_argidx);
r_arghint = make_arguments_hint(member.function, p_argidx);
return;
}
}
Expand All @@ -2801,7 +2802,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
} break;
case GDScriptParser::DataType::SCRIPT: {
if (base_type.script_type->is_valid() && base_type.script_type->has_method(p_method)) {
r_arghint = _make_arguments_hint(base_type.script_type->get_method_info(p_method), p_argidx);
r_arghint = make_arguments_hint(base_type.script_type->get_method_info(p_method), p_argidx);
return;
}
Ref<Script> base_script = base_type.script_type->get_base_script();
Expand Down Expand Up @@ -2853,7 +2854,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
}
}

r_arghint = _make_arguments_hint(info, p_argidx);
r_arghint = make_arguments_hint(info, p_argidx);
}

if (p_argidx == 1 && p_context.node && p_context.node->type == GDScriptParser::Node::CALL && ClassDB::is_parent_class(class_name, SNAME("Tween")) && p_method == SNAME("tween_property")) {
Expand Down Expand Up @@ -2989,7 +2990,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
base.get_method_list(&methods);
for (const MethodInfo &E : methods) {
if (E.name == p_method) {
r_arghint = _make_arguments_hint(E, p_argidx);
r_arghint = make_arguments_hint(E, p_argidx);
return;
}
}
Expand Down Expand Up @@ -3091,7 +3092,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
}

MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
r_arghint = _make_arguments_hint(mi, p_argidx);
r_arghint = make_arguments_hint(mi, p_argidx);
return;
} else if (p_call->type != GDScriptParser::Node::CALL) {
return;
Expand Down Expand Up @@ -3125,7 +3126,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
continue;
}
if (E.name == call->function_name) {
r_arghint += _make_arguments_hint(E, p_argidx);
r_arghint += make_arguments_hint(E, p_argidx);
return;
}
}
Expand All @@ -3149,11 +3150,11 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
}
} else if (Variant::has_utility_function(call->function_name)) {
MethodInfo info = Variant::get_utility_function_info(call->function_name);
r_arghint = _make_arguments_hint(info, p_argidx);
r_arghint = make_arguments_hint(info, p_argidx);
return;
} else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
r_arghint = _make_arguments_hint(info, p_argidx);
r_arghint = make_arguments_hint(info, p_argidx);
return;
} else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
// Complete constructor.
Expand All @@ -3168,7 +3169,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c
if (i > 0) {
r_arghint += "\n";
}
r_arghint += _make_arguments_hint(E, p_argidx);
r_arghint += make_arguments_hint(E, p_argidx);
i++;
}
return;
Expand Down
41 changes: 41 additions & 0 deletions modules/gdscript/gdscript_editor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**************************************************************************/
/* gdscript_editor.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef GDSCRIPT_EDITOR_H
#define GDSCRIPT_EDITOR_H

#include "core/object/object.h"
#include "core/string/ustring.h"
#include "gdscript_parser.h"

String make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false);
String make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx, bool p_just_args = false);

#endif // GDSCRIPT_EDITOR_H
30 changes: 10 additions & 20 deletions modules/gdscript/language_server/gdscript_extend_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "../gdscript.h"
#include "../gdscript_analyzer.h"
#include "../gdscript_editor.h"
#include "editor/editor_settings.h"
#include "gdscript_language_protocol.h"
#include "gdscript_workspace.h"
Expand Down Expand Up @@ -482,7 +483,15 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
if (is_named) {
r_symbol.detail += " " + String(p_func->identifier->name);
}
r_symbol.detail += "(";
r_symbol.detail += make_arguments_hint(p_func, -1, true);
if (p_func->get_datatype().is_hard_type()) {
if (p_func->get_datatype().builtin_type == Variant::NIL) {
r_symbol.detail += " -> void";
} else {
r_symbol.detail += " -> " + p_func->get_datatype().to_string();
}
}

r_symbol.deprecated = false;
r_symbol.range = range_of_node(p_func);
if (is_named) {
Expand All @@ -494,25 +503,6 @@ void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionN
r_symbol.uri = uri;
r_symbol.script_path = path;

String parameters;
for (int i = 0; i < p_func->parameters.size(); i++) {
const ParameterNode *parameter = p_func->parameters[i];
if (i > 0) {
parameters += ", ";
}
parameters += String(parameter->identifier->name);
if (parameter->get_datatype().is_hard_type()) {
parameters += ": " + parameter->get_datatype().to_string();
}
if (parameter->initializer != nullptr) {
parameters += " = " + parameter->initializer->reduced_value.to_json_string();
}
}
r_symbol.detail += parameters + ")";
if (p_func->get_datatype().is_hard_type()) {
r_symbol.detail += " -> " + p_func->get_datatype().to_string();
}

List<GDScriptParser::SuiteNode *> function_nodes;

List<GDScriptParser::Node *> node_stack;
Expand Down