mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Only fold groups of similar comments
This commit is contained in:
parent
4b3737510b
commit
c5069eeef5
2 changed files with 31 additions and 28 deletions
|
@ -4,7 +4,7 @@ use ra_syntax::{
|
||||||
ast,
|
ast,
|
||||||
AstNode,
|
AstNode,
|
||||||
File, TextRange, SyntaxNodeRef,
|
File, TextRange, SyntaxNodeRef,
|
||||||
SyntaxKind,
|
SyntaxKind::{self, *},
|
||||||
Direction,
|
Direction,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ 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 group_members = FxHashSet::default();
|
let mut visited_comments = FxHashSet::default();
|
||||||
|
|
||||||
for node in file.syntax().descendants() {
|
for node in file.syntax().descendants() {
|
||||||
// Fold items that span multiple lines
|
// Fold items that span multiple lines
|
||||||
|
@ -32,17 +32,13 @@ pub fn folding_ranges(file: &File) -> Vec<Fold> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also fold item *groups* that span multiple lines
|
// Also fold groups of comments
|
||||||
|
if visited_comments.contains(&node) {
|
||||||
// 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 node.kind() == COMMENT {
|
||||||
if let Some(kind) = fold_kind(node.kind()) {
|
contiguous_range_for_comment(node, &mut visited_comments)
|
||||||
contiguous_range_for_group(node.kind(), node, &mut group_members)
|
.map(|range| res.push(Fold { range, kind: FoldKind::Comment }));
|
||||||
.map(|range| res.push(Fold { range, kind }));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,8 +47,8 @@ pub fn folding_ranges(file: &File) -> Vec<Fold> {
|
||||||
|
|
||||||
fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
|
fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
|
||||||
match kind {
|
match kind {
|
||||||
SyntaxKind::COMMENT => Some(FoldKind::Comment),
|
COMMENT => Some(FoldKind::Comment),
|
||||||
SyntaxKind::USE_ITEM => Some(FoldKind::Imports),
|
USE_ITEM => Some(FoldKind::Imports),
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,17 +71,17 @@ fn has_newline(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn contiguous_range_for_group<'a>(
|
fn contiguous_range_for_comment<'a>(
|
||||||
group_kind: SyntaxKind,
|
|
||||||
first: SyntaxNodeRef<'a>,
|
first: SyntaxNodeRef<'a>,
|
||||||
visited: &mut FxHashSet<SyntaxNodeRef<'a>>,
|
visited: &mut FxHashSet<SyntaxNodeRef<'a>>,
|
||||||
) -> Option<TextRange> {
|
) -> Option<TextRange> {
|
||||||
visited.insert(first);
|
visited.insert(first);
|
||||||
|
|
||||||
let mut last = first;
|
// Only fold comments of the same flavor
|
||||||
|
let group_flavor = ast::Comment::cast(first)?.flavor();
|
||||||
|
|
||||||
|
let mut last = first;
|
||||||
for node in first.siblings(Direction::Next) {
|
for node in first.siblings(Direction::Next) {
|
||||||
visited.insert(node);
|
|
||||||
if let Some(ws) = ast::Whitespace::cast(node) {
|
if let Some(ws) = ast::Whitespace::cast(node) {
|
||||||
// There is a blank line, which means the group ends here
|
// There is a blank line, which means the group ends here
|
||||||
if ws.count_newlines_lazy().take(2).count() == 2 {
|
if ws.count_newlines_lazy().take(2).count() == 2 {
|
||||||
|
@ -96,13 +92,18 @@ fn contiguous_range_for_group<'a>(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The group ends when an element of a different kind is reached
|
match ast::Comment::cast(node) {
|
||||||
if node.kind() != group_kind {
|
Some(next_comment) if next_comment.flavor() == group_flavor => {
|
||||||
break;
|
visited.insert(node);
|
||||||
|
last = node;
|
||||||
|
}
|
||||||
|
// The comment group ends because either:
|
||||||
|
// * An element of a different kind was reached
|
||||||
|
// * A comment of a different flavor was reached
|
||||||
|
_ => {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep track of the last node in the group
|
|
||||||
last = node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if first != last {
|
if first != last {
|
||||||
|
@ -152,9 +153,11 @@ fn main() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fold_imports() {
|
fn test_fold_imports() {
|
||||||
let text = r#"
|
let text = r#"
|
||||||
use std::str;
|
use std::{
|
||||||
use std::vec;
|
str,
|
||||||
use std::io as iop;
|
vec,
|
||||||
|
io as iop
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
}"#;
|
}"#;
|
||||||
|
@ -163,7 +166,7 @@ fn main() {
|
||||||
let folds = folding_ranges(&file);
|
let folds = folding_ranges(&file);
|
||||||
assert_eq!(folds.len(), 1);
|
assert_eq!(folds.len(), 1);
|
||||||
assert_eq!(folds[0].range.start(), 1.into());
|
assert_eq!(folds[0].range.start(), 1.into());
|
||||||
assert_eq!(folds[0].range.end(), 48.into());
|
assert_eq!(folds[0].range.end(), 46.into());
|
||||||
assert_eq!(folds[0].kind, FoldKind::Imports);
|
assert_eq!(folds[0].kind, FoldKind::Imports);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -130,7 +130,7 @@ impl<'a> Comment<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum CommentFlavor {
|
pub enum CommentFlavor {
|
||||||
Line,
|
Line,
|
||||||
Doc,
|
Doc,
|
||||||
|
|
Loading…
Reference in a new issue