Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let LSP always work in a Noir workspace if there's any #5461

Merged
merged 39 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5617aa0
fix: lsp struct reference in return position
asterite Jul 3, 2024
e1ebd06
fix: lsp struct reference in Path
asterite Jul 3, 2024
c3e9c30
Introduce `ReferenceId` as an enum exclusive to the reference graph
asterite Jul 3, 2024
668bc86
feat: lsp "go to definition" for module in call path
asterite Jul 3, 2024
6923fec
feat: lsp "go to" inline module from call path
asterite Jul 3, 2024
c6af4c6
feat: lsp "go to" on `use` module path
asterite Jul 3, 2024
5f8f9b4
feat: lsp "go to" by clicking on module definition ("mod foo")
asterite Jul 3, 2024
9930c11
clippy
asterite Jul 3, 2024
76995b1
feat: lsp rename/find-all-references for traits
asterite Jul 4, 2024
30d0ee4
fix: (lsp) don't rename `self` pointing to struct
asterite Jul 4, 2024
3530c4e
Make sure `Self` isn't renamed but found as a reference
asterite Jul 4, 2024
65a5ad0
Disallow renaming "Self"
asterite Jul 4, 2024
c2d2183
Merge branch 'master' into ab/lsp-struct-more-fixes
asterite Jul 4, 2024
afd6626
clippy
asterite Jul 4, 2024
e68d25a
WIP
asterite Jul 4, 2024
3ff7b2c
feat: lsp rename/find-all-references for type aliases
asterite Jul 4, 2024
c97464a
No need to separately keep track of struct name locations
asterite Jul 4, 2024
5465f6a
No need to separately keep track of alias name locations
asterite Jul 4, 2024
d164f52
No need to separately keep track of trait locations
asterite Jul 4, 2024
b914df0
Merge branch 'master' into ab/lsp-struct-more-fixes
asterite Jul 4, 2024
ab97382
Merge branch 'master' into ab/lsp-struct-more-fixes
asterite Jul 4, 2024
ab3a9a9
Merge branch 'ab/lsp-struct-more-fixes' into ab/lsp-alias-references
asterite Jul 4, 2024
9e097aa
feat: lsp rename/find-all-references for globals
asterite Jul 4, 2024
4968178
Rename ReferenceId::Variable to ReferenceId::Reference
asterite Jul 8, 2024
56b7ea6
lsp rename/find-all-references for local variables
asterite Jul 8, 2024
37b763e
feat: lsp rename/find-all-references for struct members
asterite Jul 8, 2024
1abfd18
Merge branch 'master' into ab/lsp-local-variables
asterite Jul 9, 2024
0a8890b
Merge branch 'ab/lsp-local-variables' into ab/lsp-struct-member
asterite Jul 9, 2024
7a6f7a3
Merge branch 'master' into ab/lsp-struct-member
asterite Jul 9, 2024
5e54385
Move struct_id definition closer to its usage
asterite Jul 10, 2024
2d4d013
Easier way to track references
asterite Jul 9, 2024
e348d4e
fix: let LSP work well in Noir workspaces
asterite Jul 9, 2024
5ded06e
clippy
asterite Jul 9, 2024
a6460d1
more clippy
asterite Jul 9, 2024
024a322
Add a test for finding references across a workspace
asterite Jul 10, 2024
a38408b
Merge branch 'master' into ab/lsp-fixes
asterite Jul 10, 2024
d589faa
Merge branch 'master' into ab/lsp-fixes
asterite Jul 10, 2024
518570d
Fix: add_module_reference, not add_module_location
asterite Jul 10, 2024
e3648b7
Merge branch 'master' into ab/lsp-fixes
TomAFrench Jul 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
HirStatement, Ident, IndexExpression, Literal, MemberAccessExpression,
MethodCallExpression, PrefixExpression,
},
node_interner::{DefinitionKind, ExprId, FuncId, ReferenceId, TraitMethodId},
node_interner::{DefinitionKind, ExprId, FuncId, TraitMethodId},
token::Tokens,
QuotedType, Shared, StructType, Type,
};
Expand Down Expand Up @@ -429,9 +429,9 @@ impl<'context> Elaborator<'context> {
struct_generics,
});

