aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src/lib.rs')
-rw-r--r--crates/libeditor/src/lib.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs
index 9e44f5d92..28da457d1 100644
--- a/crates/libeditor/src/lib.rs
+++ b/crates/libeditor/src/lib.rs
@@ -12,8 +12,8 @@ mod code_actions;
12use libsyntax2::{ 12use libsyntax2::{
13 ast::{self, NameOwner}, 13 ast::{self, NameOwner},
14 AstNode, 14 AstNode,
15 algo::walk, 15 algo::{walk, find_leaf_at_offset},
16 SyntaxKind::*, 16 SyntaxKind::{self, *},
17}; 17};
18pub use libsyntax2::{File, TextRange, TextUnit}; 18pub use libsyntax2::{File, TextRange, TextUnit};
19pub use self::{ 19pub use self::{
@@ -52,6 +52,28 @@ pub fn parse(text: &str) -> ast::File {
52 ast::File::parse(text) 52 ast::File::parse(text)
53} 53}
54 54
55pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option<TextUnit> {
56 const BRACES: &[SyntaxKind] = &[
57 L_CURLY, R_CURLY,
58 L_BRACK, R_BRACK,
59 L_PAREN, R_PAREN,
60 L_ANGLE, R_ANGLE,
61 ];
62 let syntax = file.syntax();
63 let syntax = syntax.as_ref();
64 let (brace_node, brace_idx) = find_leaf_at_offset(syntax, offset)
65 .filter_map(|node| {
66 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
67 Some((node, idx))
68 })
69 .next()?;
70 let parent = brace_node.parent()?;
71 let matching_kind = BRACES[brace_idx ^ 1];
72 let matching_node = parent.children()
73 .find(|node| node.kind() == matching_kind)?;
74 Some(matching_node.range().start())
75}
76
55pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> { 77pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> {
56 let syntax = file.syntax(); 78 let syntax = file.syntax();
57 let mut res = Vec::new(); 79 let mut res = Vec::new();