1087: More future-proof comment kind r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-04-02 09:34:23 +00:00
commit 370dbc7867
4 changed files with 59 additions and 37 deletions

View file

@ -145,7 +145,10 @@ fn contiguous_range_for_comment<'a>(
visited.insert(first);
// Only fold comments of the same flavor
let group_flavor = first.flavor();
let group_kind = first.kind();
if !group_kind.shape.is_line() {
return None;
}
let mut last = first;
for element in first.syntax().siblings_with_tokens(Direction::Next) {
@ -158,7 +161,7 @@ fn contiguous_range_for_comment<'a>(
}
}
if let Some(c) = ast::Comment::cast(token) {
if c.flavor() == group_flavor {
if c.kind() == group_kind {
visited.insert(c);
last = c;
continue;

View file

@ -15,7 +15,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
.left_biased()
.and_then(ast::Comment::cast)?;
if comment.flavor() == ast::CommentFlavor::Multiline {
if comment.kind().shape.is_block() {
return None;
}

View file

@ -21,52 +21,71 @@ impl<'a> AstToken<'a> for Comment<'a> {
}
impl<'a> Comment<'a> {
pub fn flavor(&self) -> CommentFlavor {
let text = self.text();
if text.starts_with("///") {
CommentFlavor::OuterDoc
} else if text.starts_with("//!") {
CommentFlavor::InnerDoc
} else if text.starts_with("//") {
CommentFlavor::Line
} else {
CommentFlavor::Multiline
}
}
pub fn is_doc_comment(&self) -> bool {
self.flavor().is_doc_comment()
pub fn kind(&self) -> CommentKind {
kind_by_prefix(self.text())
}
pub fn prefix(&self) -> &'static str {
self.flavor().prefix()
prefix_by_kind(self.kind())
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum CommentFlavor {
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct CommentKind {
pub shape: CommentShape,
pub doc: Option<CommentPlacement>,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CommentShape {
Line,
OuterDoc,
InnerDoc,
Multiline,
Block,
}
impl CommentFlavor {
pub fn prefix(&self) -> &'static str {
match *self {
CommentFlavor::Line => "//",
CommentFlavor::OuterDoc => "///",
CommentFlavor::InnerDoc => "//!",
CommentFlavor::Multiline => "/*",
}
impl CommentShape {
pub fn is_line(self) -> bool {
self == CommentShape::Line
}
pub fn is_doc_comment(&self) -> bool {
match self {
CommentFlavor::OuterDoc | CommentFlavor::InnerDoc => true,
_ => false,
pub fn is_block(self) -> bool {
self == CommentShape::Block
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CommentPlacement {
Inner,
Outer,
}
const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = {
use {CommentShape::*, CommentPlacement::*};
&[
("///", 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 }),
]
};
fn kind_by_prefix(text: &str) -> CommentKind {
for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() {
if text.starts_with(prefix) {
return *kind;
}
}
panic!("bad comment text: {:?}", text)
}
fn prefix_by_kind(kind: CommentKind) -> &'static str {
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
if *k == kind {
return prefix;
}
}
unreachable!()
}
pub struct Whitespace<'a>(SyntaxToken<'a>);

View file

@ -111,7 +111,7 @@ pub trait DocCommentsOwner: AstNode {
let mut has_comments = false;
let docs = self
.doc_comments()
.filter(|comment| comment.is_doc_comment())
.filter(|comment| comment.kind().doc.is_some())
.map(|comment| {
has_comments = true;
let prefix_len = comment.prefix().len();