Skip to content

Commit ec9baa1

Browse files
committed
Refactor to reduce code duplication a bit
1 parent 036959f commit ec9baa1

1 file changed

Lines changed: 10 additions & 29 deletions

File tree

src/vimscript/expression/evaluate.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,18 +1419,7 @@ export class EvaluationContext {
14191419
}
14201420
// TODO: matchadd()/matchaddpos()/matcharg()/matchdelete()
14211421
// TODO: match()/matchend()/matchlist()/matchstr()/matchstrpos()
1422-
case 'max': {
1423-
const [l] = getArgs(1);
1424-
let values: Value[];
1425-
if (l?.type === 'list') {
1426-
values = l.items;
1427-
} else if (l?.type === 'dictionary') {
1428-
values = [...l.items.values()];
1429-
} else {
1430-
throw VimError.ArgumentOfFuncMustBeAListOrDictionary(call.func);
1431-
}
1432-
return int(values.length === 0 ? 0 : Math.max(...values.map(toInt)));
1433-
}
1422+
case 'max':
14341423
case 'min': {
14351424
const [l] = getArgs(1);
14361425
let values: Value[];
@@ -1441,7 +1430,8 @@ export class EvaluationContext {
14411430
} else {
14421431
throw VimError.ArgumentOfFuncMustBeAListOrDictionary(call.func);
14431432
}
1444-
return int(values.length === 0 ? 0 : Math.min(...values.map(toInt)));
1433+
const fn = call.func === 'max' ? Math.max : Math.min;
1434+
return int(values.length === 0 ? 0 : fn(...values.map(toInt)));
14451435
}
14461436
case 'mode': {
14471437
const [arg] = getArgs(1);
@@ -1467,13 +1457,17 @@ export class EvaluationContext {
14671457
return str(''); // TODO: Other modes
14681458
}
14691459
}
1470-
case 'nextnonblank': {
1460+
case 'nextnonblank':
1461+
case 'prevnonblank': {
14711462
const [_line] = getArgs(1);
14721463
const line = toInt(_line!);
1473-
if (line <= 0) {
1464+
const forward = call.func === 'nextnonblank';
1465+
if (forward ? line <= 0 : line > this.vimState!.document.lineCount) {
14741466
return int(0);
14751467
}
1476-
for (let i = line - 1; i < this.vimState!.document.lineCount; i++) {
1468+
const step = forward ? 1 : -1;
1469+
const limit = forward ? this.vimState!.document.lineCount : -1;
1470+
for (let i = line - 1; forward ? i < limit : i >= 0; i += step) {
14771471
if (this.vimState!.document.lineAt(i).text.length > 0) {
14781472
return int(i + 1);
14791473
}
@@ -1487,19 +1481,6 @@ export class EvaluationContext {
14871481
case 'pow': {
14881482
return float2(Math.pow);
14891483
}
1490-
case 'prevnonblank': {
1491-
const [_line] = getArgs(1);
1492-
const line = toInt(_line!);
1493-
if (line > this.vimState!.document.lineCount) {
1494-
return int(0);
1495-
}
1496-
for (let i = line - 1; i >= 0; i--) {
1497-
if (this.vimState!.document.lineAt(i).text.length > 0) {
1498-
return int(i + 1);
1499-
}
1500-
}
1501-
return int(0);
1502-
}
15031484
// TODO: printf()
15041485
// TODO: rand()
15051486
case 'range': {

0 commit comments

Comments
 (0)