From c4d22a01928cff4b1ac6e030c277c1dbabb22429 Mon Sep 17 00:00:00 2001 From: Chet Joswig Date: Wed, 29 Jan 2025 11:14:25 -0800 Subject: [PATCH] simplify logic on no arg commands (#1584) * simplify logic on no arg commands * Bug fix for `getFromAndTo` * Was returning a min or max that was outside the scope of the editor. * Update custom folder after addressing this issue. * Use pluralize utility function --------- Co-authored-by: rrgoetz --- src/utilities/codemirror/custom-folder.ts | 5 +- .../sequence-editor/sequence-linter.ts | 91 +++++++------------ src/utilities/sequence-editor/tree-utils.ts | 2 +- 3 files changed, 40 insertions(+), 58 deletions(-) diff --git a/src/utilities/codemirror/custom-folder.ts b/src/utilities/codemirror/custom-folder.ts index 7ba001ac26..afdfa3278b 100644 --- a/src/utilities/codemirror/custom-folder.ts +++ b/src/utilities/codemirror/custom-folder.ts @@ -127,7 +127,10 @@ export function foldVariables(containerNode: SyntaxNode, state: EditorState): { // Calculate the length of the directive (e.g. "@INPUT_PARAMETERS_BEGIN" or "@LOCALS_BEGIN") const directiveLength = state - .sliceDoc(containerNode.from, containerNode.to - variablesNodes.length ? getFromAndTo([...variablesNodes]).from : 0) + .sliceDoc( + containerNode.from, + containerNode.to - (variablesNodes.length > 0 ? getFromAndTo([...variablesNodes]).from : 0), + ) .split('\n')[0].length; // Calculate the start of the fold range after the directive diff --git a/src/utilities/sequence-editor/sequence-linter.ts b/src/utilities/sequence-editor/sequence-linter.ts index 653e849fa4..2499de346f 100644 --- a/src/utilities/sequence-editor/sequence-linter.ts +++ b/src/utilities/sequence-editor/sequence-linter.ts @@ -38,6 +38,7 @@ import { } from '../codemirror/codemirror-utils'; import { closeSuggestion, computeBlocks, openSuggestion } from '../codemirror/custom-folder'; import { SeqNCommandInfoMapper } from '../codemirror/seq-n-tree-utils'; +import { pluralize } from '../text'; import { getBalancedDuration, getDoyTime, @@ -1226,79 +1227,57 @@ function validateAndLintArguments( * Validates the command structure. * @param stemNode - The SyntaxNode representing the command stem. * @param argsNode - The SyntaxNode representing the command arguments. - * @param exactArgSize - The expected number of arguments. + * @param expectedArgSize - The expected number of arguments. * @param addDefault - The function to add default arguments. * @returns A Diagnostic object representing the validation error, or undefined if there is no error. */ function validateCommandStructure( stemNode: SyntaxNode, argsNode: SyntaxNode[] | null, - exactArgSize: number, + expectedArgSize: number, addDefault: (view: any) => any, ): Diagnostic | undefined { - if (arguments.length > 0) { - if (!argsNode || argsNode.length === 0) { - return { - actions: [], - from: stemNode.from, - message: `The stem is missing arguments.`, - severity: 'error', - to: stemNode.to, - }; - } - if (argsNode.length > exactArgSize) { - const extraArgs = argsNode.slice(exactArgSize); - const { from, to } = getFromAndTo(extraArgs); - return { - actions: [ - { - apply(view, from, to) { - view.dispatch({ changes: { from, to } }); - }, - name: `Remove ${extraArgs.length} extra argument${extraArgs.length > 1 ? 's' : ''}`, - }, - ], - from, - message: `Extra arguments, definition has ${exactArgSize}, but ${argsNode.length} are present`, - severity: 'error', - to, - }; - } - if (argsNode.length < exactArgSize) { - const { from, to } = getFromAndTo(argsNode); - const pluralS = exactArgSize > argsNode.length + 1 ? 's' : ''; - return { - actions: [ - { - apply(view) { - addDefault(view); - }, - name: `Add default missing argument${pluralS}`, - }, - ], - from, - message: `Missing argument${pluralS}, definition has ${argsNode.length}, but ${exactArgSize} are present`, - severity: 'error', - to, - }; - } - } else if (argsNode && argsNode.length > 0) { - const { from, to } = getFromAndTo(argsNode); + if ((!argsNode || argsNode.length === 0) && expectedArgSize === 0) { + return undefined; + } + if (argsNode && argsNode.length > expectedArgSize) { + const extraArgs = argsNode.slice(expectedArgSize); + const { from, to } = getFromAndTo(extraArgs); + const commandArgs = `argument${pluralize(extraArgs.length)}`; return { actions: [ { apply(view, from, to) { view.dispatch({ changes: { from, to } }); }, - name: `Remove argument${argsNode.length > 1 ? 's' : ''}`, + name: `Remove ${extraArgs.length} extra ${commandArgs}`, + }, + ], + from, + message: `Extra ${commandArgs}, definition has ${expectedArgSize}, but ${argsNode.length} are present`, + severity: 'error', + to, + }; + } + if ((argsNode && argsNode.length < expectedArgSize) || (!argsNode && expectedArgSize > 0)) { + const { from, to } = getFromAndTo(argsNode ?? [stemNode]); + const commandArgs = `argument${pluralize(expectedArgSize - (argsNode?.length ?? 0))}`; + return { + actions: [ + { + apply(view) { + addDefault(view); + }, + name: `Add default missing ${commandArgs}`, }, ], - from: from, - message: 'The command should not have arguments', + from, + message: `Missing ${commandArgs}, definition has ${expectedArgSize}, but ${argsNode?.length ?? 0} are present`, severity: 'error', - to: to, + to, }; } + return undefined; } @@ -1498,7 +1477,7 @@ function validateArgument( diagnostics.push({ actions: [], from: argNode.from, - message: `Repeat argument should have at least ${minCount} value${minCount !== 0 ? 's' : ''} but has ${ + message: `Repeat argument should have at least ${minCount} value${pluralize(minCount)} but has ${ repeatNodes.length }`, severity: 'error', @@ -1508,7 +1487,7 @@ function validateArgument( diagnostics.push({ actions: [], from: argNode.from, - message: `Repeat argument should have at most ${maxCount} value${maxCount !== 0 ? 's' : ''} but has ${ + message: `Repeat argument should have at most ${maxCount} value${pluralize(maxCount)} but has ${ repeatNodes.length }`, severity: 'error', diff --git a/src/utilities/sequence-editor/tree-utils.ts b/src/utilities/sequence-editor/tree-utils.ts index 6f8b58c5f2..3a2acd39b6 100644 --- a/src/utilities/sequence-editor/tree-utils.ts +++ b/src/utilities/sequence-editor/tree-utils.ts @@ -42,7 +42,7 @@ export function getFromAndTo(nodes: (SyntaxNode | null)[]): { from: number; to: to: Math.max(acc.to, node.to), }; }, - { from: Number.MAX_VALUE, to: Number.MIN_VALUE }, + { from: nodes[0]?.from ?? 0, to: nodes[0]?.to ?? 0 }, ); }