-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathutils.rs
More file actions
381 lines (361 loc) · 12.4 KB
/
utils.rs
File metadata and controls
381 lines (361 loc) · 12.4 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
use super::CompletionMaps;
use clarity_repl::clarity::analysis::ContractAnalysis;
use clarity_repl::clarity::diagnostic::{Diagnostic as ClarityDiagnostic, Level as ClarityLevel};
use clarity_repl::clarity::docs::{
make_api_reference, make_define_reference, make_keyword_reference,
};
use clarity_repl::clarity::functions::define::DefineFunctions;
use clarity_repl::clarity::functions::NativeFunctions;
use clarity_repl::clarity::types::{BlockInfoProperty, FunctionType};
use clarity_repl::clarity::variables::NativeVariables;
use std::path::PathBuf;
use tower_lsp::lsp_types::Diagnostic as LspDiagnostic;
use tower_lsp::lsp_types::*;
pub fn convert_clarity_diagnotic_to_lsp_diagnostic(
diagnostic: &ClarityDiagnostic,
) -> LspDiagnostic {
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
LspDiagnostic {
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,
}
}
fn build_intellisense_args(signature: &FunctionType) -> Vec<String> {
let mut args = vec![];
match signature {
FunctionType::Fixed(function) => {
for (i, arg) in function.args.iter().enumerate() {
args.push(format!("${{{}:{}:{}}}", i + 1, arg.name, arg.signature));
}
}
_ => {}
}
args
}
pub fn build_intellisense(analysis: &ContractAnalysis) -> CompletionMaps {
let mut intra_contract = vec![];
let mut inter_contract = vec![];
for (name, signature) in analysis.public_function_types.iter() {
let insert_text = format!("{} {}", name, build_intellisense_args(signature).join(" "));
intra_contract.push(CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::Module),
detail: None,
documentation: None,
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(insert_text),
insert_text_format: Some(InsertTextFormat::Snippet),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
});
let label = format!(
"contract-call::{}::{}",
analysis.contract_identifier.name.to_string(),
name.to_string()
);
let _insert = format!("{} {}", name, build_intellisense_args(signature).join(" "));
let insert_text = format!(
"contract-call? .{} {} {}",
analysis.contract_identifier.name.to_string(),
name.to_string(),
build_intellisense_args(signature).join(" ")
);
inter_contract.push(CompletionItem {
label,
kind: Some(CompletionItemKind::Event),
detail: None,
documentation: None,
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(insert_text),
insert_text_format: Some(InsertTextFormat::Snippet),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
});
}
for (name, signature) in analysis.read_only_function_types.iter() {
let insert_text = format!("{} {}", name, build_intellisense_args(signature).join(" "));
intra_contract.push(CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::Module),
detail: None,
documentation: None,
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(insert_text),
insert_text_format: Some(InsertTextFormat::Snippet),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
});
let label = format!(
"contract-call::{}::{}",
analysis.contract_identifier.name.to_string(),
name.to_string()
);
let insert_text = format!(
"contract-call? .{} {} {}",
analysis.contract_identifier.name.to_string(),
name.to_string(),
build_intellisense_args(signature).join(" ")
);
inter_contract.push(CompletionItem {
label,
kind: Some(CompletionItemKind::Event),
detail: None,
documentation: None,
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(insert_text),
insert_text_format: Some(InsertTextFormat::Snippet),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
});
}
for (name, signature) in analysis.private_function_types.iter() {
let insert_text = format!("{} {}", name, build_intellisense_args(signature).join(" "));
intra_contract.push(CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::Module),
detail: None,
documentation: None,
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(insert_text),
insert_text_format: Some(InsertTextFormat::Snippet),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
});
}
CompletionMaps {
inter_contract,
intra_contract,
data_fields: vec![],
}
}
pub fn build_default_native_keywords_list() -> Vec<CompletionItem> {
let native_functions: Vec<CompletionItem> = NativeFunctions::ALL
.iter()
.map(|func| {
let api = make_api_reference(&func);
CompletionItem {
label: api.name.to_string(),
kind: Some(CompletionItemKind::Function),
detail: Some(api.name.to_string()),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: api.description.to_string(),
})),
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(api.snippet.clone()),
insert_text_format: Some(InsertTextFormat::Snippet),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
}
})
.collect();
let define_functions: Vec<CompletionItem> = DefineFunctions::ALL
.iter()
.map(|func| {
let api = make_define_reference(&func);
CompletionItem {
label: api.name.to_string(),
kind: Some(CompletionItemKind::Class),
detail: Some(api.name.to_string()),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: api.description.to_string(),
})),
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(api.snippet.clone()),
insert_text_format: Some(InsertTextFormat::Snippet),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
}
})
.collect();
let native_variables: Vec<CompletionItem> = NativeVariables::ALL
.iter()
.map(|var| {
let api = make_keyword_reference(&var);
CompletionItem {
label: api.name.to_string(),
kind: Some(CompletionItemKind::Field),
detail: Some(api.name.to_string()),
documentation: Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: api.description.to_string(),
})),
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(api.snippet.to_string()),
insert_text_format: Some(InsertTextFormat::PlainText),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
}
})
.collect();
let block_properties: Vec<CompletionItem> = BlockInfoProperty::ALL_NAMES
.to_vec()
.iter()
.map(|func| CompletionItem::new_simple(func.to_string(), "".to_string()))
.collect();
let types = vec![
"uint",
"int",
"bool",
"list",
"tuple",
"buff",
"string-ascii",
"string-utf8",
"option",
"response",
"principal",
]
.iter()
.map(|var| CompletionItem {
label: var.to_string(),
kind: Some(CompletionItemKind::TypeParameter),
detail: None,
documentation: None,
deprecated: None,
preselect: None,
sort_text: None,
filter_text: None,
insert_text: Some(var.to_string()),
insert_text_format: Some(InsertTextFormat::PlainText),
insert_text_mode: None,
text_edit: None,
additional_text_edits: None,
command: None,
commit_characters: None,
data: None,
tags: None,
})
.collect();
let items = vec![
native_functions,
define_functions,
native_variables,
block_properties,
types,
]
.into_iter()
.flatten()
.collect::<Vec<CompletionItem>>();
items
}
pub fn get_manifest_path_from_contract_url(contract_url: &Url) -> Option<PathBuf> {
let mut manifest_path = get_contract_file(contract_url)?;
let mut manifest_found = false;
while manifest_path.pop() {
manifest_path.push("Clarinet.toml");
if manifest_path.exists() {
manifest_found = true;
break;
}
manifest_path.pop();
}
match manifest_found {
true => Some(manifest_path),
false => None,
}
}
pub fn get_manifest_file(text_document_uri: &Url) -> Option<PathBuf> {
match text_document_uri.to_file_path() {
Ok(path) if path.ends_with("Clarinet.toml") => Some(path),
_ => None,
}
}
pub fn get_contract_file(text_document_uri: &Url) -> Option<PathBuf> {
match text_document_uri.to_file_path() {
Ok(path) => match path.extension() {
Some(ext) if ext.to_str() == Some("clar") => Some(path),
_ => None,
},
_ => None,
}
}