Repository navigation
Expand file tree
/
Copy pathposition.rs
More file actions
209 lines (195 loc) · 6.66 KB
/
Copy pathposition.rs
File metadata and controls
209 lines (195 loc) · 6.66 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
//! Cursor-position analysis over strict or recovered semantic products.
use std::cmp::Reverse;
use crate::{
CheckedProgram,
ast::{ExprId, ExprKind, Span},
hir::{ExpressionResolution, TypedExpression, TypedExpressionKind},
lexer::TokenKind,
semantic::SemanticModel,
stdlib::StandardLibrary,
syntax::SourceDocument,
types::{EnumTypeId, TypeId, TypeKind},
visit::{self, Visitor},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PositionAnalysis {
pub expression: ExprId,
pub span: Span,
/// Exact identifier tokens forming a path or call target. This excludes
/// call arguments and other identifiers in child expressions.
pub segments: Vec<IdentifierSegment>,
pub ty: TypeId,
pub type_kind: TypeKind,
pub resolution: Option<ExpressionResolution>,
}
impl PositionAnalysis {
pub fn segment_at(&self, offset: usize) -> Option<&IdentifierSegment> {
self.segments
.iter()
.find(|segment| segment.span.start <= offset && offset < segment.span.end)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IdentifierSegment {
pub name: String,
pub span: Span,
}
pub(super) fn expression_segments(
checked: &CheckedProgram,
expression: &TypedExpression,
) -> Vec<IdentifierSegment> {
let names = match &expression.kind {
TypedExpressionKind::Path(names) => names.clone(),
TypedExpressionKind::Member { name, .. } => vec![name.clone()],
TypedExpressionKind::Call { source_path, .. } => source_path.clone(),
TypedExpressionKind::Enum {
enumeration,
variant,
..
} => enum_type_name(
*enumeration,
checked.enum_types(),
&checked.context().standard_library(),
)
.map(|enumeration| vec![enumeration, variant.clone()])
.unwrap_or_default(),
_ => return Vec::new(),
};
let mut tokens = checked.source_document().tokens().filter(|token| {
expression.span.start <= token.span.start && token.span.end <= expression.span.end
});
names
.iter()
.filter_map(|name| {
tokens.find_map(|token| match &token.kind {
TokenKind::Ident(spelling) if spelling == name => Some(IdentifierSegment {
name: name.clone(),
span: token.span,
}),
_ => None,
})
})
.collect()
}
pub(super) fn syntax_expression_segments(
document: &SourceDocument,
expression: &crate::ast::Expr,
) -> Vec<IdentifierSegment> {
let names = match &expression.kind {
ExprKind::Path(names) => names.clone(),
ExprKind::Member { name, .. } => vec![name.clone()],
ExprKind::Call { callee, .. } => callee.clone(),
_ => return Vec::new(),
};
let mut tokens = document.tokens().filter(|token| {
expression.span.start <= token.span.start && token.span.end <= expression.span.end
});
names
.iter()
.filter_map(|name| {
tokens.find_map(|token| match &token.kind {
TokenKind::Ident(spelling) if spelling == name => Some(IdentifierSegment {
name: name.clone(),
span: token.span,
}),
_ => None,
})
})
.collect()
}
fn enum_type_name(
enumeration: EnumTypeId,
enum_types: &[crate::ast::EnumDecl],
standard_library: &StandardLibrary,
) -> Option<String> {
match enumeration {
EnumTypeId::Source(id) => enum_types
.iter()
.find(|candidate| candidate.id == id)
.map(|declaration| declaration.name.clone()),
EnumTypeId::Standard(id) => Some(standard_library.type_decl(id).name.to_owned()),
}
}
pub(super) fn syntax_expression_resolution(
semantics: &SemanticModel,
expression: &crate::ast::Expr,
) -> Option<ExpressionResolution> {
if let Some(variant) = semantics.enum_variant(expression.id) {
return Some(ExpressionResolution::EnumConstructor { variant });
}
if let Some(call) = semantics.call(expression.id) {
return Some(ExpressionResolution::Call(call.clone()));
}
if let Some(function) = semantics.function_value(expression.id) {
return Some(ExpressionResolution::FunctionValue(function.clone()));
}
match &expression.kind {
ExprKind::Path(_) => Some(ExpressionResolution::ValuePath {
root: semantics.value(expression.id),
members: semantics
.path_members(expression.id)
.unwrap_or_default()
.to_vec(),
}),
ExprKind::Member { .. } => Some(ExpressionResolution::Member {
members: semantics
.path_members(expression.id)
.unwrap_or_default()
.to_vec(),
}),
ExprKind::Call { .. } => None,
ExprKind::Struct { .. } => semantics
.struct_literal_fields(expression.id)
.map(|fields| ExpressionResolution::StructLiteral {
structure: semantics
.struct_literal(expression.id)
.expect("resolved struct fields have a nominal struct"),
fields: fields.to_vec(),
}),
_ => None,
}
}
struct ExpressionCollector<'ast> {
expressions: Vec<&'ast crate::ast::Expr>,
}
impl<'ast> Visitor<'ast> for ExpressionCollector<'ast> {
fn visit_expr(&mut self, expression: &'ast crate::ast::Expr) {
self.expressions.push(expression);
visit::walk_expr(self, expression);
}
}
fn syntax_expressions(program: &crate::ast::Program) -> Vec<&crate::ast::Expr> {
let mut collector = ExpressionCollector {
expressions: Vec::new(),
};
collector.visit_program(program);
collector.expressions
}
pub(super) fn syntax_expression_at<'a>(
program: &'a crate::ast::Program,
semantics: &SemanticModel,
offset: usize,
) -> Option<&'a crate::ast::Expr> {
syntax_expressions(program)
.into_iter()
.filter(|expression| {
expression.span.start <= offset
&& offset < expression.span.end
&& expression.span.start != expression.span.end
&& semantics.expression_type(expression.id).is_some()
})
.min_by_key(|expression| {
(
expression.span.end - expression.span.start,
Reverse(expression.id.index()),
)
})
}
pub(super) fn syntax_expression_by_id(
program: &crate::ast::Program,
id: ExprId,
) -> Option<&crate::ast::Expr> {
syntax_expressions(program)
.into_iter()
.find(|expression| expression.id == id)
}