1
- use lsp_types:: lsp_request;
1
+ use lsp_types:: { Position , Range , lsp_request} ;
2
2
3
3
use crate :: support:: cairo_project_toml:: CAIRO_PROJECT_TOML_2023_11 ;
4
+ use crate :: support:: cursor:: render_text_with_annotations;
4
5
use crate :: support:: sandbox;
6
+ use cairo_language_server:: testing:: SemanticTokenKind ;
5
7
6
- fn caps ( base : lsp_types:: ClientCapabilities ) -> lsp_types:: ClientCapabilities {
7
- lsp_types:: ClientCapabilities {
8
- text_document : base. text_document . or_else ( Default :: default) . map ( |it| {
9
- lsp_types:: TextDocumentClientCapabilities {
10
- semantic_tokens : Some ( lsp_types:: SemanticTokensClientCapabilities {
11
- dynamic_registration : Some ( false ) ,
12
- requests : lsp_types:: SemanticTokensClientCapabilitiesRequests {
13
- full : Some ( lsp_types:: SemanticTokensFullOptions :: Bool ( true ) ) ,
14
- ..Default :: default ( )
15
- } ,
16
- ..Default :: default ( )
17
- } ) ,
18
- ..it
19
- }
20
- } ) ,
21
- ..base
22
- }
23
- }
8
+ mod complex;
24
9
25
- #[ test]
26
- fn highlights_multiline_tokens ( ) {
10
+ fn semantic_tokens ( code : & str ) -> String {
27
11
let mut ls = sandbox ! {
28
12
files {
29
13
"cairo_project.toml" => CAIRO_PROJECT_TOML_2023_11 ,
30
- "src/lib.cairo" => r#"
31
- fn main() {
32
- let _ = "
33
- ";
34
- }
35
- "# ,
14
+ "src/lib.cairo" => code,
36
15
}
37
16
client_capabilities = caps;
38
17
} ;
@@ -52,16 +31,50 @@ fn main() {
52
31
panic ! ( "expected full tokens" )
53
32
} ;
54
33
55
- // There is a multiline (2) string, check if 2 consecutive tokens are of type string.
56
- assert ! ( tokens. data. windows( 2 ) . any( |tokens| {
57
- let string_type = 16 ; // SemanticTokenKind::String.as_u32()
58
- let first = tokens[ 0 ] ;
59
- let second = tokens[ 1 ] ;
34
+ let mut line = 0 ;
35
+ let mut character = 0 ;
36
+
37
+ let legend = SemanticTokenKind :: legend ( ) ;
38
+
39
+ let tokens: Vec < _ > = tokens
40
+ . data
41
+ . into_iter ( )
42
+ . map ( |token| {
43
+ // Reset on new line.
44
+ if token. delta_line != 0 {
45
+ character = 0 ;
46
+ }
47
+
48
+ line += token. delta_line ;
49
+ character += token. delta_start ;
50
+
51
+ let start = Position { character, line } ;
52
+ let end = Position { character : start. character + token. length , ..start } ;
53
+
54
+ let token_type = legend[ token. token_type as usize ] . as_str ( ) . to_string ( ) ;
60
55
61
- let are_on_consecutive_lines = first . delta_line + 1 == second . delta_line ;
62
- let are_both_string =
63
- first . token_type == second . token_type && first . token_type == string_type ;
56
+ ( Range { start , end } , Some ( token_type ) )
57
+ } )
58
+ . collect ( ) ;
64
59
65
- are_both_string && are_on_consecutive_lines
66
- } ) ) ;
60
+ render_text_with_annotations ( code, "token" , & tokens)
61
+ }
62
+
63
+ fn caps ( base : lsp_types:: ClientCapabilities ) -> lsp_types:: ClientCapabilities {
64
+ lsp_types:: ClientCapabilities {
65
+ text_document : base. text_document . or_else ( Default :: default) . map ( |it| {
66
+ lsp_types:: TextDocumentClientCapabilities {
67
+ semantic_tokens : Some ( lsp_types:: SemanticTokensClientCapabilities {
68
+ dynamic_registration : Some ( false ) ,
69
+ requests : lsp_types:: SemanticTokensClientCapabilitiesRequests {
70
+ full : Some ( lsp_types:: SemanticTokensFullOptions :: Bool ( true ) ) ,
71
+ ..Default :: default ( )
72
+ } ,
73
+ ..Default :: default ( )
74
+ } ) ,
75
+ ..it
76
+ }
77
+ } ) ,
78
+ ..base
79
+ }
67
80
}
0 commit comments