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

fix: lsp hover wasn't always working #5515

Merged
merged 2 commits into from
Jul 15, 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
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@
}
}
TraitItem::Type { name } => {
// TODO(nickysn or alexvitkov): implement context.def_interner.push_empty_type_alias and get an id, instead of using TypeAliasId::dummy_id()

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (nickysn)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (alexvitkov)
if let Err((first_def, second_def)) = self.def_collector.def_map.modules
[trait_id.0.local_id.0]
.declare_type_alias(name.clone(), TypeAliasId::dummy_id())
Expand Down Expand Up @@ -695,7 +695,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 698 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 Expand Up @@ -732,7 +732,11 @@

context.def_interner.add_module_attributes(
mod_id,
ModuleAttributes { name: mod_name.0.contents.clone(), location: mod_location },
ModuleAttributes {
name: mod_name.0.contents.clone(),
location: mod_location,
parent: self.module_id,
},
);
}

Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
pub struct ModuleAttributes {
pub name: String,
pub location: Location,
pub parent: LocalModuleId,
}

type StructAttributes = Vec<SecondaryAttribute>;
Expand Down Expand Up @@ -193,7 +194,7 @@
/// Stores the [Location] of a [Type] reference
pub(crate) type_ref_locations: Vec<(Type, Location)>,

/// In Noir's metaprogramming, a noir type has the type `Type`. When these are spliced

Check warning on line 197 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (metaprogramming)
/// into `quoted` expressions, we preserve the original type by assigning it a unique id
/// and creating a `Token::QuotedType(id)` from this id. We cannot create a token holding
/// the actual type since types do not implement Send or Sync.
Expand Down Expand Up @@ -1016,6 +1017,10 @@
self.module_attributes.get(module_id)
}

pub fn try_module_parent(&self, module_id: &ModuleId) -> Option<LocalModuleId> {
self.try_module_attributes(module_id).map(|attrs| attrs.parent)
}

pub fn global_attributes(&self, global_id: &GlobalId) -> &[SecondaryAttribute] {
&self.global_attributes[global_id]
}
Expand Down
37 changes: 14 additions & 23 deletions tooling/lsp/src/requests/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,14 @@
}
fn format_module(id: ModuleId, args: &ProcessRequestCallbackArgs) -> String {
let module_attributes = args.interner.module_attributes(&id);
let parent_module_id = args.def_maps[&id.krate].modules()[id.local_id.0].parent;

let mut string = String::new();
if let Some(parent_module_id) = parent_module_id {
if format_parent_module_from_module_id(
&ModuleId { krate: id.krate, local_id: parent_module_id },
args,
&mut string,
) {
string.push('\n');
}
if format_parent_module_from_module_id(
&ModuleId { krate: id.krate, local_id: module_attributes.parent },
args,
&mut string,
) {
string.push('\n');
}
string.push_str(" ");
string.push_str("mod ");
Expand Down Expand Up @@ -319,6 +316,7 @@
CrateId::Stdlib(_) => Some("std".to_string()),
CrateId::Dummy => None,
};

let wrote_crate = if let Some(crate_name) = crate_name {
string.push_str(" ");
string.push_str(&crate_name);
Expand All @@ -331,27 +329,20 @@
return wrote_crate;
};

let modules = args.def_maps[&module.krate].modules();
let module_data = &modules[module.local_id.0];

if wrote_crate {
string.push_str("::");
} else {
string.push_str(" ");
}

let mut segments = Vec::new();
let mut current_parent = module_data.parent;
while let Some(parent) = current_parent {
let parent_attributes = args
.interner
.try_module_attributes(&ModuleId { krate: module.krate, local_id: parent });
if let Some(parent_attributes) = parent_attributes {
segments.push(&parent_attributes.name);
current_parent = modules[module.local_id.0].parent;
} else {
break;
}
let mut current_attributes = module_attributes;
while let Some(parent_attributes) = args.interner.try_module_attributes(&ModuleId {
krate: module.krate,
local_id: current_attributes.parent,
}) {
segments.push(&parent_attributes.name);
current_attributes = parent_attributes;
}

for segment in segments.iter().rev() {
Expand Down Expand Up @@ -411,7 +402,7 @@
"two/src/lib.nr",
Position { line: 6, character: 9 },
r#" one
mod subone"#,

Check warning on line 405 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
)
.await;
}
Expand All @@ -422,7 +413,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 9, character: 20 },
r#" one::subone

Check warning on line 416 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
struct SubOneStruct {
some_field: i32,
some_other_field: Field,
Expand All @@ -437,7 +428,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 46, character: 17 },
r#" one::subone

Check warning on line 431 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
struct GenericStruct<A, B> {
}"#,
)
Expand All @@ -450,7 +441,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 9, character: 35 },
r#" one::subone::SubOneStruct

Check warning on line 444 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
some_field: i32"#,
)
.await;
Expand All @@ -462,7 +453,7 @@
"workspace",
"two/src/lib.nr",
Position { line: 12, character: 17 },
r#" one::subone

Check warning on line 456 in tooling/lsp/src/requests/hover.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (subone)
trait SomeTrait"#,
)
.await;
Expand Down
13 changes: 2 additions & 11 deletions tooling/lsp/src/requests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
collections::{BTreeMap, HashMap},
future::Future,
};
use std::{collections::HashMap, future::Future};

use crate::{
parse_diff, resolve_workspace_for_source_path,
Expand All @@ -17,11 +14,7 @@
use nargo::insert_all_files_for_workspace_into_file_manager;
use nargo_fmt::Config;
use noirc_driver::file_manager_with_stdlib;
use noirc_frontend::{
graph::{CrateId, Dependency},
hir::def_map::CrateDefMap,
macros_api::NodeInterner,
};
use noirc_frontend::{graph::Dependency, macros_api::NodeInterner};
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -282,10 +275,9 @@
location: noirc_errors::Location,
files: &'a FileMap,
interner: &'a NodeInterner,
interners: &'a HashMap<String, NodeInterner>,

Check warning on line 278 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (interners)
root_crate_name: String,
root_crate_dependencies: &'a Vec<Dependency>,
def_maps: &'a BTreeMap<CrateId, CrateDefMap>,
}

pub(crate) fn process_request<F, T>(
Expand Down Expand Up @@ -340,7 +332,6 @@
interners: &state.cached_definitions,
root_crate_name: package.name.to_string(),
root_crate_dependencies: &context.crate_graph[context.root_crate_id()].dependencies,
def_maps: &context.def_maps,
}))
}
pub(crate) fn find_all_references_in_workspace(
Expand Down
Loading