Skip to content

Commit

Permalink
Allow comma-separated annotations: #[ann1, ann2, ...] (FuelLabs#3469)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Dec 15, 2022
1 parent 50c1b6c commit 5b35baa
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 256 deletions.
2 changes: 1 addition & 1 deletion forc-plugins/forc-doc/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn module_depth_to_path_prefix(module_depth: usize) -> String {
}
/// Creates an HTML String from an [AttributesMap]
fn attrsmap_to_html_string(attributes: &AttributesMap) -> String {
let attributes = attributes.get(&AttributeKind::Doc);
let attributes = attributes.get(&AttributeKind::DocComment);
let mut docs = String::new();

if let Some(vec_attrs) = attributes {
Expand Down
2 changes: 1 addition & 1 deletion sway-ast/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Annotated<T> {
#[derive(Clone, Debug)]
pub struct AttributeDecl {
pub hash_token: HashToken,
pub attribute: SquareBrackets<Attribute>,
pub attribute: SquareBrackets<Punctuated<Attribute, CommaToken>>,
}

impl Spanned for AttributeDecl {
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/transform/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Attribute {
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum AttributeKind {
Doc,
DocComment,
Storage,
Inline,
Test,
Expand Down
77 changes: 40 additions & 37 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sway_error::handler::{ErrorEmitted, Handler};
use sway_error::warning::{CompileWarning, Warning};
use sway_types::{
constants::{
DESTRUCTURE_PREFIX, DOC_ATTRIBUTE_NAME, INLINE_ATTRIBUTE_NAME,
DESTRUCTURE_PREFIX, DOC_ATTRIBUTE_NAME, DOC_COMMENT_ATTRIBUTE_NAME, INLINE_ATTRIBUTE_NAME,
MATCH_RETURN_VAR_NAME_PREFIX, STORAGE_PURITY_ATTRIBUTE_NAME, STORAGE_PURITY_READ_NAME,
STORAGE_PURITY_WRITE_NAME, TEST_ATTRIBUTE_NAME, TUPLE_NAME_PREFIX, VALID_ATTRIBUTE_NAMES,
},
Expand Down Expand Up @@ -105,10 +105,10 @@ fn item_to_ast_nodes(
let contents = match item.value {
ItemKind::Dependency(dependency) => {
// Check that Dependency is not annotated
if attributes.contains_key(&AttributeKind::Doc) {
if attributes.contains_key(&AttributeKind::DocComment) {
let error = ConvertParseTreeError::CannotDocCommentDependency {
span: attributes
.get(&AttributeKind::Doc)
.get(&AttributeKind::DocComment)
.unwrap()
.last()
.unwrap()
Expand All @@ -118,7 +118,7 @@ fn item_to_ast_nodes(
handler.emit_err(error.into());
}
for (attribute_kind, attributes) in attributes.iter() {
if attribute_kind != &AttributeKind::Doc {
if attribute_kind != &AttributeKind::DocComment {
for attribute in attributes {
let error = ConvertParseTreeError::CannotAnnotateDependency {
span: attribute.span.clone(),
Expand Down Expand Up @@ -3253,42 +3253,45 @@ fn item_attrs_to_map(
) -> Result<AttributesMap, ErrorEmitted> {
let mut attrs_map: HashMap<_, Vec<Attribute>> = HashMap::new();
for attr_decl in attribute_list {
let attr = attr_decl.attribute.get();
let name = attr.name.as_str();
if !VALID_ATTRIBUTE_NAMES.contains(&name) {
handler.emit_warn(CompileWarning {
span: attr_decl.span().clone(),
warning_content: Warning::UnrecognizedAttribute {
attrib_name: attr.name.clone(),
},
})
}
let attrs = attr_decl.attribute.get().into_iter();
for attr in attrs {
let name = attr.name.as_str();
if !VALID_ATTRIBUTE_NAMES.contains(&name) {
handler.emit_warn(CompileWarning {
span: attr_decl.span().clone(),
warning_content: Warning::UnrecognizedAttribute {
attrib_name: attr.name.clone(),
},
})
}

let args = attr
.args
.as_ref()
.map(|parens| parens.get().into_iter().cloned().collect())
.unwrap_or_else(Vec::new);
let args = attr
.args
.as_ref()
.map(|parens| parens.get().into_iter().cloned().collect())
.unwrap_or_else(Vec::new);

let attribute = Attribute {
name: attr.name.clone(),
args,
span: attr_decl.span(),
};
let attribute = Attribute {
name: attr.name.clone(),
args,
span: attr_decl.span(),
};

if let Some(attr_kind) = match name {
DOC_ATTRIBUTE_NAME => Some(AttributeKind::Doc),
STORAGE_PURITY_ATTRIBUTE_NAME => Some(AttributeKind::Storage),
INLINE_ATTRIBUTE_NAME => Some(AttributeKind::Inline),
TEST_ATTRIBUTE_NAME => Some(AttributeKind::Test),
_ => None,
} {
match attrs_map.get_mut(&attr_kind) {
Some(old_args) => {
old_args.push(attribute);
}
None => {
attrs_map.insert(attr_kind, vec![attribute]);
if let Some(attr_kind) = match name {
DOC_ATTRIBUTE_NAME => Some(AttributeKind::Doc),
DOC_COMMENT_ATTRIBUTE_NAME => Some(AttributeKind::DocComment),
STORAGE_PURITY_ATTRIBUTE_NAME => Some(AttributeKind::Storage),
INLINE_ATTRIBUTE_NAME => Some(AttributeKind::Inline),
TEST_ATTRIBUTE_NAME => Some(AttributeKind::Test),
_ => None,
} {
match attrs_map.get_mut(&attr_kind) {
Some(old_args) => {
old_args.push(attribute);
}
None => {
attrs_map.insert(attr_kind, vec![attribute]);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions sway-lsp/src/capabilities/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
session::Session,
token::{get_range_from_span, to_ident_key, Token, TypedAstToken},
},
utils::{attributes::doc_attributes, markdown, markup::Markup},
utils::{attributes::doc_comment_attributes, markdown, markup::Markup},
};
use std::sync::Arc;
use sway_core::{
Expand Down Expand Up @@ -53,7 +53,7 @@ fn extract_fn_signature(span: &Span) -> String {

fn format_doc_attributes(token: &Token) -> String {
let mut doc_comment = String::new();
if let Some(attributes) = doc_attributes(token) {
if let Some(attributes) = doc_comment_attributes(token) {
doc_comment = attributes
.iter()
.map(|attribute| {
Expand Down
4 changes: 2 additions & 2 deletions sway-lsp/src/utils/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub fn attributes_map(token: &Token) -> Option<&transform::AttributesMap> {
}
}

pub fn doc_attributes(token: &Token) -> Option<&[transform::Attribute]> {
pub fn doc_comment_attributes(token: &Token) -> Option<&[transform::Attribute]> {
attributes_map(token)
.and_then(|attributes| attributes.get(&transform::AttributeKind::Doc))
.and_then(|attributes| attributes.get(&transform::AttributeKind::DocComment))
.map(Vec::as_slice)
}

Expand Down
10 changes: 7 additions & 3 deletions sway-parse/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sway_ast::keywords::{HashToken, StorageToken, Token};
use sway_ast::punctuated::Punctuated;
use sway_ast::token::{DocComment, DocStyle};
use sway_error::parser_error::ParseErrorKind;
use sway_types::constants::DOC_COMMENT_ATTRIBUTE_NAME;
use sway_types::Ident;

impl Peek for DocComment {
Expand Down Expand Up @@ -40,13 +41,16 @@ impl<T: Parse> Parse for Annotated<T> {
attribute_list.push(AttributeDecl {
hash_token: HashToken::new(doc_comment.span.clone()),
attribute: SquareBrackets::new(
Attribute {
name: Ident::new_with_override("doc", doc_comment.span.clone()),
Punctuated::single(Attribute {
name: Ident::new_with_override(
DOC_COMMENT_ATTRIBUTE_NAME,
doc_comment.span.clone(),
),
args: Some(Parens::new(
Punctuated::single(value),
doc_comment.content_span,
)),
},
}),
doc_comment.span,
),
});
Expand Down
Loading

0 comments on commit 5b35baa

Please sign in to comment.