Skip to content

Commit

Permalink
LSP: Collect attribute name tokens (FuelLabs#3753)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBatty authored Jan 11, 2023
1 parent 2930f48 commit 2831ceb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
2 changes: 1 addition & 1 deletion sway-lsp/src/capabilities/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn completion_item_kind(symbol_kind: &SymbolKind) -> Option<CompletionItemKi
SymbolKind::Field => Some(CompletionItemKind::FIELD),
SymbolKind::BuiltinType => Some(CompletionItemKind::TYPE_PARAMETER),
SymbolKind::ValueParam => Some(CompletionItemKind::VALUE),
SymbolKind::Function => Some(CompletionItemKind::FUNCTION),
SymbolKind::Function | SymbolKind::DeriveHelper => Some(CompletionItemKind::FUNCTION),
SymbolKind::Const => Some(CompletionItemKind::CONSTANT),
SymbolKind::Struct => Some(CompletionItemKind::STRUCT),
SymbolKind::Trait => Some(CompletionItemKind::INTERFACE),
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/capabilities/document_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn symbol_kind(symbol_kind: &SymbolKind) -> lsp_types::SymbolKind {
match symbol_kind {
SymbolKind::Field => lsp_types::SymbolKind::FIELD,
SymbolKind::BuiltinType => lsp_types::SymbolKind::TYPE_PARAMETER,
SymbolKind::Function => lsp_types::SymbolKind::FUNCTION,
SymbolKind::Function | SymbolKind::DeriveHelper => lsp_types::SymbolKind::FUNCTION,
SymbolKind::Const => lsp_types::SymbolKind::CONSTANT,
SymbolKind::Struct => lsp_types::SymbolKind::STRUCT,
SymbolKind::Trait => lsp_types::SymbolKind::INTERFACE,
Expand Down
10 changes: 6 additions & 4 deletions sway-lsp/src/capabilities/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
SemanticTokenType::new("boolean"),
SemanticTokenType::new("keyword"),
SemanticTokenType::new("builtinType"),
SemanticTokenType::new("deriveHelper"),
];

pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
Expand Down Expand Up @@ -157,13 +158,14 @@ fn semantic_token_type(kind: &SymbolKind) -> SemanticTokenType {
SymbolKind::Variant => SemanticTokenType::ENUM_MEMBER,
SymbolKind::Trait => SemanticTokenType::INTERFACE,
SymbolKind::TypeParameter => SemanticTokenType::TYPE_PARAMETER,
SymbolKind::BoolLiteral => SemanticTokenType::new("boolean"),
SymbolKind::ByteLiteral | SymbolKind::NumericLiteral => SemanticTokenType::NUMBER,
SymbolKind::StringLiteral => SemanticTokenType::STRING,
SymbolKind::BuiltinType => SemanticTokenType::new("builtinType"),
SymbolKind::Module => SemanticTokenType::NAMESPACE,
SymbolKind::StringLiteral => SemanticTokenType::STRING,
SymbolKind::ByteLiteral | SymbolKind::NumericLiteral => SemanticTokenType::NUMBER,
SymbolKind::BoolLiteral => SemanticTokenType::new("boolean"),
SymbolKind::Keyword => SemanticTokenType::new("keyword"),
SymbolKind::Unknown => SemanticTokenType::new("generic"),
SymbolKind::BuiltinType => SemanticTokenType::new("builtinType"),
SymbolKind::DeriveHelper => SemanticTokenType::new("deriveHelper"),
}
}

Expand Down
3 changes: 3 additions & 0 deletions sway-lsp/src/core/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sway_core::{
},
ty,
},
transform::Attribute,
type_system::{TypeId, TypeInfo, TypeParameter},
TypeArgument, TypeEngine,
};
Expand All @@ -31,6 +32,7 @@ pub enum AstToken {
StorageField(StorageField),
Scrutinee(Scrutinee),
Keyword(Ident),
Attribute(Attribute),
}

