Better builtin completion

This commit is contained in:
SuperAuguste 2020-04-27 22:59:28 -04:00
parent ce443ba1dd
commit def89a90f5
3 changed files with 166 additions and 110 deletions

View File

@ -1,105 +1,141 @@
// You can use `[...$("#toc-Builtin-Functions").parentElement.lastElementChild.children].map(_ => `"${_.innerText}"`).join(",\n")` to get all of the builtin functions on the documentation page. // SCRIPT
// [...document.querySelector("#toc-Builtin-Functions").parentElement.lastElementChild.children].map(_ => {
// const code = document.querySelector("#" + _.innerText.slice(1)).nextElementSibling.children[0].innerText;
// var l = (code.lastIndexOf(") ") == -1 ? code.length : code.lastIndexOf(") ")) + 1
// var p = code.slice(0, l);
// var name = p.slice(0, p.indexOf("("));
// var body = p.slice(p.indexOf("(") + 1, -1);
// if (body.len === 0) return `${name}()`;
// var nb = "";
// let depth = 0;
// let vi = 2;
// let i = 0;
// let skip = false;
// for (const c of body) {
// if (skip) {
// skip = false;
// if (c === " ") {i++; continue;}
// }
// if (c === "(") depth++;
// else if (c === ")") depth--;
// if (c === "," && depth == 0) {
// nb += `}, \${${vi}:`;
// vi++;
// skip = true;
// } else if (i === body.length - 1) {
// nb += c;
// nb += "}";
// } else nb += c;
// i++;
// }
// return `${name}(\${1:${nb})`;
// }).map(_ => JSON.stringify(_)).join(",\n");
/// Builtin functions /// Builtin functions
pub const builtins = [_][]const u8{ pub const builtins = [_][]const u8{
"@addWithOverflow", "@addWithOverflow(${1:comptime T: type}, ${2:a: T}, ${3:b: T}, ${4:result: *T})",
"@alignCast", "@alignCast(${1:comptime alignment: u29}, ${2:ptr: var})",
"@alignOf", "@alignOf(${1:comptime T: type})",
"@as", "@as(${1:comptime T: type}, ${2:expression})",
"@asyncCall", "@asyncCall(${1:frame_buffer: []align(@alignOf(@Frame(anyAsyncFunction))) u8}, ${2:result_ptr}, ${3:function_ptr}, ${4:args: ...})",
"@atomicLoad", "@atomicLoad(${1:comptime T: type}, ${2:ptr: *const T}, ${3:comptime ordering: builtin.AtomicOrder})",
"@atomicRmw", "@atomicRmw(${1:comptime T: type}, ${2:ptr: *T}, ${3:comptime op: builtin.AtomicRmwOp}, ${4:operand: T}, ${5:comptime ordering: builtin.AtomicOrder})",
"@atomicStore", "@atomicStore(${1:comptime T: type}, ${2:ptr: *T}, ${3:value: T}, ${4:comptime ordering: builtin.AtomicOrder})",
"@bitCast", "@bitCast(${1:comptime DestType: type}, ${2:value: var})",
"@bitOffsetOf", "@bitOffsetOf(${1:comptime T: type}, ${2:comptime field_name: []const u8})",
"@boolToInt", "@boolToInt(${1:value: bool})",
"@bitSizeOf", "@bitSizeOf(${1:comptime T: type})",
"@breakpoint", "@breakpoint(${1:)",
"@mulAdd", "@mulAdd(${1:comptime T: type}, ${2:a: T}, ${3:b: T}, ${4:c: T})",
"@byteSwap", "@byteSwap(${1:comptime T: type}, ${2:operand: T})",
"@bitReverse", "@bitReverse(${1:comptime T: type}, ${2:integer: T})",
"@byteOffsetOf", "@byteOffsetOf(${1:comptime T: type}, ${2:comptime field_name: []const u8})",
"@call", "@call(${1:options: std.builtin.CallOptions}, ${2:function: var}, ${3:args: var})",
"@cDefine", "@cDefine(${1:comptime name: []u8}, ${2:value})",
"@cImport", "@cImport(${1:expression})",
"@cInclude", "@cInclude(${1:comptime path: []u8})",
"@clz", "@clz(${1:comptime T: type}, ${2:integer: T})",
"@cmpxchgStrong", "@cmpxchgStrong(${1:comptime T: type}, ${2:ptr: *T}, ${3:expected_value: T}, ${4:new_value: T}, ${5:success_order: AtomicOrder}, ${6:fail_order: AtomicOrder})",
"@cmpxchgWeak", "@cmpxchgWeak(${1:comptime T: type}, ${2:ptr: *T}, ${3:expected_value: T}, ${4:new_value: T}, ${5:success_order: AtomicOrder}, ${6:fail_order: AtomicOrder})",
"@compileError", "@compileError(${1:comptime msg: []u8})",
"@compileLog", "@compileLog(${1:args: ...})",
"@ctz", "@ctz(${1:comptime T: type}, ${2:integer: T})",
"@cUndef", "@cUndef(${1:comptime name: []u8})",
"@divExact", "@divExact(${1:numerator: T}, ${2:denominator: T})",
"@divFloor", "@divFloor(${1:numerator: T}, ${2:denominator: T})",
"@divTrunc", "@divTrunc(${1:numerator: T}, ${2:denominator: T})",
"@embedFile", "@embedFile(${1:comptime path: []const u8})",
"@enumToInt", "@enumToInt(${1:enum_or_tagged_union: var})",
"@errorName", "@errorName(${1:err: anyerror})",
"@errorReturnTrace", "@errorReturnTrace(${1:)",
"@errorToInt", "@errorToInt(${1:err: var) std.meta.IntType(false}, ${2:@sizeOf(anyerror})",
"@errSetCast", "@errSetCast(${1:comptime T: DestType}, ${2:value: var})",
"@export", "@export(${1:target: var}, ${2:comptime options: std.builtin.ExportOptions})",
"@fence", "@fence(${1:order: AtomicOrder})",
"@field", "@field(${1:lhs: var}, ${2:comptime field_name: []const u8})",
"@fieldParentPtr", "@fieldParentPtr(${1:comptime ParentType: type}, ${2:comptime field_name: []const u8}, ${3:\n field_ptr: *T})",
"@floatCast", "@floatCast(${1:comptime DestType: type}, ${2:value: var})",
"@floatToInt", "@floatToInt(${1:comptime DestType: type}, ${2:float: var})",
"@frame", "@frame(${1:)",
"@Frame", "@Frame(${1:func: var})",
"@frameAddress", "@frameAddress(${1:)",
"@frameSize", "@frameSize(${1:)",
"@hasDecl", "@hasDecl(${1:comptime Container: type}, ${2:comptime name: []const u8})",
"@hasField", "@hasField(${1:comptime Container: type}, ${2:comptime name: []const u8})",
"@import", "@import(${1:comptime path: []u8})",
"@intCast", "@intCast(${1:comptime DestType: type}, ${2:int: var})",
"@intToEnum", "@intToEnum(${1:comptime DestType: type}, ${2:int_value: @TagType(DestType)})",
"@intToError", "@intToError(${1:value: std.meta.IntType(false, @sizeOf(anyerror) * 8)})",
"@intToFloat", "@intToFloat(${1:comptime DestType: type}, ${2:int: var})",
"@intToPtr", "@intToPtr(${1:comptime DestType: type}, ${2:address: usize})",
"@memcpy", "@memcpy(${1:noalias dest: [*]u8}, ${2:noalias source: [*]const u8}, ${3:byte_count: usize})",
"@memset", "@memset(${1:dest: [*]u8}, ${2:c: u8}, ${3:byte_count: usize})",
"@mod", "@mod(${1:numerator: T}, ${2:denominator: T})",
"@mulWithOverflow", "@mulWithOverflow(${1:comptime T: type}, ${2:a: T}, ${3:b: T}, ${4:result: *T})",
"@OpaqueType", "@OpaqueType(${1:)",
"@panic", "@panic(${1:message: []const u8})",
"@popCount", "@popCount(${1:comptime T: type}, ${2:integer: T})",
"@ptrCast", "@ptrCast(${1:comptime DestType: type}, ${2:value: var})",
"@ptrToInt", "@ptrToInt(${1:value: var})",
"@rem", "@rem(${1:numerator: T}, ${2:denominator: T})",
"@returnAddress", "@returnAddress(${1:)",
"@setAlignStack", "@setAlignStack(${1:comptime alignment: u29})",
"@setCold", "@setCold(${1:is_cold: bool})",
"@setEvalBranchQuota", "@setEvalBranchQuota(${1:new_quota: usize})",
"@setFloatMode", "@setFloatMode(${1:mode: @import(\"builtin\").FloatMode})",
"@setRuntimeSafety", "@setRuntimeSafety(${1:safety_on: bool})",
"@shlExact", "@shlExact(${1:value: T}, ${2:shift_amt: Log2T})",
"@shlWithOverflow", "@shlWithOverflow(${1:comptime T: type}, ${2:a: T}, ${3:shift_amt: Log2T}, ${4:result: *T})",
"@shrExact", "@shrExact(${1:value: T}, ${2:shift_amt: Log2T})",
"@shuffle", "@shuffle(${1:comptime E: type}, ${2:a: @Vector(a_len, E)}, ${3:b: @Vector(b_len, E)}, ${4:comptime mask: @Vector(mask_len, i32)})",
"@sizeOf", "@sizeOf(${1:comptime T: type})",
"@splat", "@splat(${1:comptime len: u32}, ${2:scalar: var})",
"@sqrt", "@sqrt(${1:value: var})",
"@sin", "@sin(${1:value: var})",
"@cos", "@cos(${1:value: var})",
"@exp", "@exp(${1:value: var})",
"@exp2", "@exp2(${1:value: var})",
"@log", "@log(${1:value: var})",
"@log2", "@log2(${1:value: var})",
"@log10", "@log10(${1:value: var})",
"@fabs", "@fabs(${1:value: var})",
"@floor", "@floor(${1:value: var})",
"@ceil", "@ceil(${1:value: var})",
"@trunc", "@trunc(${1:value: var})",
"@round", "@round(${1:value: var})",
"@subWithOverflow", "@subWithOverflow(${1:comptime T: type}, ${2:a: T}, ${3:b: T}, ${4:result: *T})",
"@tagName", "@tagName(${1:value: var})",
"@TagType", "@TagType(${1:T: type})",
"@This", "@This(${1:)",
"@truncate", "@truncate(${1:comptime T: type}, ${2:integer: var})",
"@Type", "@Type(${1:comptime info: @import(\"builtin\").TypeInfo})",
"@typeInfo", "@typeInfo(${1:comptime T: type})",
"@typeName", "@typeName(${1:T: type})",
"@TypeOf", "@TypeOf(${1:...})",
"@unionInit", "@unionInit(${1:comptime Union: type}, ${2:comptime active_field_name: []const u8}, ${3:init_expr})",
"@Vector" "@Vector(${1:comptime len: u32}, ${2:comptime ElemType: type})"
}; };

