From a0e36e33a7c616ee50a8f1f2a4d44f222fc36d20 Mon Sep 17 00:00:00 2001 From: Sujit Joshi Date: Mon, 10 Dec 2018 09:54:35 +0530 Subject: [PATCH 1/5] Added Toggle Tail config in Debug. --- Sources/XiEditor/AppDelegate.swift | 7 +++++++ Sources/XiEditor/Client.swift | 3 +++ Sources/XiEditor/EditViewController.swift | 25 +++++++++++++++++++++++ Sources/XiEditor/Main.storyboard | 7 +++++++ Sources/XiEditor/RPCSending.swift | 9 ++++++++ Sources/XiEditor/XiCore.swift | 7 +++++++ 6 files changed, 58 insertions(+) diff --git a/Sources/XiEditor/AppDelegate.swift b/Sources/XiEditor/AppDelegate.swift index 7de0a7fc..97762ad1 100644 --- a/Sources/XiEditor/AppDelegate.swift +++ b/Sources/XiEditor/AppDelegate.swift @@ -252,6 +252,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { case install = "Install 'xi' Shell Command" case remove = "Remove 'xi' Shell Command" } + + func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { + DispatchQueue.main.async { [weak self] in + let document = self?.documentForViewIdentifier(viewIdentifier: viewIdentifier) + document?.editViewController?.toggleTailConfigChanged(isTailEnabled) + } + } //MARK: - top-level interactions @IBAction func openPreferences(_ sender: NSMenuItem) { diff --git a/Sources/XiEditor/Client.swift b/Sources/XiEditor/Client.swift index 6c164225..973b9cec 100644 --- a/Sources/XiEditor/Client.swift +++ b/Sources/XiEditor/Client.swift @@ -86,4 +86,7 @@ protocol XiClient: AnyObject { /// A notification containing the current replace status. func replaceStatus(viewIdentifier: String, status: ReplaceStatus) + + /// A notification telling toggle tail config was successfully changed. + func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) } diff --git a/Sources/XiEditor/EditViewController.swift b/Sources/XiEditor/EditViewController.swift index d4db2b66..6f069c5e 100644 --- a/Sources/XiEditor/EditViewController.swift +++ b/Sources/XiEditor/EditViewController.swift @@ -152,6 +152,12 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc updateLanguageMenu() } } + + var isTailEnabled: Bool = false { + didSet { + updateTailMenu() + } + } // used to calculate the gutter width. Initial -1 so that a new document // still triggers update of gutter width. @@ -711,6 +717,10 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc document.xiCore.setTheme(themeName: sender.title) } + @IBAction func debugToggleTail(_ sender: NSMenuItem) { + document.xiCore.toggleTailConfig(identifier: document.coreViewIdentifier!, enabled: !self.isTailEnabled) + } + @IBAction func debugSetLanguage(_ sender: NSMenuItem) { guard sender.state != NSControl.StateValue.on else { print("language already active"); return } document.xiCore.setLanguage(identifier: document.coreViewIdentifier!, languageName: sender.title) @@ -840,11 +850,22 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc item.state = findViewController.showMultipleSearchQueries ? .on : .off } + func updateTailMenu() { + let toggleTailSubMenu = NSApplication.shared.mainMenu!.item(withTitle: "Debug")!.submenu!.item(withTitle: "Tail File") + + if self.isTailEnabled { + toggleTailSubMenu!.state = NSControl.StateValue.on + } else { + toggleTailSubMenu!.state = NSControl.StateValue.off + } + } + // Gets called when active window changes func updateMenuState() { updatePluginMenu() updateLanguageMenu() updateFindMenu() + updateTailMenu() } @objc func handleCommand(_ sender: NSMenuItem) { @@ -919,6 +940,10 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc languagesMenu.addItem(item) } } + + public func toggleTailConfigChanged(_ isTailEnabled: Bool) { + self.isTailEnabled = isTailEnabled + } @IBAction func gotoLine(_ sender: AnyObject) { guard let window = self.view.window else { return } diff --git a/Sources/XiEditor/Main.storyboard b/Sources/XiEditor/Main.storyboard index 1dfe620d..fa5a920c 100644 --- a/Sources/XiEditor/Main.storyboard +++ b/Sources/XiEditor/Main.storyboard @@ -1209,6 +1209,12 @@ Gw + + + + + + @@ -1238,6 +1244,7 @@ Gw + diff --git a/Sources/XiEditor/RPCSending.swift b/Sources/XiEditor/RPCSending.swift index 7dcc44bb..17afc403 100644 --- a/Sources/XiEditor/RPCSending.swift +++ b/Sources/XiEditor/RPCSending.swift @@ -309,6 +309,15 @@ class StdoutRPCSender: RPCSending { self.client?.findStatus(viewIdentifier: viewIdentifier, status: status) case let .replaceStatus(viewIdentifier, status): self.client?.replaceStatus(viewIdentifier: viewIdentifier, status: status) + case "toggle_tail_config_changed": + let isTailEnabled = params["is_tail_enabled"] as! Bool + client?.toggleTailConfigChanged( + viewIdentifier: viewIdentifier!, + isTailEnabled: isTailEnabled + ) + + default: + print("unknown notification \(method)") } } diff --git a/Sources/XiEditor/XiCore.swift b/Sources/XiEditor/XiCore.swift index dd120d58..02dbbb4f 100644 --- a/Sources/XiEditor/XiCore.swift +++ b/Sources/XiEditor/XiCore.swift @@ -48,6 +48,9 @@ protocol XiCore: class { /// `Document` calls are migrated to it's own protocol. func sendRpcAsync(_ method: String, params: Any, callback: RpcCallback?) func sendRpc(_ method: String, params: Any) -> RpcResult + /// Will tail opened file if enabled. + /// If toggle succeeds the client will receive a `toggle_tail_config_changed` notification. + func toggleTailConfig(identifier: ViewIdentifier, enabled: Bool) } final class CoreConnection: XiCore { @@ -109,6 +112,10 @@ final class CoreConnection: XiCore { let params = ["destination": destination, "frontend_samples": frontendSamples] as AnyObject sendRpcAsync("save_trace", params: params) } + + func toggleTailConfig(identifier: ViewIdentifier, enabled: Bool) { + sendRpcAsync("toggle_tail", params: ["view_id": identifier, "enabled": enabled]) + } func sendRpcAsync(_ method: String, params: Any, callback: RpcCallback? = nil) { rpcSender.sendRpcAsync(method, params: params, callback: callback) From cd19a9699d2250e771e25ed34a96ace900f88a79 Mon Sep 17 00:00:00 2001 From: Sujit Joshi Date: Tue, 17 Dec 2019 12:30:12 -0500 Subject: [PATCH 2/5] Merging changes from Upstream and other refactoring changes. --- Sources/XiEditor/AppDelegate.swift | 7 ------- Sources/XiEditor/ClientImplementation.swift | 8 ++++++++ Sources/XiEditor/RPCSending.swift | 5 +---- Tests/IntegrationTests/TestClientImplementation.swift | 6 ++++++ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Sources/XiEditor/AppDelegate.swift b/Sources/XiEditor/AppDelegate.swift index 97762ad1..7de0a7fc 100644 --- a/Sources/XiEditor/AppDelegate.swift +++ b/Sources/XiEditor/AppDelegate.swift @@ -252,13 +252,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { case install = "Install 'xi' Shell Command" case remove = "Remove 'xi' Shell Command" } - - func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { - DispatchQueue.main.async { [weak self] in - let document = self?.documentForViewIdentifier(viewIdentifier: viewIdentifier) - document?.editViewController?.toggleTailConfigChanged(isTailEnabled) - } - } //MARK: - top-level interactions @IBAction func openPreferences(_ sender: NSMenuItem) { diff --git a/Sources/XiEditor/ClientImplementation.swift b/Sources/XiEditor/ClientImplementation.swift index 02cfa0c9..c3c323da 100644 --- a/Sources/XiEditor/ClientImplementation.swift +++ b/Sources/XiEditor/ClientImplementation.swift @@ -176,6 +176,14 @@ class ClientImplementation: XiClient, DocumentsProviding, ConfigCacheProviding, } } } + + func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { + DispatchQueue.main.async { [weak self] in + let document = self?.documentForViewIdentifier(viewIdentifier: viewIdentifier) + document?.editViewController?.toggleTailConfigChanged(isTailEnabled) + } + } + // Stores the config dict so new windows don't have to wait for core to send it. // The main purpose of this is ensuring that `unified_titlebar` applies immediately. diff --git a/Sources/XiEditor/RPCSending.swift b/Sources/XiEditor/RPCSending.swift index 17afc403..5abe205f 100644 --- a/Sources/XiEditor/RPCSending.swift +++ b/Sources/XiEditor/RPCSending.swift @@ -309,15 +309,12 @@ class StdoutRPCSender: RPCSending { self.client?.findStatus(viewIdentifier: viewIdentifier, status: status) case let .replaceStatus(viewIdentifier, status): self.client?.replaceStatus(viewIdentifier: viewIdentifier, status: status) - case "toggle_tail_config_changed": + case .toggleTailConfigChanged: let isTailEnabled = params["is_tail_enabled"] as! Bool client?.toggleTailConfigChanged( viewIdentifier: viewIdentifier!, isTailEnabled: isTailEnabled ) - - default: - print("unknown notification \(method)") } } diff --git a/Tests/IntegrationTests/TestClientImplementation.swift b/Tests/IntegrationTests/TestClientImplementation.swift index 4cde8f95..a9e17ba5 100644 --- a/Tests/IntegrationTests/TestClientImplementation.swift +++ b/Tests/IntegrationTests/TestClientImplementation.swift @@ -86,4 +86,10 @@ class TestClientImplementation: XiClient { func replaceStatus(viewIdentifier: String, status: ReplaceStatus) { } + + func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { + } + + func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { + } } From 8a2135c0067b769d17dde0f91244ad343ca35fd3 Mon Sep 17 00:00:00 2001 From: Sujit Joshi Date: Sat, 23 Nov 2019 17:32:39 -0500 Subject: [PATCH 3/5] Handling toggleTailChanged notification from core. --- Sources/XiEditor/ClientImplementation.swift | 4 ++-- Sources/XiEditor/Core/CoreNotification.swift | 6 ++++++ Sources/XiEditor/RPCSending.swift | 8 ++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Sources/XiEditor/ClientImplementation.swift b/Sources/XiEditor/ClientImplementation.swift index c3c323da..62f5c433 100644 --- a/Sources/XiEditor/ClientImplementation.swift +++ b/Sources/XiEditor/ClientImplementation.swift @@ -178,8 +178,8 @@ class ClientImplementation: XiClient, DocumentsProviding, ConfigCacheProviding, } func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { - DispatchQueue.main.async { [weak self] in - let document = self?.documentForViewIdentifier(viewIdentifier: viewIdentifier) + let document = documentForViewIdentifier(viewIdentifier: viewIdentifier) + DispatchQueue.main.async { document?.editViewController?.toggleTailConfigChanged(isTailEnabled) } } diff --git a/Sources/XiEditor/Core/CoreNotification.swift b/Sources/XiEditor/Core/CoreNotification.swift index d7422565..f8be8d5c 100644 --- a/Sources/XiEditor/Core/CoreNotification.swift +++ b/Sources/XiEditor/Core/CoreNotification.swift @@ -71,6 +71,7 @@ enum CoreNotification { case findStatus(viewIdentifier: ViewIdentifier, status: [FindStatus]) case replaceStatus(viewIdentifier: ViewIdentifier, status: ReplaceStatus) + case toggleTailChanged(viewIdentifier: ViewIdentifier, isTailEnabled: Bool) static func fromJson(_ json: [String: Any]) -> CoreNotification? { guard @@ -228,6 +229,11 @@ enum CoreNotification { { return .replaceStatus(viewIdentifier: viewIdentifier!, status: replaceStatus) } + case "toggle_tail_config_changed": + if let isTailEnabled = jsonParams["is_tail_enabled"] as? Bool + { + return .toggleTailChanged(viewIdentifier: viewIdentifier!, isTailEnabled: isTailEnabled) + } default: assertionFailure("Unsupported notification method from core: \(jsonMethod)") diff --git a/Sources/XiEditor/RPCSending.swift b/Sources/XiEditor/RPCSending.swift index 5abe205f..4a2c10a8 100644 --- a/Sources/XiEditor/RPCSending.swift +++ b/Sources/XiEditor/RPCSending.swift @@ -309,12 +309,8 @@ class StdoutRPCSender: RPCSending { self.client?.findStatus(viewIdentifier: viewIdentifier, status: status) case let .replaceStatus(viewIdentifier, status): self.client?.replaceStatus(viewIdentifier: viewIdentifier, status: status) - case .toggleTailConfigChanged: - let isTailEnabled = params["is_tail_enabled"] as! Bool - client?.toggleTailConfigChanged( - viewIdentifier: viewIdentifier!, - isTailEnabled: isTailEnabled - ) + case let .toggleTailChanged(viewIdentifier, isTailEnabled): + self.client?.toggleTailConfigChanged(viewIdentifier: viewIdentifier, isTailEnabled: isTailEnabled) } } From 63dfd41edec047c54f7a829fba1fac66e3ebfa65 Mon Sep 17 00:00:00 2001 From: Sujit Joshi Date: Tue, 17 Dec 2019 12:33:54 -0500 Subject: [PATCH 4/5] Refactoring. Removing toggleTailConfigChanged added as a dup during merge. --- Tests/IntegrationTests/TestClientImplementation.swift | 3 --- 1 file changed, 3 deletions(-) diff --git a/Tests/IntegrationTests/TestClientImplementation.swift b/Tests/IntegrationTests/TestClientImplementation.swift index a9e17ba5..00e6a33b 100644 --- a/Tests/IntegrationTests/TestClientImplementation.swift +++ b/Tests/IntegrationTests/TestClientImplementation.swift @@ -89,7 +89,4 @@ class TestClientImplementation: XiClient { func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { } - - func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { - } } From c7460436bb57dfa1b707647130148360e8717b36 Mon Sep 17 00:00:00 2001 From: Sujit Joshi Date: Thu, 19 Dec 2019 16:47:31 -0500 Subject: [PATCH 5/5] Disabling "Tail" option when file doesn't exist 1) "Tail" option under Debug is grayed out if a file doesnt exist. No point in tailing such files. 2) Refactoring based on comments. --- Sources/XiEditor/Client.swift | 4 +-- Sources/XiEditor/ClientImplementation.swift | 4 +-- Sources/XiEditor/Core/CoreNotification.swift | 6 ++-- Sources/XiEditor/EditViewController.swift | 30 +++++++++++++------ Sources/XiEditor/Main.storyboard | 4 +-- Sources/XiEditor/RPCSending.swift | 4 +-- Sources/XiEditor/XiDocumentController.swift | 5 ++++ .../TestClientImplementation.swift | 2 +- 8 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Sources/XiEditor/Client.swift b/Sources/XiEditor/Client.swift index 973b9cec..c6511e63 100644 --- a/Sources/XiEditor/Client.swift +++ b/Sources/XiEditor/Client.swift @@ -87,6 +87,6 @@ protocol XiClient: AnyObject { /// A notification containing the current replace status. func replaceStatus(viewIdentifier: String, status: ReplaceStatus) - /// A notification telling toggle tail config was successfully changed. - func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) + /// A notification telling whether tail was enabled/disabled. + func enableTailing(viewIdentifier: String, isTailEnabled: Bool) } diff --git a/Sources/XiEditor/ClientImplementation.swift b/Sources/XiEditor/ClientImplementation.swift index 62f5c433..b514390d 100644 --- a/Sources/XiEditor/ClientImplementation.swift +++ b/Sources/XiEditor/ClientImplementation.swift @@ -177,10 +177,10 @@ class ClientImplementation: XiClient, DocumentsProviding, ConfigCacheProviding, } } - func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { + func enableTailing(viewIdentifier: String, isTailEnabled: Bool) { let document = documentForViewIdentifier(viewIdentifier: viewIdentifier) DispatchQueue.main.async { - document?.editViewController?.toggleTailConfigChanged(isTailEnabled) + document?.editViewController?.enableTailing(isTailEnabled) } } diff --git a/Sources/XiEditor/Core/CoreNotification.swift b/Sources/XiEditor/Core/CoreNotification.swift index f8be8d5c..e42e3971 100644 --- a/Sources/XiEditor/Core/CoreNotification.swift +++ b/Sources/XiEditor/Core/CoreNotification.swift @@ -71,7 +71,7 @@ enum CoreNotification { case findStatus(viewIdentifier: ViewIdentifier, status: [FindStatus]) case replaceStatus(viewIdentifier: ViewIdentifier, status: ReplaceStatus) - case toggleTailChanged(viewIdentifier: ViewIdentifier, isTailEnabled: Bool) + case enableTailing(viewIdentifier: ViewIdentifier, isTailEnabled: Bool) static func fromJson(_ json: [String: Any]) -> CoreNotification? { guard @@ -229,10 +229,10 @@ enum CoreNotification { { return .replaceStatus(viewIdentifier: viewIdentifier!, status: replaceStatus) } - case "toggle_tail_config_changed": + case "enable_tailing": if let isTailEnabled = jsonParams["is_tail_enabled"] as? Bool { - return .toggleTailChanged(viewIdentifier: viewIdentifier!, isTailEnabled: isTailEnabled) + return .enableTailing(viewIdentifier: viewIdentifier!, isTailEnabled: isTailEnabled) } default: diff --git a/Sources/XiEditor/EditViewController.swift b/Sources/XiEditor/EditViewController.swift index 6f069c5e..695e86e3 100644 --- a/Sources/XiEditor/EditViewController.swift +++ b/Sources/XiEditor/EditViewController.swift @@ -153,11 +153,10 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc } } - var isTailEnabled: Bool = false { - didSet { - updateTailMenu() - } - } + /// Flag to determine if current file is being tailed. + var isTailEnabled: Bool = false + + var isExistingFile: Bool = false // used to calculate the gutter width. Initial -1 so that a new document // still triggers update of gutter width. @@ -335,6 +334,12 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc _previousViewportSize = CGSize.zero redrawEverything() } + + func enableTailMenu() { + let toggleTailSubMenu = NSApplication.shared.mainMenu!.item(withTitle: "Debug")!.submenu!.item(withTitle: "Tail File") + toggleTailSubMenu!.isEnabled = true + self.isExistingFile = true + } /// If font size or theme changes, we invalidate all views. func redrawEverything() { @@ -852,11 +857,17 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc func updateTailMenu() { let toggleTailSubMenu = NSApplication.shared.mainMenu!.item(withTitle: "Debug")!.submenu!.item(withTitle: "Tail File") - - if self.isTailEnabled { - toggleTailSubMenu!.state = NSControl.StateValue.on + if self.isExistingFile { + toggleTailSubMenu!.isEnabled = true + if self.isTailEnabled { + toggleTailSubMenu!.state = NSControl.StateValue.on + } else { + toggleTailSubMenu!.state = NSControl.StateValue.off + } } else { + toggleTailSubMenu!.isEnabled = false toggleTailSubMenu!.state = NSControl.StateValue.off + self.isTailEnabled = false } } @@ -941,8 +952,9 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc } } - public func toggleTailConfigChanged(_ isTailEnabled: Bool) { + public func enableTailing(_ isTailEnabled: Bool) { self.isTailEnabled = isTailEnabled + self.updateTailMenu() } @IBAction func gotoLine(_ sender: AnyObject) { diff --git a/Sources/XiEditor/Main.storyboard b/Sources/XiEditor/Main.storyboard index fa5a920c..c802a991 100644 --- a/Sources/XiEditor/Main.storyboard +++ b/Sources/XiEditor/Main.storyboard @@ -1111,7 +1111,7 @@ Gw - + @@ -1209,7 +1209,7 @@ Gw - + diff --git a/Sources/XiEditor/RPCSending.swift b/Sources/XiEditor/RPCSending.swift index 4a2c10a8..88d6331b 100644 --- a/Sources/XiEditor/RPCSending.swift +++ b/Sources/XiEditor/RPCSending.swift @@ -309,8 +309,8 @@ class StdoutRPCSender: RPCSending { self.client?.findStatus(viewIdentifier: viewIdentifier, status: status) case let .replaceStatus(viewIdentifier, status): self.client?.replaceStatus(viewIdentifier: viewIdentifier, status: status) - case let .toggleTailChanged(viewIdentifier, isTailEnabled): - self.client?.toggleTailConfigChanged(viewIdentifier: viewIdentifier, isTailEnabled: isTailEnabled) + case let .enableTailing(viewIdentifier, isTailEnabled): + self.client?.enableTailing(viewIdentifier: viewIdentifier, isTailEnabled: isTailEnabled) } } diff --git a/Sources/XiEditor/XiDocumentController.swift b/Sources/XiEditor/XiDocumentController.swift index 6a5a2a97..c7d875f4 100644 --- a/Sources/XiEditor/XiDocumentController.swift +++ b/Sources/XiEditor/XiDocumentController.swift @@ -133,6 +133,7 @@ class XiDocumentController: NSDocumentController, AlertPresenting { currentDocument.fileURL = url self.setIdentifier(result, forDocument: currentDocument) currentDocument.editViewController!.prepareForReuse() + currentDocument.editViewController!.enableTailMenu() completionHandler(currentDocument, false, nil) case .error(let error): @@ -182,6 +183,10 @@ class XiDocumentController: NSDocumentController, AlertPresenting { let viewIdentifier = result as! String self.setIdentifier(viewIdentifier, forDocument: document) document.coreViewIdentifier = viewIdentifier + if url != nil { + document.editViewController?.isExistingFile = true + } + document.editViewController?.updateTailMenu() case .error(let error): document.close() self.showAlert(with: error.message) diff --git a/Tests/IntegrationTests/TestClientImplementation.swift b/Tests/IntegrationTests/TestClientImplementation.swift index 00e6a33b..de395594 100644 --- a/Tests/IntegrationTests/TestClientImplementation.swift +++ b/Tests/IntegrationTests/TestClientImplementation.swift @@ -87,6 +87,6 @@ class TestClientImplementation: XiClient { func replaceStatus(viewIdentifier: String, status: ReplaceStatus) { } - func toggleTailConfigChanged(viewIdentifier: String, isTailEnabled: Bool) { + func enableTailing(viewIdentifier: String, isTailEnabled: Bool) { } }