Skip to content

Commit

Permalink
Implement the rest of the surround plugin
Browse files Browse the repository at this point in the history
- Implement the following keys from the surround plugin:
'cS', 'yS' (operator), 'ySs', 'ySS' (same as 'ySs') and 'gS' (visual mode)
- Now the only missing parts of surround plugin is the insert mode
mappings, but those aren't that important anyway.
- Still missing tests for these new actions
  • Loading branch information
berknam committed Jul 9, 2020
1 parent 3450102 commit 9fbd5a1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
110 changes: 102 additions & 8 deletions src/actions/plugins/surround.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ async function StartSurroundMode(
operatorString: 'change' | 'delete' | 'yank' | undefined = undefined,
keys: string[],
start: Position | undefined,
end: Position | undefined
end: Position | undefined,
registerMode: RegisterMode | undefined
): Promise<VimState> {
// Only execute the action if the configuration is set
if (!configuration.surround || !operatorString || keys.length === 0) {
Expand Down Expand Up @@ -151,6 +152,7 @@ async function StartSurroundMode(
replacement: undefined,
range: range,
previousMode: vimState.currentMode,
forcedRegisterMode: registerMode,
};

if (operatorString === 'yank' && start) {
Expand Down Expand Up @@ -180,7 +182,14 @@ class BaseSurroundCommand extends BaseCommand {
if (!this.operatorString || this.keys.length === 0) {
return vimState;
}
return StartSurroundMode(vimState, this.operatorString, this.keys, undefined, undefined);
return StartSurroundMode(
vimState,
this.operatorString,
this.keys,
undefined,
undefined,
undefined
);
}

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
Expand All @@ -200,6 +209,25 @@ class CommandChangeSurround extends BaseSurroundCommand {
operatorString: 'change' | 'delete' | 'yank' | undefined = 'change';
}

@RegisterPluginAction('surround')
class CommandChangeSurroundWithLineBreaks extends BaseSurroundCommand {
modes = [Mode.Normal];
pluginActionDefaultKeys = ['c', 'S'];
keys = ['<Plug>CSurround'];
operatorString: 'change' | 'delete' | 'yank' | undefined = 'change';

public async exec(position: Position, vimState: VimState): Promise<VimState> {
return StartSurroundMode(
vimState,
'change',
this.keys,
undefined,
undefined,
RegisterMode.LineWise
);
}
}

@RegisterPluginAction('surround')
class CommandDeleteSurround extends BaseSurroundCommand {
modes = [Mode.Normal];
Expand All @@ -220,7 +248,23 @@ class CommandSurroundModeStartVisual extends BaseSurroundCommand {
if (vimState.currentMode === Mode.VisualLine) {
[start, end] = [start.getLineBegin(), end.getLineEnd()];
}
return StartSurroundMode(vimState, 'yank', this.keys, start, end);
return StartSurroundMode(vimState, 'yank', this.keys, start, end, undefined);
}
}

@RegisterPluginAction('surround')
class CommandSurroundModeStartVisualWithLineBreaks extends BaseSurroundCommand {
modes = [Mode.Visual, Mode.VisualLine];
pluginActionDefaultKeys = ['g', 'S'];
keys = ['<Plug>VgSurround'];
operatorString: 'change' | 'delete' | 'yank' | undefined = 'yank';

public async exec(position: Position, vimState: VimState): Promise<VimState> {
let [start, end] = Position.sorted(vimState.cursorStartPosition, vimState.cursorStopPosition);
if (vimState.currentMode === Mode.VisualLine) {
[start, end] = [start.getLineBegin(), end.getLineEnd()];
}
return StartSurroundMode(vimState, 'yank', this.keys, start, end, RegisterMode.LineWise);
}
}

Expand All @@ -233,7 +277,23 @@ class CommandSurroundModeStartLine extends BaseSurroundCommand {
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const start: Position = position.getLineBeginRespectingIndent();
const end: Position = position.getLineEnd().getLastWordEnd().getRight();
return StartSurroundMode(vimState, 'yank', this.keys, start, end);
return StartSurroundMode(vimState, 'yank', this.keys, start, end, undefined);
}
}

@RegisterPluginAction('surround')
class CommandSurroundModeStartLineWithLineBreaks extends BaseSurroundCommand {
modes = [Mode.Normal];
pluginActionDefaultKeys = [
['y', 'S', 's'],
['y', 'S', 'S'],
];
keys = ['<Plug>YSsurround'];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const start: Position = position.getLineBeginRespectingIndent();
const end: Position = position.getLineEnd().getLastWordEnd().getRight();
return StartSurroundMode(vimState, 'yank', this.keys, start, end, RegisterMode.LineWise);
}
}

Expand All @@ -248,7 +308,7 @@ class SurroundModeStartOperator extends BaseOperator {
}

public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
return StartSurroundMode(vimState, 'yank', this.keys, start, end);
return StartSurroundMode(vimState, 'yank', this.keys, start, end, undefined);
}

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
Expand All @@ -260,6 +320,17 @@ class SurroundModeStartOperator extends BaseOperator {
}
}

