Skip to content

Commit

Permalink
completion: handle arrays (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince781 committed Apr 28, 2020
1 parent ed15f01 commit f9d2959
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
29 changes: 27 additions & 2 deletions src/codehelp/completionengine.vala
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ namespace Vls.CompletionEngine {
bool retry_inner = true) {
string method = "textDocument/completion";
Vala.CodeNode? peeled = null;
Vala.DataType? data_type = null;
Vala.Scope current_scope = scope ?? get_scope_containing_node (result);

// debug (@"[$method] member: got best, $(result.type_name) `$result' (semanalyzed = $(result.checked)))");
Expand Down Expand Up @@ -561,12 +562,14 @@ namespace Vls.CompletionEngine {

bool is_instance = true;
Vala.TypeSymbol? type_sym = Server.get_type_symbol (compilation.code_context,
result, is_pointer_access, ref is_instance);
result, is_pointer_access, ref is_instance,
ref data_type);

// try again
if (type_sym == null && peeled != null)
type_sym = Server.get_type_symbol (compilation.code_context,
peeled, is_pointer_access, ref is_instance);
peeled, is_pointer_access, ref is_instance,
ref data_type);

if (type_sym != null)
add_completions_for_type (lang_serv, project, type_sym, completions, current_scope, is_instance, in_oce);
Expand All @@ -577,6 +580,8 @@ namespace Vls.CompletionEngine {
add_completions_for_ns (lang_serv, project, (Vala.Namespace) peeled, completions, in_oce);
else if (peeled is Vala.Method && ((Vala.Method) peeled).coroutine)
add_completions_for_async_method ((Vala.Method) peeled, completions);
else if (data_type is Vala.ArrayType)
add_completions_for_array_type ((Vala.ArrayType) data_type, completions);
else {
if (result is Vala.MemberAccess &&
((Vala.MemberAccess)result).inner != null &&
Expand Down Expand Up @@ -899,6 +904,26 @@ namespace Vls.CompletionEngine {
});
}

/**
* Use this to complete members of an array.
*/
void add_completions_for_array_type (Vala.ArrayType atype, Set<CompletionItem> completions) {
var length_member = atype.get_member ("length");
if (length_member != null)
completions.add (new CompletionItem.from_symbol (
length_member,
CompletionItemKind.Property,
(atype.fixed_length && atype.length != null ?
new MarkupContent.plaintext(@"(= $(Server.get_expr_repr (atype.length)))") : null)));
foreach (string method_name in new string[] {"copy", "move", "resize"}) {
var method = atype.get_member (method_name);
if (method != null)
completions.add (new CompletionItem.from_symbol (
method,
CompletionItemKind.Method, null));
}
}

/**
* Use this to complete members of an async method.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/server.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,8 @@ class Vls.Server : Object {
public static Vala.TypeSymbol? get_type_symbol (Vala.CodeContext code_context,
Vala.CodeNode symbol,
bool is_pointer,
ref bool is_instance) {
Vala.DataType? data_type = null;
ref bool is_instance,
ref Vala.DataType? data_type) {
Vala.TypeSymbol? type_symbol = null;
if (symbol is Vala.Variable) {
var var_sym = symbol as Vala.Variable;
Expand Down Expand Up @@ -1454,8 +1454,10 @@ class Vls.Server : Object {
});
} else {
bool is_instance = true;
Vala.DataType? data_type = null; // XXX: currently unused
Vala.TypeSymbol? type_sym = Server.get_type_symbol (compilation.code_context,
result, false, ref is_instance);
result, false, ref is_instance,
ref data_type);
hoverInfo.contents.add (new MarkedString () {
language = "vala",
value = type_sym != null ? get_symbol_data_type (type_sym, true, null, true) :
Expand Down

0 comments on commit f9d2959

Please sign in to comment.