-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathhelpers.rs
More file actions
106 lines (95 loc) · 3.25 KB
/
helpers.rs
File metadata and controls
106 lines (95 loc) · 3.25 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
use std::cmp::Ordering;
use clarity_repl::clarity::representations::Span;
use clarity_repl::clarity::{ClarityName, SymbolicExpression, SymbolicExpressionType};
use ls_types::{Position, Range};
pub fn span_to_range(span: &Span) -> Range {
if span == &Span::zero() {
return Range::default();
}
{
Range::new(
Position::new(span.start_line - 1, span.start_column - 1),
Position::new(span.end_line - 1, span.end_column),
)
}
}
// end_offset is usded to include the end position of a keyword, for go to definition in particular
pub fn is_position_within_span(position: &Position, span: &Span, end_offset: u32) -> bool {
if position.line < span.start_line || position.line > span.end_line {
return false;
}
if position.line == span.start_line && position.character < span.start_column {
return false;
}
if position.line == span.end_line && position.character > span.end_column + end_offset {
return false;
}
true
}
pub fn get_expression_name_at_position(
position: &Position,
expressions: &[SymbolicExpression],
) -> Option<ClarityName> {
for expr in expressions {
if is_position_within_span(position, &expr.span, 0) {
match &expr.expr {
SymbolicExpressionType::Atom(function_name) => {
return Some(function_name.to_owned())
}
SymbolicExpressionType::List(expressions) => {
return get_expression_name_at_position(position, expressions)
}
_ => {}
}
}
}
None
}
pub fn get_function_at_position(
position: &Position,
expressions: &[SymbolicExpression],
) -> Option<(ClarityName, Option<u32>)> {
for expr in expressions {
if is_position_within_span(position, &expr.span, 0) {
if let Some(expressions) = expr.match_list() {
return get_function_at_position(position, expressions);
}
}
}
let mut position_in_parameters: i32 = -1;
for parameter in expressions {
match position.line.cmp(¶meter.span.end_line) {
Ordering::Equal => {
if position.character > parameter.span.end_column + 1 {
position_in_parameters += 1
}
}
Ordering::Greater => position_in_parameters += 1,
_ => {}
}
}
let (function_name, _) = expressions.split_first()?;
Some((
function_name.match_atom()?.to_owned(),
position_in_parameters.try_into().ok(),
))
}
pub fn get_atom_or_field_start_at_position(
position: &Position,
expressions: &[SymbolicExpression],
) -> Option<(u32, u32)> {
for expr in expressions {
if is_position_within_span(position, &expr.span, 1) {
match &expr.expr {
SymbolicExpressionType::Atom(_) | SymbolicExpressionType::Field(_) => {
return Some((expr.span.start_line, expr.span.start_column))
}
SymbolicExpressionType::List(expressions) => {
return get_atom_or_field_start_at_position(position, expressions)
}
_ => {}
}
}
}
None
}