@@ -38,6 +38,7 @@ import {
38
38
} from '../codemirror/codemirror-utils' ;
39
39
import { closeSuggestion , computeBlocks , openSuggestion } from '../codemirror/custom-folder' ;
40
40
import { SeqNCommandInfoMapper } from '../codemirror/seq-n-tree-utils' ;
41
+ import { pluralize } from '../text' ;
41
42
import {
42
43
getBalancedDuration ,
43
44
getDoyTime ,
@@ -1226,79 +1227,57 @@ function validateAndLintArguments(
1226
1227
* Validates the command structure.
1227
1228
* @param stemNode - The SyntaxNode representing the command stem.
1228
1229
* @param argsNode - The SyntaxNode representing the command arguments.
1229
- * @param exactArgSize - The expected number of arguments.
1230
+ * @param expectedArgSize - The expected number of arguments.
1230
1231
* @param addDefault - The function to add default arguments.
1231
1232
* @returns A Diagnostic object representing the validation error, or undefined if there is no error.
1232
1233
*/
1233
1234
function validateCommandStructure (
1234
1235
stemNode : SyntaxNode ,
1235
1236
argsNode : SyntaxNode [ ] | null ,
1236
- exactArgSize : number ,
1237
+ expectedArgSize : number ,
1237
1238
addDefault : ( view : any ) => any ,
1238
1239
) : 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 ) } ` ;
1287
1247
return {
1288
1248
actions : [
1289
1249
{
1290
1250
apply ( view , from , to ) {
1291
1251
view . dispatch ( { changes : { from, to } } ) ;
1292
1252
} ,
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 } ` ,
1294
1272
} ,
1295
1273
] ,
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` ,
1298
1276
severity : 'error' ,
1299
- to : to ,
1277
+ to,
1300
1278
} ;
1301
1279
}
1280
+
1302
1281
return undefined ;
1303
1282
}
1304
1283
@@ -1498,7 +1477,7 @@ function validateArgument(
1498
1477
diagnostics . push ( {
1499
1478
actions : [ ] ,
1500
1479
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 ${
1502
1481
repeatNodes . length
1503
1482
} `,
1504
1483
severity : 'error' ,
@@ -1508,7 +1487,7 @@ function validateArgument(
1508
1487
diagnostics . push ( {
1509
1488
actions : [ ] ,
1510
1489
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 ${
1512
1491
repeatNodes . length
1513
1492
} `,
1514
1493
severity : 'error' ,
0 commit comments