aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/matching_brace.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-23 17:25:40 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-23 17:25:40 +0000
commit18a8f48039fbfcbbf58e1dadcc95465fe9503691 (patch)
tree503037f1f48c53eb460fb730fed576070527203d /crates/ra_ide_api/src/matching_brace.rs
parent2dfb47cc3dd68b7ca575e7eb4238221fdc8e7cdb (diff)
parenta3711e08dc4e393957dff136218c47d8b77da14f (diff)
Merge #1031
1031: Move most things out of ra_ide_api_light r=matklad a=detrumi This moves everything except `structure` out of `ra_ide_api_light`. So this PR and #1019 finish up #1009, whichever is merged last should probably remove the `ra_ide_api_light` crate. Also, `LocalEdit` was removed since it wasn't used any more. Co-authored-by: Wilco Kusee <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/matching_brace.rs')
-rw-r--r--crates/ra_ide_api/src/matching_brace.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/matching_brace.rs b/crates/ra_ide_api/src/matching_brace.rs
new file mode 100644
index 000000000..d1405f14f
--- /dev/null
+++ b/crates/ra_ide_api/src/matching_brace.rs
@@ -0,0 +1,45 @@
1use ra_syntax::{
2 SourceFile, TextUnit,
3 algo::find_leaf_at_offset,
4 SyntaxKind::{self, *},
5 ast::AstNode,
6};
7
8pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
9 const BRACES: &[SyntaxKind] =
10 &[L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE];
11 let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset)
12 .filter_map(|node| {
13 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
14 Some((node, idx))
15 })
16 .next()?;
17 let parent = brace_node.parent()?;
18 let matching_kind = BRACES[brace_idx ^ 1];
19 let matching_node = parent.children().find(|node| node.kind() == matching_kind)?;
20 Some(matching_node.range().start())
21}
22
23#[cfg(test)]
24mod tests {
25 use test_utils::{add_cursor, assert_eq_text, extract_offset};
26
27 use super::*;
28
29 #[test]
30 fn test_matching_brace() {
31 fn do_check(before: &str, after: &str) {
32 let (pos, before) = extract_offset(before);
33 let file = SourceFile::parse(&before);
34 let new_pos = match matching_brace(&file, pos) {
35 None => pos,
36 Some(pos) => pos,
37 };
38 let actual = add_cursor(&before, new_pos);
39 assert_eq_text!(after, &actual);
40 }
41
42 do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }");
43 }
44
45}