Skip to content

Commit

Permalink
lsp rename/find-all-references for local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 8, 2024
1 parent 4968178 commit 1a896b3
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 1 deletion.
6 changes: 6 additions & 0 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ impl<'context> Elaborator<'context> {
// Otherwise, then it is referring to an Identifier
// 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);

Expand Down Expand Up @@ -479,6 +480,11 @@ impl<'context> Elaborator<'context> {
DefinitionKind::Local(_) => {
// 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);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
macros_api::{
ForLoopStatement, ForRange, HirStatement, LetStatement, Path, Statement, StatementKind,
},
node_interner::{DefinitionId, DefinitionKind, GlobalId, StmtId},
node_interner::{DefinitionId, DefinitionKind, GlobalId, ReferenceId, StmtId},
Type,
};

Expand Down Expand Up @@ -256,6 +256,10 @@ 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);

(HirLValue::Ident(ident.clone(), typ.clone()), typ, mutable)
}
LValue::MemberAccess { object, field_name, span } => {
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl NodeInterner {
let alias_type = alias_type.borrow();
Location::new(alias_type.name.span(), alias_type.location.file)
}
ReferenceId::Local(id) => self.definition(id).location,
ReferenceId::Reference(location, _) => location,
}
}
Expand Down
8 changes: 8 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub enum ReferenceId {
Global(GlobalId),
Function(FuncId),
Alias(TypeAliasId),
Local(DefinitionId),
Reference(Location, bool /* is Self */),
}

Expand Down Expand Up @@ -836,12 +837,19 @@ impl NodeInterner {
location: Location,
) -> DefinitionId {
let id = DefinitionId(self.definitions.len());
let is_local = matches!(definition, DefinitionKind::Local(_));

if let DefinitionKind::Function(func_id) = definition {
self.function_definition_ids.insert(func_id, id);
}

let kind = definition;
self.definitions.push(DefinitionInfo { name, mutable, comptime, kind, location });

if is_local {
self.add_definition_location(ReferenceId::Local(id));
}

id
}

Expand Down
5 changes: 5 additions & 0 deletions tooling/lsp/src/requests/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,9 @@ mod goto_definition_tests {
)
.await;
}

#[test]
async fn goto_for_local_variable() {
expect_goto_for_all_references("local_variable", "some_var", 0).await;
}
}
5 changes: 5 additions & 0 deletions tooling/lsp/src/requests/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,9 @@ mod rename_tests {
async fn test_rename_global() {
check_rename_succeeds("rename_global", "FOO").await;
}

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

[dependencies]
5 changes: 5 additions & 0 deletions tooling/lsp/test_programs/local_variable/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let mut some_var = 1;
some_var = 2;
let _ = some_var;
}

0 comments on commit 1a896b3

Please sign in to comment.