1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 02:54:31 +03:00

GH-257 Let neovim clean up when using Cmd-Q

- Refactor NeoVimView slightly
This commit is contained in:
Tae Won Ha 2016-08-20 16:59:30 +02:00
parent 2640d6d216
commit 918c743c89
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
4 changed files with 42 additions and 32 deletions

View File

@ -139,12 +139,7 @@ extension NeoVimView {
}
public func newTab() {
switch self.mode {
case .Normal:
self.agent.vimInput(":tabe<CR>")
default:
self.agent.vimInput("<Esc>:tabe<CR>")
}
self.exec(command: "tabe")
}
public func open(urls urls: [NSURL]) {
@ -152,41 +147,42 @@ extension NeoVimView {
urls.enumerate().forEach { (idx, url) in
if idx == 0 && currentBufferIsTransient {
self.open(url, cmd: ":e")
self.open(url, cmd: "e")
} else {
self.open(url, cmd: ":tabe")
self.open(url, cmd: "tabe")
}
}
}
public func openInNewTab(urls urls: [NSURL]) {
urls.forEach { self.open($0, cmd: ":tabe") }
urls.forEach { self.open($0, cmd: "tabe") }
}
public func closeCurrentTab() {
switch self.mode {
case .Normal:
self.agent.vimInput(":q<CR>")
default:
self.agent.vimInput("<Esc>:q<CR>")
}
self.exec(command: "q")
}
public func closeCurrentTabWithoutSaving() {
switch self.mode {
case .Normal:
self.agent.vimInput(":q!<CR>")
default:
self.agent.vimInput("<Esc>:q!<CR>")
}
self.exec(command: "q!")
}
public func closeAllWindows() {
self.exec(command: "qa")
}
public func closeAllWindowsWithoutSaving() {
self.exec(command: "qa!")
}
/// Does the following
/// - `Mode.Normal`: `:command<CR>`
/// - else: `:<Esc>:command<CR>`
private func exec(command cmd: String) {
switch self.mode {
case .Normal:
self.agent.vimInput(":qa!<CR>")
self.agent.vimInput(":\(cmd)<CR>")
default:
self.agent.vimInput("<Esc>:qa!<CR>")
self.agent.vimInput("<Esc>:\(cmd)<CR>")
}
}
@ -196,13 +192,7 @@ extension NeoVimView {
}
let escapedFileName = self.agent.escapedFileNames([path])[0]
switch self.mode {
case .Normal:
self.agent.vimInput("\(cmd) \(escapedFileName)<CR>")
default:
self.agent.vimInput("<Esc>\(cmd) \(escapedFileName)<CR>")
}
self.exec(command: "\(cmd) \(escapedFileName)")
}
}

View File

@ -108,14 +108,21 @@ extension AppDelegate {
alert.alertStyle = .WarningAlertStyle
if alert.runModal() == NSAlertSecondButtonReturn {
self.mainWindowManager.closeAllWindowsWithoutSaving()
self.quitWhenAllWindowsAreClosed = true
return .TerminateCancel
self.mainWindowManager.closeAllWindowsWithoutSaving()
}
return .TerminateCancel
}
if self.mainWindowManager.hasMainWindow() {
self.quitWhenAllWindowsAreClosed = true
self.mainWindowManager.closeAllWindows()
return .TerminateCancel
}
// There are no open main window, then just quit.
return .TerminateNow
}

View File

@ -62,6 +62,10 @@ class MainWindowComponent: NSObject, NSWindowDelegate, NeoVimViewDelegate, Compo
return self.neoVimView.hasDirtyDocs()
}
func closeAllNeoVimWindows() {
self.neoVimView.closeAllWindows()
}
func closeAllNeoVimWindowsWithoutSaving() {
self.neoVimView.closeAllWindowsWithoutSaving()
}

View File

@ -56,6 +56,15 @@ class MainWindowManager {
self.mainWindowComponents.values.forEach { $0.closeAllNeoVimWindowsWithoutSaving() }
}
/// Assumes that no window is dirty.
func closeAllWindows() {
self.mainWindowComponents.values.forEach { $0.closeAllNeoVimWindows() }
}
func hasMainWindow() -> Bool {
return !self.mainWindowComponents.isEmpty
}
private func addReactions() {
self.source
.filter { $0 is PrefData }