Skip to content

Commit cf07dee

Browse files
committed
Add constructor and class members to locate
1 parent d7ac540 commit cf07dee

File tree

8 files changed

+136
-22
lines changed

8 files changed

+136
-22
lines changed

compiler-core/indexing/src/algorithm.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
108108
cst::Declaration::ValueSignature(cst) => {
109109
let id = state.source.allocate_value_signature(cst);
110110
let term_id = index_value_signature(state, id, cst);
111-
state.pairs.term_declarations.push((declaration_id, term_id));
111+
state.pairs.declaration_to_term.push((declaration_id, term_id));
112112
}
113113
cst::Declaration::ValueEquation(cst) => {
114114
let id = state.source.allocate_value_equation(cst);
115115
let term_id = index_value_equation(state, id, cst);
116-
state.pairs.term_declarations.push((declaration_id, term_id));
116+
state.pairs.declaration_to_term.push((declaration_id, term_id));
117117
}
118118
cst::Declaration::InfixDeclaration(cst) => {
119119
let id = state.source.allocate_infix(cst);
@@ -129,7 +129,7 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
129129
let instance_id = state.source.allocate_instance(&cst);
130130
let term_id = index_instance(state, instance_id, &cst);
131131
state.pairs.instance_chain.push((chain_id, instance_id));
132-
state.pairs.term_declarations.push((declaration_id, term_id));
132+
state.pairs.declaration_to_term.push((declaration_id, term_id));
133133
if let Some(cst) = cst.instance_statements() {
134134
for cst in cst.children() {
135135
let m_id = state.source.allocate_instance_member(&cst);
@@ -159,7 +159,7 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
159159
}
160160
},
161161
);
162-
state.pairs.type_declarations.push((declaration_id, type_id));
162+
state.pairs.declaration_to_type.push((declaration_id, type_id));
163163
}
164164
cst::Declaration::TypeSynonymEquation(cst) => {
165165
let id = state.source.allocate_type_equation(cst);
@@ -182,7 +182,7 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
182182
}
183183
},
184184
);
185-
state.pairs.type_declarations.push((declaration_id, type_id));
185+
state.pairs.declaration_to_type.push((declaration_id, type_id));
186186
}
187187
cst::Declaration::ClassSignature(cst) => {
188188
let id = state.source.allocate_class_signature(cst);
@@ -205,7 +205,7 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
205205
}
206206
},
207207
);
208-
state.pairs.type_declarations.push((declaration_id, type_id));
208+
state.pairs.declaration_to_type.push((declaration_id, type_id));
209209
}
210210
cst::Declaration::ClassDeclaration(cst) => {
211211
let id = state.source.allocate_class_declaration(cst);
@@ -233,19 +233,20 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
233233
let member_id = state.source.allocate_class_member(&cst);
234234
let term_id = index_class_member(state, member_id, &cst);
235235
state.pairs.class_members.push((type_id, term_id));
236+
state.pairs.class_member_to_term.push((member_id, term_id));
236237
}
237238
}
238-
state.pairs.type_declarations.push((declaration_id, type_id));
239+
state.pairs.declaration_to_type.push((declaration_id, type_id));
239240
}
240241
cst::Declaration::ForeignImportDataDeclaration(cst) => {
241242
let id = state.source.allocate_foreign_data(cst);
242243
let type_id = index_foreign_data(state, id, cst);
243-
state.pairs.type_declarations.push((declaration_id, type_id));
244+
state.pairs.declaration_to_type.push((declaration_id, type_id));
244245
}
245246
cst::Declaration::ForeignImportValueDeclaration(cst) => {
246247
let id = state.source.allocate_foreign_value(cst);
247248
let term_id = index_foreign_value(state, id, cst);
248-
state.pairs.term_declarations.push((declaration_id, term_id));
249+
state.pairs.declaration_to_term.push((declaration_id, term_id));
249250
}
250251
cst::Declaration::NewtypeSignature(cst) => {
251252
let id = state.source.allocate_newtype_signature(cst);
@@ -268,7 +269,7 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
268269
}
269270
},
270271
);
271-
state.pairs.type_declarations.push((declaration_id, type_id));
272+
state.pairs.declaration_to_type.push((declaration_id, type_id));
272273
}
273274
cst::Declaration::NewtypeEquation(cst) => {
274275
let id = state.source.allocate_newtype_equation(cst);
@@ -295,8 +296,9 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
295296
let constructor_id = state.source.allocate_data_constructor(&cst);
296297
let term_id = index_data_constructor(state, constructor_id, &cst);
297298
state.pairs.data_constructors.push((type_id, term_id));
299+
state.pairs.constructor_to_term.push((constructor_id, term_id));
298300
}
299-
state.pairs.type_declarations.push((declaration_id, type_id));
301+
state.pairs.declaration_to_type.push((declaration_id, type_id));
300302
}
301303
cst::Declaration::DataSignature(cst) => {
302304
let id = state.source.allocate_data_signature(cst);
@@ -319,7 +321,7 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
319321
}
320322
},
321323
);
322-
state.pairs.type_declarations.push((declaration_id, type_id));
324+
state.pairs.declaration_to_type.push((declaration_id, type_id));
323325
}
324326
cst::Declaration::DataEquation(cst) => {
325327
let id = state.source.allocate_data_equation(cst);
@@ -346,13 +348,14 @@ fn index_declaration(state: &mut State, cst: &cst::Declaration) {
346348
let constructor_id = state.source.allocate_data_constructor(&cst);
347349
let term_id = index_data_constructor(state, constructor_id, &cst);
348350
state.pairs.data_constructors.push((type_id, term_id));
351+
state.pairs.constructor_to_term.push((constructor_id, term_id));
349352
}
350-
state.pairs.type_declarations.push((declaration_id, type_id));
353+
state.pairs.declaration_to_type.push((declaration_id, type_id));
351354
}
352355
cst::Declaration::DeriveDeclaration(cst) => {
353356
let id = state.source.allocate_derive(cst);
354357
let term_id = index_derive(state, id, cst);
355-
state.pairs.term_declarations.push((declaration_id, term_id));
358+
state.pairs.declaration_to_term.push((declaration_id, term_id));
356359
}
357360
}
358361
}

