Skip to content

Commit

Permalink
Collect tokens from missing SwayParseTree variants (FuelLabs#1430)
Browse files Browse the repository at this point in the history
* wip

* added in lots of missing SwayParseTree variants

* collecting more tokens from the parse_tree

* update is_initial_declaration logic

* handling collecting more tokens from the SwayParseTree

* cargo clippy

* small tweak to enum semantic tokens

* revert look_up_type_id to use pub(crate)

* add TokenTypeIndex enum for semantic tokens

* added comments for extract_fn_signature and get_function_details

* removed unused variants from TokenTypeIndex
  • Loading branch information
JoshuaBatty authored May 2, 2022
1 parent 88cc74b commit 13d0081
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 101 deletions.
4 changes: 2 additions & 2 deletions sway-core/src/parse_tree/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ mod type_parameter;
mod variable;

pub(crate) use abi::*;
pub(crate) use constant::*;
pub use constant::*;
pub use function::*;
pub(crate) use impl_trait::*;
pub(crate) use r#enum::*;
pub use r#enum::*;
pub use r#struct::*;
pub use r#trait::*;
pub(crate) use reassignment::*;
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/parse_tree/declaration/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use pest::iterators::Pair;
#[derive(Debug, Clone)]
pub struct AbiDeclaration {
/// The name of the abi trait (also known as a "contract trait")
pub(crate) name: Ident,
pub name: Ident,
/// The methods a contract is required to implement in order opt in to this interface
pub(crate) interface_surface: Vec<TraitFn>,
pub interface_surface: Vec<TraitFn>,
/// The methods provided to a contract "for free" upon opting in to this interface
pub(crate) methods: Vec<FunctionDeclaration>,
pub methods: Vec<FunctionDeclaration>,
pub(crate) span: Span,
}

Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/parse_tree/declaration/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use pest::iterators::Pair;
pub struct EnumDeclaration {
pub name: Ident,
pub(crate) type_parameters: Vec<TypeParameter>,
pub(crate) variants: Vec<EnumVariant>,
pub variants: Vec<EnumVariant>,
pub(crate) span: Span,
pub visibility: Visibility,
}

#[derive(Debug, Clone)]
pub(crate) struct EnumVariant {
pub(crate) name: Ident,
pub struct EnumVariant {
pub name: Ident,
pub(crate) r#type: TypeInfo,
pub(crate) tag: usize,
pub(crate) span: Span,
Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/parse_tree/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub struct FunctionDeclaration {
pub name: Ident,
pub visibility: Visibility,
pub body: CodeBlock,
pub(crate) parameters: Vec<FunctionParameter>,
pub parameters: Vec<FunctionParameter>,
pub span: Span,
pub(crate) return_type: TypeInfo,
pub return_type: TypeInfo,
pub(crate) type_parameters: Vec<TypeParameter>,
pub(crate) return_type_span: Span,
}
Expand Down Expand Up @@ -193,8 +193,8 @@ impl FunctionDeclaration {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct FunctionParameter {
pub(crate) name: Ident,
pub struct FunctionParameter {
pub name: Ident,
pub(crate) type_id: TypeId,
pub(crate) type_span: Span,
}
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/parse_tree/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pest::iterators::Pair;

#[derive(Debug, Clone)]
pub struct ImplTrait {
pub(crate) trait_name: CallPath,
pub trait_name: CallPath,
pub(crate) type_implementing_for: TypeInfo,
pub(crate) type_implementing_for_span: Span,
pub(crate) type_arguments: Vec<TypeParameter>,
Expand All @@ -22,7 +22,7 @@ pub struct ImplTrait {
/// like `impl MyType { fn foo { .. } }`
#[derive(Debug, Clone)]
pub struct ImplSelf {
pub(crate) type_implementing_for: TypeInfo,
pub type_implementing_for: TypeInfo,
pub(crate) type_implementing_for_span: Span,
pub(crate) type_parameters: Vec<TypeParameter>,
pub functions: Vec<FunctionDeclaration>,
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/parse_tree/declaration/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use pest::iterators::Pair;
#[derive(Debug, Clone)]
pub struct StructDeclaration {
pub name: Ident,
pub(crate) fields: Vec<StructField>,
pub fields: Vec<StructField>,
pub(crate) type_parameters: Vec<TypeParameter>,
pub visibility: Visibility,
pub(crate) span: Span,
}

#[derive(Debug, Clone)]
pub(crate) struct StructField {
pub(crate) name: Ident,
pub struct StructField {
pub name: Ident,
pub(crate) r#type: TypeInfo,
pub(crate) span: Span,
pub(crate) type_span: Span,
Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/parse_tree/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ impl Supertrait {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct TraitFn {
pub(crate) name: Ident,
pub(crate) parameters: Vec<FunctionParameter>,
pub(crate) return_type: TypeInfo,
pub struct TraitFn {
pub name: Ident,
pub parameters: Vec<FunctionParameter>,
pub return_type: TypeInfo,
pub(crate) return_type_span: Span,
}

Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/parse_tree/expression/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl AsmExpression {
}

#[derive(Debug, Clone)]
pub(crate) struct AsmOp {
pub struct AsmOp {
pub(crate) op_name: Ident,
pub(crate) op_args: Vec<Ident>,
pub(crate) span: Span,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl PartialEq for AsmOp {
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct AsmRegister {
pub struct AsmRegister {
pub(crate) name: String,
}

Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/parse_tree/expression/method_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum MethodName {

impl MethodName {
/// To be used for error messages and debug strings
pub(crate) fn easy_name(&self) -> Ident {
pub fn easy_name(&self) -> Ident {
match self {
MethodName::FromType { call_path, .. } => call_path.suffix.clone(),
MethodName::FromModule { method_name, .. } => method_name.clone(),
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/parse_tree/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) use match_branch::MatchBranch;
pub(crate) use match_condition::CatchAll;
pub(crate) use match_condition::MatchCondition;
use matcher::matcher;
pub(crate) use method_name::MethodName;
pub use method_name::MethodName;
pub(crate) use scrutinee::{Scrutinee, StructScrutineeField};
pub(crate) use unary_op::UnaryOp;

Expand Down Expand Up @@ -236,8 +236,8 @@ impl LazyOp {

#[derive(Debug, Clone)]
pub struct StructExpressionField {
pub(crate) name: Ident,
pub(crate) value: Expression,
pub name: Ident,
pub value: Expression,
pub(crate) span: Span,
}

Expand Down
20 changes: 14 additions & 6 deletions sway-lsp/src/capabilities/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ pub fn to_completion_items(tokens: &[Token]) -> Vec<CompletionItem> {

fn get_kind(token_type: &TokenType) -> Option<CompletionItemKind> {
match token_type {
TokenType::Enum => Some(CompletionItemKind::ENUM),
TokenType::FunctionDeclaration(_) | &TokenType::FunctionApplication => {
Some(CompletionItemKind::FUNCTION)
TokenType::VariableDeclaration(_) | TokenType::VariableExpression => {
Some(CompletionItemKind::VARIABLE)
}
TokenType::FunctionDeclaration(_)
| &TokenType::FunctionApplication
| TokenType::TraitFunction => Some(CompletionItemKind::FUNCTION),
TokenType::TraitDeclaration(_) | TokenType::ImplTrait => {
Some(CompletionItemKind::INTERFACE)
}
TokenType::StructDeclaration(_) | TokenType::Struct => Some(CompletionItemKind::STRUCT),
TokenType::EnumDeclaration(_) | TokenType::EnumVariant | TokenType::EnumApplication => {
Some(CompletionItemKind::ENUM)
}
TokenType::ConstantDeclaration(_) => Some(CompletionItemKind::CONSTANT),
TokenType::Library => Some(CompletionItemKind::MODULE),
TokenType::Struct(_) => Some(CompletionItemKind::STRUCT),
TokenType::Variable(_) => Some(CompletionItemKind::VARIABLE),
TokenType::Trait(_) => Some(CompletionItemKind::INTERFACE),
TokenType::Reassignment => Some(CompletionItemKind::OPERATOR),
_ => None,
}
}
15 changes: 10 additions & 5 deletions sway-lsp/src/capabilities/document_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ fn create_symbol_info(token: &Token, url: Url) -> SymbolInformation {

fn get_kind(token_type: &TokenType) -> SymbolKind {
match token_type {
TokenType::Enum => SymbolKind::ENUM,
TokenType::FunctionDeclaration(_) | &TokenType::FunctionApplication => SymbolKind::FUNCTION,
TokenType::VariableDeclaration(_) | TokenType::VariableExpression => SymbolKind::VARIABLE,
TokenType::FunctionDeclaration(_)
| TokenType::FunctionApplication
| TokenType::TraitFunction => SymbolKind::FUNCTION,
TokenType::TraitDeclaration(_) | TokenType::ImplTrait => SymbolKind::INTERFACE,
TokenType::StructDeclaration(_) | TokenType::Struct => SymbolKind::STRUCT,
TokenType::EnumDeclaration(_) | TokenType::EnumApplication => SymbolKind::ENUM,
TokenType::ConstantDeclaration(_) => SymbolKind::CONSTANT,
TokenType::Library => SymbolKind::MODULE,
TokenType::Struct(_) => SymbolKind::STRUCT,
TokenType::Variable(_) => SymbolKind::VARIABLE,
TokenType::Trait(_) => SymbolKind::INTERFACE,
TokenType::Reassignment => SymbolKind::OPERATOR,
// currently we return `variable` type as default
_ => SymbolKind::VARIABLE,
}
}
12 changes: 8 additions & 4 deletions sway-lsp/src/capabilities/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn get_hover_data(session: Arc<Session>, params: HoverParams) -> Option<Hove

fn get_hover_format(token: &Token, documents: &Documents) -> Hover {
let value = match &token.token_type {
TokenType::Variable(var_details) => {
TokenType::VariableDeclaration(var_details) => {
let var_type = match &var_details.var_body {
VarBody::FunctionCall(fn_name) => get_var_type_from_fn(fn_name, documents),
VarBody::Type(var_type) => var_type.clone(),
Expand All @@ -56,17 +56,21 @@ fn get_hover_format(token: &Token, documents: &Documents) -> Hover {
)
}
TokenType::FunctionDeclaration(func_details) => func_details.signature.clone(),
TokenType::Struct(struct_details) => format!(
TokenType::StructDeclaration(struct_details) => format!(
"{}struct {}",
extract_visibility(&struct_details.visibility),
&token.name
),
TokenType::Trait(trait_details) => format!(
TokenType::TraitDeclaration(trait_details) => format!(
"{}trait {}",
extract_visibility(&trait_details.visibility),
&token.name
),
TokenType::Enum => format!("enum {}", &token.name),
TokenType::EnumDeclaration(enum_details) => format!(
"{}enum {}",
extract_visibility(&enum_details.visibility),
&token.name
),
_ => token.name.clone(),
};

Expand Down
37 changes: 23 additions & 14 deletions sway-lsp/src/capabilities/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,33 @@ fn create_semantic_token(next_token: &Token, prev_token: Option<&Token>) -> Sema
}
}

// these values should reflect indexes in `token_types`
static FUNCTION: u32 = 1;
static LIBRARY: u32 = 3;
static VARIABLE: u32 = 9;
static ENUM: u32 = 10;
static STRUCT: u32 = 11;
static TRAIT: u32 = 12;
/// these values should reflect indexes in `token_types`
#[repr(u32)]
enum TokenTypeIndex {
Function = 1,
Namespace = 3,
Parameter = 5,
Variable = 9,
Enum = 10,
Struct = 11,
Interface = 12,
}

fn get_type(token_type: &TokenType) -> u32 {
match token_type {
TokenType::FunctionDeclaration(_) | &TokenType::FunctionApplication => FUNCTION,
TokenType::Library => LIBRARY,
TokenType::Variable(_) => VARIABLE,
TokenType::Enum => ENUM,
TokenType::Struct(_) => STRUCT,
TokenType::Trait(_) => TRAIT,
TokenType::FunctionDeclaration(_)
| TokenType::FunctionApplication
| TokenType::TraitFunction => TokenTypeIndex::Function as u32,
TokenType::Library => TokenTypeIndex::Namespace as u32,
TokenType::FunctionParameter => TokenTypeIndex::Parameter as u32,
TokenType::VariableDeclaration(_) | TokenType::VariableExpression => {
TokenTypeIndex::Variable as u32
}
TokenType::EnumDeclaration(_) => TokenTypeIndex::Enum as u32,
TokenType::StructDeclaration(_) | TokenType::Struct => TokenTypeIndex::Struct as u32,
TokenType::TraitDeclaration(_) | TokenType::ImplTrait => TokenTypeIndex::Interface as u32,
// currently we return `variable` type as default
_ => VARIABLE,
_ => TokenTypeIndex::Variable as u32,
}
}

Expand Down
Loading

0 comments on commit 13d0081

Please sign in to comment.