From 81ac99f60a3b847aa2b566af54e8ffa0b3b1a147 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Nov 2020 11:45:18 +0100 Subject: Simplify code --- crates/syntax/src/ast/token_ext.rs | 51 +++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index e4e512f2e..2661c753e 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -14,16 +14,15 @@ use crate::{ impl ast::Comment { pub fn kind(&self) -> CommentKind { - kind_by_prefix(self.text()) + CommentKind::from_text(self.text()) } pub fn prefix(&self) -> &'static str { - for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() { - if *k == self.kind() && self.text().starts_with(prefix) { - return prefix; - } - } - unreachable!() + let &(prefix, _kind) = CommentKind::BY_PREFIX + .iter() + .find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix)) + .unwrap(); + prefix } } @@ -55,29 +54,25 @@ pub enum CommentPlacement { Outer, } -const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = { - use {CommentPlacement::*, CommentShape::*}; - &[ - ("////", CommentKind { shape: Line, doc: None }), - ("///", CommentKind { shape: Line, doc: Some(Outer) }), - ("//!", CommentKind { shape: Line, doc: Some(Inner) }), - ("/**", CommentKind { shape: Block, doc: Some(Outer) }), - ("/*!", CommentKind { shape: Block, doc: Some(Inner) }), - ("//", CommentKind { shape: Line, doc: None }), - ("/*", CommentKind { shape: Block, doc: None }), - ] -}; +impl CommentKind { + const BY_PREFIX: [(&'static str, CommentKind); 8] = [ + ("/**/", CommentKind { shape: CommentShape::Block, doc: None }), + ("////", CommentKind { shape: CommentShape::Line, doc: None }), + ("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }), + ("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }), + ("/**", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Outer) }), + ("/*!", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Inner) }), + ("//", CommentKind { shape: CommentShape::Line, doc: None }), + ("/*", CommentKind { shape: CommentShape::Block, doc: None }), + ]; -fn kind_by_prefix(text: &str) -> CommentKind { - if text == "/**/" { - return CommentKind { shape: CommentShape::Block, doc: None }; - } - for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() { - if text.starts_with(prefix) { - return *kind; - } + pub(crate) fn from_text(text: &str) -> CommentKind { + let &(_prefix, kind) = CommentKind::BY_PREFIX + .iter() + .find(|&(prefix, _kind)| text.starts_with(prefix)) + .unwrap(); + kind } - panic!("bad comment text: {:?}", text) } impl ast::Whitespace { -- cgit v1.2.3 From a27186636d030c847193ab21c59a1857a1e93785 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Nov 2020 12:09:12 +0100 Subject: Fix attachment of inner doc comments --- crates/syntax/src/ast.rs | 19 +++++++++++++++++-- crates/syntax/src/parsing/text_tree_sink.rs | 27 +++++++++++++-------------- 2 files changed, 30 insertions(+), 16 deletions(-) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index 8a0e3d27b..7844f9ed6 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs @@ -115,10 +115,10 @@ fn test_doc_comment_none() { } #[test] -fn test_doc_comment_of_items() { +fn test_outer_doc_comment_of_items() { let file = SourceFile::parse( r#" - //! doc + /// doc // non-doc mod foo {} "#, @@ -129,6 +129,21 @@ fn test_doc_comment_of_items() { assert_eq!("doc", module.doc_comment_text().unwrap()); } +#[test] +fn test_inner_doc_comment_of_items() { + let file = SourceFile::parse( + r#" + //! doc + // non-doc + mod foo {} + "#, + ) + .ok() + .unwrap(); + let module = file.syntax().descendants().find_map(Module::cast).unwrap(); + assert!(module.doc_comment_text().is_none()); +} + #[test] fn test_doc_comment_of_statics() { let file = SourceFile::parse( diff --git a/crates/syntax/src/parsing/text_tree_sink.rs b/crates/syntax/src/parsing/text_tree_sink.rs index c1b5f246d..997bc5d28 100644 --- a/crates/syntax/src/parsing/text_tree_sink.rs +++ b/crates/syntax/src/parsing/text_tree_sink.rs @@ -5,6 +5,7 @@ use std::mem; use parser::{ParseError, TreeSink}; use crate::{ + ast, parsing::Token, syntax_node::GreenNode, SmolStr, SyntaxError, @@ -153,24 +154,22 @@ fn n_attached_trivias<'a>( while let Some((i, (kind, text))) = trivias.next() { match kind { - WHITESPACE => { - if text.contains("\n\n") { - // we check whether the next token is a doc-comment - // and skip the whitespace in this case - if let Some((peek_kind, peek_text)) = - trivias.peek().map(|(_, pair)| pair) - { - if *peek_kind == COMMENT - && peek_text.starts_with("///") - && !peek_text.starts_with("////") - { - continue; - } + WHITESPACE if text.contains("\n\n") => { + // we check whether the next token is a doc-comment + // and skip the whitespace in this case + if let Some((COMMENT, peek_text)) = trivias.peek().map(|(_, pair)| pair) { + let comment_kind = ast::CommentKind::from_text(peek_text); + if comment_kind.doc == Some(ast::CommentPlacement::Outer) { + continue; } - break; } + break; } COMMENT => { + let comment_kind = ast::CommentKind::from_text(text); + if comment_kind.doc == Some(ast::CommentPlacement::Inner) { + break; + } res = i + 1; } _ => (), -- cgit v1.2.3