@RegisterPluginAction('surround')
class SurroundModeStartOperatorWithLineBreaks extends SurroundModeStartOperator {
modes = [Mode.Normal];
pluginActionDefaultKeys = ['y', 'S'];
keys = ['<Plug>YSurround'];

public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
return StartSurroundMode(vimState, 'yank', this.keys, start, end, RegisterMode.LineWise);
}
}

@RegisterAction
export class CommandSurroundAddToReplacement extends BaseCommand {
modes = [Mode.SurroundInputMode];
Expand Down Expand Up @@ -470,7 +541,10 @@ export class CommandSurroundAddToReplacement extends BaseCommand {
stop = stop.getRight();
}

if (vimState.surround.previousMode === Mode.VisualLine) {
if (
vimState.surround.previousMode === Mode.VisualLine ||
vimState.surround.forcedRegisterMode === RegisterMode.LineWise
) {
startReplace = startReplace + '\n';
endReplace = '\n' + endReplace;
}
Expand Down Expand Up @@ -516,6 +590,11 @@ export class CommandSurroundAddToReplacement extends BaseCommand {

startReplaceRange = new Range(start, start.getRight());
endReplaceRange = new Range(stop, stop.getRight());

if (vimState.surround?.forcedRegisterMode === RegisterMode.LineWise) {
startReplace = startReplace + '\n';
endReplace = '\n' + endReplace;
}
}

const pairedMatchings: {
Expand Down Expand Up @@ -548,6 +627,11 @@ export class CommandSurroundAddToReplacement extends BaseCommand {
if (target === open) {
CommandSurroundAddToReplacement.RemoveWhitespace(vimState, start, stop);
}

if (vimState.surround?.forcedRegisterMode === RegisterMode.LineWise) {
startReplace = startReplace + '\n';
endReplace = '\n' + endReplace;
}
}

if (target === 't') {
Expand Down Expand Up @@ -579,6 +663,11 @@ export class CommandSurroundAddToReplacement extends BaseCommand {
}

endDeleteRange = new Range(innerTagContent.stop.getRight(), stop);

if (vimState.surround?.forcedRegisterMode === RegisterMode.LineWise) {
startReplace = startReplace + '\n';
endReplace = '\n' + endReplace;
}
}

if (operator === 'change') {
Expand Down Expand Up @@ -612,10 +701,15 @@ export class CommandSurroundAddToReplacement extends BaseCommand {
return CommandSurroundAddToReplacement.Finish(vimState);
}

if (addNewline === 'end-only' || addNewline === 'both') {
let isForcedLineWise = false;
if (vimState.surround?.forcedRegisterMode === RegisterMode.LineWise) {
isForcedLineWise = true;
}

if (addNewline === 'end-only' || addNewline === 'both' || isForcedLineWise) {
endReplace = '\n' + endReplace;
}
if (addNewline === 'both') {
if (addNewline === 'both' || isForcedLineWise) {
startReplace += '\n';
}

Expand Down
1 change: 1 addition & 0 deletions src/state/vimState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class VimState implements vscode.Disposable {
replacement: string | undefined;
range: Range | undefined;
previousMode: Mode;
forcedRegisterMode: RegisterMode | undefined;
} = undefined;

/**
Expand Down

0 comments on commit 9fbd5a1

Please sign in to comment.