-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
let.ts
154 lines (146 loc) · 4.7 KB
/
let.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// eslint-disable-next-line id-denylist
import { alt, optWhitespace, Parser, seq, string, whitespace } from 'parsimmon';
import { env } from 'process';
import { VimState } from '../../state/vimState';
import { StatusBar } from '../../statusBar';
import { ExCommand } from '../../vimscript/exCommand';
import {
add,
concat,
divide,
modulo,
multiply,
str,
subtract,
} from '../../vimscript/expression/build';
import { EvaluationContext } from '../../vimscript/expression/evaluate';
import {
envVariableParser,
expressionParser,
optionParser,
registerParser,
variableParser,
} from '../../vimscript/expression/parser';
import {
EnvVariableExpression,
Expression,
OptionExpression,
RegisterExpression,
VariableExpression,
} from '../../vimscript/expression/types';
import { displayValue } from '../../vimscript/expression/displayValue';
import { ErrorCode, VimError } from '../../error';
export type LetCommandOperation = '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '.=' | '..=';
export type LetCommandVariable =
| VariableExpression
| OptionExpression
| RegisterExpression
| EnvVariableExpression;
export type LetCommandArgs =
| {
operation: LetCommandOperation;
variable: LetCommandVariable;
expression: Expression;
lock: boolean;
}
| {
operation: 'print';
variables: LetCommandVariable[];
};
const operationParser: Parser<LetCommandOperation> = alt(
string('='),
string('+='),
string('-='),
string('*='),
string('/='),
string('%='),
string('.='),
string('..='),
);
const letVarParser = alt<LetCommandVariable>(
variableParser,
optionParser,
envVariableParser,
registerParser,
);
export class LetCommand extends ExCommand {
// TODO: Support unpacking
// TODO: Support indexing
// TODO: Support slicing
public static readonly argParser = (lock: boolean) =>
alt<LetCommand>(
// `:let {var} = {expr}`
// `:let {var} += {expr}`
// `:let {var} -= {expr}`
// `:let {var} .= {expr}`
whitespace.then(
seq(letVarParser, operationParser.wrap(optWhitespace, optWhitespace), expressionParser).map(
([variable, operation, expression]) =>
new LetCommand({
operation,
variable,
expression,
lock,
}),
),
),
// `:let`
// `:let {var-name} ...`
optWhitespace
.then(letVarParser.sepBy(whitespace))
.map((variables) => new LetCommand({ operation: 'print', variables })),
);
private args: LetCommandArgs;
constructor(args: LetCommandArgs) {
super();
this.args = args;
}
async execute(vimState: VimState): Promise<void> {
const context = new EvaluationContext();
if (this.args.operation === 'print') {
if (this.args.variables.length === 0) {
// TODO
} else {
const variable = this.args.variables[this.args.variables.length - 1];
const value = context.evaluate(variable);
const prefix = value.type === 'number' ? '#' : value.type === 'funcref' ? '*' : '';
StatusBar.setText(vimState, `${variable.name} ${prefix}${displayValue(value)}`);
}
} else {
const variable = this.args.variable;
if (this.args.lock) {
if (this.args.operation !== '=') {
throw VimError.fromCode(ErrorCode.CannotModifyExistingVariable);
} else if (this.args.variable.type !== 'variable') {
// TODO: this error message should vary by type
throw VimError.fromCode(ErrorCode.CannotLockARegister);
}
}
let value = context.evaluate(this.args.expression);
if (variable.type === 'variable') {
if (this.args.operation === '+=') {
value = context.evaluate(add(variable, value));
} else if (this.args.operation === '-=') {
value = context.evaluate(subtract(variable, value));
} else if (this.args.operation === '*=') {
value = context.evaluate(multiply(variable, value));
} else if (this.args.operation === '/=') {
value = context.evaluate(divide(variable, value));
} else if (this.args.operation === '%=') {
value = context.evaluate(modulo(variable, value));
} else if (this.args.operation === '.=') {
value = context.evaluate(concat(variable, value));
} else if (this.args.operation === '..=') {
value = context.evaluate(concat(variable, value));
}
context.setVariable(variable, value, this.args.lock);
} else if (variable.type === 'register') {
// TODO
} else if (variable.type === 'option') {
// TODO
} else if (variable.type === 'env_variable') {
value = str(env[variable.name] ?? '');
}
}
}
}