Unified completions into a function, small snippet improvement

This commit is contained in:
Alexandros Naskos 2020-05-13 20:30:57 +03:00
parent 20afe9ef3c
commit 0e0789b2d5
2 changed files with 57 additions and 74 deletions

View File

@ -115,8 +115,13 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
var curr_tok = param_decl.type_node.firstToken(); var curr_tok = param_decl.type_node.firstToken();
var end_tok = param_decl.type_node.lastToken(); var end_tok = param_decl.type_node.lastToken();
while (curr_tok <= end_tok) : (curr_tok += 1) { while (curr_tok <= end_tok) : (curr_tok += 1) {
const id = tree.tokens.at(curr_tok).id;
const is_comma = tree.tokens.at(curr_tok).id == .Comma;
if (curr_tok == end_tok and is_comma) continue;
try buffer.appendSlice(tree.tokenSlice(curr_tok)); try buffer.appendSlice(tree.tokenSlice(curr_tok));
if (tree.tokens.at(curr_tok).id == .Comma) try buffer.append(' '); if (is_comma or id == .Keyword_const) try buffer.append(' ');
} }
try buffer.append('}'); try buffer.append('}');

View File

@ -221,6 +221,53 @@ fn publishDiagnostics(document: *types.TextDocument, config: Config) !void {
}); });
} }
fn nodeToCompletion(alloc: *std.mem.Allocator, tree: *std.zig.ast.Tree, decl: *std.zig.ast.Node, config: Config) !?types.CompletionItem {
var doc_comments = try analysis.getDocComments(alloc, tree, decl);
var doc = types.MarkupContent{
.kind = .Markdown,
.value = doc_comments orelse "",
};
switch (decl.id) {
.FnProto => {
const func = decl.cast(std.zig.ast.Node.FnProto).?;
if (func.name_token) |name_token| {
const insert_text = if (config.enable_snippets)
try analysis.getFunctionSnippet(alloc, tree, func)
else
null;
return types.CompletionItem{
.label = tree.tokenSlice(name_token),
.kind = .Function,
.documentation = doc,
.detail = analysis.getFunctionSignature(tree, func),
.insertText = insert_text,
.insertTextFormat = if (config.enable_snippets) .Snippet else .PlainText,
};
}
},
.VarDecl => {
const var_decl = decl.cast(std.zig.ast.Node.VarDecl).?;
return types.CompletionItem{
.label = tree.tokenSlice(var_decl.name_token),
.kind = .Variable,
.documentation = doc,
.detail = analysis.getVariableSignature(tree, var_decl),
};
},
else => if (analysis.nodeToString(tree, decl)) |string| {
return types.CompletionItem{
.label = string,
.kind = .Field,
.documentation = doc,
};
}
}
return null;
}
fn completeGlobal(id: i64, document: *types.TextDocument, config: Config) !void { fn completeGlobal(id: i64, document: *types.TextDocument, config: Config) !void {
// The tree uses its own arena, so we just pass our main allocator. // The tree uses its own arena, so we just pass our main allocator.
var tree = try std.zig.parse(allocator, document.text); var tree = try std.zig.parse(allocator, document.text);
@ -244,47 +291,9 @@ fn completeGlobal(id: i64, document: *types.TextDocument, config: Config) !void
var decls = tree.root_node.decls.iterator(0); var decls = tree.root_node.decls.iterator(0);
while (decls.next()) |decl_ptr| { while (decls.next()) |decl_ptr| {
var decl = decl_ptr.*; var decl = decl_ptr.*;
switch (decl.id) { if (try nodeToCompletion(&arena.allocator, tree, decl, config)) |completion| {
.FnProto => { try completions.append(completion);
const func = decl.cast(std.zig.ast.Node.FnProto).?;
if (func.name_token) |name_token| {
const insert_text = if (config.enable_snippets)
try analysis.getFunctionSnippet(&arena.allocator, tree, func)
else
null;
var doc_comments = try analysis.getDocComments(&arena.allocator, tree, decl);
var doc = types.MarkupContent{
.kind = .Markdown,
.value = doc_comments orelse "",
};
try completions.append(.{
.label = tree.tokenSlice(name_token),
.kind = .Function,
.documentation = doc,
.detail = analysis.getFunctionSignature(tree, func),
.insertText = insert_text,
.insertTextFormat = if (config.enable_snippets) .Snippet else .PlainText,
});
}
},
.VarDecl => {
const var_decl = decl.cast(std.zig.ast.Node.VarDecl).?;
var doc_comments = try analysis.getDocComments(&arena.allocator, tree, decl);
var doc = types.MarkupContent{
.kind = .Markdown,
.value = doc_comments orelse "",
};
try completions.append(.{
.label = tree.tokenSlice(var_decl.name_token),
.kind = .Variable,
.documentation = doc,
.detail = analysis.getVariableSignature(tree, var_decl),
});
},
else => {}
} }
} }
try send(types.Response{ try send(types.Response{
@ -315,40 +324,9 @@ fn completeFieldAccess(id: i64, document: *types.TextDocument, position: types.P
if (analysis.getNodeFromTokens(tree, &tree.root_node.base, &tokenizer)) |node| { if (analysis.getNodeFromTokens(tree, &tree.root_node.base, &tokenizer)) |node| {
var index: usize = 0; var index: usize = 0;
while (node.iterate(index)) |child_node| { while (node.iterate(index)) |child_node| {
if (analysis.nodeToString(tree, child_node)) |string| { if (try nodeToCompletion(&arena.allocator, tree, child_node, config)) |completion| {
var doc_comments = try analysis.getDocComments(&arena.allocator, tree, child_node); try completions.append(completion);
var doc = types.MarkupContent{
.kind = .Markdown,
.value = doc_comments orelse "",
};
switch (child_node.id) {
.FnProto => {
const func = child_node.cast(std.zig.ast.Node.FnProto).?;
const insert_text = if (config.enable_snippets)
try analysis.getFunctionSnippet(&arena.allocator, tree, func)
else
null;
try completions.append(.{
.label = string,
.kind = .Function,
.documentation = doc,
.detail = analysis.getFunctionSignature(tree, func),
.insertText = insert_text,
.insertTextFormat = if (config.enable_snippets) .Snippet else .PlainText,
});
},
else => {
try completions.append(.{
.label = string,
.kind = .Field,
.documentation = doc
});
}
}
} }
index += 1; index += 1;
} }
} }