-
Notifications
You must be signed in to change notification settings - Fork 147
/
XiCore.swift
120 lines (102 loc) · 5 KB
/
XiCore.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2018 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Identifies a given document view for routing with xi-core
typealias ViewIdentifier = String
/// Protocol describing the frontend interface with core.
/// Documentation for the protocol can be found here:
/// https://xi-editor.github.io/xi-editor/docs/frontend-protocol.html
protocol XiCore: class {
/// Sent by the client immediately after establishing the core connection.
func clientStarted(configDir: String?, clientExtrasDir: String?)
/// Asks core to change the theme. If the change succeeds the client will receive a `theme_changed` notification.
func setTheme(themeName: String)
/// Changes the state of the tracing config.
func tracingConfig(enabled: Bool)
/// Creates a new view, returning the view identifier as a string. `file_path` is optional;
func newView(filePath: String?, callback: @escaping RpcCallback)
/// The type of the view identifier.
typealias ViewIdentifier = String
/// Closes the view with provided identifier.
func closeView(identifier: ViewIdentifier)
/// Saves the buffer associated with `view_id` to `file_path`.
func save(identifier: ViewIdentifier, filePath: String)
/// Starts the named plugin for the given view.
func start(plugin: String, in identifier: ViewIdentifier)
/// Stops the named plugin for the given view.
func stop(plugin: String, in identifier: ViewIdentifier)
/// Asks core to change the language of the buffer associated with the `view_id`.
/// If the change succeeds the client will receive a `language_changed` notification.
func setLanguage(identifier: ViewIdentifier, languageName: String)
/// Requests that core collect and write out traces to a file
/// at `destination`, including the samples from this frontend.
func saveTrace(destination: String, frontendSamples: [[String: AnyObject]])
/// The following calls break XiCore interface. We should remove them as soon as
/// `Document` calls are migrated to it's own protocol.
func sendRpcAsync(_ method: String, params: Any, callback: RpcCallback?)
func sendRpc(_ method: String, params: Any) -> RpcResult
}
final class CoreConnection: XiCore {
private let rpcSender: RPCSending
init(rpcSender: RPCSending) {
self.rpcSender = rpcSender
}
func clientStarted(configDir: String?, clientExtrasDir: String?) {
let params = ["client_extras_dir": clientExtrasDir,
"config_dir": configDir]
sendRpcAsync("client_started", params: params)
}
func setTheme(themeName: String) {
sendRpcAsync("set_theme", params: ["theme_name": themeName])
}
func tracingConfig(enabled: Bool) {
sendRpcAsync("tracing_config", params: ["enabled": enabled])
}
func newView(filePath: String?, callback: @escaping RpcCallback) {
var params: [String: String] = [:]
if let filePath = filePath {
params = ["file_path": filePath]
}
sendRpcAsync("new_view", params: params, callback: callback)
}
func closeView(identifier: ViewIdentifier) {
sendRpcAsync("close_view", params: ["view_id": identifier])
}
func save(identifier: ViewIdentifier, filePath: String) {
let params = ["view_id": identifier,
"file_path": filePath]
sendRpcAsync("save", params: params)
}
func start(plugin: String, in identifier: ViewIdentifier) {
let params = ["command": "start", "view_id": identifier, "plugin_name": plugin]
sendRpcAsync("plugin", params: params)
}
func stop(plugin: String, in identifier: ViewIdentifier) {
let params = ["command": "stop", "view_id": identifier, "plugin_name": plugin]
sendRpcAsync("plugin", params: params)
}
func setLanguage(identifier: ViewIdentifier, languageName: String) {
let params = ["view_id": identifier, "language_id": languageName]
sendRpcAsync("set_language", params: params)
}
func saveTrace(destination: String, frontendSamples: [[String: AnyObject]]) {
let params = ["destination": destination, "frontend_samples": frontendSamples] as AnyObject
sendRpcAsync("save_trace", params: params)
}
func sendRpcAsync(_ method: String, params: Any, callback: RpcCallback? = nil) {
rpcSender.sendRpcAsync(method, params: params, callback: callback)
}
func sendRpc(_ method: String, params: Any) -> RpcResult {
return rpcSender.sendRpc(method, params: params)
}
}