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

Copy to and paste from system clipboard #260

Merged
merged 5 commits into from
May 31, 2020
Merged
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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
"default": true,
"description": "Set to `false` to keep VSCode's keybinding when searching."
},
"amVim.useSystemClipboard": {
"type": "boolean",
"default": false,
"description": "Set to `true` to copy to and paste from the system clipboard."
},
"amVim.vimStyleNavigationInListView": {
"type": "boolean",
"default": true,
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/Action.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface Action {
(args?: {}): Thenable<boolean | undefined>;
(args?: {}): Thenable<boolean | undefined> | Promise<boolean | undefined>;
}
65 changes: 54 additions & 11 deletions src/Actions/Register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window, Position, Range } from 'vscode';
import { env, window, Position, Range } from 'vscode';
import { StaticReflect } from '../LanguageExtensions/StaticReflect';
import { SymbolMetadata } from '../Symbols/Metadata';
import { ActionMoveCursor } from './MoveCursor';
Expand All @@ -10,6 +10,7 @@ import { MotionLine } from '../Motions/Line';
import { TextObject } from '../TextObjects/TextObject';
import { UtilRange } from '../Utils/Range';
import { UtilText } from '../Utils/Text';
import { Configuration } from '../Configuration';

export class Register {
readonly text: string;
Expand All @@ -36,7 +37,7 @@ export class ActionRegister {
return ActionRegister.stash;
}

static yankRanges(args: { ranges: Range[]; isLinewise: boolean }): Thenable<boolean> {
static async yankRanges(args: { ranges: Range[]; isLinewise: boolean }): Promise<boolean> {
const activeTextEditor = window.activeTextEditor;

if (!activeTextEditor) {
Expand All @@ -57,6 +58,12 @@ export class ActionRegister {
})
.join('');

if (Configuration.useSystemClipboard === true) {
// Write to clipboard but then continue to allow
// for saving `isLinewise` state
await env.clipboard.writeText(text);
}

ActionRegister.stash = new Register({
text: text,
isLinewise: args.isLinewise,
Expand Down Expand Up @@ -94,7 +101,7 @@ export class ActionRegister {
});
}

static yankByTextObject(args: { textObject: TextObject }): Thenable<boolean> {
static async yankByTextObject(args: { textObject: TextObject }): Promise<boolean> {
const activeTextEditor = window.activeTextEditor;

if (!activeTextEditor) {
Expand Down Expand Up @@ -122,6 +129,12 @@ export class ActionRegister {
})
.join('');

if (Configuration.useSystemClipboard === true) {
// Write to clipboard but then continue to allow
// for saving `isLinewise` state
await env.clipboard.writeText(text);
}

ActionRegister.stash = new Register({
text: text,
isLinewise: args.textObject.isLinewise,
Expand Down Expand Up @@ -170,16 +183,31 @@ export class ActionRegister {
}

@StaticReflect.metadata(SymbolMetadata.Action.isChange, true)
static putAfter(args: { n?: number }): Thenable<boolean> {
static async putAfter(args: { n?: number }): Promise<boolean> {
args.n = args.n === undefined ? 1 : args.n;

if (!ActionRegister.stash) {
const activeTextEditor = window.activeTextEditor;

if (!activeTextEditor) {
return Promise.resolve(false);
}

const activeTextEditor = window.activeTextEditor;
if (Configuration.useSystemClipboard === true) {
const text = await env.clipboard.readText();
const existingStash = ActionRegister.stash || {};

// Don't add a new register if:
// - there is nothing returned from the system clipboard
// - the existing register has the text already
if (text && existingStash.text !== text) {
ActionRegister.stash = new Register({
text,
isLinewise: false,
});
}
}

if (!activeTextEditor) {
if (!ActionRegister.stash) {
return Promise.resolve(false);
}

Expand Down Expand Up @@ -220,16 +248,31 @@ export class ActionRegister {
}

@StaticReflect.metadata(SymbolMetadata.Action.isChange, true)
static putBefore(args: { n?: number }): Thenable<boolean> {
static async putBefore(args: { n?: number }): Promise<boolean> {
args.n = args.n === undefined ? 1 : args.n;

if (!ActionRegister.stash) {
const activeTextEditor = window.activeTextEditor;

if (!activeTextEditor) {
return Promise.resolve(false);
}

const activeTextEditor = window.activeTextEditor;
if (Configuration.useSystemClipboard === true) {
const text = await env.clipboard.readText();
const existingStash = ActionRegister.stash || {};

// Don't add a new register if:
// - there is nothing returned from the system clipboard
// - the existing register has the text already
if (text && existingStash.text !== text) {
ActionRegister.stash = new Register({
text,
isLinewise: false,
});
}
}

if (!activeTextEditor) {
if (!ActionRegister.stash) {
return Promise.resolve(false);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export class Configuration {
return this._smartRelativeLineNumbers;
}

private static _useSystemClipboard: boolean;
static get useSystemClipboard(): boolean {
return this._useSystemClipboard;
}

static init(): void {
if (this.isReady) {
return;
Expand All @@ -43,6 +48,7 @@ export class Configuration {
'smartRelativeLineNumbers',
false,
);
this._useSystemClipboard = this.getExtensionSetting<boolean>('useSystemClipboard', false);

UtilWord.updateCharacterKindCache(
this.getEditorSetting<string>('wordSeparators', '`~!@#$%^&*()-=+[{]}\\|;:\'",.<>/?'),
Expand Down