From 2daad92cc008f85fd523854ff9fd996af211eb6e Mon Sep 17 00:00:00 2001 From: Ryan C Date: Wed, 12 Jul 2017 17:45:52 +0100 Subject: [PATCH] Open multiple files in tabs/splits. (#526) * Swapped split and tab open to allow multiple file selections at once. * Swaps launch.json to use the .cmd file when in a Windows enviroment. Based on the code from https://code.visualstudio.com/updates/v1_6#_launch-configuration-supports-npm-and-other-tools. * Started moving code over to the renderer process, to add robust file checks. * Add more thorough check to opening of multiple files. * Reverted changes for splits. * Move VimL logic into the Oni Core plugin. --- .vscode/launch.json | 6 ++++++ Menu.js | 8 +++++--- browser/src/Editor/NeovimEditor.tsx | 19 +++++++++++++++++-- vim/core/oni-core-interop/plugin/init.vim | 10 ++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 4c4e0af05f..87f9a70bd1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,9 @@ "cwd": "${workspaceRoot}", "program": "${workspaceRoot}/main.js", "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron", + "windows": { + "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd" + }, "runtimeArgs": [ "--enable-logging" ], @@ -21,6 +24,9 @@ "type": "chrome", // <-- requires Extension "Debugger for Chrome" "request": "launch", "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron", + "windows": { + "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd" + }, "runtimeArgs": [ "--enable-logging", "${workspaceRoot}/main.js" diff --git a/Menu.js b/Menu.js index 488ea4e3a2..5a1c7c19b8 100644 --- a/Menu.js +++ b/Menu.js @@ -15,6 +15,8 @@ const buildMenu = (mainWindow, loadInit) => { const executeVimCommand = (command) => mainWindow.webContents.send("menu-item-click", command) + const executeVimCommandForMultipleFiles = (command, files) => mainWindow.webContents.send("open-files", command, files) + const executeOniCommand = (command) => mainWindow.webContents.send("execute-command", command) const executeVimCommandForFiles = (command, files) => { @@ -57,7 +59,7 @@ const buildMenu = (mainWindow, loadInit) => { { label: 'Split Open...', click: (item, focusedWindow) => { - dialog.showOpenDialog(mainWindow, ['openFile'], (files) => { + dialog.showOpenDialog(mainWindow, 'openFile', (files) => { executeVimCommandForFiles(":sp", files) }) } @@ -65,8 +67,8 @@ const buildMenu = (mainWindow, loadInit) => { { label: 'Tab Open...', click: (item, focusedWindow) => { - dialog.showOpenDialog(mainWindow, ['openFile'], (files) => { - executeVimCommandForFiles(":tabnew", files) + dialog.showOpenDialog(mainWindow, {properties: ['openFile', 'multiSelections']}, (files) => { + executeVimCommandForMultipleFiles(":tabnew ", files) }) } }, diff --git a/browser/src/Editor/NeovimEditor.tsx b/browser/src/Editor/NeovimEditor.tsx index 6d69f3632c..de4f99bb1d 100644 --- a/browser/src/Editor/NeovimEditor.tsx +++ b/browser/src/Editor/NeovimEditor.tsx @@ -283,6 +283,21 @@ export class NeovimEditor implements IEditor { } }) + const normalizePath = (fileName: string) => fileName.split("\\").join("/") + + const openFiles = async (files: string[], action: string) => { + + await this._neovimInstance.callFunction("OniOpenFile", [action, files[0]]) + + for (let i = 1; i < files.length; i++) { + this._neovimInstance.command("exec \"" + action + " " + normalizePath(files[i]) + "\"") + } + } + + ipcRenderer.on("open-files", (_evt: any, message: string, files: string[]) => { + openFiles(files, message) + }) + // enable opening a file via drag-drop document.ondragover = (ev) => { ev.preventDefault() @@ -292,10 +307,10 @@ export class NeovimEditor implements IEditor { let files = ev.dataTransfer.files // open first file in current editor - this._neovimInstance.open(files[0].path.split("\\").join("/")) + this._neovimInstance.open(normalizePath(files[0].path)) // open any subsequent files in new tabs for (let i = 1; i < files.length; i++) { - this._neovimInstance.command("exec \":tabe " + files.item(i).path.split("\\").join("/") + "\"") + this._neovimInstance.command("exec \":tabe " + normalizePath(files.item(i).path) + "\"") } } } diff --git a/vim/core/oni-core-interop/plugin/init.vim b/vim/core/oni-core-interop/plugin/init.vim index 9d57562d22..0a076e85a9 100644 --- a/vim/core/oni-core-interop/plugin/init.vim +++ b/vim/core/oni-core-interop/plugin/init.vim @@ -30,6 +30,16 @@ function OniNotifyEvent(eventName) call OniNotify(["event", a:eventName, context]) endfunction +function OniOpenFile(strategy, file) + if bufname('%') != '' + exec a:strategy . a:file + elseif &modified + exec a:strategy . a:file + else + exec ":e " . a:file + endif +endfunction + augroup OniNotifyBufferUpdates autocmd! autocmd! CursorMovedI * :call OniNotifyBufferUpdate()