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): allow function rename #4294

Merged
merged 52 commits into from
Jul 2, 2024
Merged
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
f941c04
feat(lsp): allow function rename
kobyhallx Feb 7, 2024
8456a58
Merge remote-tracking branch 'origin/master' into kh-rename-functions
kobyhallx Feb 7, 2024
daf83ed
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 8, 2024
e685934
chore: move collection point
kobyhallx Feb 8, 2024
c9ba252
feat: add func rename with imports
kobyhallx Feb 11, 2024
df38fb6
chore: cleanup
kobyhallx Feb 12, 2024
d73f177
Merge remote-tracking branch 'origin/master' into kh-rename-functions
kobyhallx Feb 12, 2024
3cef0a4
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 12, 2024
39d7d56
chore: remove globals for now
kobyhallx Feb 12, 2024
d77236c
chore: clippy
kobyhallx Feb 12, 2024
2d124bd
chore: clippy - unnecesary ret
kobyhallx Feb 12, 2024
09e898f
chore: clippy - map instead and_then
kobyhallx Feb 12, 2024
78913ac
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 12, 2024
9718234
Merge branch 'master' into kh-rename-functions
kobyhallx Feb 12, 2024
ab455b1
chore: review nits
kobyhallx Feb 12, 2024
d7cd372
chore: spells
kobyhallx Feb 12, 2024
4421a2d
chore: name change
kobyhallx Feb 13, 2024
d564ac8
chore: name change
kobyhallx Feb 13, 2024
f0b81a2
Fix bug with renaming imported functions from submodules
jfecher Feb 13, 2024
0b0eca7
Add unreachable cases
jfecher Feb 13, 2024
7cb7bc7
Merge branch 'master' into kh-rename-functions
jfecher Feb 13, 2024
33b50dc
Fix merge
jfecher Feb 13, 2024
7280c9d
Merge branch 'master' into kh-rename-functions
jfecher Feb 22, 2024
0802086
clippy
jfecher Feb 22, 2024
8122dc8
Merge branch 'kh-rename-functions' of https://github.com/noir-lang/no…
jfecher Feb 22, 2024
a9b785c
Fmt and clippy
jfecher Feb 22, 2024
62c5cad
Merge branch 'master' into kh-rename-functions
TomAFrench Feb 22, 2024
a34e4db
Merge branch 'master' into kh-rename-functions
TomAFrench Jun 25, 2024
d598adf
chore: fix merge
TomAFrench Jun 25, 2024
9d5577c
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
7724b01
Add tests for lsp rename, and use the legacy resolver to make them work
asterite Jun 27, 2024
d18e0a9
Implement lsp rename using elaborator
asterite Jun 27, 2024
52ce934
Check that matches actually match the name being renamed
asterite Jun 27, 2024
4db9e6c
Use separate programs for lsp tests
asterite Jun 27, 2024
aee04c7
clippy
asterite Jun 27, 2024
58bc455
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
2d89ab4
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
2d92617
Undo changes to resolver
asterite Jun 27, 2024
a2fc465
Don't use legacy resolver in another place either
asterite Jun 27, 2024
8b5aaae
Skip empty location spans (otherwise they produce a panic)
asterite Jun 27, 2024
33a695c
Merge branch 'master' into kh-rename-functions
asterite Jun 27, 2024
4da3335
Remove some code duplication in lsp rename tests
asterite Jun 28, 2024
b9f3d76
Extract test_utils::init_lsp_server
asterite Jun 28, 2024
bf87231
process_rename_request only needs TextDocumentPositionParams
asterite Jun 28, 2024
07b2ccb
chore: refactor tests to make it easier to check all cases
TomAFrench Jun 28, 2024
b8dbd04
chore: fix type
TomAFrench Jun 28, 2024
684389a
chore: fmt
TomAFrench Jun 28, 2024
7a2f0f7
chore: add instance of qualified path to test program
TomAFrench Jun 28, 2024
7532717
Let lsp rename function work well with qualified paths
asterite Jun 28, 2024
303dc9b
Remove extra tests, and move consts inside functions
asterite Jun 28, 2024
641c74f
Merge branch 'master' into kh-rename-functions
asterite Jun 28, 2024
777bbbf
Merge branch 'master' into kh-rename-functions
TomAFrench Jul 2, 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
Prev Previous commit
Next Next commit
feat: add func rename with imports
kobyhallx committed Feb 11, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
nsoranzo Nicola Soranzo
commit c9ba2528a87cb1ca411cad28e9ee3d5a9ae3008f
32 changes: 30 additions & 2 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
@@ -15,17 +15,20 @@ use crate::hir::type_check::{type_check_func, TypeCheckError, TypeChecker};
use crate::hir::Context;

use crate::macros_api::{MacroError, MacroProcessor};
use crate::node_interner::{FuncId, NodeInterner, StmtId, StructId, TraitId, TypeAliasId};
use crate::node_interner::{
DependencyId, FuncId, NodeInterner, StmtId, StructId, TraitId, TypeAliasId,
};

use crate::parser::{ParserError, SortedModule};
use crate::{
ExpressionKind, Ident, LetStatement, Literal, NoirFunction, NoirStruct, NoirTrait,
NoirTypeAlias, Path, PathKind, Type, TypeBindings, UnresolvedGenerics,
UnresolvedTraitConstraint, UnresolvedType,
};

use fm::FileId;
use iter_extended::vecmap;
use noirc_errors::{CustomDiagnostic, Span};
use noirc_errors::{CustomDiagnostic, Location, Span};
use std::collections::{BTreeMap, HashMap};

