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 will now suggest private items if they are visible #5923

Merged
merged 1 commit into from
Sep 4, 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
5 changes: 1 addition & 4 deletions compiler/noirc_frontend/src/elaborator/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ impl<'context> Elaborator<'context> {
let module = self.module_id();
self.interner.push_function(id, &function.def, module, location);

if self.interner.is_in_lsp_mode()
&& !function.def.is_test()
&& !function.def.is_private()
{
if self.interner.is_in_lsp_mode() && !function.def.is_test() {
self.interner.register_function(id, &function.def);
}

Expand Down
5 changes: 1 addition & 4 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,7 @@
let location = Location::new(function.span(), self.file_id);
context.def_interner.push_function(func_id, &function.def, module, location);

if context.def_interner.is_in_lsp_mode()
&& !function.def.is_test()
&& !function.def.is_private()
{
if context.def_interner.is_in_lsp_mode() && !function.def.is_test() {
context.def_interner.register_function(func_id, &function.def);
}

Expand Down Expand Up @@ -761,7 +758,7 @@
// if it's an inline module, or the first char of a the file if it's an external module.
// - `location` will always point to the token "foo" in `mod foo` regardless of whether
// it's inline or external.
// Eventually the location put in `ModuleData` is used for codelenses about `contract`s,

Check warning on line 761 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (codelenses)
// so we keep using `location` so that it continues to work as usual.
let location = Location::new(mod_name.span(), mod_location.file);
let new_module = ModuleData::new(parent, location, is_contract);
Expand Down
5 changes: 3 additions & 2 deletions tooling/lsp/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ pub(crate) fn module_full_path(
current_module_id: ModuleId,
current_module_parent_id: Option<ModuleId>,
interner: &NodeInterner,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
) -> Option<String> {
let full_path;
if let ModuleDefId::ModuleId(module_id) = module_def_id {
if !is_visible(visibility, current_module_id, module_id) {
if !is_visible(module_id, current_module_id, visibility, def_maps) {
return None;
}

Expand All @@ -61,7 +62,7 @@ pub(crate) fn module_full_path(
return None;
};

if !is_visible(visibility, current_module_id, parent_module) {
if !is_visible(parent_module, current_module_id, visibility, def_maps) {
return None;
}

Expand Down
1 change: 1 addition & 0 deletions tooling/lsp/src/requests/code_action/import_or_qualify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl<'a> CodeActionFinder<'a> {
self.module_id,
current_module_parent_id,
self.interner,
self.def_maps,
) else {
continue;
};
Expand Down
22 changes: 2 additions & 20 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
UnresolvedGenerics, UnresolvedType, UseTree, UseTreeKind, Visitor,
},
graph::{CrateId, Dependency},
hir::{
def_map::{CrateDefMap, LocalModuleId, ModuleId},
resolution::import::can_reference_module_id,
},
hir::def_map::{CrateDefMap, LocalModuleId, ModuleId},
hir_def::traits::Trait,
macros_api::{ModuleDefId, NodeInterner},
node_interner::ReferenceId,
Expand All @@ -34,12 +31,12 @@
};
use sort_text::underscore_sort_text;

use crate::{requests::to_lsp_location, utils, LspState};
use crate::{requests::to_lsp_location, utils, visibility::is_visible, LspState};

use super::process_request;

mod auto_import;
mod builtins;

Check warning on line 39 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (builtins)
mod completion_items;
mod kinds;
mod sort_text;
Expand Down Expand Up @@ -218,7 +215,7 @@
let mut idents: Vec<Ident> = Vec::new();

// Find in which ident we are in, and in which part of it
// (it could be that we are completting in the middle of an ident)

Check warning on line 218 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (completting)
for segment in &path.segments {
let ident = &segment.ident;

Expand Down Expand Up @@ -1214,8 +1211,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1214 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
/// name_matches("compute_merkle_root", "merk_ro") == true

Check warning on line 1215 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
///
/// // "ro" matches "root", but "merkle" comes before it, so no match
/// name_matches("compute_merkle_root", "ro_mer") == false
Expand Down Expand Up @@ -1263,21 +1260,6 @@
}
}

fn is_visible(
target_module_id: ModuleId,
current_module_id: ModuleId,
visibility: ItemVisibility,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
) -> bool {
can_reference_module_id(
def_maps,
current_module_id.krate,
current_module_id.local_id,
target_module_id,
visibility,
)
}

#[cfg(test)]
mod completion_name_matches_tests {
use crate::requests::completion::name_matches;
Expand Down
1 change: 1 addition & 0 deletions tooling/lsp/src/requests/completion/auto_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl<'a> NodeFinder<'a> {
self.module_id,
current_module_parent_id,
self.interner,
self.def_maps,
) else {
continue;
};
Expand Down
25 changes: 25 additions & 0 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 122 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
mod foobar {}
use foob>|<

Check warning on line 124 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
"#;

assert_completion(
src,
vec![module_completion_item("foobaz"), module_completion_item("foobar")],

Check warning on line 129 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
)
.await;
}
Expand Down Expand Up @@ -296,7 +296,7 @@
mod bar {
mod something {}

use super::foob>|<

Check warning on line 299 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand Down Expand Up @@ -1538,7 +1538,7 @@
async fn test_auto_import_suggests_modules_too() {
let src = r#"
mod foo {
mod barbaz {

Check warning on line 1541 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
fn hello_world() {}
}
}
Expand Down Expand Up @@ -1863,4 +1863,29 @@
Some("(use bar::foobar)".to_string()),
);
}

#[test]
async fn test_auto_import_suggests_private_function_if_visibile() {
let src = r#"
mod foo {
fn qux() {
barba>|<
}
}

fn barbaz() {}

fn main() {}
"#;

let items = get_completions(src).await;
assert_eq!(items.len(), 1);

let item = &items[0];
assert_eq!(item.label, "barbaz()");
assert_eq!(
item.label_details.as_ref().unwrap().detail,
Some("(use super::barbaz)".to_string()),
);
}
}
28 changes: 20 additions & 8 deletions tooling/lsp/src/visibility.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
use noirc_frontend::{ast::ItemVisibility, hir::def_map::ModuleId};
use std::collections::BTreeMap;

use noirc_frontend::{
ast::ItemVisibility,
graph::CrateId,
hir::{
def_map::{CrateDefMap, ModuleId},
resolution::import::can_reference_module_id,
},
};

pub(super) fn is_visible(
target_module_id: ModuleId,
current_module_id: ModuleId,
visibility: ItemVisibility,
current_module: ModuleId,
target_module: ModuleId,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
) -> bool {
match visibility {
ItemVisibility::Public => true,
ItemVisibility::Private => false,
ItemVisibility::PublicCrate => current_module.krate == target_module.krate,
}
can_reference_module_id(
def_maps,
current_module_id.krate,
current_module_id.local_id,
target_module_id,
visibility,
)
}
Loading