/// The `TypedAstToken` holds the types produced by the [sway_core::language::ty::TyProgram].
Expand Down Expand Up @@ -67,6 +69,7 @@ pub enum SymbolKind {
NumericLiteral,
Variable,
BuiltinType,
DeriveHelper,
Module,
TypeParameter,
Keyword,
Expand Down
13 changes: 6 additions & 7 deletions sway-lsp/src/traverse/lexed_tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::core::{
token::{to_ident_key, AstToken, SymbolKind, Token},
token_map::TokenMap,
use crate::{
core::{
token::{to_ident_key, AstToken, SymbolKind, Token},
token_map::TokenMap,
},
traverse::Parse,
};
use std::ops::ControlFlow;
use sway_ast::{
Expand Down Expand Up @@ -43,10 +46,6 @@ fn insert_keyword(tokens: &TokenMap, span: Span) {
tokens.insert(to_ident_key(&ident), token);
}

pub trait Parse {
fn parse(&self, tokens: &TokenMap);
}

impl Parse for ItemKind {
fn parse(&self, tokens: &TokenMap) {
match self {
Expand Down
7 changes: 7 additions & 0 deletions sway-lsp/src/traverse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use crate::core::token_map::TokenMap;

pub(crate) mod dependency;
pub(crate) mod lexed_tree;
pub(crate) mod parsed_tree;
pub(crate) mod typed_tree;

/// The `Parse` trait is used to parse tokens from an AST during traversal.
pub trait Parse {
fn parse(&self, tokens: &TokenMap);
}
50 changes: 45 additions & 5 deletions sway-lsp/src/traverse/parsed_tree.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#![allow(dead_code)]
use std::iter;

use crate::core::{
token::{
desugared_op, to_ident_key, type_info_to_symbol_kind, AstToken, SymbolKind, Token,
TypeDefinition,
use crate::{
core::{
token::{
desugared_op, to_ident_key, type_info_to_symbol_kind, AstToken, SymbolKind, Token,
TypeDefinition,
},
token_map::TokenMap,
},
token_map::TokenMap,
traverse::Parse,
};

use sway_core::{
language::{
parsed::{
Expand All @@ -21,6 +25,7 @@ use sway_core::{
},
Literal,
},
transform::{AttributeKind, AttributesMap},
type_system::{TypeArgument, TypeParameter},
TypeEngine, TypeInfo,
};
Expand Down Expand Up @@ -77,6 +82,8 @@ impl<'a> ParsedTree<'a> {
Some(func.return_type_span.clone()),
None,
);

func.attributes.parse(self.tokens);
}

fn handle_declaration(&self, declaration: &Declaration) {
Expand Down Expand Up @@ -166,6 +173,8 @@ impl<'a> ParsedTree<'a> {
Some(field.type_span.clone()),
None,
);

field.attributes.parse(self.tokens);
}

for type_param in &struct_dec.type_parameters {
Expand All @@ -174,6 +183,8 @@ impl<'a> ParsedTree<'a> {
AstToken::Declaration(declaration.clone()),
);
}

struct_dec.attributes.parse(self.tokens);
}
Declaration::EnumDeclaration(enum_decl) => {
self.tokens.insert(
Expand Down Expand Up @@ -205,7 +216,10 @@ impl<'a> ParsedTree<'a> {
Some(variant.type_span.clone()),
Some(SymbolKind::Variant),
);
variant.attributes.parse(self.tokens);
}

enum_decl.attributes.parse(self.tokens);
}
Declaration::ImplTrait(impl_trait) => {
for ident in &impl_trait.trait_name.prefixes {
Expand Down Expand Up @@ -290,6 +304,8 @@ impl<'a> ParsedTree<'a> {
for trait_fn in &abi_decl.interface_surface {
self.collect_trait_fn(trait_fn);
}

abi_decl.attributes.parse(self.tokens);
}
Declaration::ConstantDeclaration(const_decl) => {
let token = Token::from_parsed(
Expand All @@ -306,6 +322,8 @@ impl<'a> ParsedTree<'a> {
None,
);
self.handle_expression(&const_decl.value);

const_decl.attributes.parse(self.tokens);
}
Declaration::StorageDeclaration(storage_decl) => {
for field in &storage_decl.fields {
Expand All @@ -322,7 +340,10 @@ impl<'a> ParsedTree<'a> {
None,
);
self.handle_expression(&field.initializer);

field.attributes.parse(self.tokens);
}
storage_decl.attributes.parse(self.tokens);
}
}
}
Expand Down Expand Up @@ -881,6 +902,8 @@ impl<'a> ParsedTree<'a> {
Some(trait_fn.return_type_span.clone()),
None,
);

trait_fn.attributes.parse(self.tokens);
}

fn collect_type_parameter(&self, type_param: &TypeParameter, token: AstToken) {
Expand All @@ -891,6 +914,23 @@ impl<'a> ParsedTree<'a> {
}
}

impl Parse for AttributesMap {
fn parse(&self, tokens: &TokenMap) {
self.iter()
.filter(|(kind, ..)| **kind != AttributeKind::DocComment)
.flat_map(|(.., attrs)| attrs)
.for_each(|attribute| {
tokens.insert(
to_ident_key(&attribute.name),
Token::from_parsed(
AstToken::Attribute(attribute.clone()),
SymbolKind::DeriveHelper,
),
);
});
}
}

fn literal_to_symbol_kind(value: &Literal) -> SymbolKind {
match value {
Literal::U8(..)
Expand Down

0 comments on commit 2831ceb

Please sign in to comment.