mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +00:00
Merge #5178
5178: Fold multiline calls r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
8943c2cb97
4 changed files with 329 additions and 318 deletions
|
@ -173,12 +173,19 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use expect::{expect, Expect};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use insta::assert_debug_snapshot;
|
|
||||||
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
|
let file = SourceFile::parse(ra_fixture).ok().unwrap();
|
||||||
|
let structure = file_structure(&file);
|
||||||
|
expect.assert_debug_eq(&structure)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_file_structure() {
|
fn test_file_structure() {
|
||||||
let file = SourceFile::parse(
|
check(
|
||||||
r#"
|
r#"
|
||||||
struct Foo {
|
struct Foo {
|
||||||
x: i32
|
x: i32
|
||||||
|
@ -223,12 +230,7 @@ fn obsolete() {}
|
||||||
#[deprecated(note = "for awhile")]
|
#[deprecated(note = "for awhile")]
|
||||||
fn very_obsolete() {}
|
fn very_obsolete() {}
|
||||||
"#,
|
"#,
|
||||||
)
|
expect![[r#"
|
||||||
.ok()
|
|
||||||
.unwrap();
|
|
||||||
let structure = file_structure(&file);
|
|
||||||
assert_debug_snapshot!(structure,
|
|
||||||
@r###"
|
|
||||||
[
|
[
|
||||||
StructureNode {
|
StructureNode {
|
||||||
parent: None,
|
parent: None,
|
||||||
|
@ -432,7 +434,7 @@ fn very_obsolete() {}
|
||||||
deprecated: true,
|
deprecated: true,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
"###
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ pub enum FoldKind {
|
||||||
Imports,
|
Imports,
|
||||||
Mods,
|
Mods,
|
||||||
Block,
|
Block,
|
||||||
|
ArgList,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -83,6 +84,7 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
|
||||||
match kind {
|
match kind {
|
||||||
COMMENT => Some(FoldKind::Comment),
|
COMMENT => Some(FoldKind::Comment),
|
||||||
USE_ITEM => Some(FoldKind::Imports),
|
USE_ITEM => Some(FoldKind::Imports),
|
||||||
|
ARG_LIST => Some(FoldKind::ArgList),
|
||||||
RECORD_FIELD_DEF_LIST
|
RECORD_FIELD_DEF_LIST
|
||||||
| RECORD_FIELD_PAT_LIST
|
| RECORD_FIELD_PAT_LIST
|
||||||
| ITEM_LIST
|
| ITEM_LIST
|
||||||
|
@ -196,89 +198,85 @@ fn contiguous_range_for_comment(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use test_utils::extract_tags;
|
||||||
use test_utils::extract_ranges;
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn check(ra_fixture: &str) {
|
||||||
|
let (ranges, text) = extract_tags(ra_fixture, "fold");
|
||||||
|
|
||||||
fn do_check(text: &str, fold_kinds: &[FoldKind]) {
|
|
||||||
let (ranges, text) = extract_ranges(text, "fold");
|
|
||||||
let parse = SourceFile::parse(&text);
|
let parse = SourceFile::parse(&text);
|
||||||
let folds = folding_ranges(&parse.tree());
|
let folds = folding_ranges(&parse.tree());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
folds.len(),
|
folds.len(),
|
||||||
ranges.len(),
|
ranges.len(),
|
||||||
"The amount of folds is different than the expected amount"
|
"The amount of folds is different than the expected amount"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
|
||||||
folds.len(),
|
for (fold, (range, attr)) in folds.iter().zip(ranges.into_iter()) {
|
||||||
fold_kinds.len(),
|
|
||||||
"The amount of fold kinds is different than the expected amount"
|
|
||||||
);
|
|
||||||
for ((fold, range), fold_kind) in
|
|
||||||
folds.iter().zip(ranges.into_iter()).zip(fold_kinds.iter())
|
|
||||||
{
|
|
||||||
assert_eq!(fold.range.start(), range.start());
|
assert_eq!(fold.range.start(), range.start());
|
||||||
assert_eq!(fold.range.end(), range.end());
|
assert_eq!(fold.range.end(), range.end());
|
||||||
assert_eq!(&fold.kind, fold_kind);
|
|
||||||
|
let kind = match fold.kind {
|
||||||
|
FoldKind::Comment => "comment",
|
||||||
|
FoldKind::Imports => "imports",
|
||||||
|
FoldKind::Mods => "mods",
|
||||||
|
FoldKind::Block => "block",
|
||||||
|
FoldKind::ArgList => "arglist",
|
||||||
|
};
|
||||||
|
assert_eq!(kind, &attr.unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fold_comments() {
|
fn test_fold_comments() {
|
||||||
let text = r#"
|
check(
|
||||||
<fold>// Hello
|
r#"
|
||||||
|
<fold comment>// Hello
|
||||||
// this is a multiline
|
// this is a multiline
|
||||||
// comment
|
// comment
|
||||||
//</fold>
|
//</fold>
|
||||||
|
|
||||||
// But this is not
|
// But this is not
|
||||||
|
|
||||||
fn main() <fold>{
|
fn main() <fold block>{
|
||||||
<fold>// We should
|
<fold comment>// We should
|
||||||
// also
|
// also
|
||||||
// fold
|
// fold
|
||||||
// this one.</fold>
|
// this one.</fold>
|
||||||
<fold>//! But this one is different
|
<fold comment>//! But this one is different
|
||||||
//! because it has another flavor</fold>
|
//! because it has another flavor</fold>
|
||||||
<fold>/* As does this
|
<fold comment>/* As does this
|
||||||
multiline comment */</fold>
|
multiline comment */</fold>
|
||||||
}</fold>"#;
|
}</fold>"#,
|
||||||
|
);
|
||||||
let fold_kinds = &[
|
|
||||||
FoldKind::Comment,
|
|
||||||
FoldKind::Block,
|
|
||||||
FoldKind::Comment,
|
|
||||||
FoldKind::Comment,
|
|
||||||
FoldKind::Comment,
|
|
||||||
];
|
|
||||||
do_check(text, fold_kinds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fold_imports() {
|
fn test_fold_imports() {
|
||||||
let text = r#"
|
check(
|
||||||
<fold>use std::<fold>{
|
r#"
|
||||||
|
<fold imports>use std::<fold block>{
|
||||||
str,
|
str,
|
||||||
vec,
|
vec,
|
||||||
io as iop
|
io as iop
|
||||||
}</fold>;</fold>
|
}</fold>;</fold>
|
||||||
|
|
||||||
fn main() <fold>{
|
fn main() <fold block>{
|
||||||
}</fold>"#;
|
}</fold>"#,
|
||||||
|
);
|
||||||
let folds = &[FoldKind::Imports, FoldKind::Block, FoldKind::Block];
|
|
||||||
do_check(text, folds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fold_mods() {
|
fn test_fold_mods() {
|
||||||
let text = r#"
|
check(
|
||||||
|
r#"
|
||||||
|
|
||||||
pub mod foo;
|
pub mod foo;
|
||||||
<fold>mod after_pub;
|
<fold mods>mod after_pub;
|
||||||
mod after_pub_next;</fold>
|
mod after_pub_next;</fold>
|
||||||
|
|
||||||
<fold>mod before_pub;
|
<fold mods>mod before_pub;
|
||||||
mod before_pub_next;</fold>
|
mod before_pub_next;</fold>
|
||||||
pub mod bar;
|
pub mod bar;
|
||||||
|
|
||||||
|
@ -286,90 +284,93 @@ mod not_folding_single;
|
||||||
pub mod foobar;
|
pub mod foobar;
|
||||||
pub not_folding_single_next;
|
pub not_folding_single_next;
|
||||||
|
|
||||||
<fold>#[cfg(test)]
|
<fold mods>#[cfg(test)]
|
||||||
mod with_attribute;
|
mod with_attribute;
|
||||||
mod with_attribute_next;</fold>
|
mod with_attribute_next;</fold>
|
||||||
|
|
||||||
fn main() <fold>{
|
fn main() <fold block>{
|
||||||
}</fold>"#;
|
}</fold>"#,
|
||||||
|
);
|
||||||
let folds = &[FoldKind::Mods, FoldKind::Mods, FoldKind::Mods, FoldKind::Block];
|
|
||||||
do_check(text, folds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fold_import_groups() {
|
fn test_fold_import_groups() {
|
||||||
let text = r#"
|
check(
|
||||||
<fold>use std::str;
|
r#"
|
||||||
|
<fold imports>use std::str;
|
||||||
use std::vec;
|
use std::vec;
|
||||||
use std::io as iop;</fold>
|
use std::io as iop;</fold>
|
||||||
|
|
||||||
<fold>use std::mem;
|
<fold imports>use std::mem;
|
||||||
use std::f64;</fold>
|
use std::f64;</fold>
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
// Some random comment
|
// Some random comment
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
fn main() <fold>{
|
fn main() <fold block>{
|
||||||
}</fold>"#;
|
}</fold>"#,
|
||||||
|
);
|
||||||
let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Block];
|
|
||||||
do_check(text, folds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fold_import_and_groups() {
|
fn test_fold_import_and_groups() {
|
||||||
let text = r#"
|
check(
|
||||||
<fold>use std::str;
|
r#"
|
||||||
|
<fold imports>use std::str;
|
||||||
use std::vec;
|
use std::vec;
|
||||||
use std::io as iop;</fold>
|
use std::io as iop;</fold>
|
||||||
|
|
||||||
<fold>use std::mem;
|
<fold imports>use std::mem;
|
||||||
use std::f64;</fold>
|
use std::f64;</fold>
|
||||||
|
|
||||||
<fold>use std::collections::<fold>{
|
<fold imports>use std::collections::<fold block>{
|
||||||
HashMap,
|
HashMap,
|
||||||
VecDeque,
|
VecDeque,
|
||||||
}</fold>;</fold>
|
}</fold>;</fold>
|
||||||
// Some random comment
|
// Some random comment
|
||||||
|
|
||||||
fn main() <fold>{
|
fn main() <fold block>{
|
||||||
}</fold>"#;
|
}</fold>"#,
|
||||||
|
);
|
||||||
let folds = &[
|
|
||||||
FoldKind::Imports,
|
|
||||||
FoldKind::Imports,
|
|
||||||
FoldKind::Imports,
|
|
||||||
FoldKind::Block,
|
|
||||||
FoldKind::Block,
|
|
||||||
];
|
|
||||||
do_check(text, folds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_folds_macros() {
|
fn test_folds_macros() {
|
||||||
let text = r#"
|
check(
|
||||||
macro_rules! foo <fold>{
|
r#"
|
||||||
|
macro_rules! foo <fold block>{
|
||||||
($($tt:tt)*) => { $($tt)* }
|
($($tt:tt)*) => { $($tt)* }
|
||||||
}</fold>
|
}</fold>
|
||||||
"#;
|
"#,
|
||||||
|
);
|
||||||
let folds = &[FoldKind::Block];
|
|
||||||
do_check(text, folds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fold_match_arms() {
|
fn test_fold_match_arms() {
|
||||||
let text = r#"
|
check(
|
||||||
fn main() <fold>{
|
r#"
|
||||||
match 0 <fold>{
|
fn main() <fold block>{
|
||||||
|
match 0 <fold block>{
|
||||||
0 => 0,
|
0 => 0,
|
||||||
_ => 1,
|
_ => 1,
|
||||||
}</fold>
|
}</fold>
|
||||||
}</fold>"#;
|
}</fold>"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let folds = &[FoldKind::Block, FoldKind::Block];
|
#[test]
|
||||||
do_check(text, folds);
|
fn fold_big_calls() {
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
fn main() <fold block>{
|
||||||
|
frobnicate<fold arglist>(
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
)</fold>
|
||||||
|
}</fold>
|
||||||
|
"#,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,7 +352,7 @@ pub(crate) fn folding_range(
|
||||||
let kind = match fold.kind {
|
let kind = match fold.kind {
|
||||||
FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
|
FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
|
||||||
FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
|
FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
|
||||||
FoldKind::Mods | FoldKind::Block => None,
|
FoldKind::Mods | FoldKind::Block | FoldKind::ArgList => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let range = range(line_index, fold.range);
|
let range = range(line_index, fold.range);
|
||||||
|
@ -685,32 +685,27 @@ pub(crate) fn runnable(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use test_utils::extract_ranges;
|
use ra_ide::Analysis;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn conv_fold_line_folding_only_fixup() {
|
fn conv_fold_line_folding_only_fixup() {
|
||||||
let text = r#"<fold>mod a;
|
let text = r#"mod a;
|
||||||
mod b;
|
mod b;
|
||||||
mod c;</fold>
|
mod c;
|
||||||
|
|
||||||
fn main() <fold>{
|
fn main() {
|
||||||
if cond <fold>{
|
if cond {
|
||||||
a::do_a();
|
a::do_a();
|
||||||
}</fold> else <fold>{
|
} else {
|
||||||
b::do_b();
|
b::do_b();
|
||||||
}</fold>
|
}
|
||||||
}</fold>"#;
|
}"#;
|
||||||
|
|
||||||
let (ranges, text) = extract_ranges(text, "fold");
|
let (analysis, file_id) = Analysis::from_single_file(text.to_string());
|
||||||
assert_eq!(ranges.len(), 4);
|
let folds = analysis.folding_ranges(file_id).unwrap();
|
||||||
let folds = vec![
|
assert_eq!(folds.len(), 4);
|
||||||
Fold { range: ranges[0], kind: FoldKind::Mods },
|
|
||||||
Fold { range: ranges[1], kind: FoldKind::Block },
|
|
||||||
Fold { range: ranges[2], kind: FoldKind::Block },
|
|
||||||
Fold { range: ranges[3], kind: FoldKind::Block },
|
|
||||||
];
|
|
||||||
|
|
||||||
let line_index = LineIndex::new(&text);
|
let line_index = LineIndex::new(&text);
|
||||||
let converted: Vec<lsp_types::FoldingRange> =
|
let converted: Vec<lsp_types::FoldingRange> =
|
||||||
|
|
|
@ -118,8 +118,8 @@ pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts ranges, marked with `<tag> </tag>` pairs from the `text`
|
/// Extracts ranges, marked with `<tag> </tag>` pairs from the `text`
|
||||||
pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
|
pub fn extract_tags(mut text: &str, tag: &str) -> (Vec<(TextRange, Option<String>)>, String) {
|
||||||
let open = format!("<{}>", tag);
|
let open = format!("<{}", tag);
|
||||||
let close = format!("</{}>", tag);
|
let close = format!("</{}>", tag);
|
||||||
let mut ranges = Vec::new();
|
let mut ranges = Vec::new();
|
||||||
let mut res = String::new();
|
let mut res = String::new();
|
||||||
|
@ -134,22 +134,35 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
|
||||||
res.push_str(&text[..i]);
|
res.push_str(&text[..i]);
|
||||||
text = &text[i..];
|
text = &text[i..];
|
||||||
if text.starts_with(&open) {
|
if text.starts_with(&open) {
|
||||||
text = &text[open.len()..];
|
let close_open = text.find('>').unwrap();
|
||||||
|
let attr = text[open.len()..close_open].trim();
|
||||||
|
let attr = if attr.is_empty() { None } else { Some(attr.to_string()) };
|
||||||
|
text = &text[close_open + '>'.len_utf8()..];
|
||||||
let from = TextSize::of(&res);
|
let from = TextSize::of(&res);
|
||||||
stack.push(from);
|
stack.push((from, attr));
|
||||||
} else if text.starts_with(&close) {
|
} else if text.starts_with(&close) {
|
||||||
text = &text[close.len()..];
|
text = &text[close.len()..];
|
||||||
let from = stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
|
let (from, attr) =
|
||||||
|
stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
|
||||||
let to = TextSize::of(&res);
|
let to = TextSize::of(&res);
|
||||||
ranges.push(TextRange::new(from, to));
|
ranges.push((TextRange::new(from, to), attr));
|
||||||
|
} else {
|
||||||
|
res.push('<');
|
||||||
|
text = &text['<'.len_utf8()..];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert!(stack.is_empty(), "unmatched <{}>", tag);
|
assert!(stack.is_empty(), "unmatched <{}>", tag);
|
||||||
ranges.sort_by_key(|r| (r.start(), r.end()));
|
ranges.sort_by_key(|r| (r.0.start(), r.0.end()));
|
||||||
(ranges, res)
|
(ranges, res)
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_extract_tags() {
|
||||||
|
let (tags, text) = extract_tags(r#"<tag fn>fn <tag>main</tag>() {}</tag>"#, "tag");
|
||||||
|
let actual = tags.into_iter().map(|(range, attr)| (&text[range], attr)).collect::<Vec<_>>();
|
||||||
|
assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]);
|
||||||
|
}
|
||||||
|
|
||||||
/// Inserts `<|>` marker into the `text` at `offset`.
|
/// Inserts `<|>` marker into the `text` at `offset`.
|
||||||
pub fn add_cursor(text: &str, offset: TextSize) -> String {
|
pub fn add_cursor(text: &str, offset: TextSize) -> String {
|
||||||
|
|
Loading…
Reference in a new issue