-
Notifications
You must be signed in to change notification settings - Fork 624
/
main.rs
579 lines (523 loc) · 20.8 KB
/
main.rs
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
#![cfg(not(target_arch = "wasm32"))]
#![allow(clippy::await_holding_refcell_ref)]
#[cfg(all(feature = "preview-engine", not(feature = "preview-builtin")))]
compile_error!("Feature preview-engine and preview-builtin need to be enabled together when building native LSP");
mod common;
mod fmt;
mod language;
pub mod lsp_ext;
#[cfg(feature = "preview-engine")]
mod preview;
pub mod util;
use common::Result;
use language::*;
use lsp_types::notification::{
DidChangeConfiguration, DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument,
DidOpenTextDocument, Notification,
};
use lsp_types::{
DidChangeTextDocumentParams, DidChangeWatchedFilesParams, DidCloseTextDocumentParams,
DidOpenTextDocumentParams, InitializeParams, Url,
};
use clap::{Args, Parser, Subcommand};
use itertools::Itertools;
use lsp_server::{Connection, ErrorCode, IoThreads, Message, RequestId, Response};
use std::cell::RefCell;
use std::collections::HashMap;
use std::future::Future;
use std::io::Write as _;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::{atomic, Arc, Mutex};
use std::task::{Poll, Waker};
use crate::common::document_cache::CompilerConfiguration;
#[derive(Clone, clap::Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// Add include paths for the import statements
#[arg(short = 'I', name = "/path/to/import", number_of_values = 1, action)]
include_paths: Vec<std::path::PathBuf>,
/// Specify library location of the '@library' in the form 'library=/path/to/library'
#[arg(short = 'L', value_name = "library=path", number_of_values = 1, action)]
library_paths: Vec<String>,
/// The style name for the preview ('native' or 'fluent')
#[arg(long, name = "style name", default_value_t, action)]
style: String,
/// The backend or renderer used for the preview ('qt', 'femtovg', 'skia' or 'software')
#[arg(long, name = "backend", default_value_t, action)]
backend: String,
/// Start the preview in full screen mode
#[arg(long, action)]
fullscreen: bool,
/// Hide the preview toolbar
#[arg(long, action)]
no_toolbar: bool,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand, Clone)]
enum Commands {
/// Format slint files
Format(Format),
}
#[derive(Args, Clone)]
struct Format {
#[arg(name = "path to .slint file(s)", action)]
paths: Vec<std::path::PathBuf>,
/// modify the file inline instead of printing to stdout
#[arg(short, long, action)]
inline: bool,
}
enum OutgoingRequest {
Start,
Pending(Waker),
Done(lsp_server::Response),
}
type OutgoingRequestQueue = Arc<Mutex<HashMap<RequestId, OutgoingRequest>>>;
/// A handle that can be used to communicate with the client
///
/// This type is duplicated, with the same interface, in wasm_main.rs
#[derive(Clone)]
pub struct ServerNotifier {
sender: crossbeam_channel::Sender<Message>,
queue: OutgoingRequestQueue,
use_external_preview: Arc<atomic::AtomicBool>,
#[cfg(feature = "preview-engine")]
preview_to_lsp_sender: crossbeam_channel::Sender<crate::common::PreviewToLspMessage>,
}
impl ServerNotifier {
pub fn use_external_preview(&self) -> bool {
self.use_external_preview.load(atomic::Ordering::Relaxed)
}
pub fn set_use_external_preview(&self, is_external: bool) {
self.use_external_preview.store(is_external, atomic::Ordering::Release);
}
pub fn send_notification<N: Notification>(&self, params: N::Params) -> Result<()> {
self.sender.send(Message::Notification(lsp_server::Notification::new(
N::METHOD.to_string(),
params,
)))?;
Ok(())
}
pub fn send_request<T: lsp_types::request::Request>(
&self,
request: T::Params,
) -> Result<impl Future<Output = Result<T::Result>>> {
static REQ_ID: atomic::AtomicI32 = atomic::AtomicI32::new(0);
let id = RequestId::from(REQ_ID.fetch_add(1, atomic::Ordering::Relaxed));
let msg =
Message::Request(lsp_server::Request::new(id.clone(), T::METHOD.to_string(), request));
self.sender.send(msg)?;
let queue = self.queue.clone();
queue.lock().unwrap().insert(id.clone(), OutgoingRequest::Start);
Ok(std::future::poll_fn(move |ctx| {
let mut queue = queue.lock().unwrap();
match queue.remove(&id).unwrap() {
OutgoingRequest::Pending(_) | OutgoingRequest::Start => {
queue.insert(id.clone(), OutgoingRequest::Pending(ctx.waker().clone()));
Poll::Pending
}
OutgoingRequest::Done(d) => {
if let Some(err) = d.error {
Poll::Ready(Err(err.message.into()))
} else {
Poll::Ready(
serde_json::from_value(d.result.unwrap_or_default())
.map_err(|e| format!("cannot deserialize response: {e:?}").into()),
)
}
}
}
}))
}
pub fn send_message_to_preview(&self, message: common::LspToPreviewMessage) {
if self.use_external_preview() {
let _ = self.send_notification::<common::LspToPreviewMessage>(message);
} else {
#[cfg(feature = "preview-builtin")]
preview::lsp_to_preview_message(message);
}
}
#[cfg(feature = "preview-engine")]
pub fn send_message_to_lsp(&self, message: common::PreviewToLspMessage) {
let _ = self.preview_to_lsp_sender.send(message);
}
#[cfg(test)]
pub fn dummy() -> Self {
Self {
sender: crossbeam_channel::unbounded().0,
queue: Default::default(),
use_external_preview: Default::default(),
#[cfg(feature = "preview-engine")]
preview_to_lsp_sender: crossbeam_channel::unbounded().0,
}
}
}
impl RequestHandler {
async fn handle_request(&self, request: lsp_server::Request, ctx: &Rc<Context>) -> Result<()> {
if let Some(x) = self.0.get(&request.method.as_str()) {
match x(request.params, ctx.clone()).await {
Ok(r) => ctx
.server_notifier
.sender
.send(Message::Response(Response::new_ok(request.id, r)))?,
Err(e) => ctx.server_notifier.sender.send(Message::Response(Response::new_err(
request.id,
match e.code {
LspErrorCode::InvalidParameter => ErrorCode::InvalidParams as i32,
LspErrorCode::InternalError => ErrorCode::InternalError as i32,
LspErrorCode::RequestFailed => ErrorCode::RequestFailed as i32,
LspErrorCode::ContentModified => ErrorCode::ContentModified as i32,
},
e.message,
)))?,
};
} else {
ctx.server_notifier.sender.send(Message::Response(Response::new_err(
request.id,
ErrorCode::MethodNotFound as i32,
"Cannot handle request".into(),
)))?;
}
Ok(())
}
}
fn main() {
let args: Cli = Cli::parse();
if !args.backend.is_empty() {
std::env::set_var("SLINT_BACKEND", &args.backend);
}
if let Some(Commands::Format(args)) = args.command {
let _ = fmt::tool::run(args.paths, args.inline).map_err(|e| {
eprintln!("{e}");
std::process::exit(1);
});
std::process::exit(0);
}
if let Ok(panic_log_file) = std::env::var("SLINT_LSP_PANIC_LOG") {
let default_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
let _ =
std::path::Path::new(&panic_log_file).parent().map(|x| std::fs::create_dir_all(x));
if let Ok(mut file) = std::fs::File::create(&panic_log_file) {
let _ = writeln!(
file,
"slint-lsp v{}.{}.{}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
env!("CARGO_PKG_VERSION_PATCH")
);
let _ = if let Some(l) = info.location() {
writeln!(file, "{}:{}:{}", l.file(), l.line(), l.column())
} else {
writeln!(file, "unknown location")
};
let _ = writeln!(file, "{info}");
}
default_hook(info);
}));
}
#[cfg(feature = "preview-engine")]
{
let cli_args = args.clone();
let lsp_thread = std::thread::Builder::new()
.name("LanguageServer".into())
.spawn(move || {
/// Make sure we quit the event loop even if we panic
struct QuitEventLoop;
impl Drop for QuitEventLoop {
fn drop(&mut self) {
preview::quit_ui_event_loop();
}
}
let quit_ui_loop = QuitEventLoop;
let threads = match run_lsp_server(args) {
Ok(threads) => threads,
Err(error) => {
eprintln!("Error running LSP server: {}", error);
return;
}
};
drop(quit_ui_loop);
threads.join().unwrap();
})
.unwrap();
preview::start_ui_event_loop(cli_args);
lsp_thread.join().unwrap();
}
#[cfg(not(feature = "preview-engine"))]
match run_lsp_server(args) {
Ok(threads) => threads.join().unwrap(),
Err(error) => {
eprintln!("Error running LSP server: {}", error);
}
}
}
fn run_lsp_server(args: Cli) -> Result<IoThreads> {
let (connection, io_threads) = Connection::stdio();
let (id, params) = connection.initialize_start()?;
let init_param: InitializeParams = serde_json::from_value(params).unwrap();
let initialize_result =
serde_json::to_value(language::server_initialize_result(&init_param.capabilities))?;
connection.initialize_finish(id, initialize_result)?;
main_loop(connection, init_param, args)?;
Ok(io_threads)
}
fn main_loop(connection: Connection, init_param: InitializeParams, cli_args: Cli) -> Result<()> {
let mut rh = RequestHandler::default();
register_request_handlers(&mut rh);
let request_queue = OutgoingRequestQueue::default();
#[cfg_attr(not(feature = "preview-engine"), allow(unused))]
let (preview_to_lsp_sender, preview_to_lsp_receiver) =
crossbeam_channel::unbounded::<crate::common::PreviewToLspMessage>();
let server_notifier = ServerNotifier {
sender: connection.sender.clone(),
queue: request_queue.clone(),
use_external_preview: Default::default(),
#[cfg(feature = "preview-engine")]
preview_to_lsp_sender,
};
#[cfg(feature = "preview-builtin")]
preview::set_server_notifier(server_notifier.clone());
let server_notifier_ = server_notifier.clone();
let compiler_config = CompilerConfiguration {
style: Some(if cli_args.style.is_empty() { "native".into() } else { cli_args.style }),
include_paths: cli_args.include_paths,
library_paths: cli_args
.library_paths
.iter()
.filter_map(|entry| entry.split('=').collect_tuple().map(|(k, v)| (k.into(), v.into())))
.collect(),
open_import_fallback: Some(Rc::new(move |path| {
let server_notifier = server_notifier_.clone();
Box::pin(async move {
let contents = std::fs::read_to_string(&path);
if let Ok(url) = Url::from_file_path(&path) {
if let Ok(contents) = &contents {
server_notifier.send_message_to_preview(
common::LspToPreviewMessage::SetContents {
url: common::VersionedUrl::new(url, None),
contents: contents.clone(),
},
)
} else {
server_notifier.send_message_to_preview(
common::LspToPreviewMessage::ForgetFile { url },
)
}
}
Some(contents.map(|c| (None, c)))
})
})),
..Default::default()
};
let ctx = Rc::new(Context {
document_cache: RefCell::new(crate::common::DocumentCache::new(compiler_config)),
preview_config: RefCell::new(Default::default()),
server_notifier,
init_param,
#[cfg(any(feature = "preview-external", feature = "preview-engine"))]
to_show: Default::default(),
open_urls: Default::default(),
});
let mut futures = Vec::<Pin<Box<dyn Future<Output = Result<()>>>>>::new();
let mut first_future = Box::pin(startup_lsp(&ctx));
// We are waiting in this loop for two kind of futures:
// - The compiler future should always be ready immediately because we do not set a callback to load files
// - the future from `send_request` are blocked waiting for a response from the client.
// Responses are sent on the `connection.receiver` which will wake the loop, so there
// is no need to do anything in the Waker.
struct DummyWaker;
impl std::task::Wake for DummyWaker {
fn wake(self: Arc<Self>) {}
}
let waker = Arc::new(DummyWaker).into();
match first_future.as_mut().poll(&mut std::task::Context::from_waker(&waker)) {
Poll::Ready(x) => x?,
Poll::Pending => futures.push(first_future),
};
loop {
crossbeam_channel::select! {
recv(connection.receiver) -> msg => {
match msg? {
Message::Request(req) => {
// ignore errors when shutdown
if connection.handle_shutdown(&req).unwrap_or(false) {
return Ok(());
}
futures.push(Box::pin(rh.handle_request(req, &ctx)));
}
Message::Response(resp) => {
if let Some(q) = request_queue.lock().unwrap().get_mut(&resp.id) {
match q {
OutgoingRequest::Done(_) => {
return Err("Response to unknown request".into())
}
OutgoingRequest::Start => { /* nothing to do */ }
OutgoingRequest::Pending(x) => x.wake_by_ref(),
};
*q = OutgoingRequest::Done(resp)
} else {
return Err("Response to unknown request".into());
}
}
Message::Notification(notification) => {
futures.push(Box::pin(handle_notification(notification, &ctx)))
}
}
},
recv(preview_to_lsp_receiver) -> _msg => {
// Messages from the native preview come in here:
#[cfg(feature = "preview-engine")]
futures.push(Box::pin(handle_preview_to_lsp_message(_msg?, &ctx)))
},
};
let mut result = Ok(());
futures.retain_mut(|f| {
if result.is_err() {
return true;
}
match f.as_mut().poll(&mut std::task::Context::from_waker(&waker)) {
Poll::Ready(x) => {
result = x;
false
}
Poll::Pending => true,
}
});
result?;
}
}
async fn handle_notification(req: lsp_server::Notification, ctx: &Rc<Context>) -> Result<()> {
match &*req.method {
DidOpenTextDocument::METHOD => {
let params: DidOpenTextDocumentParams = serde_json::from_value(req.params)?;
open_document(
ctx,
params.text_document.text,
params.text_document.uri,
Some(params.text_document.version),
&mut ctx.document_cache.borrow_mut(),
)
.await
}
DidCloseTextDocument::METHOD => {
let params: DidCloseTextDocumentParams = serde_json::from_value(req.params)?;
close_document(ctx, params.text_document.uri).await
}
DidChangeTextDocument::METHOD => {
let mut params: DidChangeTextDocumentParams = serde_json::from_value(req.params)?;
reload_document(
ctx,
params.content_changes.pop().unwrap().text,
params.text_document.uri,
Some(params.text_document.version),
&mut ctx.document_cache.borrow_mut(),
)
.await
}
DidChangeConfiguration::METHOD => load_configuration(ctx).await,
DidChangeWatchedFiles::METHOD => {
let params: DidChangeWatchedFilesParams = serde_json::from_value(req.params)?;
for fe in params.changes {
trigger_file_watcher(ctx, fe.uri, fe.typ).await?;
}
Ok(())
}
#[cfg(any(feature = "preview-builtin", feature = "preview-external"))]
language::SHOW_PREVIEW_COMMAND => {
match language::show_preview_command(
req.params.as_array().map_or(&[], |x| x.as_slice()),
ctx,
) {
Ok(()) => Ok(()),
Err(e) => match e.code {
LspErrorCode::RequestFailed => ctx
.server_notifier
.send_notification::<lsp_types::notification::ShowMessage>(
lsp_types::ShowMessageParams {
typ: lsp_types::MessageType::ERROR,
message: e.message,
},
),
_ => Err(e.message.into()),
},
}
}
// Messages from the WASM preview come in as notifications sent by the "editor":
#[cfg(any(feature = "preview-external", feature = "preview-engine"))]
"slint/preview_to_lsp" => {
handle_preview_to_lsp_message(serde_json::from_value(req.params)?, ctx).await
}
_ => Ok(()),
}
}
#[cfg(any(feature = "preview-external", feature = "preview-engine"))]
async fn send_workspace_edit(
server_notifier: ServerNotifier,
label: Option<String>,
edit: Result<lsp_types::WorkspaceEdit>,
) -> Result<()> {
let edit = edit?;
let response = server_notifier
.send_request::<lsp_types::request::ApplyWorkspaceEdit>(
lsp_types::ApplyWorkspaceEditParams { label, edit },
)?
.await?;
if !response.applied {
return Err(response
.failure_reason
.unwrap_or("Operation failed, no specific reason given".into())
.into());
}
Ok(())
}
#[cfg(any(feature = "preview-external", feature = "preview-engine"))]
async fn handle_preview_to_lsp_message(
message: crate::common::PreviewToLspMessage,
ctx: &Rc<Context>,
) -> Result<()> {
use crate::common::PreviewToLspMessage as M;
match message {
M::Status { message, health } => {
crate::common::lsp_to_editor::send_status_notification(
&ctx.server_notifier,
&message,
health,
);
}
M::Diagnostics { uri, version, diagnostics } => {
crate::common::lsp_to_editor::notify_lsp_diagnostics(
&ctx.server_notifier,
uri,
version,
diagnostics,
);
}
M::ShowDocument { file, selection, take_focus } => {
crate::common::lsp_to_editor::send_show_document_to_editor(
ctx.server_notifier.clone(),
file,
selection,
take_focus,
)
.await;
}
M::PreviewTypeChanged { is_external } => {
ctx.server_notifier.set_use_external_preview(is_external);
}
M::RequestState { .. } => {
crate::language::request_state(ctx);
}
M::SendWorkspaceEdit { label, edit } => {
let _ = send_workspace_edit(ctx.server_notifier.clone(), label, Ok(edit)).await;
}
M::SendShowMessage { message } => {
ctx.server_notifier
.send_notification::<lsp_types::notification::ShowMessage>(message)?;
}
}
Ok(())
}