From 1f48797aa0eadca31dcfc2652b112bf4cfe64eae Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Tue, 26 Jul 2022 14:06:19 +1000 Subject: [PATCH] Return the local struct Span as a new field on `TypedExpressionVariant::StructExpression` (#2380) This PR uses the local span of the call_path_binding to create a Span for the TypedExpressionVariant::StructExpression. Previously it was only passing along the Ident of the declaration which would prohibit the token from being collected in the language server. --- .../src/control_flow_analysis/dead_code_analysis.rs | 1 + .../ast_node/expression/typed_expression.rs | 1 + .../ast_node/expression/typed_expression_variant.rs | 9 ++++++++- sway-lsp/src/core/traverse_typed_tree.rs | 7 ++----- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sway-core/src/control_flow_analysis/dead_code_analysis.rs b/sway-core/src/control_flow_analysis/dead_code_analysis.rs index 8d7c379bfab..024a1b58902 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -821,6 +821,7 @@ fn connect_expression( StructExpression { struct_name, fields, + .. } => { let decl = match graph.namespace.find_struct_decl(struct_name.as_str()) { Some(ix) => *ix, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index 13a286180bc..e7023864252 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -1021,6 +1021,7 @@ impl TypedExpression { expression: TypedExpressionVariant::StructExpression { struct_name: struct_name.clone(), fields: typed_fields_buf, + span: call_path_binding.inner.suffix.1.clone(), }, return_type: type_id, is_constant: IsConstant::No, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs index 9cf8bc7d4be..e2bc6371a52 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs @@ -49,6 +49,7 @@ pub enum TypedExpressionVariant { StructExpression { struct_name: Ident, fields: Vec, + span: Span, }, CodeBlock(TypedCodeBlock), // a flag that this value will later be provided as a parameter, but is currently unknown @@ -177,12 +178,18 @@ impl PartialEq for TypedExpressionVariant { Self::StructExpression { struct_name: l_struct_name, fields: l_fields, + span: l_span, }, Self::StructExpression { struct_name: r_struct_name, fields: r_fields, + span: r_span, }, - ) => l_struct_name == r_struct_name && l_fields.clone() == r_fields.clone(), + ) => { + l_struct_name == r_struct_name + && l_fields.clone() == r_fields.clone() + && l_span == r_span + } (Self::CodeBlock(l0), Self::CodeBlock(r0)) => l0 == r0, ( Self::IfExp { diff --git a/sway-lsp/src/core/traverse_typed_tree.rs b/sway-lsp/src/core/traverse_typed_tree.rs index 3487fac24cf..47bd769de03 100644 --- a/sway-lsp/src/core/traverse_typed_tree.rs +++ b/sway-lsp/src/core/traverse_typed_tree.rs @@ -230,11 +230,8 @@ fn handle_expression(expression: &TypedExpression, tokens: &TokenMap) { handle_expression(prefix, tokens); handle_expression(index, tokens); } - TypedExpressionVariant::StructExpression { - ref struct_name, - ref fields, - } => { - if let Some(mut token) = tokens.get_mut(&to_ident_key(struct_name)) { + TypedExpressionVariant::StructExpression { fields, span, .. } => { + if let Some(mut token) = tokens.get_mut(&to_ident_key(&Ident::new(span.clone()))) { token.typed = Some(TypedAstToken::TypedExpression(expression.clone())); }