Skip to content

Commit c4d22a0

Browse files
joswiggoetzrrGit
andauthoredJan 29, 2025
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 <ryan.r.goetz@jpl.nasa.gov>
1 parent 49bb143 commit c4d22a0

File tree

3 files changed

+40
-58
lines changed

3 files changed

+40
-58
lines changed
 

‎src/utilities/codemirror/custom-folder.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export function foldVariables(containerNode: SyntaxNode, state: EditorState): {
127127

128128
// Calculate the length of the directive (e.g. "@INPUT_PARAMETERS_BEGIN" or "@LOCALS_BEGIN")
129129
const directiveLength = state
130-
.sliceDoc(containerNode.from, containerNode.to - variablesNodes.length ? getFromAndTo([...variablesNodes]).from : 0)
130+
.sliceDoc(
131+
containerNode.from,
132+
containerNode.to - (variablesNodes.length > 0 ? getFromAndTo([...variablesNodes]).from : 0),
133+
)
131134
.split('\n')[0].length;
132135

133136
// Calculate the start of the fold range after the directive

‎src/utilities/sequence-editor/sequence-linter.ts

+35-56
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from '../codemirror/codemirror-utils';
3939
import { closeSuggestion, computeBlocks, openSuggestion } from '../codemirror/custom-folder';
4040
import { SeqNCommandInfoMapper } from '../codemirror/seq-n-tree-utils';
41+
import { pluralize } from '../text';
4142
import {
4243
getBalancedDuration,
4344
getDoyTime,
@@ -1226,79 +1227,57 @@ function validateAndLintArguments(
12261227
* Validates the command structure.
12271228
* @param stemNode - The SyntaxNode representing the command stem.
12281229
* @param argsNode - The SyntaxNode representing the command arguments.
1229-
* @param exactArgSize - The expected number of arguments.
1230+
* @param expectedArgSize - The expected number of arguments.
12301231
* @param addDefault - The function to add default arguments.
12311232
* @returns A Diagnostic object representing the validation error, or undefined if there is no error.
12321233
*/
12331234
function validateCommandStructure(
12341235
stemNode: SyntaxNode,
12351236
argsNode: SyntaxNode[] | null,
1236-
exactArgSize: number,
1237+
expectedArgSize: number,
12371238
addDefault: (view: any) => any,
12381239
): Diagnostic | undefined {
1239-
if (arguments.length > 0) {
1240-
if (!argsNode || argsNode.length === 0) {
1241-
return {
1242-
actions: [],
1243-
from: stemNode.from,
1244-
message: `The stem is missing arguments.`,
1245-
severity: 'error',
1246-
to: stemNode.to,
1247-
};
1248-
}
1249-
if (argsNode.length > exactArgSize) {
1250-
const extraArgs = argsNode.slice(exactArgSize);
1251-
const { from, to } = getFromAndTo(extraArgs);
1252-
return {
1253-
actions: [
1254-
{
1255-
apply(view, from, to) {
1256-
view.dispatch({ changes: { from, to } });
1257-
},
1258-
name: `Remove ${extraArgs.length} extra argument${extraArgs.length > 1 ? 's' : ''}`,
1259-
},
1260-
],
1261-
from,
1262-
message: `Extra arguments, definition has ${exactArgSize}, but ${argsNode.length} are present`,
1263-
severity: 'error',
1264-
to,
1265-
};
1266-
}
1267-
if (argsNode.length < exactArgSize) {
1268-
const { from, to } = getFromAndTo(argsNode);
1269-
const pluralS = exactArgSize > argsNode.length + 1 ? 's' : '';
1270-
return {
1271-
actions: [
1272-
{
1273-
apply(view) {
1274-
addDefault(view);
1275-
},
1276-
name: `Add default missing argument${pluralS}`,
1277-
},
1278-
],
1279-
from,
1280-
message: `Missing argument${pluralS}, definition has ${argsNode.length}, but ${exactArgSize} are present`,
1281-
severity: 'error',
1282-
to,
1283-
};
1284-
}
1285-
} else if (argsNode && argsNode.length > 0) {
1286-
const { from, to } = getFromAndTo(argsNode);
1240+
if ((!argsNode || argsNode.length === 0) && expectedArgSize === 0) {
1241+
return undefined;
1242+
}
1243+
if (argsNode && argsNode.length > expectedArgSize) {
1244+
const extraArgs = argsNode.slice(expectedArgSize);
1245+
const { from, to } = getFromAndTo(extraArgs);
1246+
const commandArgs = `argument${pluralize(extraArgs.length)}`;
12871247
return {
12881248
actions: [
12891249
{
12901250
apply(view, from, to) {
12911251
view.dispatch({ changes: { from, to } });
12921252
},
1293-
name: `Remove argument${argsNode.length > 1 ? 's' : ''}`,
1253+
name: `Remove ${extraArgs.length} extra ${commandArgs}`,
1254+
},
1255+
],
1256+
from,
1257+
message: `Extra ${commandArgs}, definition has ${expectedArgSize}, but ${argsNode.length} are present`,
1258+
severity: 'error',
1259+
to,
1260+
};
1261+
}
1262+
if ((argsNode && argsNode.length < expectedArgSize) || (!argsNode && expectedArgSize > 0)) {
1263+
const { from, to } = getFromAndTo(argsNode ?? [stemNode]);
1264+
const commandArgs = `argument${pluralize(expectedArgSize - (argsNode?.length ?? 0))}`;
1265+
return {
1266+
actions: [
1267+
{
1268+
apply(view) {
1269+
addDefault(view);
1270+
},
1271+
name: `Add default missing ${commandArgs}`,
12941272
},
12951273
],
1296-
from: from,
1297-
message: 'The command should not have arguments',
1274+
from,
1275+
message: `Missing ${commandArgs}, definition has ${expectedArgSize}, but ${argsNode?.length ?? 0} are present`,
12981276
severity: 'error',
1299-
to: to,
1277+
to,
13001278
};
13011279
}
1280+
13021281
return undefined;
13031282
}
13041283

@@ -1498,7 +1477,7 @@ function validateArgument(
14981477
diagnostics.push({
14991478
actions: [],
15001479
from: argNode.from,
1501-
message: `Repeat argument should have at least ${minCount} value${minCount !== 0 ? 's' : ''} but has ${
1480+
message: `Repeat argument should have at least ${minCount} value${pluralize(minCount)} but has ${
15021481
repeatNodes.length
15031482
}`,
15041483
severity: 'error',
@@ -1508,7 +1487,7 @@ function validateArgument(
15081487
diagnostics.push({
15091488
actions: [],
15101489
from: argNode.from,
1511-
message: `Repeat argument should have at most ${maxCount} value${maxCount !== 0 ? 's' : ''} but has ${
1490+
message: `Repeat argument should have at most ${maxCount} value${pluralize(maxCount)} but has ${
15121491
repeatNodes.length
15131492
}`,
15141493
severity: 'error',

‎src/utilities/sequence-editor/tree-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function getFromAndTo(nodes: (SyntaxNode | null)[]): { from: number; to:
4242
to: Math.max(acc.to, node.to),
4343
};
4444
},
45-
{ from: Number.MAX_VALUE, to: Number.MIN_VALUE },
45+
{ from: nodes[0]?.from ?? 0, to: nodes[0]?.to ?? 0 },
4646
);
4747
}
4848

0 commit comments

Comments
 (0)