From bf211df67dee9576c33ac60eb541966524dbc041 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Tue, 10 Oct 2023 01:58:55 +0200 Subject: [PATCH] Adding function usage to auto complete. --- .../Setting/AutocompletionCategory.qml | 2 ++ .../LogarithmPlotter/Setting/ExpressionEditor.qml | 2 +- .../eu/ad5001/LogarithmPlotter/js/math/common.js | 10 +++++----- .../ad5001/LogarithmPlotter/js/parsing/parsing.js | 1 + .../ad5001/LogarithmPlotter/js/parsing/reference.js | 13 ++++++++----- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/AutocompletionCategory.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/AutocompletionCategory.qml index d5cc9af..cf42238 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/AutocompletionCategory.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/AutocompletionCategory.qml @@ -113,9 +113,11 @@ ListView { Text { id: annotationText anchors.right: parent.right + anchors.verticalCenter: parent.verticalCenter topPadding: 2 bottomPadding: 2 rightPadding: 15 + font.pixelSize: autocompleteText.font.pixelSize - 2 text: listFiltered.model[index].annotation color: parent.selected ? sysPaletteIn.highlightedText : sysPaletteIn.windowText } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml index 6315e3d..bde4962 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml @@ -448,7 +448,7 @@ Item { itemSelected: parent.itemSelected categoryItems: Parsing.FUNCTIONS_LIST autocompleteGenerator: (item) => {return { - 'text': item, 'annotation': '', + 'text': item, 'annotation': Parsing.FUNCTIONS_USAGE[item].join(', '), 'autocomplete': item+'()', 'cursorFinalOffset': -1 }} baseText: parent.visible ? parent.currentToken.value : "" diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/math/common.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/math/common.js index 86d16e6..450ae6d 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/math/common.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/math/common.js @@ -68,7 +68,7 @@ function parseArgumentsForFunction(args, usage1, usage2) { throw EvalError(qsTranslate('usage', 'Usage: %1').arg(usage2)) f = parser.parse(f).toJSFunction(variable, currentVars) } else - throw EvalError(qsTranslate('usage', 'Usage: %1 or\n%2').arg(usage1).arg(usage2))) + throw EvalError(qsTranslate('usage', 'Usage: %1 or\n%2').arg(usage1).arg(usage2)) return f } @@ -78,7 +78,7 @@ parser.functions.integral = function(a, b, ...args) { let usage2 = qsTranslate('usage', 'integral(, , , )') let f = parseArgumentsForFunction(args, usage1, usage2) if(a == null || b == null) - throw EvalError(qsTranslate('usage', 'Usage: %1 or\n%2').arg(usage1).arg(usage2))) + throw EvalError(qsTranslate('usage', 'Usage: %1 or\n%2').arg(usage1).arg(usage2)) // https://en.wikipedia.org/wiki/Simpson%27s_rule // Simpler, faster than tokenizing the expression @@ -86,12 +86,12 @@ parser.functions.integral = function(a, b, ...args) { } parser.functions.derivative = function(...args) { - let usage1 = qsTranslate('usage', 'derivative(, )') - let usage2 = qsTranslate('usage', 'derivative(, , )') + let usage1 = qsTranslate('usage', 'derivative(, )') + let usage2 = qsTranslate('usage', 'derivative(, , )') let x = args.pop() let f = parseArgumentsForFunction(args, usage1, usage2) if(x == null) - throw EvalError(qsTranslate('usage', 'Usage: %1 or\n%2').arg(usage1).arg(usage2))) + throw EvalError(qsTranslate('usage', 'Usage: %1 or\n%2').arg(usage1).arg(usage2)) let derivative_precision = x/10 return (f(x+derivative_precision/2)-f(x-derivative_precision/2))/derivative_precision diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/parsing.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/parsing.js index e1c4019..a7d5d48 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/parsing.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/parsing.js @@ -29,5 +29,6 @@ var Tokenizer = TK.ExpressionTokenizer var FUNCTIONS_LIST = Reference.FUNCTIONS_LIST var FUNCTIONS = Reference.FUNCTIONS +var FUNCTIONS_USAGE = Reference.FUNCTIONS_USAGE var CONSTANTS_LIST = Reference.CONSTANTS_LIST var CONSTANTS = Reference.CONSTANTS diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/reference.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/reference.js index 31a5963..36d4111 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/reference.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/reference.js @@ -106,7 +106,7 @@ class P { } toString() { - base_string = this.type + let base_string = this.type if(this.name != '') base_string = `${this.name}: ${base_string}` if(this.multipleAllowed) @@ -120,7 +120,7 @@ class P { } let string = new P('string') -let bool = new P('boolean') +let bool = new P('bool') let number = new P('number') let array = new P('array') @@ -153,9 +153,9 @@ const FUNCTIONS_USAGE = { 'log10': [number], 'log1p': [number], 'log2': [number], - 'max': [number, number, new P('number', '', true, null, true)], - 'min': [number, number, new P('number', '', true, null, true)], - 'pow': [number, new P('number', 'exponent')], + 'max': [number, number, new P('numbers', '', true, true)], + 'min': [number, number, new P('numbers', '', true, true)], + 'pow': [number, new P('number', 'exp')], 'random': [number, number], 'round': [number], 'sign': [number], @@ -170,5 +170,8 @@ const FUNCTIONS_USAGE = { 'gamma': [number], 'Γ': [number], 'roundTo': [number, new P('number')], + // Function manipulation + 'derivative': [new P('f'), new P('string', 'var', true), number], + 'integral': [new P('from'), new P('to'), new P('f'), new P('string', 'var', true)], }