mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Merge #116
116: Collapse comments upon join r=matklad a=aochagavia Todo: - [x] Write tests - [x] Resolve fixmes - [x] Implement `comment_start_length` using the parser I left a bunch of questions as fixmes. Can someone take a look at them? Also, I would love to use the parser to calculate the length of the leading characters in a comment (`//`, `///`, `//!`, `/*`), so any hints are greatly appreciated. Co-authored-by: Adolfo Ochagavía <aochagavia92@gmail.com> Co-authored-by: Adolfo Ochagavía <github@adolfo.ochagavia.xyz>
This commit is contained in:
commit
77e9bf9b5f
4 changed files with 199 additions and 58 deletions
|
@ -231,6 +231,24 @@ impl<'a> AstNode<'a> for CastExpr<'a> {
|
|||
|
||||
impl<'a> CastExpr<'a> {}
|
||||
|
||||
// Comment
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Comment<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<'a> AstNode<'a> for Comment<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
COMMENT => Some(Comment { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<'a> Comment<'a> {}
|
||||
|
||||
// Condition
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Condition<'a> {
|
||||
|
|
|
@ -99,6 +99,49 @@ impl<'a> Lifetime<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Comment<'a> {
|
||||
pub fn text(&self) -> SmolStr {
|
||||
self.syntax().leaf_text().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn flavor(&self) -> CommentFlavor {
|
||||
let text = self.text();
|
||||
if text.starts_with("///") {
|
||||
CommentFlavor::Doc
|
||||
} else if text.starts_with("//!") {
|
||||
CommentFlavor::ModuleDoc
|
||||
} else if text.starts_with("//") {
|
||||
CommentFlavor::Line
|
||||
} else {
|
||||
CommentFlavor::Multiline
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefix(&self) -> &'static str {
|
||||
self.flavor().prefix()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CommentFlavor {
|
||||
Line,
|
||||
Doc,
|
||||
ModuleDoc,
|
||||
Multiline
|
||||
}
|
||||
|
||||
impl CommentFlavor {
|
||||
pub fn prefix(&self) -> &'static str {
|
||||
use self::CommentFlavor::*;
|
||||
match *self {
|
||||
Line => "//",
|
||||
Doc => "///",
|
||||
ModuleDoc => "//!",
|
||||
Multiline => "/*"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Name<'a> {
|
||||
pub fn text(&self) -> SmolStr {
|
||||
let ident = self.syntax().first_child()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue