import * as monaco from 'monaco-editor'; const EXPR_KEYWORDS = ['let', 'true', 'false', 'nil', 'in', 'not', 'and', 'or', 'if', 'else']; const EXPR_FUNCTIONS: { name: string; signature: string; description: string; category: string }[] = [ // String functions { name: 'trim', signature: 'trim(str[, chars])', description: 'Removes white spaces from both ends of a string', category: 'string' }, { name: 'trimPrefix', signature: 'trimPrefix(str, prefix)', description: 'Removes the specified prefix from the string', category: 'string' }, { name: 'trimSuffix', signature: 'trimSuffix(str, suffix)', description: 'Removes the specified suffix from the string', category: 'string' }, { name: 'upper', signature: 'upper(str)', description: 'Converts all characters to uppercase', category: 'string' }, { name: 'lower', signature: 'lower(str)', description: 'Converts all characters to lowercase', category: 'string' }, { name: 'split', signature: 'split(str, delimiter[, n])', description: 'Splits a string at each instance of the delimiter', category: 'string' }, { name: 'splitAfter', signature: 'splitAfter(str, delimiter[, n])', description: 'Splits a string after each instance of the delimiter', category: 'string' }, { name: 'replace', signature: 'replace(str, old, new)', description: 'Replaces all occurrences of old with new', category: 'string' }, { name: 'repeat', signature: 'repeat(str, n)', description: 'Repeats a string n times', category: 'string' }, { name: 'indexOf', signature: 'indexOf(str, substring)', description: 'Returns the index of the first occurrence of substring', category: 'string' }, { name: 'lastIndexOf', signature: 'lastIndexOf(str, substring)', description: 'Returns the index of the last occurrence of substring', category: 'string' }, { name: 'hasPrefix', signature: 'hasPrefix(str, prefix)', description: 'Returns true if string starts with the given prefix', category: 'string' }, { name: 'hasSuffix', signature: 'hasSuffix(str, suffix)', description: 'Returns true if string ends with the given suffix', category: 'string' }, { name: 'contains', signature: 'contains(str, substr)', description: 'Returns true if string contains the given substring', category: 'string' }, { name: 'startsWith', signature: 'startsWith(str, prefix)', description: 'Returns true if string starts with the given prefix', category: 'string' }, { name: 'endsWith', signature: 'endsWith(str, suffix)', description: 'Returns true if string ends with the given suffix', category: 'string' }, // Date functions { name: 'now', signature: 'now()', description: 'Returns the current date as a time.Time value', category: 'date' }, { name: 'duration', signature: 'duration(str)', description: 'Returns time.Duration value of the given string', category: 'date' }, { name: 'date', signature: 'date(str[, format[, timezone]])', description: 'Converts the given string into a date representation', category: 'date' }, { name: 'timezone', signature: 'timezone(str)', description: 'Returns the timezone of the given string', category: 'date' }, // Number functions { name: 'max', signature: 'max(n1, n2)', description: 'Returns the maximum of two numbers', category: 'number' }, { name: 'min', signature: 'min(n1, n2)', description: 'Returns the minimum of two numbers', category: 'number' }, { name: 'abs', signature: 'abs(n)', description: 'Returns the absolute value of a number', category: 'number' }, { name: 'ceil', signature: 'ceil(n)', description: 'Returns the least integer value greater than or equal to x', category: 'number' }, { name: 'floor', signature: 'floor(n)', description: 'Returns the greatest integer value less than or equal to x', category: 'number' }, { name: 'round', signature: 'round(n)', description: 'Returns the nearest integer, rounding half away from zero', category: 'number' }, // Array functions { name: 'all', signature: 'all(array, predicate)', description: 'Returns true if all elements satisfy the predicate', category: 'array' }, { name: 'any', signature: 'any(array, predicate)', description: 'Returns true if any elements satisfy the predicate', category: 'array' }, { name: 'one', signature: 'one(array, predicate)', description: 'Returns true if exactly one element satisfies the predicate', category: 'array' }, { name: 'none', signature: 'none(array, predicate)', description: 'Returns true if no elements satisfy the predicate', category: 'array' }, { name: 'map', signature: 'map(array, predicate)', description: 'Returns new array by applying the predicate to each element', category: 'array' }, { name: 'filter', signature: 'filter(array, predicate)', description: 'Returns new array by filtering elements by predicate', category: 'array' }, { name: 'find', signature: 'find(array, predicate)', description: 'Finds the first element satisfying the predicate', category: 'array' }, { name: 'findIndex', signature: 'findIndex(array, predicate)', description: 'Finds the index of the first element satisfying the predicate', category: 'array' }, { name: 'findLast', signature: 'findLast(array, predicate)', description: 'Finds the last element satisfying the predicate', category: 'array' }, { name: 'findLastIndex', signature: 'findLastIndex(array, predicate)', description: 'Finds the index of the last element satisfying the predicate', category: 'array' }, { name: 'groupBy', signature: 'groupBy(array, predicate)', description: 'Groups elements by the result of the predicate', category: 'array' }, { name: 'count', signature: 'count(array[, predicate])', description: 'Returns the number of elements satisfying the predicate', category: 'array' }, { name: 'concat', signature: 'concat(array1, array2[, ...])', description: 'Concatenates two or more arrays', category: 'array' }, { name: 'flatten', signature: 'flatten(array)', description: 'Flattens an array into one-dimensional array', category: 'array' }, { name: 'uniq', signature: 'uniq(array)', description: 'Removes duplicates from an array', category: 'array' }, { name: 'join', signature: 'join(array[, delimiter])', description: 'Joins an array of strings into a single string', category: 'array' }, { name: 'reduce', signature: 'reduce(array, predicate[, initial])', description: 'Reduces an array to a single value', category: 'array' }, { name: 'sum', signature: 'sum(array[, predicate])', description: 'Returns the sum of all numbers in an array', category: 'array' }, { name: 'mean', signature: 'mean(array)', description: 'Returns the average of all numbers in an array', category: 'array' }, { name: 'median', signature: 'median(array)', description: 'Returns the median of all numbers in an array', category: 'array' }, { name: 'first', signature: 'first(array)', description: 'Returns the first element from an array', category: 'array' }, { name: 'last', signature: 'last(array)', description: 'Returns the last element from an array', category: 'array' }, { name: 'take', signature: 'take(array, n)', description: 'Returns the first n elements from an array', category: 'array' }, { name: 'reverse', signature: 'reverse(array)', description: 'Returns a new reversed copy of the array', category: 'array' }, { name: 'sort', signature: 'sort(array[, order])', description: 'Sorts an array in ascending or descending order', category: 'array' }, { name: 'sortBy', signature: 'sortBy(array[, predicate, order])', description: 'Sorts an array by the result of the predicate', category: 'array' }, // Map functions { name: 'keys', signature: 'keys(map)', description: 'Returns an array containing the keys of the map', category: 'map' }, { name: 'values', signature: 'values(map)', description: 'Returns an array containing the values of the map', category: 'map' }, // Type conversion functions { name: 'type', signature: 'type(v)', description: 'Returns the type of the given value', category: 'conversion' }, { name: 'int', signature: 'int(v)', description: 'Returns the integer value of a number or string', category: 'conversion' }, { name: 'float', signature: 'float(v)', description: 'Returns the float value of a number or string', category: 'conversion' }, { name: 'string', signature: 'string(v)', description: 'Converts a value to its string representation', category: 'conversion' }, { name: 'toJSON', signature: 'toJSON(v)', description: 'Converts a value to JSON string representation', category: 'conversion' }, { name: 'fromJSON', signature: 'fromJSON(v)', description: 'Parses a JSON string to a value', category: 'conversion' }, { name: 'toBase64', signature: 'toBase64(v)', description: 'Encodes a string into Base64 format', category: 'conversion' }, { name: 'fromBase64', signature: 'fromBase64(v)', description: 'Decodes a Base64 encoded string', category: 'conversion' }, { name: 'toPairs', signature: 'toPairs(map)', description: 'Converts a map to an array of key-value pairs', category: 'conversion' }, { name: 'fromPairs', signature: 'fromPairs(array)', description: 'Converts an array of key-value pairs to a map', category: 'conversion' }, // Miscellaneous functions { name: 'len', signature: 'len(v)', description: 'Returns the length of an array, map, or string', category: 'misc' }, { name: 'get', signature: 'get(v, index)', description: 'Retrieves element at the specified index from an array or map', category: 'misc' }, // Bitwise functions { name: 'bitand', signature: 'bitand(int, int)', description: 'Returns the bitwise AND of two integers', category: 'bitwise' }, { name: 'bitor', signature: 'bitor(int, int)', description: 'Returns the bitwise OR of two integers', category: 'bitwise' }, { name: 'bitxor', signature: 'bitxor(int, int)', description: 'Returns the bitwise XOR of two integers', category: 'bitwise' }, { name: 'bitnand', signature: 'bitnand(int, int)', description: 'Returns the bitwise AND NOT of two integers', category: 'bitwise' }, { name: 'bitnot', signature: 'bitnot(int)', description: 'Returns the bitwise NOT of an integer', category: 'bitwise' }, { name: 'bitshl', signature: 'bitshl(int, int)', description: 'Returns the Left Shift of an integer', category: 'bitwise' }, { name: 'bitshr', signature: 'bitshr(int, int)', description: 'Returns the Right Shift of an integer', category: 'bitwise' }, { name: 'bitushr', signature: 'bitushr(int, int)', description: 'Returns the unsigned Right Shift of an integer', category: 'bitwise' }, ]; export const getExprCompletionProvider = () => { return { provideCompletionItems( model: monaco.editor.ITextModel, position: monaco.Position, _context: monaco.languages.CompletionContext, _token: monaco.CancellationToken, ): monaco.languages.ProviderResult { const word = model.getWordUntilPosition(position); const range = new monaco.Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn); const suggestions: monaco.languages.CompletionItem[] = [ ...EXPR_KEYWORDS.map((keyword) => ({ label: keyword, kind: monaco.languages.CompletionItemKind.Keyword, insertText: keyword, range: range, sortText: '1' + keyword, })), ...EXPR_FUNCTIONS.map((func) => ({ label: func.name, kind: monaco.languages.CompletionItemKind.Function, insertText: func.name, detail: func.signature, documentation: func.description + ` (${func.category})`, range: range, sortText: '2' + func.name, insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet, })), ]; return { suggestions: suggestions, }; }, }; };