Skip to content

Commit

Permalink
improve handling of delegates and anon f-calls
Browse files Browse the repository at this point in the history
Changes to signature help engine, symbol extractor, and symbol
representer to handle anonymous function calls and calls to
delegate-typed variables.
  • Loading branch information
Prince781 committed Sep 24, 2020
1 parent 83536db commit e0412a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
26 changes: 22 additions & 4 deletions src/codehelp/codehelp.vala
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ namespace Vls.CodeHelp {
builder.append ("async ");
}

if (callable_sym is Vala.Delegate)
builder.append ("delegate ");
else if (callable_sym is Vala.Signal) {
if (callable_sym is Vala.Delegate) {
if (override_name == null)
builder.append ("delegate ");
} else if (callable_sym is Vala.Signal) {
if (((Vala.Signal)callable_sym).is_virtual && instance_type == null)
builder.append ("virtual ");
builder.append ("signal ");
Expand Down Expand Up @@ -261,6 +262,13 @@ namespace Vls.CodeHelp {
type_parameters = ((Vala.Method)callable_sym).get_type_parameters ();

if (type_parameters != null && !type_parameters.is_empty) {
Vala.List<Vala.DataType>? delegate_type_arguments = null;
if (instance_type is Vala.DelegateType) {
var delegate_type = (Vala.DelegateType) instance_type;
if (delegate_type.delegate_symbol == callable_sym)
delegate_type_arguments = delegate_type.get_type_arguments ();
}

int i = 1;
builder.append_c ('<');
foreach (var type_parameter in type_parameters) {
Expand All @@ -272,6 +280,8 @@ namespace Vls.CodeHelp {
((Vala.Method)callable_sym).get_type_parameter_index (type_parameter.name);
if (method_type_arguments != null && idx < method_type_arguments.size) {
builder.append (get_data_type_representation (method_type_arguments[idx], scope));
} else if (delegate_type_arguments != null && idx < delegate_type_arguments.size) {
builder.append (get_data_type_representation (delegate_type_arguments[idx], scope));
} else {
builder.append (type_parameter.name);
}
Expand Down Expand Up @@ -603,6 +613,9 @@ namespace Vls.CodeHelp {

// just a datatype
if (sym == null) {
if (data_type is Vala.DelegateType)
return get_callable_representation (data_type, method_type_arguments,
((Vala.DelegateType)data_type).delegate_symbol, scope, true);
return get_data_type_representation (data_type, scope);
}

Expand All @@ -614,8 +627,13 @@ namespace Vls.CodeHelp {
if (sym is Vala.Parameter && ((Vala.Parameter)sym).ellipsis)
return "...";

if (sym is Vala.Variable)
if (sym is Vala.Variable) {
if (data_type is Vala.DelegateType && ((Vala.Variable)sym).variable_type.equals (data_type)) {
return get_callable_representation (data_type, method_type_arguments,
((Vala.DelegateType)data_type).delegate_symbol, scope, true, sym.name);
}
return get_variable_representation (data_type, method_type_arguments, (Vala.Variable)sym, scope, override_name, show_initializers);
}

if (sym is Vala.Property)
return get_property_representation (data_type, method_type_arguments, (Vala.Property)sym, scope, show_initializers);
Expand Down
7 changes: 1 addition & 6 deletions src/codehelp/signaturehelpengine.vala
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace Vls.SignatureHelpEngine {
// now make data_type refer to the parent expression's type (if it exists)
// note: if this is a call like `this(...)` or `base(...)`, then the data_type
// will already be the parent type of the implied default constructor
if (!(data_type is Vala.ObjectType || data_type is Vala.StructValueType)) {
if (!(data_type is Vala.ObjectType || data_type is Vala.StructValueType || data_type is Vala.DelegateType)) {
data_type = null;
if (mc.call is Vala.MemberAccess && ((Vala.MemberAccess)mc.call).inner != null)
data_type = ((Vala.MemberAccess)mc.call).inner.value_type;
Expand Down Expand Up @@ -267,11 +267,6 @@ namespace Vls.SignatureHelpEngine {
}

void finish (Jsonrpc.Client client, Variant id, Collection<SignatureInformation> signatures, int active_param) {
var json_array = new Json.Array ();

foreach (var sinfo in signatures)
json_array.add_element (Json.gobject_serialize (sinfo));

try {
// debug ("sending with active_param = %d", active_param);
client.reply (id, Util.object_to_variant (new SignatureHelp () {
Expand Down
48 changes: 19 additions & 29 deletions src/codehelp/symbolextractor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ class Vls.SymbolExtractor : Object {

class FakeMethodCall : FakeExpr {
public int arguments_count { get; private set; }
public FakeMemberAccess member_access {
get { return (FakeMemberAccess) inner; }
}
public FakeExpr call { get { return inner; } }

public FakeMethodCall (int arguments_count, FakeMemberAccess member_access) {
base (member_access);
public FakeMethodCall (int arguments_count, FakeExpr call) {
base (call);
this.arguments_count = arguments_count;
}

public override string to_string () {
return @"$member_access ([$arguments_count arg(s)])";
return @"$call ([$arguments_count arg(s)])";
}
}

Expand Down Expand Up @@ -736,34 +734,22 @@ class Vls.SymbolExtractor : Object {
FakeExpr? expr = parse_fake_member_access_expr ();
// debug ("after parsing member access, char at idx is %c", source_file.content[idx]);

if (expr == null && have_tuple) {
if (method_arguments.size != 1) {
// invalid expression (<expr1>, ...)
// debug ("invalid expression (<expr1>, ...)");
return null;
}
if (!(method_arguments.first () is FakeEmptyExpr))
// expression wrapped in parentheses
return method_arguments.first ();
}

if (expr == null && (expr = parse_literal ()) != null)
if (!have_tuple && expr == null && (expr = parse_literal ()) != null)
return expr;

if (expr == null) {
// debug ("expr is null and have_tuple = %s", have_tuple.to_string ());
// try parsing array access
if (parse_expr_tuple (!at_member_access && accept_incomplete_method_call, method_arguments, '[', ']')) {
skip_whitespace ();
expr = parse_fake_expr (/* oce_allowed = false */);
if (expr != null)
return new FakeMethodCall (method_arguments.size, new FakeMemberAccess ("get", null, expr));
// try parsing array access
var array_arguments = new ArrayList<FakeExpr> ();
if (expr == null && parse_expr_tuple (!at_member_access && accept_incomplete_method_call, array_arguments, '[', ']')) {
skip_whitespace ();
var array_expr = parse_fake_expr (/* oce_allowed = false */);
if (array_expr != null) {
expr = new FakeMethodCall (array_arguments.size, new FakeMemberAccess ("get", null, array_expr));
oce_allowed = false;
}
return null;
}

if (have_tuple)
expr = new FakeMethodCall (method_arguments.size, (FakeMemberAccess)expr);
if (have_tuple && expr != null)
expr = new FakeMethodCall (method_arguments.size, expr);

if (oce_allowed) {
skip_whitespace ();
Expand All @@ -775,6 +761,10 @@ class Vls.SymbolExtractor : Object {
}
}

if (expr == null && have_tuple && method_arguments.size == 1 && !(method_arguments.first () is FakeEmptyExpr))
// expression wrapped in parentheses
return method_arguments.first ();

return expr;
}
}
Expand Down

0 comments on commit e0412a3

Please sign in to comment.