-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhover.rs
108 lines (92 loc) · 3.61 KB
/
hover.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
use cairo_lang_test_utils::parse_test_file::TestRunnerResult;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use lsp_types::{
ClientCapabilities, Hover, HoverClientCapabilities, HoverContents, HoverParams, MarkupContent,
MarkupKind, TextDocumentClientCapabilities, TextDocumentPositionParams, lsp_request,
};
use crate::support::cairo_project_toml::CAIRO_PROJECT_TOML_2023_11;
use crate::support::cursor::{peek_caret, peek_selection};
use crate::support::{cursors, sandbox};
cairo_lang_test_utils::test_file_test!(
hover,
"tests/test_data/hover",
{
basic: "basic.txt",
missing_module: "missing_module.txt",
partial: "partial.txt",
starknet: "starknet.txt",
literals: "literals.txt",
structs: "structs.txt",
paths: "paths.txt",
variables: "variables.txt",
},
test_hover
);
fn caps(base: ClientCapabilities) -> ClientCapabilities {
ClientCapabilities {
text_document: base.text_document.or_else(Default::default).map(|it| {
TextDocumentClientCapabilities {
hover: Some(HoverClientCapabilities {
dynamic_registration: Some(false),
content_format: Some(vec![MarkupKind::Markdown, MarkupKind::PlainText]),
}),
..it
}
}),
..base
}
}
/// Perform hover test.
///
/// This function spawns a sandbox language server with the given code in the `src/lib.cairo` file.
/// The Cairo source code is expected to contain caret markers.
/// The function then requests hover information at each caret position and compares the result with
/// the expected hover information from the snapshot file.
fn test_hover(
inputs: &OrderedHashMap<String, String>,
_args: &OrderedHashMap<String, String>,
) -> TestRunnerResult {
let (cairo, cursors) = cursors(&inputs["cairo_code"]);
let mut ls = sandbox! {
files {
"cairo_project.toml" => CAIRO_PROJECT_TOML_2023_11,
"src/lib.cairo" => cairo.clone(),
}
client_capabilities = caps;
};
ls.open_all_cairo_files_and_wait_for_project_update();
let mut hovers = OrderedHashMap::default();
for (n, position) in cursors.carets().into_iter().enumerate() {
let hover_name = format!("hover #{n}");
let mut report = String::new();
report.push_str("// = source context\n");
report.push_str(&peek_caret(&cairo, position));
let hover = ls.send_request::<lsp_request!("textDocument/hover")>(HoverParams {
text_document_position_params: TextDocumentPositionParams {
text_document: ls.doc_id("src/lib.cairo"),
position,
},
work_done_progress_params: Default::default(),
});
report.push_str("// = highlight\n");
if let Some(Hover { range: Some(range), .. }) = &hover {
report.push_str(&peek_selection(&cairo, range));
} else {
report.push_str("No highlight information.\n");
}
report.push_str("// = popover\n");
report.push_str(hover.as_ref().map(render).unwrap_or("No hover information.\n"));
hovers.insert(hover_name, report);
}
TestRunnerResult::success(hovers)
}
/// Render a hover response to a Markdown string that resembles what would be shown in a hover popup
/// in the text editor.
fn render(h: &Hover) -> &str {
match &h.contents {
HoverContents::Markup(MarkupContent { value, .. }) => value,
contents => {
panic!("LS returned deprecated MarkedString-based hover contents: {:#?}", contents);
}
}
}