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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Attachment admission, publication, trust and server lifetime ownership.
mod attach_outcomes;
mod spawn;
mod warm;
#[cfg(test)]
pub(crate) use warm::warm_language;
use super::attach::AttachKey;
use super::*;
use strop_core::id::DocumentId;
impl Editor {
/// Try to attach a language server for the current buffer. The
/// synchronous part only touches in-memory state; discovery
/// (config, root, trust, executability) is owned worker work behind the
/// replay gate.
/// The LSP half of the startup "start services" action: enable
/// attach, then attach for the current buffer. A pure state
/// transition performed identically live and replayed — the tape
/// gates the native discovery inside `lsp_maybe_attach`.
pub fn lsp_start_services(&mut self) {
self.lsp_state.attach.enabled = true;
self.lsp_maybe_attach();
}
pub(crate) fn lsp_maybe_attach(&mut self) {
if !self.lsp_state.attach.enabled {
return;
}
if self.cur().remote_metadata().is_some() && !self.remote_window_complete() {
// Typed refusal, never silence: a partial/follow window is
// not a document a server can be told about.
self.message = "lsp unavailable — partial remote window".into();
return;
}
let Some(doc) = self.lsp_current_doc_path() else {
return;
};
// Extensionless/ambiguous headers keep a navigation-bound
// context (0049 §4.4); without one there is nothing to attach.
let Some(language) = self.lsp_doc_language(self.current(), &doc.path) else {
return;
};
if self
.lsp_server_for(self.current(), &doc.path, &language, &doc.filesystem)
.is_some()
{
self.lsp_did_open_current();
return;
}
// Discovery runs only for an unambiguous extension — never
// rediscover project commands from /usr/include (0049 §4.7).
let Some(ext) = doc
.path
.extension()
.map(|e| format!(".{}", e.to_string_lossy()))
else {
return;
};
let Some(language) = registry::language_for_extension(&ext) else {
return;
};
let key = AttachKey {
target: doc.filesystem.clone(),
language: language.to_string(),
path: doc.path.clone(),
};
match self.lsp_state.attach.refused.get(&key) {
// Trust decisions change (`:trust`); re-discover those.
Some(attach::AttachDecision::TrustRequired { .. })
| Some(attach::AttachDecision::TrustError { .. })
// Remote infrastructure failures are transient: the
// connection may be back; re-discover.
| Some(attach::AttachDecision::RemoteIo { .. }) => {}
// Everything else was reported once and stays refused.
Some(_) => return,
None => {}
}
if self.lsp_state.attach.pending.contains_key(&key) {
return;
}
let ticket = match self.worker_ids.allocate() {
Ok(ticket) => ticket,
Err(error) => {
self.message = error.message;
return;
}
};
self.lsp_state.attach.pending.insert(key, ticket);
let args = attach::AttachArgs {
ticket,
path: doc.path.clone(),
language: language.to_string(),
target: doc.filesystem.clone(),
};
// Replay gate: no native config/trust/executability before this
// registration (R11).
match self.tape.request("lsp.attach", &args) {
Ok(true) => self.lsp_spawn_discovery(ticket, doc, ext, language),
Ok(false) => {}
Err(error) => {
self.lsp_state.attach.pending.remove(&args_key(&args));
self.message = format!("lsp attach diverged from trace: {error}");
}
}
}
/// The server placement for a buffer, if one is attached: exact
/// language match on the same filesystem target, longest covering
/// root wins.
/// Resolve the serving (server, root) for a document: the
/// navigation-bound context first (0049 §4.2 — the server that
/// brought this document here keeps answering inside it), then
/// longest-covering-root discovery for unbound ordinary opens.
/// Never a scan that picks the first server speaking the language.
pub(crate) fn lsp_server_for(
&self,
document: DocumentId,
path: &Path,
language: &str,
target: &Filesystem,
) -> Option<(ServerId, PathBuf)> {
// The maps are the authority: `lsp_failed` drops a dead
// server's bindings and carried contexts, so a present entry is
// a live context. Replay resolves identically without a client.
if let Some(binding) = self.lsp_state.bindings.get(&document) {
if binding.target == *target && binding.language == language {
return Some((binding.server, binding.root.clone()));
}
}
if let Some(context) = self.lsp_state.jump_contexts.get(&document) {
if context.target == *target && context.language == language {
return Some((context.server, context.root.clone()));
}
}
let attach = &self.lsp_state.attach;
let best = attach
.attached
.iter()
.filter(|a| a.language == language && &a.target == target && path.starts_with(&a.root))
.max_by_key(|a| a.root.as_os_str().len())?;
Some((best.server, best.root.clone()))
}
/// The first recorded layer diagnostic, when any — readiness and
/// later messages must not erase it (0033 §2).
pub(super) fn layer_warning(&self) -> Option<String> {
layer_suffix(&self.lsp_state.attach.layer_diagnostics)
}
/// Retire remote servers whose workspace no longer has a document:
/// closing the last owning remote workspace retires its server
/// (0036 RW8) — no orphan ssh, no orphan remote process. Local
/// servers keep their session-long lifetime.
pub(crate) fn lsp_retire_remote_servers(&mut self) {
let retired: Vec<ServerId> = self
.lsp_state
.attach
.attached
.iter()
.filter(|a| a.target.is_remote())
.filter(|a| {
let endpoint = match &a.target {
Filesystem::Remote(endpoint) => endpoint,
_ => return false,
};
!self.docs.iter().any(|(id, document)| {
document.remote_metadata().is_some_and(|source| {
source.file.endpoint() == endpoint
&& source.file.path().starts_with(&a.root)
&& lsp_language(source.file.path()) == Some(a.language.as_str())
&& source.window.is_complete()
&& !self.remote_following(id)
})
})
})
.map(|a| a.server)
.collect();
for server in retired {
self.lsp_retire_server(server, "remote workspace closed");
}
}
/// Remove one server's placements, bindings and connection with a
/// graceful shutdown that never blocks the input thread.
fn lsp_retire_server(&mut self, server: ServerId, reason: &str) {
// Remove the placements first: the closes below re-enter the
// retirement scan, and this server must already be gone from
// the tables so the recursion is empty.
self.lsp_state
.attach
.attached
.retain(|a| a.server != server);
let connection = self
.lsp_servers
.iter()
.position(|s| s.id == server)
.map(|index| self.lsp_servers.remove(index));
let mut documents: Vec<_> = self
.lsp_state
.bindings
.iter()
.filter(|(_, binding)| binding.server == server)
.map(|(document, _)| *document)
.collect();
documents.sort();
for document in documents {
self.lsp_close_document(document);
}
if let Some(connection) = connection {
if let Some(client) = connection.client {
std::thread::spawn(move || {
client.shutdown();
client.wait(std::time::Duration::from_secs(2));
});
}
}
trace::services::rejected("lsp", reason);
}
}
impl Editor {
pub(crate) fn remote_trust_target(&self) -> Result<strop_workspace::RemoteFile, String> {
let doc = self
.lsp_current_doc_path()
.ok_or("trust requires a file buffer")?;
let Filesystem::Remote(endpoint) = doc.filesystem else {
return Err("not a remote workspace".into());
};
let language = lsp_language(&doc.path).ok_or("no language server for this file")?;
let key = AttachKey {
target: Filesystem::Remote(endpoint.clone()),
language: language.to_owned(),
path: doc.path,
};
let root = self
.lsp_state
.attach
.trust_roots
.get(&key)
.ok_or("no pending remote project trust request")?;
strop_workspace::RemoteFile::from_path(endpoint, root.clone())
.map_err(|error| error.to_string())
}
}
fn args_key(args: &attach::AttachArgs) -> AttachKey {
AttachKey {
target: args.target.clone(),
language: args.language.clone(),
path: args.path.clone(),
}
}
/// Modeline suffix for malformed layers: the first diagnostic's exact
/// path, plus a count when more follow (0033 §2).
fn layer_suffix(layers: &[strop_lsp::languages::LayerDiagnostic]) -> Option<String> {
let first = layers.first()?;
Some(if layers.len() == 1 {
first.display()
} else {
format!("{} (+{} more)", first.display(), layers.len() - 1)
})
}