Skip to content

Commit

Permalink
Fix for onivim#2414 - truncated copy/paste
Browse files Browse the repository at this point in the history
  • Loading branch information
psxpaul committed Aug 20, 2018
1 parent dd4b33d commit 3575709
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
24 changes: 11 additions & 13 deletions browser/src/Editor/NeovimEditor/NeovimEditorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
* Contextual commands for NeovimEditor
*
*/
import * as os from "os"

import { clipboard } from "electron"
import * as Oni from "oni-api"

import { NeovimInstance } from "./../../neovim"
import { CallbackCommand, CommandManager } from "./../../Services/CommandManager"
import { ContextMenuManager } from "./../../Services/ContextMenu"
import { findAllReferences, format, LanguageEditorIntegration } from "./../../Services/Language"
import { replaceAll } from "./../../Utility"

import { Definition } from "./Definition"
import { Rename } from "./Rename"
Expand Down Expand Up @@ -66,14 +62,10 @@ export class NeovimEditorCommands {
})

const pasteContents = async (neovimInstance: NeovimInstance) => {
const textToPaste = clipboard.readText()
const sanitizedText = replaceAll(textToPaste, { "<": "<lt>" })
.split(os.EOL)
.join("<cr>")

await neovimInstance.command("set paste")
await neovimInstance.input(sanitizedText)
await neovimInstance.command("set nopaste")
// const textToPaste = clipboard.readText()
// const sanitizedTextLines = replaceAll(textToPaste, { "'": "''" })
// await neovimInstance.command("let @+='" + sanitizedTextLines + "'")
await neovimInstance.command('normal! "+p')
}

const commands = [
Expand Down Expand Up @@ -116,7 +108,13 @@ export class NeovimEditorCommands {
"editor.clipboard.yank",
"Clipboard: Yank",
"Yank contents to clipboard",
() => this._neovimInstance.input("y"),
() => this._neovimInstance.command('normal! "+y'),
),
new CallbackCommand(
"editor.clipboard.cut",
"Clipboard: Cut",
"Cut contents to clipboard",
() => this._neovimInstance.command('normal! "+x'),
),
new CallbackCommand("oni.editor.findAllReferences", null, null, () =>
findAllReferences(),
Expand Down
2 changes: 2 additions & 0 deletions browser/src/Input/KeyBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const applyDefaultKeyBindings = (oni: Oni.Plugin.Api, config: Configurati
input.bind("<m-,>", "oni.config.openConfigJs")

if (config.getValue("editor.clipboard.enabled")) {
input.bind("<m-x>", "editor.clipboard.cut", isVisualMode)
input.bind("<m-c>", "editor.clipboard.yank", isVisualMode)
input.bind("<m-v>", "editor.clipboard.paste", isInsertOrCommandMode)
}
Expand All @@ -79,6 +80,7 @@ export const applyDefaultKeyBindings = (oni: Oni.Plugin.Api, config: Configurati
input.bind("<c-,>", "oni.config.openConfigJs")

if (config.getValue("editor.clipboard.enabled")) {
input.bind("<c-x>", "editor.clipboard.cut", isVisualMode)
input.bind("<c-c>", "editor.clipboard.yank", isVisualMode)
input.bind("<c-v>", "editor.clipboard.paste", isInsertOrCommandMode)
}
Expand Down
6 changes: 3 additions & 3 deletions main/src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,19 @@ export const buildMenu = (mainWindow, loadInit) => {
{
label: "Copy",
click(item, focusedWindow) {
executeVimCommand(focusedWindow, '\\"+y')
executeOniCommand(focusedWindow, "editor.clipboard.yank")
},
},
{
label: "Cut",
click(item, focusedWindow) {
executeVimCommand(focusedWindow, '\\"+x')
executeOniCommand(focusedWindow, "editor.clipboard.cut")
},
},
{
label: "Paste",
click(item, focusedWindow) {
executeVimCommand(focusedWindow, '\\"+gP')
executeOniCommand(focusedWindow, "editor.clipboard.paste")
},
},
{
Expand Down
1 change: 1 addition & 0 deletions test/CiTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const CiTests = [
"Neovim.CallOniCommands",
"NoInstalledNeovim",
"Sidebar.ToggleSplitTest",
"LargePasteTest",

"Snippets.BasicInsertTest",

Expand Down
72 changes: 72 additions & 0 deletions test/ci/LargePasteTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Test for pasting a large number of lines
*
* Regression test for #2414
*/
import * as Oni from "oni-api"

import { createNewFile, getTemporaryFilePath, navigateToFile } from "./Common"

export const test = async (oni: Oni.Plugin.Api) => {
const filePath = createLargeTestFile()
await oni.automation.waitForEditors()

navigateToFile(filePath, oni)

await oni.automation.sendKeys("<s-v>")
await oni.automation.waitFor(() => oni.editors.activeEditor.mode === "visual", 30000)

await oni.automation.sendKeys("G")
await oni.automation.waitFor(
() => oni.editors.activeEditor.activeBuffer.cursor.line === 1999,
30000,
)

await copy(oni)
await paste(oni)

await oni.automation.waitFor(
() => oni.editors.activeEditor.activeBuffer.lineCount === 4001,
30000,
)
}

import * as fs from "fs"
import * as os from "os"

const createLargeTestFile = (): string => {
const filePath = getTemporaryFilePath("js")
const line =
"this is a line of 'text' that will be repeated a bunch of times to make for a large wall of 'text' to paste"

const lines = []
for (let i = 0; i < 2000; i++) {
lines.push(line)
}

fs.writeFileSync(filePath, lines.join(os.EOL))
return filePath
}

import { isMac } from "../../browser/src/Platform"

const copy = async (oni: Oni.Plugin.Api) => {
if (isMac()) {
await oni.automation.sendKeys("<m-c>")
} else {
await oni.automation.sendKeys("<c-c>")
}

await oni.automation.waitFor(() => oni.editors.activeEditor.mode === "normal", 30000)
}

const paste = async (oni: Oni.Plugin.Api) => {
await oni.automation.sendKeys("o")
await oni.automation.waitFor(() => oni.editors.activeEditor.mode === "insert", 30000)

if (isMac()) {
await oni.automation.sendKeys("<m-v>")
} else {
await oni.automation.sendKeys("<c-v>")
}
}

0 comments on commit 3575709

Please sign in to comment.