forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
202 lines (180 loc) · 6.19 KB
/
main.rs
File metadata and controls
202 lines (180 loc) · 6.19 KB
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
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
#![cfg(not(target_arch = "wasm32"))]
mod completion;
mod goto;
mod lsp_ext;
#[cfg(feature = "preview")]
mod preview;
mod semantic_tokens;
mod server_loop;
mod util;
use i_slint_compiler::CompilerConfiguration;
use lsp_types::notification::{DidChangeTextDocument, DidOpenTextDocument, Notification};
use lsp_types::{DidChangeTextDocumentParams, DidOpenTextDocumentParams, InitializeParams};
use server_loop::*;
use clap::Parser;
use lsp_server::{Connection, Message, Request, Response};
#[derive(Clone, clap::Parser)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[clap(
short = 'I',
name = "Add include paths for the import statements",
number_of_values = 1,
parse(from_os_str)
)]
include_paths: Vec<std::path::PathBuf>,
/// The style name for the preview ('native' or 'fluent')
#[clap(long, name = "style name", default_value_t)]
style: String,
/// The backend used for the preview ('GL' or 'Qt')
#[clap(long, name = "backend", default_value_t)]
backend: String,
}
#[derive(Clone)]
pub struct ServerNotifier(crossbeam_channel::Sender<Message>);
impl ServerNotifier {
pub fn send_notification(
&self,
method: String,
params: impl serde::Serialize,
) -> Result<(), Error> {
self.0.send(Message::Notification(lsp_server::Notification::new(method, params)))?;
Ok(())
}
}
pub struct RequestHolder(Request, crossbeam_channel::Sender<Message>);
impl RequestHolder {
pub fn handle_request<
Kind: lsp_types::request::Request,
F: FnOnce(Kind::Params) -> Result<Kind::Result, Error>,
>(
&self,
f: F,
) -> Result<bool, Error> {
let (id, param) = match self.0.clone().extract::<Kind::Params>(Kind::METHOD) {
Ok(value) => value,
Err(lsp_server::ExtractError::MethodMismatch(_)) => {
return Ok(false);
}
Err(e) => {
return Err(format!("error when deserializing request: {e:?}").into());
}
};
let result = f(param)?;
self.1.send(Message::Response(Response::new_ok(id, result)))?;
Ok(true)
}
pub fn server_notifier(&self) -> ServerNotifier {
ServerNotifier(self.1.clone())
}
}
fn main() {
let args: Cli = Cli::parse();
if !args.backend.is_empty() {
std::env::set_var("SLINT_BACKEND", &args.backend);
}
#[cfg(feature = "preview")]
{
let lsp_thread = std::thread::spawn(|| {
/// 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;
match run_lsp_server() {
Ok(_) => {}
Err(error) => {
eprintln!("Error running LSP server: {}", error);
}
}
});
preview::start_ui_event_loop();
lsp_thread.join().unwrap();
}
#[cfg(not(feature = "preview"))]
match run_lsp_server() {
Ok(_) => {}
Err(error) => {
eprintln!("Error running LSP server: {}", error);
}
}
}
pub fn run_lsp_server() -> Result<(), Error> {
let (connection, io_threads) = Connection::stdio();
let capabilities = server_loop::server_capabilities();
let server_capabilities = serde_json::to_value(&capabilities).unwrap();
let initialization_params = connection.initialize(server_capabilities)?;
main_loop(&connection, initialization_params)?;
io_threads.join()?;
Ok(())
}
fn main_loop(connection: &Connection, params: serde_json::Value) -> Result<(), Error> {
let params: InitializeParams = serde_json::from_value(params).unwrap();
let mut compiler_config =
CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Interpreter);
let cli_args = Cli::parse();
compiler_config.style =
Some(if cli_args.style.is_empty() { "fluent".into() } else { cli_args.style });
compiler_config.include_paths = cli_args.include_paths;
let mut document_cache = DocumentCache::new(compiler_config);
for msg in &connection.receiver {
match msg {
Message::Request(req) => {
if connection.handle_shutdown(&req)? {
return Ok(());
}
handle_request(
RequestHolder(req, connection.sender.clone()),
¶ms,
&mut document_cache,
)?;
}
Message::Response(_resp) => {}
Message::Notification(notifi) => {
handle_notification(connection, notifi, &mut document_cache)?
}
}
}
Ok(())
}
pub fn handle_notification(
connection: &Connection,
req: lsp_server::Notification,
document_cache: &mut DocumentCache,
) -> Result<(), Error> {
match &*req.method {
DidOpenTextDocument::METHOD => {
let params: DidOpenTextDocumentParams = serde_json::from_value(req.params)?;
spin_on::spin_on(reload_document(
&ServerNotifier(connection.sender.clone()),
params.text_document.text,
params.text_document.uri,
document_cache,
))?;
}
DidChangeTextDocument::METHOD => {
let mut params: DidChangeTextDocumentParams = serde_json::from_value(req.params)?;
spin_on::spin_on(reload_document(
&ServerNotifier(connection.sender.clone()),
params.content_changes.pop().unwrap().text,
params.text_document.uri,
document_cache,
))?;
}
#[cfg(feature = "preview")]
"slint/showPreview" => {
show_preview_command(
req.params.as_array().map_or(&[], |x| x.as_slice()),
&ServerNotifier(connection.sender.clone()),
document_cache,
)?;
}
_ => (),
}
Ok(())
}