mirror of
https://github.com/VSCodeVim/Vim.git
synced 2024-09-11 12:25:59 +03:00
delete range into register
This commit is contained in:
parent
0e82bd195a
commit
99a7c43424
@ -296,7 +296,7 @@ Status | Command | Description
|
|||||||
:white_check_mark: | {visual}J | join the highlighted lines
|
:white_check_mark: | {visual}J | join the highlighted lines
|
||||||
:white_check_mark: | :1234: gJ | like "J", but without inserting spaces
|
:white_check_mark: | :1234: gJ | like "J", but without inserting spaces
|
||||||
:white_check_mark:| {visual}gJ | like "{visual}J", but without inserting spaces
|
:white_check_mark:| {visual}gJ | like "{visual}J", but without inserting spaces
|
||||||
| :[range]d [x] | delete [range] lines [into register x]
|
:white_check_mark:| :[range]d [x] | delete [range] lines [into register x]
|
||||||
|
|
||||||
## Copying and moving text
|
## Copying and moving text
|
||||||
|
|
||||||
|
86
src/cmd_line/commands/deleteRange.ts
Normal file
86
src/cmd_line/commands/deleteRange.ts
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
import * as node from "../node";
|
||||||
|
import * as token from '../token';
|
||||||
|
import { ModeHandler } from "../../mode/modeHandler";
|
||||||
|
import { TextEditor } from "../../textEditor";
|
||||||
|
import { Register, RegisterMode } from '../../register/register';
|
||||||
|
import { Position } from '../../motion/position';
|
||||||
|
|
||||||
|
export interface IDeleteRangeCommandArguments extends node.ICommandArgs {
|
||||||
|
register?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class DeleteRangeCommand extends node.CommandBase {
|
||||||
|
protected _arguments : IDeleteRangeCommandArguments;
|
||||||
|
|
||||||
|
constructor(args : IDeleteRangeCommandArguments) {
|
||||||
|
super();
|
||||||
|
this._name = 'delete';
|
||||||
|
this._shortName = 'd';
|
||||||
|
this._arguments = args;
|
||||||
|
}
|
||||||
|
|
||||||
|
get arguments() : IDeleteRangeCommandArguments{
|
||||||
|
return this._arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteRange(start: Position, end: Position): Promise<string> {
|
||||||
|
start = start.getLineBegin();
|
||||||
|
end = end.getLineEnd();
|
||||||
|
end = Position.FromVSCodePosition(end.with(end.line, end.character + 1));
|
||||||
|
|
||||||
|
const isOnLastLine = end.line === TextEditor.getLineCount() - 1;
|
||||||
|
|
||||||
|
if (end.character === TextEditor.getLineAt(end).text.length + 1) {
|
||||||
|
end = end.getDown(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isOnLastLine && start.line !== 0) {
|
||||||
|
start = start.getPreviousLineBegin().getLineEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
let text = vscode.window.activeTextEditor.document.getText(new vscode.Range(start, end));
|
||||||
|
text = text.endsWith("\r\n") ? text.slice(0, -2) : text.slice(0, -1);
|
||||||
|
await TextEditor.delete(new vscode.Range(start, end));
|
||||||
|
|
||||||
|
let resultPosition = Position.EarlierOf(start, end);
|
||||||
|
if (start.character > TextEditor.getLineAt(start).text.length) {
|
||||||
|
resultPosition = start.getLeft();
|
||||||
|
} else {
|
||||||
|
resultPosition = start;
|
||||||
|
}
|
||||||
|
|
||||||
|
resultPosition = resultPosition.getLineBegin();
|
||||||
|
vscode.window.activeTextEditor.selection = new vscode.Selection(resultPosition, resultPosition);
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
async execute(): Promise<void> {
|
||||||
|
if (!vscode.window.activeTextEditor) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cursorPosition = Position.FromVSCodePosition(vscode.window.activeTextEditor.selection.active);
|
||||||
|
let text = await this.deleteRange(cursorPosition, cursorPosition);
|
||||||
|
Register.putByKey(text, this._arguments.register, RegisterMode.LineWise);
|
||||||
|
}
|
||||||
|
|
||||||
|
async executeWithRange(modeHandler : ModeHandler, range: node.LineRange) {
|
||||||
|
let start: vscode.Position;
|
||||||
|
let end: vscode.Position;
|
||||||
|
|
||||||
|
if (range.left[0].type === token.TokenType.Percent) {
|
||||||
|
start = new vscode.Position(0, 0);
|
||||||
|
end = new vscode.Position(TextEditor.getLineCount() - 1, 0);
|
||||||
|
} else {
|
||||||
|
start = range.lineRefToPosition(vscode.window.activeTextEditor, range.left, modeHandler);
|
||||||
|
end = range.lineRefToPosition(vscode.window.activeTextEditor, range.right, modeHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
let text = await this.deleteRange(Position.FromVSCodePosition(start), Position.FromVSCodePosition(end));
|
||||||
|
Register.putByKey(text, this._arguments.register, RegisterMode.LineWise);
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@ import {parseOptionsCommandArgs} from './subparsers/setoptions';
|
|||||||
import {parseSubstituteCommandArgs} from './subparsers/substitute';
|
import {parseSubstituteCommandArgs} from './subparsers/substitute';
|
||||||
import {parseReadCommandArgs} from './subparsers/read';
|
import {parseReadCommandArgs} from './subparsers/read';
|
||||||
import {parseRegisterCommandArgs} from './subparsers/register';
|
import {parseRegisterCommandArgs} from './subparsers/register';
|
||||||
|
import {parseDeleteRangeLinesCommandArgs} from './subparsers/deleteRange';
|
||||||
|
|
||||||
// maps command names to parsers for said commands.
|
// maps command names to parsers for said commands.
|
||||||
export const commandParsers = {
|
export const commandParsers = {
|
||||||
@ -68,5 +69,8 @@ export const commandParsers = {
|
|||||||
read: parseReadCommandArgs,
|
read: parseReadCommandArgs,
|
||||||
r: parseReadCommandArgs,
|
r: parseReadCommandArgs,
|
||||||
|
|
||||||
reg: parseRegisterCommandArgs
|
reg: parseRegisterCommandArgs,
|
||||||
|
|
||||||
|
d: parseDeleteRangeLinesCommandArgs,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
17
src/cmd_line/subparsers/deleteRange.ts
Normal file
17
src/cmd_line/subparsers/deleteRange.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
import * as node from "../commands/deleteRange";
|
||||||
|
import {Scanner} from '../scanner';
|
||||||
|
|
||||||
|
export function parseDeleteRangeLinesCommandArgs(args: string): node.DeleteRangeCommand {
|
||||||
|
if (!args) {
|
||||||
|
return new node.DeleteRangeCommand({});
|
||||||
|
}
|
||||||
|
|
||||||
|
let scanner = new Scanner(args);
|
||||||
|
let register = scanner.nextWord();
|
||||||
|
|
||||||
|
return new node.DeleteRangeCommand({
|
||||||
|
register: register
|
||||||
|
});
|
||||||
|
}
|
@ -58,6 +58,23 @@ export class Register {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static putByKey(content: string | string[], register?: string, registerMode?: RegisterMode): void {
|
||||||
|
register = register || '"';
|
||||||
|
|
||||||
|
if (!Register.isValidRegister(register)) {
|
||||||
|
throw new Error(`Invalid register ${register}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (register === '*') {
|
||||||
|
clipboard.copy(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
Register.registers[register] = {
|
||||||
|
text : content,
|
||||||
|
registerMode: registerMode || RegisterMode.FigureItOutFromCurrentMode,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets content from a register. If none is specified, uses the default
|
* Gets content from a register. If none is specified, uses the default
|
||||||
* register ".
|
* register ".
|
||||||
|
Loading…
Reference in New Issue
Block a user