Skip to content

Commit

Permalink
feat: builtin functions completion
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Dec 18, 2023
1 parent ab39036 commit b2c9360
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,54 @@ function autoCompletionItems(): vscode.CompletionItem[] {
{label: 'Undefined', text: 'Undefined'},
];

const BuiltinFunctionsCompletions: {label: string, text: string, doc: string}[] = [
{label: "option(key: str)", text: "option(${1:key})", doc: "Return the top level argument by the key"},
{label: "print()", text: "print(${1:value})", doc: "Prints the values to a stream, or to sys.stdout by default.\n\nOptional keyword arguments:\n\nsep: string inserted between values, default a space.\n\nend: string appended after the last value, default a newline."},
{label: "multiplyof(a: int, b: int)", text: "multiplyof(${1:a}, ${2:b})", doc: "Check if the modular result of a and b is 0."},
{label: "isunique(inval: [])", text: "isunique(${1:inval})", doc: "Check if a list has duplicated elements"},
{label: "len(inval: [])", text: "len(${1:inval})", doc: "Return the length of a value."},
{label: "abs(inval: any)", text: "abs(${1:inval})", doc: "Return the absolute value of the argument."},
{label: "all_true(inval: [])", text: "all_true(${1:inval})", doc: "Return True if bool(x) is True for all values x in the iterable.\n\nIf the iterable is empty, return True."},
{label: "any_true(inval: [])", text: "any_true(${1:inval})", doc: "Return True if bool(x) is True for any x in the iterable.\n\nIf the iterable is empty, return False."},
{label: "hex(number: int)", text: "hex(${1:number})", doc: "Return the hexadecimal representation of an integer."},
{label: "bin(number: int)", text: "bin(${1:number})", doc: "Return the binary representation of an integer."},
{label: "oct(number: int)", text: "oct(${1:number})", doc: "Return the octal representation of an integer."},
{label: "ord(c: str)", text: "ord(${1:c})", doc: "Return the Unicode code point for a one-character string."},
{label: "sorted(inval: str|[]|{:}, reverse: bool)", text: "sorted(${1:inval}, ${2:reverse})", doc: "Return a new list containing all items from the iterable in ascending order.\n\nA custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order."},
{label: "range(start: int, stop: int, step: int)", text: "range(${1:start}, ${2:stop}, ${3:step})", doc: "Return the range of a value."},
{label: "max()", text: "max()", doc: "With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument."},
{label: "min()", text: "min()", doc: "With a single iterable argument, return its smallest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the smallest argument."},
{label: "sum(iterable: [], start: any)", text: "sum(${1:iterable}, ${2: start})", doc: "When the iterable is empty, return the start value. This function is intended specifically for use with numeric values and may reject non-numeric types."},
{label: "pow(x: int|float, y: int|float, z: int|float)", text: "pow(${1:x}, ${2: y}, ${3: z})", doc: "Equivalent to x**y (with two arguments) or x**y % z (with three arguments).\n\nSome types, such as ints, are able to use a more efficient algorithm when invoked using the three argument form."},
{label: "round(number: int|float, ndigits: int)", text: "round(${1:number}, ${2:ndigits})", doc: "Round a number to a given precision in decimal digits.\n\nThe return value is an integer if ndigits is omitted or None.Otherwise the return value has the same type as the number. ndigits may be negative."},
{label: "zip()", text: "zip()", doc: "Return a zip object whose next method returns a tuple where the i-th element comes from the i-th iterable argument."},
{label: "int(number: any, base: int)", text: "int(${1:number}, ${2:base})", doc: "Convert a number or string to an integer, or return 0 if no arguments are given. For floating point numbers, this truncates towards zero."},
{label: "float(number: any)", text: "float(${1:number})", doc: "Convert a string or number to a floating point number, if possible."},
{label: "bool(x: any)", text: "bool(${1:x})", doc: "Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed."},
{label: "str(x: any)", text: "str(${1:x})", doc: "Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler."},
{label: "list(x: any)", text: "list(${1:number})", doc: "Built-in mutable sequence. If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified."},
{label: "dict(x: any)", text: "dict(${1:number})", doc: "Built-in mutable dict."},
{label: "typeof(x: any, full_name: bool)", text: "typeof(${1:x}, ${2: full_name})", doc: "Return the type of the object"},
];

return [
...snippetCompletions.map(({label, text}) => createSnippetCompletion(label, vscode.CompletionItemKind.Snippet, text)),
...KeywordCompletions.map(({label, text}) => createSnippetCompletion(label, vscode.CompletionItemKind.Keyword, text)),
...snippetCompletions.map(({label, text}) => createSnippetCompletion(label, vscode.CompletionItemKind.Snippet, text, "")),
...KeywordCompletions.map(({label, text}) => createSnippetCompletion(label, vscode.CompletionItemKind.Keyword, text, "")),
...BuiltinFunctionsCompletions.map(({label, text, doc}) => createSnippetCompletion(label, vscode.CompletionItemKind.Function, text, doc)),
];
}

function createSnippetCompletion(label: string, kind: vscode.CompletionItemKind, insertText: string): vscode.CompletionItem {
function createSnippetCompletion(label: string, kind: vscode.CompletionItemKind, insertText: string, doc: string): vscode.CompletionItem {
const completion = new vscode.CompletionItem(label);
completion.kind = kind;
switch (kind) {
case vscode.CompletionItemKind.Snippet:
completion.insertText = new vscode.SnippetString(insertText);
return completion;
case vscode.CompletionItemKind.Function:
completion.insertText = new vscode.SnippetString(insertText);
completion.documentation = new vscode.MarkdownString(doc);
return completion;
default:
completion.insertText = insertText;
return completion;
Expand Down

0 comments on commit b2c9360

Please sign in to comment.