Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve multicursor support #4570

Merged
merged 7 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 7 additions & 14 deletions src/actions/commands/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ export class CommandBackspaceInInsertMode extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const line = TextEditor.getLineAt(position).text;
const selection = TextEditor.getSelection();
const selection = vimState.editor.selections.find(s => s.contains(position));

if (!selection.isEmpty) {
if (selection && !selection.isEmpty) {
// If a selection is active, delete it
vimState.recordedState.transformations.push({
type: 'deleteRange',
Expand Down Expand Up @@ -293,18 +293,11 @@ export class CommandInsertInInsertMode extends BaseCommand {
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const char = this.keysPressed[this.keysPressed.length - 1];

if (vimState.isMultiCursor) {
vimState.recordedState.transformations.push({
type: 'insertText',
text: char,
position: vimState.cursorStopPosition,
});
} else {
vimState.recordedState.transformations.push({
type: 'insertTextVSCode',
text: char,
});
}
vimState.recordedState.transformations.push({
type: 'insertTextVSCode',
text: char,
isMultiCursor: vimState.isMultiCursor,
});

return vimState;
}
Expand Down
31 changes: 30 additions & 1 deletion src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import {
areAnyTransformationsOverlapping,
isTextTransformation,
TextTransformations,
areAllSameTransformation,
isMultiCursorTextTransformation,
InsertTextVSCodeTransformation,
} from './../transformations/transformations';
import { globalState } from '../state/globalState';
import { reportSearch } from '../util/statusBarTextUtils';
Expand Down Expand Up @@ -834,7 +837,13 @@ export class ModeHandler implements vscode.Disposable {
const textTransformations: TextTransformations[] = transformations.filter(x =>
isTextTransformation(x)
) as any;
const otherTransformations = transformations.filter(x => !isTextTransformation(x));
const multicursorTextTransformations: InsertTextVSCodeTransformation[] = transformations.filter(
x => isMultiCursorTextTransformation(x)
) as any;

const otherTransformations = transformations.filter(
x => !isTextTransformation(x) && !isMultiCursorTextTransformation(x)
);

let accumulatedPositionDifferences: { [key: number]: PositionDiff[] } = {};

Expand Down Expand Up @@ -907,6 +916,26 @@ export class ModeHandler implements vscode.Disposable {
}
}

if (multicursorTextTransformations.length > 0) {
if (areAllSameTransformation(multicursorTextTransformations)) {
/**
* Apply the transformation only once instead of to each cursor
* if they are all the same.
*
* This lets VSCode do multicursor snippets, auto braces and
* all the usual jazz VSCode does on text insertion.
*/
const { text } = multicursorTextTransformations[0];

// await vscode.commands.executeCommand('default:type', { text });
await TextEditor.insert(text);
} else {
this._logger.warn(
`Unhandled multicursor transformations. Not all transformations are the same!`
);
}
}

for (const command of otherTransformations) {
switch (command.type) {
case 'insertTextVSCode':
Expand Down
21 changes: 21 additions & 0 deletions src/transformations/transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ export interface InsertTextVSCodeTransformation {
*/
text: string;

/**
* Whether this transformation was created in multicursor mode.
*/
isMultiCursor?: boolean;

/**
* The index of the cursor that this transformation applies to.
*/
Expand Down Expand Up @@ -291,6 +296,9 @@ export const isTextTransformation = (x: Transformation): x is TextTransformation
x.type === 'moveCursor'
);
};
export const isMultiCursorTextTransformation = (x: Transformation): Boolean => {
return (x.type === 'insertTextVSCode' && x.isMultiCursor) ?? false;
};

const getRangeFromTextTransformation = (transformation: TextTransformations): Range | undefined => {
J-Fields marked this conversation as resolved.
Show resolved Hide resolved
switch (transformation.type) {
Expand Down Expand Up @@ -330,3 +338,16 @@ export const areAnyTransformationsOverlapping = (transformations: TextTransforma

return false;
};

export const areAllSameTransformation = (transformations: Transformation[]): Boolean => {
const firstTransformation = transformations[0];

return transformations.every(t => {
for (const [key, value] of Object.entries(firstTransformation)) {
J-Fields marked this conversation as resolved.
Show resolved Hide resolved
if (t[key] !== value) {
return false;
}
}
return true;
});
};