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: lsp rename/find-all-references for globals #5415

Merged
merged 25 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 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
292caa6
Merge branch 'master' into ab/lsp-global-references
asterite Jul 8, 2024
701606f
More places where globals can show up
asterite Jul 8, 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
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,8 @@ impl<'context> Elaborator<'context> {
self.elaborate_comptime_global(global_id);
}

self.interner.add_definition_location(ReferenceId::Global(global_id));

self.local_module = old_module;
self.file = old_file;
self.current_item = old_item;
Expand Down
15 changes: 12 additions & 3 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ impl<'context> Elaborator<'context> {
};

if let Some(unresolved_span) = typ.span {
let reference =
ReferenceId::Variable(Location::new(unresolved_span, self.file), is_self_type_name);

match resolved_type {
Type::Struct(ref struct_type, _) => {
// Record the location of the type reference
Expand All @@ -167,11 +164,19 @@ impl<'context> Elaborator<'context> {

if !is_synthetic {
let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Variable(
Location::new(unresolved_span, self.file),
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::Variable(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
}
_ => (),
Expand Down Expand Up @@ -364,6 +369,10 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, id);
}

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

Some(Type::Constant(self.eval_global_as_array_length(id, path)))
}
_ => None,
Expand Down
27 changes: 10 additions & 17 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,25 +511,18 @@ fn add_import_reference(
return;
}

match def_id {
crate::macros_api::ModuleDefId::FunctionId(func_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Function(func_id), variable);
}
crate::macros_api::ModuleDefId::TypeId(struct_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Struct(struct_id), variable);
}
crate::macros_api::ModuleDefId::TraitId(trait_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Trait(trait_id), variable);
}
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),
crate::macros_api::ModuleDefId::TypeAliasId(type_alias_id) => {
let variable = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(ReferenceId::Alias(type_alias_id), variable);
ReferenceId::Alias(type_alias_id)
}
_ => (),
}
crate::macros_api::ModuleDefId::GlobalId(global_id) => ReferenceId::Global(global_id),
};
let reference = ReferenceId::Variable(Location::new(name.span(), file_id), false);
interner.add_reference(referenced, reference);
}

fn inject_prelude(
Expand Down
30 changes: 11 additions & 19 deletions compiler/noirc_frontend/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,18 @@ impl NodeInterner {
let node_index = self.location_indices.get_node_from_location(location)?;

let reference_node = self.reference_graph[node_index];
let found_locations: Vec<Location> = match reference_node {
ReferenceId::Global(_) | ReferenceId::Module(_) => todo!(),
ReferenceId::Function(_)
| ReferenceId::Struct(_)
| ReferenceId::Trait(_)
| ReferenceId::Alias(_) => self.find_all_references_for_index(
node_index,
include_referenced,
include_self_type_name,
),

ReferenceId::Variable(_, _) => {
let referenced_node_index = self.referenced_index(node_index)?;
self.find_all_references_for_index(
referenced_node_index,
include_referenced,
include_self_type_name,
)
}
let referenced_node_index = if let ReferenceId::Variable(_, _) = reference_node {
self.referenced_index(node_index)?
} else {
node_index
};

let found_locations = self.find_all_references_for_index(
referenced_node_index,
include_referenced,
include_self_type_name,
);

Some(found_locations)
}

Expand Down
5 changes: 5 additions & 0 deletions tooling/lsp/src/requests/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,9 @@ mod rename_tests {
async fn test_rename_type_alias() {
check_rename_succeeds("rename_type_alias", "Bar").await;
}

#[test]
async fn test_rename_global() {
check_rename_succeeds("rename_global", "FOO").await;
}
}
6 changes: 6 additions & 0 deletions tooling/lsp/test_programs/rename_global/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "rename_global"
type = "bin"
authors = [""]

[dependencies]
22 changes: 22 additions & 0 deletions tooling/lsp/test_programs/rename_global/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
mod foo {
global FOO = 1;
}

use foo::FOO;

fn main() {
let _ = foo::FOO;
let _ = FOO;
let _: [Field; FOO] = [1];
}

trait WithNumber<N> {

}

struct Some {
}

impl WithNumber<FOO> for Some {

}
7 changes: 7 additions & 0 deletions tooling/lsp/test_programs/rename_type_alias/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ mod foo {
}

type Bar = Foo;

mod bar {
struct Baz {

}
}
}

use foo::Foo;
use foo::Bar;
use foo::bar;

fn main() {
let x: Bar = Foo {};
Expand Down
Loading