let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Reference(Location::new(span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);
let struct_id = struct_type.borrow().id;
let reference_location = Location::new(span, self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

(expr, Type::Struct(struct_type, generics))
}
Expand Down Expand Up @@ -485,11 +485,11 @@ impl<'context> Elaborator<'context> {
}

if let Some(expected_index) = expected_index {
let struct_id = struct_type.borrow().id;
let referenced = ReferenceId::StructMember(struct_id, expected_index);
let reference =
ReferenceId::Reference(Location::new(field_name.span(), self.file), false);
self.interner.add_reference(referenced, reference);
self.interner.add_struct_member_reference(
struct_type.borrow().id,
expected_index,
Location::new(field_name.span(), self.file),
);
}

ret.push((field_name, resolved));
Expand Down
6 changes: 2 additions & 4 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,13 +1471,11 @@ impl<'context> Elaborator<'context> {

if let Some(trait_id) = trait_id {
let trait_name = trait_impl.trait_path.last_segment();

let referenced = ReferenceId::Trait(trait_id);
let reference = ReferenceId::Reference(
self.interner.add_trait_reference(
trait_id,
Location::new(trait_name.span(), trait_impl.file_id),
trait_name.is_self_type_name(),
);
self.interner.add_reference(referenced, reference);
}
}
}
Expand Down
31 changes: 9 additions & 22 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use crate::{
stmt::HirPattern,
},
macros_api::{HirExpression, Ident, Path, Pattern},
node_interner::{
DefinitionId, DefinitionKind, ExprId, FuncId, GlobalId, ReferenceId, TraitImplKind,
},
node_interner::{DefinitionId, DefinitionKind, ExprId, FuncId, GlobalId, TraitImplKind},
Shared, StructType, Type, TypeBindings,
};

