Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,16 @@
"description": "Use system clipboard for unnamed register.",
"default": false
},
"vim.clipboard": {
"type": "string",
"enum": [
"",
"unnamed",
"unnamedplus"
],
"description": "Vim's `clipboard` option. When set to `unnamed` or `unnamedplus`, the unnamed register is aliased to the system clipboard (equivalent to enabling `vim.useSystemClipboard`). Also settable from a vimrc via `set clipboard=unnamed`.",
"default": ""
},
"vim.overrideCopy": {
"type": "boolean",
"description": "Override VS Code's copy command with our own copy command, which works better with VSCodeVim. Turn this off if copying is not working.",
Expand Down
4 changes: 3 additions & 1 deletion src/actions/commands/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ abstract class BasePutCommand extends BaseCommand {
};

if (this.overwritesRegisterWithSelection) {
vimState.recordedState.registerName = configuration.useSystemClipboard ? '*' : '"';
vimState.recordedState.registerName = configuration.clipboardAliasesUnnamedRegister
? '*'
: '"';
Register.put(
vimState,
vimState.document.getText(replaceRange),
Expand Down
3 changes: 2 additions & 1 deletion src/cmd_line/commands/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export class PutExCommand extends ExCommand {
Register.overwriteRegister(vimState, this.arguments.register, stringified, 0);
}

const registerName = this.arguments.register || (configuration.useSystemClipboard ? '*' : '"');
const registerName =
this.arguments.register || (configuration.clipboardAliasesUnnamedRegister ? '*' : '"');

if (!Register.isValidRegister(registerName)) {
StatusBar.displayError(vimState, VimError.TrailingCharacters());
Expand Down
282 changes: 27 additions & 255 deletions src/cmd_line/commands/set.ts
Original file line number Diff line number Diff line change
@@ -1,147 +1,17 @@
// eslint-disable-next-line id-denylist
import { alt, oneOf, Parser, regexp, seq, string, whitespace } from 'parsimmon';
import { Parser } from 'parsimmon';
import { configuration, optionAliases } from '../../configuration/configuration';
import { VimError } from '../../error';
import { VimState } from '../../state/vimState';
import { StatusBar } from '../../statusBar';
import { ExCommand } from '../../vimscript/exCommand';
import {
applyOperationToConfig,
setCommandListeners,
SetOperation,
setOperationParser,
} from './setOperation';

type SetOperation =
| {
// :se[t]
// :se[t] {option}
type: 'show_or_set';
option: string | undefined;
}
| {
// :se[t] {option}?
type: 'show';
option: string;
}
| {
// :se[t] no{option}
type: 'unset';
option: string;
}
| {
// :se[t] {option}!
// :se[t] inv{option}
type: 'invert';
option: string;
}
| {
// :se[t] {option}&
// :se[t] {option}&vi
// :se[t] {option}&vim
type: 'default';
option: string;
source: 'vi' | 'vim' | '';
}
| {
// :se[t] {option}={value}
// :se[t] {option}:{value}
type: 'equal';
option: string;
value: string;
}
| {
// :se[t] {option}+={value}
type: 'add';
option: string;
value: string;
}
| {
// :se[t] {option}^={value}
type: 'multiply';
option: string;
value: string;
}
| {
// :se[t] {option}-={value}
type: 'subtract';
option: string;
value: string;
};

const optionParser = regexp(/[a-z]+/);
const valueParser = regexp(/\S*/);
const setOperationParser: Parser<SetOperation> = whitespace
.then(
alt<SetOperation>(
string('no')
.then(optionParser)
.map((option) => {
return {
type: 'unset',
option,
};
}),
string('inv')
.then(optionParser)
.map((option) => {
return {
type: 'invert',
option,
};
}),
optionParser.skip(string('!')).map((option) => {
return {
type: 'invert',
option,
};
}),
optionParser.skip(string('?')).map((option) => {
return {
type: 'show',
option,
};
}),
seq(optionParser.skip(string('&')), alt(string('vim'), string('vi'), string(''))).map(
([option, source]) => {
return {
type: 'default',
option,
source,
};
},
),
seq(optionParser.skip(oneOf('=:')), valueParser).map(([option, value]) => {
return {
type: 'equal',
option,
value,
};
}),
seq(optionParser.skip(string('+=')), valueParser).map(([option, value]) => {
return {
type: 'add',
option,
value,
};
}),
seq(optionParser.skip(string('^=')), valueParser).map(([option, value]) => {
return {
type: 'multiply',
option,
value,
};
}),
seq(optionParser.skip(string('-=')), valueParser).map(([option, value]) => {
return {
type: 'subtract',
option,
value,
};
}),
optionParser.map((option) => {
return {
type: 'show_or_set',
option,
};
}),
),
)
.fallback({ type: 'show_or_set', option: undefined });
export { SetOperation } from './setOperation';

export class SetCommand extends ExCommand {
public static readonly argParser: Parser<SetCommand> = setOperationParser.map(
Expand All @@ -155,10 +25,11 @@ export class SetCommand extends ExCommand {
}

// Listeners for options that need to be updated when they change
private static listeners: { [key: string]: Array<() => void> } = {};
static addListener(option: string, listener: () => void) {
if (!(option in SetCommand.listeners)) SetCommand.listeners[option] = [];
SetCommand.listeners[option].push(listener);
if (!(option in setCommandListeners)) {
setCommandListeners[option] = [];
}
setCommandListeners[option].push(listener);
}

async execute(vimState: VimState): Promise<void> {
Expand All @@ -172,123 +43,24 @@ export class SetCommand extends ExCommand {
if (currentValue === undefined) {
throw VimError.UnknownOption(option);
}
const type =
typeof currentValue === 'boolean'
? 'boolean'
: typeof currentValue === 'string'
? 'string'
: 'number';

switch (this.operation.type) {
case 'show_or_set': {
if (this.operation.option === 'all') {
// TODO: Show all options
} else {
if (type === 'boolean') {
configuration[option] = true;
} else {
this.showOption(vimState, option, currentValue);
}
}
break;
}
case 'show': {
this.showOption(vimState, option, currentValue);
break;
}
case 'unset': {
if (type === 'boolean') {
configuration[option] = false;
} else {
throw VimError.InvalidArgument474(`no${option}`);
}
break;
}
case 'invert': {
if (type === 'boolean') {
configuration[option] = !currentValue;
} else {
// TODO: Could also be {option}!
throw VimError.InvalidArgument474(`inv${option}`);
}
break;
}
case 'default': {
if (this.operation.option === 'all') {
// TODO: Set all options to default
} else {
// TODO: Set the option to default
}
break;
}
case 'equal': {
if (type === 'boolean') {
// TODO: Could also be {option}:{value}
throw VimError.InvalidArgument474(`${option}=${this.operation.value}`);
} else if (type === 'string') {
configuration[option] = this.operation.value;
} else {
const value = Number.parseInt(this.operation.value, 10);
if (isNaN(value)) {
// TODO: Could also be {option}:{value}
throw VimError.NumberRequiredAfterEqual(`${option}=${this.operation.value}`);
}
configuration[option] = value;
}
break;
}
case 'add': {
if (type === 'boolean') {
throw VimError.InvalidArgument474(`${option}+=${this.operation.value}`);
} else if (type === 'string') {
configuration[option] = currentValue + this.operation.value;
} else {
const value = Number.parseInt(this.operation.value, 10);
if (isNaN(value)) {
throw VimError.NumberRequiredAfterEqual(`${option}+=${this.operation.value}`);
}
configuration[option] = (currentValue as number) + value;
}
break;
}
case 'multiply': {
if (type === 'boolean') {
throw VimError.InvalidArgument474(`${option}^=${this.operation.value}`);
} else if (type === 'string') {
configuration[option] = this.operation.value + currentValue;
} else {
const value = Number.parseInt(this.operation.value, 10);
if (isNaN(value)) {
throw VimError.NumberRequiredAfterEqual(`${option}^=${this.operation.value}`);
}
configuration[option] = (currentValue as number) * value;
}
break;
}
case 'subtract': {
if (type === 'boolean') {
throw VimError.InvalidArgument474(`${option}-=${this.operation.value}`);
} else if (type === 'string') {
configuration[option] = (currentValue as string).split(this.operation.value).join('');
} else {
const value = Number.parseInt(this.operation.value, 10);
if (isNaN(value)) {
throw VimError.NumberRequiredAfterEqual(`${option}-=${this.operation.value}`);
}
configuration[option] = (currentValue as number) - value;
}
break;
}
default:
const guard: never = this.operation;
throw new Error('Got unexpected SetOperation.type');
// `show` and `show_or_set` on a non-boolean option need a VimState to
// write to the status bar, so handle those here before delegating the
// mutation to `applyOperationToConfig`.
if (this.operation.type === 'show') {
this.showOption(vimState, option, currentValue);
return;
}

if (option in SetCommand.listeners) {
for (const listener of SetCommand.listeners[option]) {
listener();
}
if (
this.operation.type === 'show_or_set' &&
typeof currentValue !== 'boolean' &&
this.operation.option !== 'all'
) {
this.showOption(vimState, option, currentValue);
return;
}

applyOperationToConfig(configuration, this.operation);
}

private showOption(vimState: VimState, option: string, value: boolean | string | number) {
Expand Down
Loading