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 2886e83b90..33c90f67d7 100644 --- a/src/utilities/sequence-editor/sequence-linter.ts +++ b/src/utilities/sequence-editor/sequence-linter.ts @@ -1212,79 +1212,56 @@ 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); return { actions: [ { apply(view, from, to) { view.dispatch({ changes: { from, to } }); }, - name: `Remove argument${argsNode.length > 1 ? 's' : ''}`, + name: `Remove ${extraArgs.length} extra argument${extraArgs.length > 1 ? 's' : ''}`, }, ], - from: from, - message: 'The command should not have arguments', + from, + message: `Extra arguments, definition has ${expectedArgSize}, but ${argsNode.length} are present`, severity: 'error', - to: to, + to, }; } + if ((argsNode && argsNode.length < expectedArgSize) || (!argsNode && expectedArgSize > 0)) { + const { from, to } = getFromAndTo(argsNode ?? [stemNode]); + const pluralS = expectedArgSize - (argsNode?.length ?? 0) > 1 ? 's' : ''; + return { + actions: [ + { + apply(view) { + addDefault(view); + }, + name: `Add default missing argument${pluralS}`, + }, + ], + from, + message: `Missing argument${pluralS}, definition has ${expectedArgSize}, but ${argsNode?.length ?? 0} are present`, + severity: 'error', + to, + }; + } + return undefined; } 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 }, ); }