use std::vec;
@@ -268,6 +271,7 @@ impl DefCollector {

// Resolve unresolved imports collected from the crate, one by one.
for collected_import in def_collector.collected_imports {
let module_id = collected_import.module_id;
match resolve_import(crate_id, collected_import, &context.def_maps) {
Ok(resolved_import) => {
// Populate module namespaces according to the imports used
@@ -278,6 +282,9 @@ impl DefCollector {
let result = current_def_map.modules[resolved_import.module_scope.0]
.import(name.clone(), ns, resolved_import.is_prelude);

let file_id = current_def_map.file_id(module_id);
add_import_refrence(ns, &name, &mut context.def_interner, file_id);

if let Err((first_def, second_def)) = result {
let err = DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Import,
@@ -381,6 +388,27 @@ impl DefCollector {
}
}

fn add_import_refrence(
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
ns: crate::macros_api::ModuleDefId,
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
name: &Ident,
interner: &mut NodeInterner,
file_id: FileId,
) {
if name.span().start() == 0 && name.span().end() == 0 {
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
// We ignore empty spans at 0 lcoation, this must be Stdlib
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
return;
}
match ns {
crate::macros_api::ModuleDefId::FunctionId(func_id) => {
interner.add_reference_for(
DependencyId::Function(func_id),
(DependencyId::FunctionCall, Location::new(name.span(), file_id)),
);
}
_ => (),
};
}

fn inject_prelude(
crate_id: CrateId,
context: &Context,
16 changes: 7 additions & 9 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
@@ -1416,15 +1416,13 @@ impl<'a> Resolver<'a> {
span,
self.interner.function_visibility(id),
);
if let Some(func_meta) = self.interner.func_meta.get(&id) {
self.interner.add_reference(
(DependencyId::Function(id), func_meta.location),
(
DependencyId::FunctionCall(hir_ident.id),
hir_ident.location,
),
);
}
}

if let Some(func_meta) = self.interner.func_meta.get(&id) {
self.interner.add_reference(
(DependencyId::Function(id), func_meta.location),
(DependencyId::FunctionCall, hir_ident.location),
);
}
}
DefinitionKind::Global(_) => {}
39 changes: 28 additions & 11 deletions compiler/noirc_frontend/src/locations.rs
Original file line number Diff line number Diff line change
@@ -30,12 +30,30 @@ impl NodeInterner {
reference: (DependencyId, Location),
) {
let referenced_index = self.get_or_insert_reference(referenced);
let reference_index = self.get_or_insert_reference(reference);
self.graph_references.update_edge(referenced_index, reference_index, ());
let reference_index = self.graph_references.add_node((reference.0, reference.1));
self.graph_references.add_edge(referenced_index, reference_index, ());
self.location_store.add_location(referenced.1, referenced_index);
self.location_store.add_location(reference.1, reference_index);
}

pub(crate) fn add_reference_for(
&mut self,
referenced_id: DependencyId,
reference: (DependencyId, Location),
) {
let Some(referenced_index) = self.graph_references_indices.get(&referenced_id) else { panic!("Compiler Error: Referenced index not found") };

let reference_index = self.graph_references.add_node((reference.0, reference.1));
self.graph_references.add_edge(*referenced_index, reference_index, ());
self.location_store.add_location(reference.1, reference_index);
}

pub(crate) fn add_definiton(&mut self, referenced: (DependencyId, Location)) {
let referenced_index = self.get_or_insert_reference(referenced);
self.location_store.add_location(referenced.1, referenced_index);
}

#[tracing::instrument(skip(self), ret)]
pub(crate) fn get_or_insert_reference(
&mut self,
(id, location): (DependencyId, Location),
@@ -56,18 +74,17 @@ impl NodeInterner {
pub fn find_rename_symbols_at(&self, location: Location) -> Option<Vec<Location>> {
let node_index = self.location_store.get_node_from_location(location)?;

let mut edit_locations: Vec<Location> = Vec::new();
// let mut edit_locations: Vec<Location> = Vec::new();

let reference_node = self.graph_references[node_index];
let found_locations = match reference_node.0 {
DependencyId::Struct(_) => todo!(),
DependencyId::Global(_) => todo!(),
DependencyId::Function(_) => {
edit_locations.extend(self.get_edit_locations(node_index));
edit_locations
let found_locations: Vec<Location> = match reference_node.0 {
DependencyId::Alias(_) | DependencyId::Struct(_) | DependencyId::Global(_) => todo!(),
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
DependencyId::Function(_) | DependencyId::GlobalDefinition(_) => {
self.get_edit_locations(node_index)
}
DependencyId::Alias(_) => todo!(),
DependencyId::CallExpression(_) => {

DependencyId::GlobalReference | DependencyId::FunctionCall => {
let mut edit_locations: Vec<Location> = Vec::new();
if let Some(referenced_node_index) = self
.graph_references
.neighbors_directed(node_index, petgraph::Direction::Incoming)
8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
@@ -188,9 +188,11 @@ pub struct NodeInterner {
pub enum DependencyId {
Struct(StructId),
Global(StmtId),
GlobalDefinition(DefinitionId),
GlobalReference,
Function(FuncId),
Alias(TypeAliasId),
CallExpression(ExprId),
FunctionCall,
jfecher marked this conversation as resolved.
Show resolved Hide resolved
}

/// A trait implementation is either a normal implementation that is present in the source
@@ -767,6 +769,10 @@ impl NodeInterner {
contract_function_type: Some(if function.is_open { Open } else { Secret }),
is_internal: Some(function.is_internal),
};
self.add_definiton((
DependencyId::Function(id),
Location::new(function.name.span(), location.file),
));
self.push_function_definition(id, modifiers, module, location)
}