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