Skip to content

Commit

Permalink
simplify logic on no arg commands (#1584)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
joswig and goetzrrGit authored Jan 29, 2025
1 parent 49bb143 commit c4d22a0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 58 deletions.
5 changes: 4 additions & 1 deletion src/utilities/codemirror/custom-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 35 additions & 56 deletions src/utilities/sequence-editor/sequence-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/sequence-editor/tree-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
);
}

Expand Down

0 comments on commit c4d22a0

Please sign in to comment.