rust-analyzer/crates/ra_syntax/src/ast/tokens.rs

93 lines
2.1 KiB
Rust
Raw Normal View History

2019-04-02 07:23:18 +00:00
use crate::{
SyntaxToken,
SyntaxKind::{COMMENT, WHITESPACE},
ast::AstToken,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Comment<'a>(SyntaxToken<'a>);
impl<'a> AstToken<'a> for Comment<'a> {
fn cast(token: SyntaxToken<'a>) -> Option<Self> {
if token.kind() == COMMENT {
Some(Comment(token))
} else {
None
}
}
fn syntax(&self) -> SyntaxToken<'a> {
self.0
}
}
impl<'a> Comment<'a> {
pub fn flavor(&self) -> CommentFlavor {
let text = self.text();
if text.starts_with("///") {
2019-04-02 07:48:59 +00:00
CommentFlavor::OuterDoc
2019-04-02 07:23:18 +00:00
} else if text.starts_with("//!") {
2019-04-02 07:48:59 +00:00
CommentFlavor::InnerDoc
2019-04-02 07:23:18 +00:00
} else if text.starts_with("//") {
CommentFlavor::Line
} else {
CommentFlavor::Multiline
}
}
pub fn is_doc_comment(&self) -> bool {
self.flavor().is_doc_comment()
}
pub fn prefix(&self) -> &'static str {
self.flavor().prefix()
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum CommentFlavor {
Line,
2019-04-02 07:48:59 +00:00
OuterDoc,
InnerDoc,
2019-04-02 07:23:18 +00:00
Multiline,
}
impl CommentFlavor {
pub fn prefix(&self) -> &'static str {
match *self {
2019-04-02 07:48:59 +00:00
CommentFlavor::Line => "//",
CommentFlavor::OuterDoc => "///",
CommentFlavor::InnerDoc => "//!",
CommentFlavor::Multiline => "/*",
2019-04-02 07:23:18 +00:00
}
}
pub fn is_doc_comment(&self) -> bool {
match self {
2019-04-02 07:48:59 +00:00
CommentFlavor::OuterDoc | CommentFlavor::InnerDoc => true,
2019-04-02 07:23:18 +00:00
_ => false,
}
}
}
pub struct Whitespace<'a>(SyntaxToken<'a>);
impl<'a> AstToken<'a> for Whitespace<'a> {
fn cast(token: SyntaxToken<'a>) -> Option<Self> {
if token.kind() == WHITESPACE {
Some(Whitespace(token))
} else {
None
}
}
fn syntax(&self) -> SyntaxToken<'a> {
self.0
}
}
impl<'a> Whitespace<'a> {
pub fn spans_multiple_lines(&self) -> bool {
let text = self.text();
text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
}
}