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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! Formatting: a whole document, a range, and as you type.
use super::*;
#[derive(Debug)]
pub struct FormatResult {
pub request_id: u64,
pub path: PathBuf,
/// `None` when no server advertises `documentFormattingProvider`, the
/// server declined, or it returned no edits. Otherwise the char-indexed
/// spans to apply to the single formatted document. Unlike rename,
/// `textDocument/formatting` only ever touches the requested file.
pub edits: Option<Vec<TextSpanEdit>>,
/// True when no spawned server advertises a usable
/// `documentFormattingProvider`, so the app can tell "nothing to format"
/// apart from "no formatter for this language".
pub unsupported: bool,
/// The server that answered, `None` when none did (#1511), so the app
/// can attribute an empty reply to the formatter that sent it.
pub server: Option<String>,
}
impl LspManager {
pub fn request_formatting(&mut self, path: PathBuf, tab_size: u32, insert_spaces: bool) -> u64 {
self.send_request(|request_id| Cmd::RequestFormatting {
request_id,
path,
tab_size,
insert_spaces,
})
}
/// Fire `textDocument/onTypeFormatting` for the just-typed trigger
/// character (#254). Silent for a language without the provider —
/// on-type formatting is a background behavior, not a command.
#[allow(clippy::too_many_arguments)]
pub fn request_on_type_formatting(
&mut self,
path: PathBuf,
line: u32,
character: u32,
ch: char,
tab_size: u32,
insert_spaces: bool,
) -> u64 {
self.send_request(|request_id| Cmd::RequestOnTypeFormatting {
request_id,
path,
line,
character,
ch,
tab_size,
insert_spaces,
})
}
pub fn drain_on_type_formatting(&self) -> Option<FormatResult> {
self.replies.on_type_formatting.try_recv().ok()
}
/// The on-type trigger characters some spawned server advertises for
/// `lang` — `None` until a server for the language has been probed,
/// or when none carries the provider. The app consults this before
/// firing, so nothing is ever sent unadvertised.
pub fn language_on_type_triggers(&self, lang: crate::lsp::Language) -> Option<String> {
self.capability_support
.lock()
.ok()?
.on_type_triggers
.get(&lang)
.cloned()
}
/// Format only a UTF-16 span ("Format Selection", #254). Same reply
/// channel and unsupported contract as whole-document formatting;
/// the app tells the two apart by the returned id.
pub fn request_range_formatting(
&mut self,
path: PathBuf,
start: (u32, u32),
end: (u32, u32),
tab_size: u32,
insert_spaces: bool,
) -> u64 {
self.send_request(|request_id| Cmd::RequestRangeFormatting {
request_id,
path,
start,
end,
tab_size,
insert_spaces,
})
}
pub fn drain_formatting(&self) -> Option<FormatResult> {
self.replies.formatting.try_recv().ok()
}
/// Whether the server for `lang` implements `textDocument/formatting`.
/// `None` means no server has reported yet. Read synchronously by the app
/// to decide whether to offer "Format Document".
pub fn language_supports_formatting(&self, lang: Language) -> Option<bool> {
self.language_supports(lang, |s| &s.formatting)
}
}
impl WorkerState {
pub(super) async fn request_formatting(
&mut self,
request_id: u64,
path: PathBuf,
tab_size: u32,
insert_spaces: bool,
tx: &std_mpsc::Sender<FormatResult>,
) {
// Every bail below MUST still answer. The app defers the format-on-save
// write until a reply lands, so a silent `return` strands the latch:
// no write, no error, and the user believes the file was saved.
let Some(doc) = self.docs.get(&path) else {
answer_unsupported(tx, request_id, path);
return;
};
let lang = doc.language;
let root = doc.project_root.clone();
// Re-probe so a formatter installed since the file opened is picked up
// without reopening (mirrors `request_rename`).
self.ensure_clients(lang, &root).await;
let Some(clients) = self.clients.get(&(lang, root)) else {
answer_unsupported(tx, request_id, path);
return;
};
let picked = clients
.iter()
.find(|c| c.supports_formatting)
.map(|c| (c.name.clone(), c.client.clone()));
let Some((server_name, client_arc)) = picked else {
let _ = tx.send(FormatResult {
request_id,
path,
edits: None,
unsupported: true,
server: None,
});
return;
};
let Ok(uri) = Url::from_file_path(&path) else {
answer_unsupported(tx, request_id, path);
return;
};
let tx = tx.clone();
let path_clone = path.clone();
log_file::log(&format!(
"formatting request id={request_id} server={server_name} path={} tab_size={tab_size} insert_spaces={insert_spaces}",
path.display()
));
tokio::spawn(async move {
let mut client = client_arc.lock().await.requests();
let resp = client.formatting(uri, tab_size, insert_spaces).await;
drop(client);
let edits = match resp {
Ok(Some(tes)) => Some(tes.iter().map(text_edit_to_span).collect()),
Ok(None) => None,
Err(e) => {
log_file::log(&format!("lsp[{server_name}] formatting error: {e}"));
None
}
};
log_file::log(&format!(
"formatting response id={request_id} server={server_name} edits={}",
edits.as_ref().map(Vec::len).unwrap_or(0)
));
let _ = tx.send(FormatResult {
request_id,
path: path_clone,
edits,
unsupported: false,
server: Some(server_name),
});
});
}
/// On-type formatting (#254): the server's reaction to one just-typed
/// trigger character. Silent on every early-out — this is a
/// background behavior with no status line waiting on it, and the
/// app pre-checks the trigger set so an unroutable request means a
/// race (doc closed), not a misconfiguration. This deliberately
/// diverges from `request_formatting`'s "every bail must still
/// answer" invariant: nothing blocks on this reply, so an unanswered
/// bail costs nothing.
#[allow(clippy::too_many_arguments)]
pub(super) async fn request_on_type_formatting(
&mut self,
request_id: u64,
path: PathBuf,
line: u32,
character: u32,
ch: char,
tab_size: u32,
insert_spaces: bool,
tx: &std_mpsc::Sender<FormatResult>,
) {
let Some(doc) = self.docs.get(&path) else {
return;
};
let lang = doc.language;
let root = doc.project_root.clone();
self.ensure_clients(lang, &root).await;
let Some(clients) = self.clients.get(&(lang, root)) else {
return;
};
let picked = clients
.iter()
.find(|c| c.on_type_triggers.as_ref().is_some_and(|t| t.contains(ch)))
.map(|c| (c.name.clone(), c.client.clone()));
let Some((server_name, client_arc)) = picked else {
return;
};
let Ok(uri) = Url::from_file_path(&path) else {
return;
};
let tx = tx.clone();
let path_clone = path.clone();
tokio::spawn(async move {
let mut client = client_arc.lock().await.requests();
let resp = client
.on_type_formatting(
uri,
line,
character,
ch.to_string(),
tab_size,
insert_spaces,
)
.await;
drop(client);
let edits = match resp {
Ok(Some(tes)) => Some(tes.iter().map(text_edit_to_span).collect()),
Ok(None) => None,
Err(e) => {
log_file::log(&format!("lsp[{server_name}] onTypeFormatting error: {e}"));
None
}
};
let _ = tx.send(FormatResult {
request_id,
path: path_clone,
edits,
unsupported: false,
server: Some(server_name),
});
});
}
/// "Format Selection" (#254): `textDocument/rangeFormatting` over one
/// UTF-16 span. Same always-answer contract as whole-document
/// formatting — the app shows status off this reply.
#[allow(clippy::too_many_arguments)]
pub(super) async fn request_range_formatting(
&mut self,
request_id: u64,
path: PathBuf,
start: (u32, u32),
end: (u32, u32),
tab_size: u32,
insert_spaces: bool,
tx: &std_mpsc::Sender<FormatResult>,
) {
let Ok((server_name, client_arc)) = self
.pick_server(&path, |c| c.supports_range_formatting)
.await
else {
answer_unsupported(tx, request_id, path);
return;
};
let Ok(uri) = Url::from_file_path(&path) else {
answer_unsupported(tx, request_id, path);
return;
};
let tx = tx.clone();
let path_clone = path.clone();
tokio::spawn(async move {
let mut client = client_arc.lock().await.requests();
let resp = client
.range_formatting(uri, start.0, start.1, end.0, end.1, tab_size, insert_spaces)
.await;
drop(client);
let edits = match resp {
Ok(Some(tes)) => Some(tes.iter().map(text_edit_to_span).collect()),
Ok(None) => None,
Err(e) => {
log_file::log(&format!("lsp[{server_name}] range_formatting error: {e}"));
None
}
};
let _ = tx.send(FormatResult {
request_id,
path: path_clone,
edits,
unsupported: false,
server: Some(server_name),
});
});
}
}
/// Answer a formatting request that cannot reach a server. `unsupported` is
/// the honest shape for every one of these: no client in this worker can
/// format the file. The app turns it into "No formatter available for this
/// file" and, crucially, still performs the deferred save.
pub(super) fn answer_unsupported(
tx: &std_mpsc::Sender<FormatResult>,
request_id: u64,
path: PathBuf,
) {
let _ = tx.send(FormatResult {
request_id,
path,
edits: None,
unsupported: true,
server: None,
});
}