forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic_tokens.rs
184 lines (180 loc) · 8.5 KB
/
semantic_tokens.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
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
#[cfg(target_arch = "wasm32")]
use crate::wasm_prelude::*;
use i_slint_compiler::parser::SyntaxKind;
use lsp_types::{
SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens, SemanticTokensResult,
};
use crate::DocumentCache;
/// Give all the used types/modifier a number in an indexed array
macro_rules! declare_legend {
($what:ident: $ty:ty = [$($tk:ident)*]) => {
pub const $what : &[$ty] = &[
$(<$ty>::$tk),*
];
declare_legend!{@ [ $($tk)* ], 0}
};
(@ [$head:ident $($tail:ident)*], $n:expr) => {
const $head: u32 = $n;
declare_legend!{@ [ $($tail)* ], $n + 1}
};
(@ [], $n:expr) => {};
}
// the id of the element
declare_legend!(LEGEND_TYPES : SemanticTokenType = [TYPE PARAMETER VARIABLE PROPERTY FUNCTION MACRO KEYWORD COMMENT STRING NUMBER OPERATOR]);
declare_legend!(LEGEND_MODS: SemanticTokenModifier = [DEFINITION DECLARATION]);
pub fn get_semantic_tokens(
document_cache: &mut DocumentCache,
text_document: &lsp_types::TextDocumentIdentifier,
) -> Option<SemanticTokensResult> {
let uri = &text_document.uri;
let filepath = uri.to_file_path().ok()?;
let doc = document_cache.documents.get_document(&filepath)?;
let doc_node = doc.node.as_ref()?;
let mut token = doc_node.first_token()?;
let mut data = vec![];
let mut delta_start = 0;
let mut delta_line = 0;
loop {
let t_m = match token.kind() {
SyntaxKind::Comment => Some((self::COMMENT, 0)),
SyntaxKind::StringLiteral => Some((self::STRING, 0)),
SyntaxKind::NumberLiteral => Some((self::NUMBER, 0)),
SyntaxKind::ColorLiteral => Some((self::NUMBER, 0)),
SyntaxKind::Identifier => match token.parent().kind() {
SyntaxKind::Component => Some((self::KEYWORD, 0)),
// the id of the element
SyntaxKind::SubElement => Some((self::VARIABLE, 1 << self::DEFINITION)),
SyntaxKind::RepeatedElement => Some((self::KEYWORD, 0)),
SyntaxKind::RepeatedIndex => Some((self::VARIABLE, 1 << self::DEFINITION)),
SyntaxKind::ConditionalElement => Some((self::KEYWORD, 0)),
SyntaxKind::CallbackDeclaration => Some((self::KEYWORD, 0)),
SyntaxKind::CallbackConnection => Some((self::FUNCTION, 0)),
SyntaxKind::PropertyDeclaration => Some((self::KEYWORD, 0)),
SyntaxKind::Function => Some((self::KEYWORD, 0)),
SyntaxKind::PropertyAnimation => Some((self::KEYWORD, 0)),
SyntaxKind::QualifiedName => match token.parent().parent()?.kind() {
SyntaxKind::Type => Some((self::TYPE, 0)),
// the base type
SyntaxKind::Element => Some((self::TYPE, 0)),
// FIXME: we should do actual lookup
SyntaxKind::Expression => None,
SyntaxKind::StatePropertyChange => Some((self::PROPERTY, 0)),
SyntaxKind::PropertyAnimation => Some((self::PROPERTY, 0)),
_ => None,
},
SyntaxKind::DeclaredIdentifier => {
match token.parent().parent()?.kind() {
SyntaxKind::Component => Some((self::TYPE, 1 << self::DEFINITION)),
SyntaxKind::RepeatedElement => {
Some((self::PROPERTY, 1 << self::DEFINITION))
}
SyntaxKind::CallbackDeclaration => {
Some((self::FUNCTION, 1 << self::DEFINITION))
}
SyntaxKind::CallbackConnection => {
Some((self::PARAMETER, 1 << self::DEFINITION))
}
SyntaxKind::PropertyDeclaration => {
Some((self::PROPERTY, 1 << self::DEFINITION))
}
SyntaxKind::State | SyntaxKind::Transition => {
// This is the state name, but what semantic type is that?
None
}
SyntaxKind::StructDeclaration => Some((self::TYPE, 1 << self::DEFINITION)),
_ => None,
}
}
SyntaxKind::ChildrenPlaceholder => Some((self::MACRO, 0)),
SyntaxKind::Binding | SyntaxKind::TwoWayBinding => Some((self::PROPERTY, 0)),
SyntaxKind::ReturnStatement => Some((self::KEYWORD, 0)),
SyntaxKind::AtImageUrl => Some((self::MACRO, 0)),
SyntaxKind::AtGradient => Some((self::MACRO, 0)),
SyntaxKind::ConditionalExpression => Some((self::KEYWORD, 0)),
SyntaxKind::ObjectMember => Some((self::PROPERTY, 1 << self::DECLARATION)),
SyntaxKind::States => Some((self::KEYWORD, 0)),
SyntaxKind::State => Some((self::KEYWORD, 0)),
SyntaxKind::Transitions => Some((self::KEYWORD, 0)),
SyntaxKind::Transition => Some((self::KEYWORD, 0)),
SyntaxKind::ExportsList => Some((self::KEYWORD, 0)),
SyntaxKind::ExportSpecifier => Some((self::KEYWORD, 0)),
SyntaxKind::ExportIdentifier => {
Some((
self::TYPE,
if token.parent().parent().map_or(false, |p| {
p.children().any(|n| n.kind() == SyntaxKind::ExportName)
}) {
0
} else {
1 << self::DECLARATION
},
))
}
SyntaxKind::ExportName => Some((self::TYPE, 1 << self::DECLARATION)),
SyntaxKind::ImportSpecifier => Some((self::KEYWORD, 0)),
SyntaxKind::ImportIdentifier => Some((self::KEYWORD, 0)),
SyntaxKind::ExternalName => Some((
self::TYPE,
if token.parent().parent().map_or(false, |p| {
p.children().any(|n| n.kind() == SyntaxKind::InternalName)
}) {
0
} else {
1 << self::DECLARATION
},
)),
SyntaxKind::InternalName => Some((self::TYPE, 1 << self::DECLARATION)),
SyntaxKind::ObjectTypeMember => Some((self::PROPERTY, 1 << self::DEFINITION)),
SyntaxKind::StructDeclaration => Some((self::KEYWORD, 0)),
_ => None,
},
SyntaxKind::PlusEqual
| SyntaxKind::MinusEqual
| SyntaxKind::StarEqual
| SyntaxKind::DivEqual
| SyntaxKind::LessEqual
| SyntaxKind::GreaterEqual
| SyntaxKind::EqualEqual
| SyntaxKind::NotEqual
| SyntaxKind::OrOr
| SyntaxKind::AndAnd => Some((self::OPERATOR, 0)),
SyntaxKind::LAngle | SyntaxKind::RAngle => (token.parent().kind()
== SyntaxKind::PropertyDeclaration)
.then(|| (self::OPERATOR, 0)),
SyntaxKind::Plus
| SyntaxKind::Minus
| SyntaxKind::Star
| SyntaxKind::Div
| SyntaxKind::Equal => Some((self::OPERATOR, 0)),
SyntaxKind::Question => Some((self::OPERATOR, 0)),
SyntaxKind::At => Some((self::MACRO, 0)),
_ => None,
};
if let Some((token_type, token_modifiers_bitset)) = t_m {
data.push(SemanticToken {
delta_line,
delta_start,
length: token.text().encode_utf16().count() as u32,
token_type,
token_modifiers_bitset,
});
delta_line = 0;
delta_start = 0;
}
let text = token.text();
let l = text.bytes().filter(|x| *x == b'\n').count();
if l == 0 {
delta_start += text.encode_utf16().count() as u32;
} else {
delta_line += l as u32;
delta_start = text[(text.rfind('\n').unwrap() + 1)..].encode_utf16().count() as u32;
}
token = match token.next_token() {
None => break,
Some(token) => token,
}
}
Some(SemanticTokensResult::Tokens(SemanticTokens { result_id: None, data }))
}