aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/matching_brace.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-13 16:59:50 +0100
committerGitHub <[email protected]>2020-08-13 16:59:50 +0100
commit018a6cac072767dfd630c22e6d9ce134b7bb09af (patch)
tree4293492e643f9a604c5f30e051289bcea182694c /crates/ide/src/matching_brace.rs
parent00fb411f3edea72a1a9739f7df6f21cca045730b (diff)
parent6bc2633c90cedad057c5201d1ab7f67b57247004 (diff)
Merge #5750
5750: Rename ra_ide -> ide r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide/src/matching_brace.rs')
-rw-r--r--crates/ide/src/matching_brace.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/crates/ide/src/matching_brace.rs b/crates/ide/src/matching_brace.rs
new file mode 100644
index 000000000..cb6abb0db
--- /dev/null
+++ b/crates/ide/src/matching_brace.rs
@@ -0,0 +1,73 @@
1use syntax::{
2 ast::{self, AstNode},
3 SourceFile, SyntaxKind, TextSize, T,
4};
5use test_utils::mark;
6
7// Feature: Matching Brace
8//
9// If the cursor is on any brace (`<>(){}[]||`) which is a part of a brace-pair,
10// moves cursor to the matching brace. It uses the actual parser to determine
11// braces, so it won't confuse generics with comparisons.
12//
13// |===
14// | Editor | Action Name
15//
16// | VS Code | **Rust Analyzer: Find matching brace**
17// |===
18pub fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> {
19 const BRACES: &[SyntaxKind] =
20 &[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]];
21 let (brace_token, brace_idx) = file
22 .syntax()
23 .token_at_offset(offset)
24 .filter_map(|node| {
25 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
26 Some((node, idx))
27 })
28 .next()?;
29 let parent = brace_token.parent();
30 if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
31 mark::hit!(pipes_not_braces);
32 return None;
33 }
34 let matching_kind = BRACES[brace_idx ^ 1];
35 let matching_node = parent
36 .children_with_tokens()
37 .filter_map(|it| it.into_token())
38 .find(|node| node.kind() == matching_kind && node != &brace_token)?;
39 Some(matching_node.text_range().start())
40}
41
42#[cfg(test)]
43mod tests {
44 use test_utils::{add_cursor, assert_eq_text, extract_offset};
45
46 use super::*;
47
48 #[test]
49 fn test_matching_brace() {
50 fn do_check(before: &str, after: &str) {
51 let (pos, before) = extract_offset(before);
52 let parse = SourceFile::parse(&before);
53 let new_pos = match matching_brace(&parse.tree(), pos) {
54 None => pos,
55 Some(pos) => pos,
56 };
57 let actual = add_cursor(&before, new_pos);
58 assert_eq_text!(after, &actual);
59 }
60
61 do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }");
62 do_check("fn main() { |x: i32|<|> x * 2;}", "fn main() { <|>|x: i32| x * 2;}");
63 do_check("fn main() { <|>|x: i32| x * 2;}", "fn main() { |x: i32<|>| x * 2;}");
64
65 {
66 mark::check!(pipes_not_braces);
67 do_check(
68 "fn main() { match 92 { 1 | 2 |<|> 3 => 92 } }",
69 "fn main() { match 92 { 1 | 2 |<|> 3 => 92 } }",
70 );
71 }
72 }
73}