-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathmod.rs
235 lines (209 loc) · 8.04 KB
/
mod.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
mod native_bridge;
use self::native_bridge::LspNativeBridge;
use clarity_lsp::utils;
use clarity_repl::clarity::vm::diagnostic::{
Diagnostic as ClarityDiagnostic, Level as ClarityLevel,
};
use crossbeam_channel::unbounded;
use std::sync::mpsc;
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, Position, Range};
use tower_lsp::{LspService, Server};
pub fn run_lsp() {
if let Err(_e) = block_on(do_run_lsp()) {
std::process::exit(1)
};
}
pub fn block_on<F, R>(future: F) -> R
where
F: std::future::Future<Output = R>,
{
let rt = hiro_system_kit::create_basic_runtime();
rt.block_on(future)
}
async fn do_run_lsp() -> Result<(), String> {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let (notification_tx, notification_rx) = unbounded();
let (request_tx, request_rx) = unbounded();
let (response_tx, response_rx) = mpsc::channel();
std::thread::spawn(move || {
hiro_system_kit::nestable_block_on(native_bridge::start_language_server(
notification_rx,
request_rx,
response_tx,
));
});
let (service, socket) = LspService::new(|client| {
LspNativeBridge::new(client, notification_tx, request_tx, response_rx)
});
Server::new(stdin, stdout, socket).serve(service).await;
Ok(())
}
pub fn clarity_diagnostics_to_tower_lsp_type(
diagnostics: &mut [ClarityDiagnostic],
) -> Vec<tower_lsp::lsp_types::Diagnostic> {
let mut dst = vec![];
for d in diagnostics.iter_mut() {
dst.push(clarity_diagnostic_to_tower_lsp_type(d));
}
dst
}
pub fn clarity_diagnostic_to_tower_lsp_type(
diagnostic: &ClarityDiagnostic,
) -> tower_lsp::lsp_types::Diagnostic {
let range = match diagnostic.spans.len() {
0 => Range::default(),
_ => Range {
start: Position {
line: diagnostic.spans[0].start_line - 1,
character: diagnostic.spans[0].start_column - 1,
},
end: Position {
line: diagnostic.spans[0].end_line - 1,
character: diagnostic.spans[0].end_column,
},
},
};
// TODO(lgalabru): add hint for contracts not found errors
Diagnostic {
range,
severity: match diagnostic.level {
ClarityLevel::Error => Some(DiagnosticSeverity::ERROR),
ClarityLevel::Warning => Some(DiagnosticSeverity::WARNING),
ClarityLevel::Note => Some(DiagnosticSeverity::INFORMATION),
},
code: None,
code_description: None,
source: Some("clarity".to_string()),
message: diagnostic.message.clone(),
related_information: None,
tags: None,
data: None,
}
}
#[test]
fn test_opening_counter_contract_should_return_fresh_analysis() {
use crate::lsp::native_bridge::LspResponse;
use clarinet_files::FileLocation;
use clarity_lsp::backend::{LspNotification, LspNotificationResponse};
use crossbeam_channel::unbounded;
use std::sync::mpsc::channel;
let (notification_tx, notification_rx) = unbounded();
let (_request_tx, request_rx) = unbounded();
let (response_tx, response_rx) = channel();
std::thread::spawn(move || {
hiro_system_kit::nestable_block_on(native_bridge::start_language_server(
notification_rx,
request_rx,
response_tx,
));
});
let contract_location = {
let mut counter_path = std::env::current_dir().expect("Unable to get current dir");
counter_path.push("examples");
counter_path.push("counter");
counter_path.push("contracts");
counter_path.push("counter.clar");
FileLocation::from_path(counter_path)
};
let _ = notification_tx.send(LspNotification::ContractOpened(contract_location.clone()));
let response = response_rx.recv().expect("Unable to get response");
let response = if let LspResponse::Notification(response) = response {
response
} else {
panic!("Unable to get response")
};
// the counter project should emit 2 warnings and 2 notes coming from counter.clar
assert_eq!(response.aggregated_diagnostics.len(), 2);
let (_url, diags) = &response.aggregated_diagnostics[0];
assert_eq!(diags.len(), 4);
// re-opening this contract should not trigger a full analysis
let _ = notification_tx.send(LspNotification::ContractOpened(contract_location));
let response = response_rx.recv().expect("Unable to get response");
let response = if let LspResponse::Notification(response) = response {
response
} else {
panic!("Unable to get response")
};
assert_eq!(response, LspNotificationResponse::default());
}
#[test]
fn test_opening_counter_manifest_should_return_fresh_analysis() {
use crate::lsp::native_bridge::LspResponse;
use clarinet_files::FileLocation;
use clarity_lsp::backend::{LspNotification, LspNotificationResponse};
use crossbeam_channel::unbounded;
use std::sync::mpsc::channel;
let (notification_tx, notification_rx) = unbounded();
let (_request_tx, request_rx) = unbounded();
let (response_tx, response_rx) = channel();
std::thread::spawn(move || {
hiro_system_kit::nestable_block_on(native_bridge::start_language_server(
notification_rx,
request_rx,
response_tx,
));
});
let manifest_location = {
let mut manifest_path = std::env::current_dir().expect("Unable to get current dir");
manifest_path.push("examples");
manifest_path.push("counter");
manifest_path.push("Clarinet.toml");
FileLocation::from_path(manifest_path)
};
let _ = notification_tx.send(LspNotification::ManifestOpened(manifest_location.clone()));
let response = response_rx.recv().expect("Unable to get response");
let response = if let LspResponse::Notification(response) = response {
response
} else {
panic!("Unable to get response")
};
// the counter project should emit 2 warnings and 2 notes coming from counter.clar
assert_eq!(response.aggregated_diagnostics.len(), 2);
let (_url, diags) = &response.aggregated_diagnostics[0];
assert_eq!(diags.len(), 4);
// re-opening this manifest should not trigger a full analysis
let _ = notification_tx.send(LspNotification::ManifestOpened(manifest_location));
let response = response_rx.recv().expect("Unable to get response");
let response = if let LspResponse::Notification(response) = response {
response
} else {
panic!("Unable to get response")
};
assert_eq!(response, LspNotificationResponse::default());
}
#[test]
fn test_opening_simple_nft_manifest_should_return_fresh_analysis() {
use crate::lsp::native_bridge::LspResponse;
use clarinet_files::FileLocation;
use clarity_lsp::backend::LspNotification;
use crossbeam_channel::unbounded;
use std::sync::mpsc::channel;
let (notification_tx, notification_rx) = unbounded();
let (_request_tx, request_rx) = unbounded();
let (response_tx, response_rx) = channel();
std::thread::spawn(move || {
hiro_system_kit::nestable_block_on(native_bridge::start_language_server(
notification_rx,
request_rx,
response_tx,
));
});
let mut manifest_location = std::env::current_dir().expect("Unable to get current dir");
manifest_location.push("examples");
manifest_location.push("simple-nft");
manifest_location.push("Clarinet.toml");
let _ = notification_tx.send(LspNotification::ManifestOpened(FileLocation::from_path(
manifest_location,
)));
let response = response_rx.recv().expect("Unable to get response");
let response = if let LspResponse::Notification(response) = response {
response
} else {
panic!("Unable to get response")
};
assert_eq!(response.aggregated_diagnostics.len(), 1);
let (_, diags_0) = &response.aggregated_diagnostics[0];
// the counter project should emit 4 warnings and 4 notes coming from counter.clar
assert_eq!(diags_0.len(), 8);
}