-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathbackend.rs
More file actions
1094 lines (964 loc) · 41.8 KB
/
backend.rs
File metadata and controls
1094 lines (964 loc) · 41.8 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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use clarinet_files::{paths, FileAccessor, ProjectManifest};
use clarity_repl::clarity::diagnostic::Diagnostic;
use clarity_repl::repl::boot::get_boot_contract_epoch_and_clarity_version;
use clarity_repl::repl::ContractDeployer;
use ls_types::{
CodeLens, CodeLensParams, CompletionItem, CompletionParams, DocumentFormattingParams,
DocumentRangeFormattingParams, DocumentSymbol, DocumentSymbolParams, GotoDefinitionParams,
Hover, HoverParams, InitializeParams, InitializeResult, Location, MessageType, ServerInfo,
SignatureHelp, SignatureHelpParams, TextEdit,
};
use serde::{Deserialize, Serialize};
use super::requests::capabilities::{get_capabilities, InitializationOptions};
use crate::state::{build_state, EditorState, ProtocolState};
use crate::utils::get_contract_location;
#[derive(Debug, Clone)]
pub enum EditorStateInput {
Owned(EditorState),
RwLock(Arc<RwLock<EditorState>>),
}
impl EditorStateInput {
pub fn try_read<F, R>(&self, closure: F) -> Result<R, String>
where
F: FnOnce(&EditorState) -> R,
{
match self {
EditorStateInput::Owned(editor_state) => Ok(closure(editor_state)),
EditorStateInput::RwLock(editor_state_lock) => match editor_state_lock.try_read() {
Ok(editor_state) => Ok(closure(&editor_state)),
Err(_) => Err("failed to read editor_state".to_string()),
},
}
}
pub fn try_write<F, R>(&mut self, closure: F) -> Result<R, String>
where
F: FnOnce(&mut EditorState) -> R,
{
match self {
EditorStateInput::Owned(editor_state) => Ok(closure(editor_state)),
EditorStateInput::RwLock(editor_state_lock) => match editor_state_lock.try_write() {
Ok(mut editor_state) => Ok(closure(&mut editor_state)),
Err(_) => Err("failed to write editor_state".to_string()),
},
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum LspNotification {
ManifestOpened(PathBuf),
ManifestSaved(PathBuf),
ContractOpened(PathBuf),
ContractSaved(PathBuf),
ContractChanged(PathBuf, String),
ContractClosed(PathBuf),
}
#[derive(Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct LspNotificationResponse {
pub aggregated_diagnostics: Vec<(PathBuf, Vec<Diagnostic>)>,
pub notification: Option<(MessageType, String)>,
}
impl LspNotificationResponse {
pub fn error(message: &str) -> LspNotificationResponse {
LspNotificationResponse {
aggregated_diagnostics: vec![],
notification: Some((MessageType::ERROR, format!("Internal error: {message}"))),
}
}
}
pub async fn process_notification(
command: LspNotification,
editor_state: &mut EditorStateInput,
file_accessor: Option<&dyn FileAccessor>,
) -> Result<LspNotificationResponse, String> {
match command {
LspNotification::ManifestOpened(manifest_location) => {
// Only build the initial protocol state if it does not exist
if editor_state.try_read(|es| es.protocols.contains_key(&manifest_location))? {
return Ok(LspNotificationResponse::default());
}
// With this manifest_location, let's initialize our state.
let mut protocol_state = ProtocolState::new();
match build_state(&manifest_location, &mut protocol_state, file_accessor).await {
Ok(_) => {
editor_state
.try_write(|es| es.index_protocol(manifest_location, protocol_state))?;
let (aggregated_diagnostics, notification) =
editor_state.try_read(|es| es.get_aggregated_diagnostics())?;
Ok(LspNotificationResponse {
aggregated_diagnostics,
notification,
})
}
Err(e) => Ok(LspNotificationResponse::error(&e)),
}
}
LspNotification::ManifestSaved(manifest_location) => {
// We will rebuild the entire state, without to try any optimizations for now
let mut protocol_state = ProtocolState::new();
match build_state(&manifest_location, &mut protocol_state, file_accessor).await {
Ok(_) => {
editor_state
.try_write(|es| es.index_protocol(manifest_location, protocol_state))?;
let (aggregated_diagnostics, notification) =
editor_state.try_read(|es| es.get_aggregated_diagnostics())?;
Ok(LspNotificationResponse {
aggregated_diagnostics,
notification,
})
}
Err(e) => Ok(LspNotificationResponse::error(&e)),
}
}
LspNotification::ContractOpened(contract_location) => {
let manifest_location = match file_accessor {
None => paths::find_manifest_location(&contract_location)?,
Some(file_accessor) => {
paths::find_manifest_location_async(&contract_location, file_accessor).await?
}
};
// store the contract in the active_contracts map
if !editor_state.try_read(|es| es.active_contracts.contains_key(&contract_location))? {
let contract_source = match file_accessor {
None => paths::read_content_as_utf8(&contract_location),
Some(file_accessor) => {
file_accessor
.read_file(contract_location.to_string_lossy().to_string())
.await
}
}?;
let metadata = editor_state.try_read(|es| {
es.contracts_lookup
.get(&contract_location)
.map(|metadata| (metadata.clarity_version, metadata.deployer.clone()))
})?;
// if the contract isn't in lookup yet, fallback on manifest, to be improved in #668
let clarity_version = match metadata {
Some((clarity_version, _)) => clarity_version,
None => {
let manifest = match file_accessor {
None => ProjectManifest::from_location(&manifest_location, false),
Some(file_accessor) => {
ProjectManifest::from_file_accessor(
&manifest_location,
false,
file_accessor,
)
.await
}
}?;
if let Some(contract_metadata) =
manifest.contracts_settings.get(&contract_location)
{
contract_metadata.clarity_version
} else {
// boot contracts path checking
let mut found_boot_contract = None;
for (contract_name, contract_path) in
&manifest.project.override_boot_contracts_source
{
let resolved_path = manifest.root_dir.join(contract_path);
if resolved_path == contract_location {
found_boot_contract = Some(contract_name);
break;
}
}
if let Some(contract_name) = found_boot_contract {
let (_, version) =
get_boot_contract_epoch_and_clarity_version(contract_name);
version
} else {
return Err(format!(
"No Clarinet.toml is associated to the contract {}",
contract_location.display()
));
}
}
}
};
let issuer = metadata.and_then(|(_, deployer)| match deployer {
ContractDeployer::ContractIdentifier(id) => Some(id.issuer),
_ => None,
});
editor_state.try_write(|es| {
es.insert_active_contract(
contract_location.clone(),
clarity_version,
issuer,
contract_source,
)
})?;
}
// Only build the initial protocol state if it does not exist
if editor_state.try_read(|es| es.protocols.contains_key(&manifest_location))? {
return Ok(LspNotificationResponse::default());
}
let mut protocol_state = ProtocolState::new();
match build_state(&manifest_location, &mut protocol_state, file_accessor).await {
Ok(_) => {
editor_state
.try_write(|es| es.index_protocol(manifest_location, protocol_state))?;
let (aggregated_diagnostics, notification) =
editor_state.try_read(|es| es.get_aggregated_diagnostics())?;
Ok(LspNotificationResponse {
aggregated_diagnostics,
notification,
})
}
Err(e) => Ok(LspNotificationResponse::error(&e)),
}
}
LspNotification::ContractSaved(contract_location) => {
let manifest_location = match editor_state
.try_write(|es| es.clear_protocol_associated_with_contract(&contract_location))?
{
Some(manifest_location) => manifest_location,
None => match file_accessor {
None => paths::find_manifest_location(&contract_location)?,
Some(file_accessor) => {
paths::find_manifest_location_async(&contract_location, file_accessor)
.await?
}
},
};
// TODO(): introduce partial analysis #604
let mut protocol_state = ProtocolState::new();
match build_state(&manifest_location, &mut protocol_state, file_accessor).await {
Ok(_) => {
editor_state.try_write(|es| {
es.index_protocol(manifest_location, protocol_state);
if let Some(contract) = es.active_contracts.get_mut(&contract_location) {
contract.update_definitions();
};
})?;
let (aggregated_diagnostics, notification) =
editor_state.try_read(|es| es.get_aggregated_diagnostics())?;
Ok(LspNotificationResponse {
aggregated_diagnostics,
notification,
})
}
Err(e) => Ok(LspNotificationResponse::error(&e)),
}
}
LspNotification::ContractChanged(contract_location, contract_source) => {
match editor_state.try_write(|es| {
es.update_active_contract(&contract_location, &contract_source, false)
})? {
Ok(_result) => Ok(LspNotificationResponse::default()),
Err(err) => Ok(LspNotificationResponse::error(&err)),
}
}
LspNotification::ContractClosed(contract_location) => {
editor_state.try_write(|es| es.active_contracts.remove_entry(&contract_location))?;
Ok(LspNotificationResponse::default())
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum LspRequest {
Completion(CompletionParams),
SignatureHelp(SignatureHelpParams),
Definition(GotoDefinitionParams),
Hover(HoverParams),
DocumentSymbol(DocumentSymbolParams),
DocumentFormatting(DocumentFormattingParams),
DocumentRangeFormatting(DocumentRangeFormattingParams),
CodeLens(CodeLensParams),
Initialize(Box<InitializeParams>),
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum LspRequestResponse {
CompletionItems(Vec<CompletionItem>),
SignatureHelp(Option<SignatureHelp>),
Definition(Option<Location>),
DocumentSymbol(Vec<DocumentSymbol>),
DocumentFormatting(Option<Vec<TextEdit>>),
DocumentRangeFormatting(Option<Vec<TextEdit>>),
Hover(Option<Hover>),
CodeLens(Vec<CodeLens>),
Initialize(Box<InitializeResult>),
}
pub fn process_request(
command: LspRequest,
editor_state: &EditorStateInput,
) -> Result<LspRequestResponse, String> {
match command {
LspRequest::Completion(params) => {
let file_url = params.text_document_position.text_document.uri;
let position = params.text_document_position.position;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::CompletionItems(vec![]));
};
let Ok(completion_items) = editor_state
.try_read(|es| es.get_completion_items_for_contract(&contract_location, &position))
else {
return Ok(LspRequestResponse::CompletionItems(vec![]));
};
Ok(LspRequestResponse::CompletionItems(completion_items))
}
LspRequest::Definition(params) => {
let file_url = params.text_document_position_params.text_document.uri;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::Definition(None));
};
let position = params.text_document_position_params.position;
let location = editor_state
.try_read(|es| es.get_definition_location(&contract_location, &position))
.unwrap_or_default();
Ok(LspRequestResponse::Definition(location))
}
LspRequest::SignatureHelp(params) => {
let file_url = params.text_document_position_params.text_document.uri;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::SignatureHelp(None));
};
let position = params.text_document_position_params.position;
// if the developer selects a specific signature
// it can be retrieved in the context and kept selected
let active_signature = params
.context
.and_then(|c| c.active_signature_help)
.and_then(|s| s.active_signature);
let signature = editor_state
.try_read(|es| {
es.get_signature_help(&contract_location, &position, active_signature)
})
.unwrap_or_default();
Ok(LspRequestResponse::SignatureHelp(signature))
}
LspRequest::DocumentSymbol(params) => {
let file_url = params.text_document.uri;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::DocumentSymbol(vec![]));
};
let document_symbols = editor_state
.try_read(|es| es.get_document_symbols_for_contract(&contract_location))
.unwrap_or_default();
Ok(LspRequestResponse::DocumentSymbol(document_symbols))
}
LspRequest::DocumentFormatting(param) => {
let file_url = param.text_document.uri;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::DocumentFormatting(None));
};
let Ok(Some(contract_data)) =
editor_state.try_read(|es| es.active_contracts.get(&contract_location).cloned())
else {
return Ok(LspRequestResponse::DocumentFormatting(None));
};
let source = &contract_data.source;
let tab_size = param.options.tab_size as usize;
let prefer_space = param.options.insert_spaces;
let props = param.options.properties;
let max_line_length = props
.get("maxLineLength")
.and_then(|value| {
// FormattingProperty can be boolean, number, or string
match value {
ls_types::FormattingProperty::Number(num) => Some(*num as usize),
ls_types::FormattingProperty::String(s) => s.parse::<usize>().ok(),
_ => None,
}
})
.unwrap_or(80);
let formatting_options = clarinet_format::formatter::Settings {
indentation: if !prefer_space {
clarinet_format::formatter::Indentation::Tab
} else {
clarinet_format::formatter::Indentation::Space(tab_size)
},
max_line_length,
};
let formatter = clarinet_format::formatter::ClarityFormatter::new(formatting_options);
let formatted_result = formatter.format_file(source, Some(contract_data.epoch));
let text_edit = ls_types::TextEdit {
range: ls_types::Range {
start: ls_types::Position {
line: 0,
character: 0,
},
end: ls_types::Position {
line: source.lines().count() as u32,
character: 0,
},
},
new_text: formatted_result,
};
Ok(LspRequestResponse::DocumentFormatting(Some(vec![
text_edit,
])))
}
LspRequest::DocumentRangeFormatting(param) => {
let file_url = param.text_document.uri;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::DocumentRangeFormatting(None));
};
let Ok(Some(contract_data)) =
editor_state.try_read(|es| es.active_contracts.get(&contract_location).cloned())
else {
return Ok(LspRequestResponse::DocumentRangeFormatting(None));
};
let source = &contract_data.source;
let tab_size = param.options.tab_size as usize;
let max_line_length = param
.options
.properties
.get("maxLineLength")
.and_then(|value| {
// FormattingProperty can be boolean, number, or string
match value {
ls_types::FormattingProperty::Number(num) => Some(*num as usize),
ls_types::FormattingProperty::String(s) => s.parse::<usize>().ok(),
_ => None,
}
})
.unwrap_or(80);
let prefer_space = param.options.insert_spaces;
let formatting_options = clarinet_format::formatter::Settings {
indentation: if !prefer_space {
clarinet_format::formatter::Indentation::Tab
} else {
clarinet_format::formatter::Indentation::Space(tab_size)
},
max_line_length,
};
let epoch = Some(contract_data.epoch);
// extract the text of just this range
let lines: Vec<&str> = source.lines().collect();
let start_line = param.range.start.line as usize;
let end_line = param.range.end.line as usize;
// Validate range boundaries
if start_line >= lines.len() {
return Ok(LspRequestResponse::DocumentRangeFormatting(None));
}
// Get the substring representing just the selected range
let range_text = if start_line == end_line {
// Single line selection
let line = lines.get(start_line).unwrap_or(&"");
let start_char = param.range.start.character as usize;
let end_char = param.range.end.character as usize;
let start_char = start_char.min(line.len());
let end_char = end_char.min(line.len());
if start_char >= end_char {
return Ok(LspRequestResponse::DocumentRangeFormatting(None));
}
line[start_char..end_char].to_string()
} else {
let mut result = String::new();
// First line (might be partial)
if let Some(first_line) = lines.get(start_line) {
let start_char = (param.range.start.character as usize).min(first_line.len());
result.push_str(&first_line[start_char..]);
}
// Middle lines (complete lines)
for line_idx in (start_line + 1)..end_line {
if let Some(line) = lines.get(line_idx) {
result.push('\n');
result.push_str(line);
}
}
// Last line (might be partial) - only if end_line is different from start_line
if end_line > start_line && end_line < lines.len() {
if let Some(last_line) = lines.get(end_line) {
let end_char = (param.range.end.character as usize).min(last_line.len());
result.push('\n');
result.push_str(&last_line[..end_char]);
}
}
result
};
// If the range text is empty or only whitespace, return None
if range_text.trim().is_empty() {
return Ok(LspRequestResponse::DocumentRangeFormatting(None));
}
// Count the number of trailing newlines in the original selection
let mut trailing_newlines = 0;
let mut temp_text = range_text.clone();
while temp_text.ends_with('\n') {
trailing_newlines += 1;
temp_text.pop();
}
let formatter = clarinet_format::formatter::ClarityFormatter::new(formatting_options);
// Try to format the range text, but handle panics/errors gracefully
let formatted_result = formatter.format_section(&range_text, epoch);
let formatted_result = match formatted_result {
Ok(formatted_text) => {
let mut result = formatted_text.trim_end().to_string();
// Add back the same number of trailing newlines that were in the original
for _ in 0..trailing_newlines {
result.push('\n');
}
result
}
Err(_) => {
// If the selected range contains malformed/incomplete Clarity code,
// return None to indicate formatting is not possible
return Ok(LspRequestResponse::DocumentRangeFormatting(None));
}
};
let text_edit = ls_types::TextEdit {
range: param.range,
new_text: formatted_result,
};
Ok(LspRequestResponse::DocumentRangeFormatting(Some(vec![
text_edit,
])))
}
LspRequest::Hover(params) => {
let file_url = params.text_document_position_params.text_document.uri;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::Hover(None));
};
let position = params.text_document_position_params.position;
let hover_data = editor_state
.try_read(|es| es.get_hover_data(&contract_location, &position))
.unwrap_or_default();
Ok(LspRequestResponse::Hover(hover_data))
}
LspRequest::CodeLens(params) => {
let file_url = params.text_document.uri;
let Some(contract_location) = get_contract_location(&file_url) else {
return Ok(LspRequestResponse::CodeLens(vec![]));
};
let code_lenses = editor_state
.try_read(|es| es.get_code_lenses(&contract_location))
.unwrap_or_default();
Ok(LspRequestResponse::CodeLens(code_lenses))
}
_ => Err(format!("Unexpected command: {command:?}")),
}
}
// lsp requests are not supposed to mut the editor_state (only the notifications do)
// this is to ensure there is no concurrency between notifications and requests to
// acquire write lock on the editor state in a wasm context
// except for the Initialize request, which is the first interaction between the client and the server
// and can therefore safely acquire write lock on the editor state
pub fn process_mutating_request(
command: LspRequest,
editor_state: &mut EditorStateInput,
) -> Result<LspRequestResponse, String> {
match command {
LspRequest::Initialize(params) => {
let initialization_options: InitializationOptions = params
.initialization_options
.and_then(|o| serde_json::from_value(o).ok())
.unwrap_or_default();
editor_state
.try_write(|es| es.settings = initialization_options.clone())
.map(|_| {
LspRequestResponse::Initialize(Box::new(InitializeResult {
server_info: Some(ServerInfo {
name: "clarinet lsp".to_owned(),
version: Some(String::from(env!("CARGO_PKG_VERSION"))),
}),
capabilities: get_capabilities(&initialization_options),
}))
})
}
_ => Err(format!(
"Unexpected command: {command:?}, should not mutate state"
)),
}
}
#[cfg(test)]
mod lsp_tests {
use std::collections::HashMap;
use std::path::PathBuf;
use clarity_repl::clarity::ClarityVersion;
use indoc::indoc;
use ls_types::{
DocumentRangeFormattingParams, FormattingOptions, Position, Range, TextDocumentIdentifier,
WorkDoneProgressParams,
};
use serde_json::{json, Value};
use super::*;
use crate::common::state::EditorState;
fn get_root_path() -> PathBuf {
if cfg!(windows) {
PathBuf::from(std::env::var("SystemDrive").unwrap_or_else(|_| "C:".to_string()) + "\\")
} else {
PathBuf::from("/")
}
}
fn create_test_editor_state(source: String) -> EditorStateInput {
let mut editor_state = EditorState::new();
let contract_location = get_root_path().join("test.clar");
editor_state.insert_active_contract(
contract_location,
ClarityVersion::Clarity2,
None,
source,
);
EditorStateInput::Owned(editor_state)
}
#[test]
fn test_range_formatting_comments() {
let source = "(ok true)\n\n(define-public (foo)\n ;; this is a comment\n (ok true)\n)";
let editor_state_input = create_test_editor_state(source.to_owned());
let params = DocumentRangeFormattingParams {
text_document: TextDocumentIdentifier {
uri: "file:///test.clar".parse().unwrap(),
},
range: Range {
start: Position {
line: 3,
character: 1,
},
end: Position {
line: 6,
character: 2,
},
},
options: FormattingOptions {
tab_size: 2,
insert_spaces: true,
properties: HashMap::new(),
trim_trailing_whitespace: None,
insert_final_newline: None,
trim_final_newlines: None,
},
work_done_progress_params: WorkDoneProgressParams {
work_done_token: None,
},
};
let request = LspRequest::DocumentRangeFormatting(params);
assert!(process_request(request, &editor_state_input).is_ok());
}
#[test]
fn test_go_to_definition() {
let source = "(define-constant N 1) (define-read-only (get-N) N)";
let editor_state_input = create_test_editor_state(source.to_owned());
let path = get_root_path().join("test.clar");
let params = GotoDefinitionParams {
text_document_position_params: ls_types::TextDocumentPositionParams {
text_document: ls_types::TextDocumentIdentifier {
uri: paths::path_to_url_string(&path).unwrap().parse().unwrap(),
},
// Position inside the 'N' constant
position: Position {
line: 0,
character: 49,
},
},
work_done_progress_params: WorkDoneProgressParams {
work_done_token: None,
},
partial_result_params: ls_types::PartialResultParams {
partial_result_token: None,
},
};
let request = LspRequest::Definition(params);
let response =
process_request(request, &editor_state_input).expect("Failed to process request");
let LspRequestResponse::Definition(Some(location)) = &response else {
panic!("Expected a Definition response, got: {response:?}");
};
assert_eq!(location.uri.scheme().as_str(), "file");
let response_json = json!(response);
assert!(response_json
.get("Definition")
.expect("Expected 'Definition' key")
.get("uri")
.expect("Expected 'uri' key")
.to_string()
.ends_with("test.clar\""));
}
struct TestFileAccessor {
project_manifest: String,
network_manifest: String,
contract: String,
}
impl TestFileAccessor {
fn new(contract: String) -> Self {
let project_manifest = indoc! {r#"
[project]
name = 'test-project'
[contracts.counter]
path = 'contracts/test.clar'
epoch = 'latest'
clarity_version = 3
"#}
.to_string();
let network_manifest = indoc! {r#"
[network]
name = "devnet"
deployment_fee_rate = 10
[accounts.deployer]
mnemonic = "twice kind fence tip hidden tilt action fragile skin nothing glory cousin green tomorrow spring wrist shed math olympic multiply hip blue scout claw"
balance = 100_000_000_000_000
sbtc_balance = 1_000_000_000
"#}
.to_string();
Self {
project_manifest,
network_manifest,
contract,
}
}
}
impl FileAccessor for TestFileAccessor {
fn file_exists(&self, _path: String) -> clarinet_files::FileAccessorResult<bool> {
Box::pin(async { Ok(true) })
}
fn read_file(&self, path: String) -> clarinet_files::FileAccessorResult<String> {
let content = if path.ends_with("Clarinet.toml") {
self.project_manifest.clone()
} else if path.ends_with("Devnet.toml") {
self.network_manifest.clone()
} else {
self.contract.clone()
};
Box::pin(async { Ok(content) })
}
fn read_files(
&self,
contracts_paths: Vec<String>,
) -> clarinet_files::FileAccessorResult<HashMap<String, String>> {
let contract = self.contract.clone();
Box::pin(async move {
Ok(contracts_paths
.into_iter()
.map(|path| (path, contract.clone()))
.collect())
})
}
fn write_file(
&self,
_path: String,
_content: &[u8],
) -> clarinet_files::FileAccessorResult<()> {
Box::pin(async { Ok(()) })
}
}
#[tokio::test]
async fn test_env_simnet() {
let with_env_simnet = indoc! {r#"
(define-data-var count uint u0)
;; #[env(simnet)]
(define-public (increment)
(ok (var-set count (+ (var-get count) u1)))
)
(define-public (increment2)
(increment)
)
"#};
let file_accessor = TestFileAccessor::new(with_env_simnet.to_string());
let mut editor_state_input = EditorStateInput::Owned(EditorState::new());
let contract_location = PathBuf::from("test.clar".to_string());
let notification = LspNotification::ContractSaved(contract_location);
let response =
process_notification(notification, &mut editor_state_input, Some(&file_accessor))
.await
.expect("Failed to process notification");
let response_json = json!(response);
println!("full response: {response_json}");
let aggregated_diagnostics = response_json
.get("aggregated_diagnostics")
.expect("Expected 'aggregate_diagnostics' key");
let Some(aggregate_array) = aggregated_diagnostics.as_array() else {
panic!("aggregated_diagnostics should be an array");
};
assert_eq!(aggregate_array.len(), 1);
let element = aggregate_array
.first()
.expect("Expected an element in outer array");
let Some(element_array) = element.as_array() else {
panic!("element should be an array");
};
assert_eq!(
element_array.len(),
2,
"Element array should have two items"
);
let path = element_array
.first()
.expect("Failed to get path in element array")
.to_string();
assert_eq!(path, "\"contracts/test.clar\"");
let diagnostics = element_array
.get(1)
.expect("Failed to get diagnostics in element array");
let Some(diagnostics_array) = diagnostics.as_array() else {
panic!("diagnostics should be an array");
};
assert_eq!(
diagnostics_array.len(),
1,
"Diagnostics array should have one item"
);
let diagnostic = diagnostics_array
.first()
.expect("Failed to get expected diagnostic");
let level = diagnostic
.get("level")
.expect("Failed to find \"level\": in diagnostic")
.to_string();
assert_eq!(level, "\"Error\"");
let message = diagnostic
.get("message")
.expect("Failed to find \"message\": in diagnostic");
assert_eq!(message, "use of unresolved function 'increment' (onchain)");
let spans = diagnostic
.get("spans")
.expect("Failed to find \"spans\": in diagnostic");
let Some(spans_array) = spans.as_array() else {
panic!("spans should be an array");
};
assert_eq!(spans_array.len(), 1, "Spans array should have one item");
let span = spans_array.first().expect("Failed to get expected span");
let start_line = span
.get("start_line")
.expect("span didn't have a \"start_line\" field");
let end_line = span
.get("end_line")
.expect("span didn't have a \"end_line\" field");
assert_eq!(start_line, &Value::from(8));
assert_eq!(end_line, &Value::from(10));
}
#[test]
fn test_custom_boot_contract_recognition() {
let manifest_content = indoc! {r#"
[project]
name = "test-project"
telemetry = false
[project.override_boot_contracts_source]
"pox-4" = "./custom-boot-contracts/pox-4.clar"
"costs" = "./custom-boot-contracts/costs.clar"
[contracts.test-contract]
path = "contracts/test.clar"
clarity_version = 1
"#};
// Create a test contract in custom-boot-contracts
let contract_content = indoc! {r#"
(define-data-var counter uint u0)
(define-public (increment)
(begin
(set-data-var! counter (+ (var-get counter) u1))
(ok (var-get counter))
)
)
"#};
// This test verifies that the LSP infrastructure can handle custom-boot-contracts
// The actual file system operations would be handled by the file accessor
// but we can verify the contract recognition logic works
assert!(manifest_content.contains("custom-boot-contracts"));
assert!(contract_content.contains("define-public"));
}
#[tokio::test]
async fn test_env_simnet_opened() {
let with_env_simnet = indoc! {r#"
;; token definitions
;;
(define-fungible-token drachma)
;; constants
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-OWNER-ONLY (err u100))
(define-constant ERR-NOT-TOKEN-OWNER (err u101))
;; data vars
;;
(define-data-var token-uri (optional (string-utf8 256)) (some u"https://en.wikipedia.org/wiki/Ancient_drachma"))
;; data maps
;;
;; public functions
;;
(define-public (mint (amount uint) (recipient principal))
(begin
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-OWNER-ONLY)
(minty-fresh amount recipient)
)
)
;; #[env(simnet)]
(define-public (minty-fresh (amount uint) (recipient principal)) ;; eol
(begin
(ft-mint? drachma amount recipient)
)
)
;; post comment
"#};
let file_accessor = TestFileAccessor::new(with_env_simnet.to_string());
let mut editor_state_input = EditorStateInput::Owned(EditorState::new());