Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide/src/folding_ranges.rs')
-rwxr-xr-xcrates/ide/src/folding_ranges.rs105
1 files changed, 60 insertions, 45 deletions
diff --git a/crates/ide/src/folding_ranges.rs b/crates/ide/src/folding_ranges.rs
index c081796d07..ac64413eff 100755
--- a/crates/ide/src/folding_ranges.rs
+++ b/crates/ide/src/folding_ranges.rs
@@ -48,7 +48,6 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
let mut res = vec![];
let mut visited_comments = FxHashSet::default();
let mut visited_nodes = FxHashSet::default();
- let mut merged_fn_bodies = FxHashSet::default();
// regions can be nested, here is a LIFO buffer
let mut region_starts: Vec<TextSize> = vec![];
@@ -62,29 +61,29 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
};
if is_multiline {
// for the func with multiline param list
- if matches!(element.kind(), FN) {
- if let NodeOrToken::Node(node) = &element {
- if let Some(fn_node) = ast::Fn::cast(node.clone()) {
- if !fn_node
- .param_list()
- .map(|param_list| param_list.syntax().text().contains_char('\n'))
- .unwrap_or(false)
- {
- continue;
- }
+ if matches!(element.kind(), FN)
+ && let NodeOrToken::Node(node) = &element
+ && let Some(fn_node) = ast::Fn::cast(node.clone())
+ {
+ if !fn_node
+ .param_list()
+ .map(|param_list| param_list.syntax().text().contains_char('\n'))
+ .unwrap_or(false)
+ {
+ continue;
+ }
- if let Some(body) = fn_node.body() {
- res.push(Fold {
- range: TextRange::new(
- node.text_range().start(),
- node.text_range().end(),
- ),
- kind: FoldKind::Function,
- });
- merged_fn_bodies.insert(body.syntax().text_range());
- continue;
- }
- }
+ if fn_node.body().is_some() {
+ // Get the actual start of the function (excluding doc comments)
+ let fn_start = fn_node
+ .fn_token()
+ .map(|token| token.text_range().start())
+ .unwrap_or(node.text_range().start());
+ res.push(Fold {
+ range: TextRange::new(fn_start, node.text_range().end()),
+ kind: FoldKind::Function,
+ });
+ continue;
}
}
res.push(Fold { range: element.text_range(), kind });
@@ -120,14 +119,13 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
match_ast! {
match node {
ast::Module(module) => {
- if module.item_list().is_none() {
- if let Some(range) = contiguous_range_for_item_group(
+ if module.item_list().is_none()
+ && let Some(range) = contiguous_range_for_item_group(
module,
&mut visited_nodes,
) {
res.push(Fold { range, kind: FoldKind::Modules })
}
- }
},
ast::Use(use_) => {
if let Some(range) = contiguous_range_for_item_group(use_, &mut visited_nodes) {
@@ -212,11 +210,11 @@ where
for element in first.syntax().siblings_with_tokens(Direction::Next) {
let node = match element {
NodeOrToken::Token(token) => {
- if let Some(ws) = ast::Whitespace::cast(token) {
- if !ws.spans_multiple_lines() {
- // Ignore whitespace without blank lines
- continue;
- }
+ if let Some(ws) = ast::Whitespace::cast(token)
+ && !ws.spans_multiple_lines()
+ {
+ // Ignore whitespace without blank lines
+ continue;
}
// There is a blank line or another token, which means that the
// group ends here
@@ -270,21 +268,21 @@ fn contiguous_range_for_comment(
for element in first.syntax().siblings_with_tokens(Direction::Next) {
match element {
NodeOrToken::Token(token) => {
- if let Some(ws) = ast::Whitespace::cast(token.clone()) {
- if !ws.spans_multiple_lines() {
- // Ignore whitespace without blank lines
- continue;
- }
+ if let Some(ws) = ast::Whitespace::cast(token.clone())
+ && !ws.spans_multiple_lines()
+ {
+ // Ignore whitespace without blank lines
+ continue;
}
- if let Some(c) = ast::Comment::cast(token) {
- if c.kind() == group_kind {
- let text = c.text().trim_start();
- // regions are not real comments
- if !(text.starts_with(REGION_START) || text.starts_with(REGION_END)) {
- visited.insert(c.clone());
- last = c;
- continue;
- }
+ if let Some(c) = ast::Comment::cast(token)
+ && c.kind() == group_kind
+ {
+ let text = c.text().trim_start();
+ // regions are not real comments
+ if !(text.starts_with(REGION_START) || text.starts_with(REGION_END)) {
+ visited.insert(c.clone());
+ last = c;
+ continue;
}
}
// The comment group ends because either:
@@ -690,4 +688,21 @@ type Foo<T, U> = foo<fold arglist><
"#,
)
}
+
+ #[test]
+ fn test_fold_doc_comments_with_multiline_paramlist_function() {
+ check(
+ r#"
+<fold comment>/// A very very very very very very very very very very very very very very very
+/// very very very long description</fold>
+<fold function>fn foo<fold arglist>(
+ very_long_parameter_name: u32,
+ another_very_long_parameter_name: u32,
+ third_very_long_param: u32,
+)</fold> <fold block>{
+ todo!()
+}</fold></fold>
+"#,
+ );
+ }
}