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: implement LSP code action "Implement missing members" #6020

Merged
merged 20 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
71 changes: 64 additions & 7 deletions tooling/lsp/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;

use noirc_frontend::{
ast::ItemVisibility,
graph::CrateId,
graph::{CrateId, Dependency},
hir::def_map::{CrateDefMap, ModuleId},
macros_api::{ModuleDefId, NodeInterner},
node_interner::ReferenceId,
Expand Down Expand Up @@ -41,7 +41,7 @@ pub(crate) fn module_def_id_to_reference_id(module_def_id: ModuleDefId) -> Refer
/// Returns the fully-qualified path of the given `ModuleDefId` relative to `current_module_id`:
/// - If `ModuleDefId` is a module, that module's path is returned
/// - Otherwise, that item's parent module's path is returned
pub(crate) fn module_full_path(
pub(crate) fn relative_module_full_path(
module_def_id: ModuleDefId,
visibility: ItemVisibility,
current_module_id: ModuleId,
Expand All @@ -55,8 +55,12 @@ pub(crate) fn module_full_path(
return None;
}

full_path =
module_id_path(module_id, &current_module_id, current_module_parent_id, interner);
full_path = relative_module_id_path(
module_id,
&current_module_id,
current_module_parent_id,
interner,
);
} else {
let Some(parent_module) = get_parent_module(interner, module_def_id) else {
return None;
Expand All @@ -66,15 +70,19 @@ pub(crate) fn module_full_path(
return None;
}

full_path =
module_id_path(parent_module, &current_module_id, current_module_parent_id, interner);
full_path = relative_module_id_path(
parent_module,
&current_module_id,
current_module_parent_id,
interner,
);
}
Some(full_path)
}

/// Returns the path to reach an item inside `target_module_id` from inside `current_module_id`.
/// Returns a relative path if possible.
pub(crate) fn module_id_path(
pub(crate) fn relative_module_id_path(
target_module_id: ModuleId,
current_module_id: &ModuleId,
current_module_parent_id: Option<ModuleId>,
Expand Down Expand Up @@ -130,3 +138,52 @@ pub(crate) fn module_id_path(
segments.reverse();
segments.join("::")
}

pub(crate) fn module_full_path(
module: &ModuleId,
interner: &NodeInterner,
crate_id: CrateId,
crate_name: &str,
dependencies: &Vec<Dependency>,
) -> String {
let mut segments: Vec<String> = Vec::new();

if let Some(module_attributes) = interner.try_module_attributes(module) {
segments.push(module_attributes.name.clone());

let mut current_attributes = module_attributes;
loop {
let Some(parent_local_id) = current_attributes.parent else {
break;
};

let Some(parent_attributes) = interner.try_module_attributes(&ModuleId {
krate: module.krate,
local_id: parent_local_id,
}) else {
break;
};

segments.push(parent_attributes.name.clone());
current_attributes = parent_attributes;
}
}

// We don't record module attributes for the root module,
// so we handle that case separately
if module.krate.is_root() {
if module.krate == crate_id {
segments.push(crate_name.to_string());
} else {
for dep in dependencies {
if dep.crate_id == crate_id {
segments.push(dep.name.to_string());
break;
}
}
}
};

segments.reverse();
segments.join("::")
}
9 changes: 8 additions & 1 deletion tooling/lsp/src/requests/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lsp_types::{
};
use noirc_errors::Span;
use noirc_frontend::{
ast::{ConstructorExpression, Path, Visitor},
ast::{ConstructorExpression, NoirTraitImpl, Path, Visitor},
graph::CrateId,
hir::def_map::{CrateDefMap, LocalModuleId, ModuleId},
macros_api::NodeInterner,
Expand All @@ -26,6 +26,7 @@ use crate::{utils, LspState};
use super::{process_request, to_lsp_location};

mod fill_struct_fields;
mod implement_missing_members;
mod import_or_qualify;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -219,4 +220,10 @@ impl<'a> Visitor for CodeActionFinder<'a> {

true
}

fn visit_noir_trait_impl(&mut self, noir_trait_impl: &NoirTraitImpl, span: Span) -> bool {
self.implement_missing_members(noir_trait_impl, span);

true
}
}
Loading
Loading