Skip to content

Commit

Permalink
Add LSP tests for TyAbiDeclaration (FuelLabs#4165)
Browse files Browse the repository at this point in the history
## Description
Adds goto and hover tests for ABI's. Also cleaned up the typed_tree
module by storing a ref to namespace in the type instead of passing it
around everywhere.

Found a bug where we weren't collecting attributes for `AbiDeclaration`.
Hover docs now works on these types.

works towards FuelLabs#3989
  • Loading branch information
JoshuaBatty authored Feb 23, 2023
1 parent 1f150cc commit 91feedc
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 184 deletions.
3 changes: 3 additions & 0 deletions sway-lsp/src/capabilities/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ fn hover_format(engines: Engines<'_>, token: &Token, ident: &Ident) -> lsp_types
)
})
.ok(),
ty::TyDeclaration::AbiDeclaration { .. } => {
Some(format!("{} {}", decl.friendly_type_name(), &token_name))
}
_ => None,
},
TypedAstToken::TypedFunctionDeclaration(func) => {
Expand Down
35 changes: 10 additions & 25 deletions sway-lsp/src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sway_core::{
parsed::{AstNode, ParseProgram},
ty,
},
namespace, BuildTarget, CompileResult, Engines, TypeEngine,
BuildTarget, CompileResult, Engines, TypeEngine,
};
use sway_types::{Span, Spanned};
use sway_utils::helpers::get_sway_files;
Expand Down Expand Up @@ -187,10 +187,11 @@ impl Session {
// Finally, create runnables and populate our token_map with typed ast nodes.
self.create_runnables(typed_program);

let typed_tree = TypedTree::new(engines, &self.token_map);
let typed_tree =
TypedTree::new(engines, &self.token_map, &typed_program.root.namespace);
typed_tree.collect_module_spans(typed_program);
self.parse_ast_to_typed_tokens(typed_program, |node, namespace| {
typed_tree.traverse_node(node, namespace)
self.parse_ast_to_typed_tokens(typed_program, |node| {
typed_tree.traverse_node(node)
});

self.save_lexed_program(lexed.to_owned().clone());
Expand All @@ -203,7 +204,7 @@ impl Session {
let dependency = Dependency::new(&self.token_map);
self.parse_ast_to_tokens(&parsed, |an| dependency.collect_parsed_declaration(an));

self.parse_ast_to_typed_tokens(typed_program, |node, _module| {
self.parse_ast_to_typed_tokens(typed_program, |node| {
dependency.collect_typed_declaration(node)
});
}
Expand Down Expand Up @@ -361,31 +362,15 @@ impl Session {
}

/// Parse the [ty::TyProgram] AST to populate the [TokenMap] with typed AST nodes.
fn parse_ast_to_typed_tokens(
&self,
typed_program: &ty::TyProgram,
f: impl Fn(&ty::TyAstNode, &namespace::Module),
) {
let root_nodes = typed_program
.root
.all_nodes
.iter()
.map(|node| (node, &typed_program.root.namespace));
fn parse_ast_to_typed_tokens(&self, typed_program: &ty::TyProgram, f: impl Fn(&ty::TyAstNode)) {
let root_nodes = typed_program.root.all_nodes.iter();
let sub_nodes = typed_program
.root
.submodules
.iter()
.flat_map(|(_, submodule)| {
submodule
.module
.all_nodes
.iter()
.map(|node| (node, &submodule.module.namespace))
});
.flat_map(|(_, submodule)| submodule.module.all_nodes.iter());

root_nodes
.chain(sub_nodes)
.for_each(|(node, namespace)| f(node, namespace));
root_nodes.chain(sub_nodes).for_each(f);
}

/// Get a reference to the [ty::TyProgram] AST.
Expand Down
4 changes: 3 additions & 1 deletion sway-lsp/src/core/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ pub fn type_info_to_symbol_kind(type_engine: &TypeEngine, type_info: &TypeInfo)
SymbolKind::BuiltinType
}
TypeInfo::Numeric | TypeInfo::Str(..) => SymbolKind::NumericLiteral,
TypeInfo::Custom { .. } | TypeInfo::Struct { .. } => SymbolKind::Struct,
TypeInfo::Custom { .. } | TypeInfo::Struct { .. } | TypeInfo::Contract => {
SymbolKind::Struct
}
TypeInfo::Enum { .. } => SymbolKind::Enum,
TypeInfo::Array(elem_ty, ..) => {
let type_info = type_engine.get(elem_ty.type_id);
Expand Down
Loading

0 comments on commit 91feedc

Please sign in to comment.