View File

@ -260,16 +260,20 @@ pub fn processJsonRpc(json: []const u8) !void {
var builtin_completions: [data.builtins.len]types.CompletionItem = undefined; var builtin_completions: [data.builtins.len]types.CompletionItem = undefined;
for (data.builtins) |builtin, i| { for (data.builtins) |builtin, i| {
var cutoff = std.mem.indexOf(u8, builtin, "(") orelse builtin.len;
builtin_completions[i] = types.CompletionItem{ builtin_completions[i] = types.CompletionItem{
.label = builtin, .label = builtin[0..cutoff],
.kind = types.CompletionItemKind.Function, .kind = types.CompletionItemKind.Function,
.textEdit = types.TextEdit{ // .textEdit = types.TextEdit{
.range = types.Range{ // .range = types.Range{
.start = pos, // .start = pos,
.end = pos, // .end = pos,
}, // },
.newText = builtin, // .newText = builtin,
}, // },
.filterText = builtin[1..cutoff],
.insertText = builtin[1..],
.insertTextFormat = types.InsertTextFormat.Snippet
}; };
} }

View File

@ -220,10 +220,26 @@ pub const CompletionItemKind = enum(Integer) {
} }
}; };
pub const InsertTextFormat = enum(Integer) {
PlainText = 1,
Snippet = 2,
pub fn jsonStringify(
value: InsertTextFormat,
options: json.StringifyOptions,
out_stream: var,
) !void {
try json.stringify(@enumToInt(value), options, out_stream);
}
};
pub const CompletionItem = struct { pub const CompletionItem = struct {
label: String, label: String,
kind: CompletionItemKind, kind: CompletionItemKind,
textEdit: ?TextEdit = null, textEdit: ?TextEdit = null,
filterText: ?String = null,
insertText: ?String = null,
insertTextFormat: ?InsertTextFormat = InsertTextFormat.PlainText
// filterText: String = .NotDefined, // filterText: String = .NotDefined,
}; };