From 6e723ddeebb5c893fe4c1b15007108ede0e8b75e Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Thu, 11 Aug 2016 22:37:41 +0200 Subject: [PATCH] GH-228 Add open --- SwiftNeoVim/NeoVimView.swift | 13 +++++++++++++ VimR/AppDelegate.swift | 10 ++++++++++ VimR/MainWindowComponent.swift | 14 ++++++++++---- VimR/MainWindowManager.swift | 5 +++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/SwiftNeoVim/NeoVimView.swift b/SwiftNeoVim/NeoVimView.swift index 6a6e70ee..283e3494 100644 --- a/SwiftNeoVim/NeoVimView.swift +++ b/SwiftNeoVim/NeoVimView.swift @@ -228,6 +228,19 @@ public class NeoVimView: NSView, NSUserInterfaceValidations { self.agent.vimInput(":table") } } + + public func open(url: NSURL) { + guard let path = url.path else { + return + } + + switch self.mode { + case .Normal: + self.agent.vimInput(":e \(path)") + default: + self.agent.vimInput(":e \(path)") + } + } public func openInNewTab(url: NSURL) { guard let path = url.path else { diff --git a/VimR/AppDelegate.swift b/VimR/AppDelegate.swift index cd270051..6355e125 100644 --- a/VimR/AppDelegate.swift +++ b/VimR/AppDelegate.swift @@ -92,5 +92,15 @@ extension AppDelegate { } @IBAction func openDocument(sender: AnyObject!) { + let panel = NSOpenPanel() + panel.canChooseDirectories = true + panel.beginWithCompletionHandler { result in + guard result == NSFileHandlingPanelOKButton else { + return + } + + let url = panel.URLs[0] + self.mainWindowManager.newMainWindow(url) + } } } diff --git a/VimR/MainWindowComponent.swift b/VimR/MainWindowComponent.swift index 9ec6d7b5..be19444f 100644 --- a/VimR/MainWindowComponent.swift +++ b/VimR/MainWindowComponent.swift @@ -23,6 +23,8 @@ class MainWindowComponent: NSObject, NSWindowDelegate, NeoVimViewDelegate, Compo private let windowController = NSWindowController(windowNibName: "MainWindow") private let window: NSWindow + private var urlToBeOpenedWhenReady: NSURL? + private var defaultEditorFont: NSFont private var usesLigatures: Bool @@ -31,13 +33,14 @@ class MainWindowComponent: NSObject, NSWindowDelegate, NeoVimViewDelegate, Compo } private let neoVimView = NeoVimView(forAutoLayout: ()) - - init(source: Observable, manager: MainWindowManager, initialData: PrefData) { + + init(source: Observable, manager: MainWindowManager, url: NSURL? = nil, initialData: PrefData) { self.source = source self.mainWindowManager = manager self.window = self.windowController.window! self.defaultEditorFont = initialData.appearance.editorFont self.usesLigatures = initialData.appearance.editorUsesLigatures + self.urlToBeOpenedWhenReady = url super.init() @@ -49,7 +52,6 @@ class MainWindowComponent: NSObject, NSWindowDelegate, NeoVimViewDelegate, Compo self.window.makeFirstResponder(self.neoVimView) self.windowController.showWindow(self) - } deinit { @@ -83,7 +85,7 @@ extension MainWindowComponent { @IBAction func newTab(sender: AnyObject!) { self.neoVimView.newTab() } - + @IBAction func openInTab(sender: AnyObject!) { let panel = NSOpenPanel() panel.canChooseDirectories = true @@ -131,6 +133,10 @@ extension MainWindowComponent { func neoVimReady() { self.neoVimView.font = self.defaultEditorFont self.neoVimView.usesLigatures = self.usesLigatures + + if let url = self.urlToBeOpenedWhenReady { + self.neoVimView.open(url) + } } func neoVimStopped() { diff --git a/VimR/MainWindowManager.swift b/VimR/MainWindowManager.swift index 3700161d..4d64ccf5 100644 --- a/VimR/MainWindowManager.swift +++ b/VimR/MainWindowManager.swift @@ -22,9 +22,10 @@ class MainWindowManager { self.addReactions() } - func newMainWindow() { - let mainWindowComponent = MainWindowComponent(source: self.source, manager: self, initialData: self.data) + func newMainWindow(url: NSURL? = nil) -> MainWindowComponent { + let mainWindowComponent = MainWindowComponent(source: self.source, manager: self, url: url, initialData: self.data) self.mainWindowComponents[mainWindowComponent.uuid] = mainWindowComponent + return mainWindowComponent } func closeMainWindow(mainWindowComponent: MainWindowComponent) {