mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Cleanup fold code and split logic to fold single elements
This commit is contained in:
parent
ee0a6bf053
commit
4b3737510b
4 changed files with 115 additions and 43 deletions
|
@ -22,65 +22,96 @@ pub struct Fold {
|
||||||
|
|
||||||
pub fn folding_ranges(file: &File) -> Vec<Fold> {
|
pub fn folding_ranges(file: &File) -> Vec<Fold> {
|
||||||
let mut res = vec![];
|
let mut res = vec![];
|
||||||
let mut visited = FxHashSet::default();
|
let mut group_members = FxHashSet::default();
|
||||||
|
|
||||||
for node in file.syntax().descendants() {
|
for node in file.syntax().descendants() {
|
||||||
if visited.contains(&node) {
|
// Fold items that span multiple lines
|
||||||
|
if let Some(kind) = fold_kind(node.kind()) {
|
||||||
|
if has_newline(node) {
|
||||||
|
res.push(Fold { range: node.range(), kind });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also fold item *groups* that span multiple lines
|
||||||
|
|
||||||
|
// Note: we need to skip elements of the group that we have already visited,
|
||||||
|
// otherwise there will be folds for the whole group and for its sub groups
|
||||||
|
if group_members.contains(&node) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(comment) = ast::Comment::cast(node) {
|
if let Some(kind) = fold_kind(node.kind()) {
|
||||||
// Multiline comments (`/* ... */`) can only be folded if they span multiple lines
|
contiguous_range_for_group(node.kind(), node, &mut group_members)
|
||||||
let range = if let ast::CommentFlavor::Multiline = comment.flavor() {
|
.map(|range| res.push(Fold { range, kind }));
|
||||||
if comment.text().contains('\n') {
|
|
||||||
Some(comment.syntax().range())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
contiguous_range_for(SyntaxKind::COMMENT, node, &mut visited)
|
|
||||||
};
|
|
||||||
|
|
||||||
range.map(|range| res.push(Fold { range, kind: FoldKind::Comment }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let SyntaxKind::USE_ITEM = node.kind() {
|
|
||||||
contiguous_range_for(SyntaxKind::USE_ITEM, node, &mut visited)
|
|
||||||
.map(|range| res.push(Fold { range, kind: FoldKind::Imports}));
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn contiguous_range_for<'a>(
|
fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
|
||||||
kind: SyntaxKind,
|
match kind {
|
||||||
node: SyntaxNodeRef<'a>,
|
SyntaxKind::COMMENT => Some(FoldKind::Comment),
|
||||||
visited: &mut FxHashSet<SyntaxNodeRef<'a>>,
|
SyntaxKind::USE_ITEM => Some(FoldKind::Imports),
|
||||||
) -> Option<TextRange> {
|
_ => None
|
||||||
visited.insert(node);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let left = node;
|
fn has_newline(
|
||||||
let mut right = node;
|
node: SyntaxNodeRef,
|
||||||
for node in node.siblings(Direction::Next) {
|
) -> bool {
|
||||||
visited.insert(node);
|
for descendant in node.descendants() {
|
||||||
match node.kind() {
|
if let Some(ws) = ast::Whitespace::cast(descendant) {
|
||||||
SyntaxKind::WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (),
|
if ws.has_newlines() {
|
||||||
k => {
|
return true;
|
||||||
if k == kind {
|
}
|
||||||
right = node
|
} else if let Some(comment) = ast::Comment::cast(descendant) {
|
||||||
} else {
|
if comment.has_newlines() {
|
||||||
break;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if left != right {
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn contiguous_range_for_group<'a>(
|
||||||
|
group_kind: SyntaxKind,
|
||||||
|
first: SyntaxNodeRef<'a>,
|
||||||
|
visited: &mut FxHashSet<SyntaxNodeRef<'a>>,
|
||||||
|
) -> Option<TextRange> {
|
||||||
|
visited.insert(first);
|
||||||
|
|
||||||
|
let mut last = first;
|
||||||
|
|
||||||
|
for node in first.siblings(Direction::Next) {
|
||||||
|
visited.insert(node);
|
||||||
|
if let Some(ws) = ast::Whitespace::cast(node) {
|
||||||
|
// There is a blank line, which means the group ends here
|
||||||
|
if ws.count_newlines_lazy().take(2).count() == 2 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore whitespace without blank lines
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The group ends when an element of a different kind is reached
|
||||||
|
if node.kind() != group_kind {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep track of the last node in the group
|
||||||
|
last = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
if first != last {
|
||||||
Some(TextRange::from_to(
|
Some(TextRange::from_to(
|
||||||
left.range().start(),
|
first.range().start(),
|
||||||
right.range().end(),
|
last.range().end(),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
|
// The group consists of only one element, therefore it cannot be folded
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2193,3 +2193,21 @@ impl<'a> WhileExpr<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Whitespace
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct Whitespace<'a> {
|
||||||
|
syntax: SyntaxNodeRef<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AstNode<'a> for Whitespace<'a> {
|
||||||
|
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||||
|
match syntax.kind() {
|
||||||
|
WHITESPACE => Some(Whitespace { syntax }),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Whitespace<'a> {}
|
||||||
|
|
||||||
|
|
|
@ -100,8 +100,8 @@ impl<'a> Lifetime<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Comment<'a> {
|
impl<'a> Comment<'a> {
|
||||||
pub fn text(&self) -> SmolStr {
|
pub fn text(&self) -> &SmolStr {
|
||||||
self.syntax().leaf_text().unwrap().clone()
|
self.syntax().leaf_text().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn flavor(&self) -> CommentFlavor {
|
pub fn flavor(&self) -> CommentFlavor {
|
||||||
|
@ -120,6 +120,14 @@ impl<'a> Comment<'a> {
|
||||||
pub fn prefix(&self) -> &'static str {
|
pub fn prefix(&self) -> &'static str {
|
||||||
self.flavor().prefix()
|
self.flavor().prefix()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
|
||||||
|
self.text().chars().filter(|&c| c == '\n').map(|_| &())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_newlines(&self) -> bool {
|
||||||
|
self.count_newlines_lazy().count() > 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -142,6 +150,20 @@ impl CommentFlavor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> Whitespace<'a> {
|
||||||
|
pub fn text(&self) -> &SmolStr {
|
||||||
|
&self.syntax().leaf_text().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
|
||||||
|
self.text().chars().filter(|&c| c == '\n').map(|_| &())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_newlines(&self) -> bool {
|
||||||
|
self.count_newlines_lazy().count() > 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Name<'a> {
|
impl<'a> Name<'a> {
|
||||||
pub fn text(&self) -> SmolStr {
|
pub fn text(&self) -> SmolStr {
|
||||||
let ident = self.syntax().first_child()
|
let ident = self.syntax().first_child()
|
||||||
|
|
|
@ -538,5 +538,6 @@ Grammar(
|
||||||
options: [ "NameRef" ]
|
options: [ "NameRef" ]
|
||||||
),
|
),
|
||||||
"Comment": (),
|
"Comment": (),
|
||||||
|
"Whitespace": (),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue