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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
mod catalog;
pub mod manifest;
pub mod rpc;
use std::fmt;
use std::io::BufReader;
use std::path::Path;
use std::process::{Child, Command as ProcCommand, Stdio};
use std::sync::Arc;
use std::thread;
use serde_json::Value;
use xi_rpc::{self, RpcLoop, RpcPeer};
use xi_trace;
use crate::config::Table;
use crate::syntax::LanguageId;
use crate::tabs::ViewId;
use crate::WeakXiCore;
use self::rpc::{PluginBufferInfo, PluginUpdate};
pub(crate) use self::catalog::PluginCatalog;
pub use self::manifest::{Command, PlaceholderRpc, PluginDescription};
pub type PluginName = String;
#[derive(
Serialize, Deserialize, Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord,
)]
pub struct PluginPid(pub(crate) usize);
pub type PluginId = PluginPid;
impl fmt::Display for PluginPid {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "plugin-{}", self.0)
}
}
pub struct Plugin {
peer: RpcPeer,
pub(crate) id: PluginId,
pub(crate) name: String,
#[allow(dead_code)]
process: Child,
}
impl Plugin {
pub fn initialize(&self, info: Vec<PluginBufferInfo>) {
self.peer.send_rpc_notification(
"initialize",
&json!({
"plugin_id": self.id,
"buffer_info": info,
}),
)
}
pub fn shutdown(&self) {
self.peer.send_rpc_notification("shutdown", &json!({}));
}
pub fn new_buffer(&self, info: &PluginBufferInfo) {
self.peer.send_rpc_notification("new_buffer", &json!({ "buffer_info": [info] }))
}
pub fn close_view(&self, view_id: ViewId) {
self.peer.send_rpc_notification("did_close", &json!({ "view_id": view_id }))
}
pub fn did_save(&self, view_id: ViewId, path: &Path) {
self.peer.send_rpc_notification(
"did_save",
&json!({
"view_id": view_id,
"path": path,
}),
)
}
pub fn update<F>(&self, update: &PluginUpdate, callback: F)
where
F: FnOnce(Result<Value, xi_rpc::Error>) + Send + 'static,
{
self.peer.send_rpc_request_async("update", &json!(update), Box::new(callback))
}
pub fn toggle_tracing(&self, enabled: bool) {
self.peer.send_rpc_notification("tracing_config", &json!({ "enabled": enabled }))
}
pub fn collect_trace(&self) -> Result<Value, xi_rpc::Error> {
self.peer.send_rpc_request("collect_trace", &json!({}))
}
pub fn config_changed(&self, view_id: ViewId, changes: &Table) {
self.peer.send_rpc_notification(
"config_changed",
&json!({
"view_id": view_id,
"changes": changes,
}),
)
}
pub fn language_changed(&self, view_id: ViewId, new_lang: &LanguageId) {
self.peer.send_rpc_notification(
"language_changed",
&json!({
"view_id": view_id,
"new_lang": new_lang,
}),
)
}
pub fn get_hover(&self, view_id: ViewId, request_id: usize, position: usize) {
self.peer.send_rpc_notification(
"get_hover",
&json!({
"view_id": view_id,
"request_id": request_id,
"position": position,
}),
)
}
pub fn dispatch_command(&self, view_id: ViewId, method: &str, params: &Value) {
self.peer.send_rpc_notification(
"custom_command",
&json!({
"view_id": view_id,
"method": method,
"params": params,
}),
)
}
}
pub(crate) fn start_plugin_process(
plugin_desc: Arc<PluginDescription>,
id: PluginId,
core: WeakXiCore,
) {
let spawn_result = thread::Builder::new()
.name(format!("<{}> core host thread", &plugin_desc.name))
.spawn(move || {
info!("starting plugin {}", &plugin_desc.name);
let child = ProcCommand::new(&plugin_desc.exec_path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn();
match child {
Ok(mut child) => {
let child_stdin = child.stdin.take().unwrap();
let child_stdout = child.stdout.take().unwrap();
let mut looper = RpcLoop::new(child_stdin);
let peer: RpcPeer = Box::new(looper.get_raw_peer());
let name = plugin_desc.name.clone();
peer.send_rpc_notification("ping", &Value::Array(Vec::new()));
let plugin = Plugin { peer, process: child, name, id };
if xi_trace::is_enabled() {
plugin.toggle_tracing(true);
}
core.plugin_connect(Ok(plugin));
let mut core = core;
let err = looper.mainloop(|| BufReader::new(child_stdout), &mut core);
core.plugin_exit(id, err);
}
Err(err) => core.plugin_connect(Err(err)),
}
});
if let Err(err) = spawn_result {
error!("thread spawn failed for {}, {:?}", id, err);
}
}