compiler-core/indexing/src/lib.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,14 @@ impl IndexingImport {
109109
pub struct IndexingPairs {
110110
class_members: Vec<(TypeItemId, TermItemId)>,
111111
data_constructors: Vec<(TypeItemId, TermItemId)>,
112+
112113
instance_chain: Vec<(InstanceChainId, InstanceId)>,
113114
instance_members: Vec<(InstanceId, InstanceMemberId)>,
114-
term_declarations: Vec<(DeclarationId, TermItemId)>,
115-
type_declarations: Vec<(DeclarationId, TypeItemId)>,
115+
116+
declaration_to_term: Vec<(DeclarationId, TermItemId)>,
117+
declaration_to_type: Vec<(DeclarationId, TypeItemId)>,
118+
constructor_to_term: Vec<(DataConstructorId, TermItemId)>,
119+
class_member_to_term: Vec<(ClassMemberId, TermItemId)>,
116120
}
117121

118122
impl IndexingPairs {
@@ -134,17 +138,29 @@ impl IndexingPairs {
134138
)
135139
}
136140

137-
pub fn declaration_term(&self, id: DeclarationId) -> Option<TermItemId> {
138-
self.term_declarations.iter().find_map(move |(declaration_id, term_id)| {
141+
pub fn declaration_to_term(&self, id: DeclarationId) -> Option<TermItemId> {
142+
self.declaration_to_term.iter().find_map(move |(declaration_id, term_id)| {
139143
if *declaration_id == id { Some(*term_id) } else { None }
140144
})
141145
}
142146

143-
pub fn declaration_type(&self, id: DeclarationId) -> Option<TypeItemId> {
144-
self.type_declarations.iter().find_map(move |(declaration_id, type_id)| {
147+
pub fn declaration_to_type(&self, id: DeclarationId) -> Option<TypeItemId> {
148+
self.declaration_to_type.iter().find_map(move |(declaration_id, type_id)| {
145149
if *declaration_id == id { Some(*type_id) } else { None }
146150
})
147151
}
152+
153+
pub fn constructor_to_term(&self, id: DataConstructorId) -> Option<TermItemId> {
154+
self.constructor_to_term.iter().find_map(move |(constructor_id, term_id)| {
155+
if *constructor_id == id { Some(*term_id) } else { None }
156+
})
157+
}
158+
159+
pub fn class_member_to_term(&self, id: ClassMemberId) -> Option<TermItemId> {
160+
self.class_member_to_term.iter().find_map(move |(class_member_id, term_id)| {
161+
if *class_member_id == id { Some(*term_id) } else { None }
162+
})
163+
}
148164
}
149165

150166
pub fn index_module(cst: &cst::Module) -> FullIndexedModule {

compiler-lsp/analyzer/src/locate.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,18 @@ fn locate_node(
150150
} else if cst::Declaration::can_cast(kind) {
151151
let ptr = ptr.cast()?;
152152
let id = indexed.source.lookup_declaration(&ptr)?;
153-
None.or_else(|| indexed.pairs.declaration_term(id).map(Located::TermItem))
154-
.or_else(|| indexed.pairs.declaration_type(id).map(Located::TypeItem))
153+
None.or_else(|| indexed.pairs.declaration_to_term(id).map(Located::TermItem))
154+
.or_else(|| indexed.pairs.declaration_to_type(id).map(Located::TypeItem))
155+
} else if cst::DataConstructor::can_cast(kind) {
156+
let ptr = ptr.cast()?;
157+
let id = indexed.source.lookup_data_constructor(&ptr)?;
158+
let id = indexed.pairs.constructor_to_term(id)?;
159+
Some(Located::TermItem(id))
160+
} else if cst::ClassMemberStatement::can_cast(kind) {
161+
let ptr = ptr.cast()?;
162+
let id = indexed.source.lookup_class_member(&ptr)?;
163+
let id = indexed.pairs.class_member_to_term(id)?;
164+
Some(Located::TermItem(id))
155165
} else {
156166
None
157167
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Main where
2+
3+
data Maybe a = Just a | Nothing
4+
-- % %
5+
6+
just = Just
7+
nothing = Nothing
8+
9+
newtype Id a = Id a
10+
-- %
11+
12+
id = Id
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main where
2+
3+
class Eq a where
4+
eq :: a -> a -> Boolean
5+
-- %
6+
7+
test = eq

tests-integration/tests/lsp.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,25 @@ fn test_029_locate_declaration_main() {
318318
let report = tests_integration::lsp::report(&engine, &files, id);
319319
insta::assert_snapshot!(report);
320320
}
321+
322+
#[rustfmt::skip]
323+
#[test]
324+
fn test_030_locate_constructor_main() {
325+
let (engine, files) = tests_integration::load_compiler(std::path::Path::new("fixtures/lsp/030_locate_constructor"));
326+
let Some(id) = engine.module_file("Main") else {
327+
return;
328+
};
329+
let report = tests_integration::lsp::report(&engine, &files, id);
330+
insta::assert_snapshot!(report);
331+
}
332+
333+
#[rustfmt::skip]
334+
#[test]
335+
fn test_031_locate_class_member_main() {
336+
let (engine, files) = tests_integration::load_compiler(std::path::Path::new("fixtures/lsp/031_locate_class_member"));
337+
let Some(id) = engine.module_file("Main") else {
338+
return;
339+
};
340+
let report = tests_integration::lsp::report(&engine, &files, id);
341+
insta::assert_snapshot!(report);
342+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
source: tests-integration/tests/lsp.rs
3+
expression: report
4+
---
5+
References at Position { line: 2, character: 15 }
6+
7+
```
8+
data Maybe a = Just a | Nothing
9+
-- % %
10+
```
11+
12+
file:///tests-integration/fixtures/lsp/030_locate_constructor/Main.purs @ 5:7..5:11
13+
14+
15+
References at Position { line: 2, character: 24 }
16+
17+
```
18+
data Maybe a = Just a | Nothing
19+
-- % %
20+
```
21+
22+
file:///tests-integration/fixtures/lsp/030_locate_constructor/Main.purs @ 6:10..6:17
23+
24+
25+
References at Position { line: 8, character: 15 }
26+
27+
```
28+
newtype Id a = Id a
29+
-- %
30+
```
31+
32+
file:///tests-integration/fixtures/lsp/030_locate_constructor/Main.purs @ 11:5..11:7
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: tests-integration/tests/lsp.rs
3+
expression: report
4+
---
5+
References at Position { line: 3, character: 3 }
6+
7+
```
8+
eq :: a -> a -> Boolean
9+
-- %
10+
```
11+
12+
file:///tests-integration/fixtures/lsp/031_locate_class_member/Main.purs @ 6:7..6:9

0 commit comments

Comments
 (0)