Expand Down Expand Up @@ -204,14 +202,12 @@ impl<'context> Elaborator<'context> {

let struct_id = struct_type.borrow().id;

let referenced = ReferenceId::Struct(struct_id);
let reference = ReferenceId::Reference(Location::new(name_span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(name_span, self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

for (field_index, field) in fields.iter().enumerate() {
let referenced = ReferenceId::StructMember(struct_id, field_index);
let reference = ReferenceId::Reference(Location::new(field.0.span(), self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(field.0.span(), self.file);
self.interner.add_struct_member_reference(struct_id, field_index, reference_location);
}

HirPattern::Struct(expected_type, fields, location)
Expand Down Expand Up @@ -494,7 +490,6 @@ impl<'context> Elaborator<'context> {
// This lookup allows support of such statements: let x = foo::bar::SOME_GLOBAL + 10;
// If the expression is a singular indent, we search the resolver's current scope as normal.
let span = path.span();
let is_self_type_name = path.last_segment().is_self_type_name();
let (hir_ident, var_scope_index) = self.get_ident_from_path(path);

if hir_ident.id != DefinitionId::dummy_id() {
Expand All @@ -504,10 +499,7 @@ impl<'context> Elaborator<'context> {
self.interner.add_function_dependency(current_item, func_id);
}

let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let function = ReferenceId::Function(func_id);
self.interner.add_reference(function, variable);
self.interner.add_function_reference(func_id, hir_ident.location);
}
DefinitionKind::Global(global_id) => {
if let Some(global) = self.unresolved_globals.remove(&global_id) {
Expand All @@ -517,10 +509,7 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, global_id);
}

let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let global = ReferenceId::Global(global_id);
self.interner.add_reference(global, variable);
self.interner.add_global_reference(global_id, hir_ident.location);
}
DefinitionKind::GenericType(_) => {
// Initialize numeric generics to a polymorphic integer type in case
Expand All @@ -536,10 +525,8 @@ impl<'context> Elaborator<'context> {
// only local variables can be captured by closures.
self.resolve_local_variable(hir_ident.clone(), var_scope_index);

let referenced = ReferenceId::Local(hir_ident.id);
let reference =
ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_local_reference(hir_ident.id, reference_location);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl<'context> Elaborator<'context> {
resolver.resolve(self.def_maps, path.clone(), &mut Some(&mut references))?;

for (referenced, ident) in references.iter().zip(path.segments) {
let reference = ReferenceId::Reference(
self.interner.add_reference(
*referenced,
Location::new(ident.span(), self.file),
ident.is_self_type_name(),
);
self.interner.add_reference(*referenced, reference);
}
} else {
path_resolution = resolver.resolve(self.def_maps, path, &mut None)?;
Expand Down
12 changes: 5 additions & 7 deletions compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
macros_api::{
ForLoopStatement, ForRange, HirStatement, LetStatement, Path, Statement, StatementKind,
},
node_interner::{DefinitionId, DefinitionKind, GlobalId, ReferenceId, StmtId},
node_interner::{DefinitionId, DefinitionKind, GlobalId, StmtId},
Type,
};

Expand Down Expand Up @@ -255,9 +255,8 @@ impl<'context> Elaborator<'context> {
typ.follow_bindings()
};

let referenced = ReferenceId::Local(ident.id);
let reference = ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_local_reference(ident.id, reference_location);

(HirLValue::Ident(ident.clone(), typ.clone()), typ, mutable)
}
Expand Down Expand Up @@ -380,9 +379,8 @@ impl<'context> Elaborator<'context> {
Type::Struct(s, args) => {
let s = s.borrow();
if let Some((field, index)) = s.get_field(field_name, args) {
let referenced = ReferenceId::StructMember(s.id, index);
let reference = ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_struct_member_reference(s.id, index, reference_location);

return Some((field, index));
}
Expand Down
30 changes: 10 additions & 20 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ use crate::{
UnaryOp, UnresolvedType, UnresolvedTypeData,
},
node_interner::{
DefinitionKind, DependencyId, ExprId, GlobalId, ReferenceId, TraitId, TraitImplKind,
TraitMethodId,
DefinitionKind, DependencyId, ExprId, GlobalId, TraitId, TraitImplKind, TraitMethodId,
},
Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeVariable, TypeVariableKind,
};
Expand Down Expand Up @@ -154,30 +153,23 @@ impl<'context> Elaborator<'context> {
};

if let Some(unresolved_span) = typ.span {
let location = Location::new(unresolved_span, self.file);

match resolved_type {
Type::Struct(ref struct_type, _) => {
// Record the location of the type reference
self.interner.push_type_ref_location(
resolved_type.clone(),
Location::new(unresolved_span, self.file),
);
self.interner.push_type_ref_location(resolved_type.clone(), location);

if !is_synthetic {
let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
self.interner.add_struct_reference(
struct_type.borrow().id,
location,
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
}
}
Type::Alias(ref alias_type, _) => {
let referenced = ReferenceId::Alias(alias_type.borrow().id);
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
self.interner.add_alias_reference(alias_type.borrow().id, location);
}
_ => (),
}
Expand Down Expand Up @@ -369,10 +361,8 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, id);
}

let referenced = ReferenceId::Global(id);
let reference =
ReferenceId::Reference(Location::new(path.span(), self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(path.span(), self.file);
self.interner.add_global_reference(id, reference_location);

Some(Type::Constant(self.eval_global_as_array_length(id, path)))
}
Expand Down
36 changes: 24 additions & 12 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,11 @@ impl DefCollector {
let file_id = current_def_map.file_id(module_id);

for (referenced, ident) in references.iter().zip(&collected_import.path.segments) {
let reference =
ReferenceId::Reference(Location::new(ident.span(), file_id), false);
context.def_interner.add_reference(*referenced, reference);
context.def_interner.add_reference(
*referenced,
Location::new(ident.span(), file_id),
false,
);
}

resolved_import
Expand Down Expand Up @@ -545,18 +547,28 @@ fn add_import_reference(
return;
}

let referenced = match def_id {
crate::macros_api::ModuleDefId::ModuleId(module_id) => ReferenceId::Module(module_id),
crate::macros_api::ModuleDefId::FunctionId(func_id) => ReferenceId::Function(func_id),
crate::macros_api::ModuleDefId::TypeId(struct_id) => ReferenceId::Struct(struct_id),
crate::macros_api::ModuleDefId::TraitId(trait_id) => ReferenceId::Trait(trait_id),
let location = Location::new(name.span(), file_id);

match def_id {
crate::macros_api::ModuleDefId::ModuleId(module_id) => {
interner.add_module_reference(module_id, location);
}
crate::macros_api::ModuleDefId::FunctionId(func_id) => {
interner.add_function_reference(func_id, location);
}
crate::macros_api::ModuleDefId::TypeId(struct_id) => {
interner.add_struct_reference(struct_id, location, false);
}
crate::macros_api::ModuleDefId::TraitId(trait_id) => {
interner.add_trait_reference(trait_id, location, false);
}
crate::macros_api::ModuleDefId::TypeAliasId(type_alias_id) => {
ReferenceId::Alias(type_alias_id)
interner.add_alias_reference(type_alias_id, location);
}
crate::macros_api::ModuleDefId::GlobalId(global_id) => {
interner.add_global_reference(global_id, location);
}
crate::macros_api::ModuleDefId::GlobalId(global_id) => ReferenceId::Global(global_id),
};
let reference = ReferenceId::Reference(Location::new(name.span(), file_id), false);
interner.add_reference(referenced, reference);
}

fn inject_prelude(
Expand Down
4 changes: 1 addition & 3 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,7 @@ impl<'a> ModCollector<'a> {
) {
Ok(child_mod_id) => {
// Track that the "foo" in `mod foo;` points to the module "foo"
let referenced = ReferenceId::Module(child_mod_id);
let reference = ReferenceId::Reference(location, false);
context.def_interner.add_reference(referenced, reference);
context.def_interner.add_module_reference(child_mod_id, location);

errors.extend(collect_defs(
self.def_collector,
Expand Down
Loading
Loading