diff options
author | Aleksey Kladov <[email protected]> | 2018-08-17 20:00:13 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-17 20:00:13 +0100 |
commit | d3c90ded2b9a4f75e101fa3abc60cd3aebc439c9 (patch) | |
tree | 6d2388eb68605331a0dd090269372bc98dd038cd /crates/libeditor/src | |
parent | 70097504f78c4c41368a0b864a94df95fb9c27f7 (diff) |
Borrowed AST
Diffstat (limited to 'crates/libeditor/src')
-rw-r--r-- | crates/libeditor/src/code_actions.rs | 15 | ||||
-rw-r--r-- | crates/libeditor/src/extend_selection.rs | 5 | ||||
-rw-r--r-- | crates/libeditor/src/lib.rs | 26 | ||||
-rw-r--r-- | crates/libeditor/src/symbols.rs | 50 |
4 files changed, 47 insertions, 49 deletions
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index bb6eb0d61..80c396337 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use {TextUnit, File, EditBuilder, Edit}; | 1 | use {TextUnit, EditBuilder, Edit}; |
2 | use libsyntax2::{ | 2 | use libsyntax2::{ |
3 | ast::{self, AstNode, AttrsOwner}, | 3 | ast::{self, AstNode, AttrsOwner, ParsedFile}, |
4 | SyntaxKind::COMMA, | 4 | SyntaxKind::COMMA, |
5 | SyntaxNodeRef, RefRoot, | 5 | SyntaxNodeRef, |
6 | algo::{ | 6 | algo::{ |
7 | Direction, siblings, | 7 | Direction, siblings, |
8 | find_leaf_at_offset, ancestors, | 8 | find_leaf_at_offset, ancestors, |
@@ -19,9 +19,8 @@ pub enum CursorPosition { | |||
19 | Offset(TextUnit), | 19 | Offset(TextUnit), |
20 | } | 20 | } |
21 | 21 | ||
22 | pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { | 22 | pub fn flip_comma<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { |
23 | let syntax = file.syntax(); | 23 | let syntax = file.syntax(); |
24 | let syntax = syntax.as_ref(); | ||
25 | 24 | ||
26 | let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; | 25 | let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; |
27 | let left = non_trivia_sibling(comma, Direction::Backward)?; | 26 | let left = non_trivia_sibling(comma, Direction::Backward)?; |
@@ -37,8 +36,8 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() | |||
37 | }) | 36 | }) |
38 | } | 37 | } |
39 | 38 | ||
40 | pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { | 39 | pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { |
41 | let nominal = find_node::<ast::NominalDef<_>>(file.syntax_ref(), offset)?; | 40 | let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?; |
42 | Some(move || { | 41 | Some(move || { |
43 | let derive_attr = nominal | 42 | let derive_attr = nominal |
44 | .attrs() | 43 | .attrs() |
@@ -70,7 +69,7 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta | |||
70 | .find(|node| !node.kind().is_trivia()) | 69 | .find(|node| !node.kind().is_trivia()) |
71 | } | 70 | } |
72 | 71 | ||
73 | pub fn find_node<'a, N: AstNode<RefRoot<'a>>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> { | 72 | pub fn find_node<'a, N: AstNode<'a>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> { |
74 | let leaves = find_leaf_at_offset(syntax, offset); | 73 | let leaves = find_leaf_at_offset(syntax, offset); |
75 | let leaf = leaves.clone() | 74 | let leaf = leaves.clone() |
76 | .find(|leaf| !leaf.kind().is_trivia()) | 75 | .find(|leaf| !leaf.kind().is_trivia()) |
diff --git a/crates/libeditor/src/extend_selection.rs b/crates/libeditor/src/extend_selection.rs index ed7d9b3f7..cb6edb576 100644 --- a/crates/libeditor/src/extend_selection.rs +++ b/crates/libeditor/src/extend_selection.rs | |||
@@ -1,11 +1,10 @@ | |||
1 | use libsyntax2::{ | 1 | use libsyntax2::{ |
2 | ast, AstNode, | 2 | ParsedFile, TextRange, SyntaxNodeRef, |
3 | TextRange, SyntaxNodeRef, | ||
4 | SyntaxKind::WHITESPACE, | 3 | SyntaxKind::WHITESPACE, |
5 | algo::{find_leaf_at_offset, find_covering_node, ancestors}, | 4 | algo::{find_leaf_at_offset, find_covering_node, ancestors}, |
6 | }; | 5 | }; |
7 | 6 | ||
8 | pub fn extend_selection(file: &ast::File, range: TextRange) -> Option<TextRange> { | 7 | pub fn extend_selection(file: &ParsedFile, range: TextRange) -> Option<TextRange> { |
9 | let syntax = file.syntax(); | 8 | let syntax = file.syntax(); |
10 | extend(syntax.as_ref(), range) | 9 | extend(syntax.as_ref(), range) |
11 | } | 10 | } |
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 76cb4d028..d9d0369f6 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs | |||
@@ -15,7 +15,7 @@ use libsyntax2::{ | |||
15 | algo::{walk, find_leaf_at_offset}, | 15 | algo::{walk, find_leaf_at_offset}, |
16 | SyntaxKind::{self, *}, | 16 | SyntaxKind::{self, *}, |
17 | }; | 17 | }; |
18 | pub use libsyntax2::{File, TextRange, TextUnit}; | 18 | pub use libsyntax2::{ParsedFile, TextRange, TextUnit}; |
19 | pub use self::{ | 19 | pub use self::{ |
20 | line_index::{LineIndex, LineCol}, | 20 | line_index::{LineIndex, LineCol}, |
21 | extend_selection::extend_selection, | 21 | extend_selection::extend_selection, |
@@ -51,18 +51,18 @@ pub enum RunnableKind { | |||
51 | Bin, | 51 | Bin, |
52 | } | 52 | } |
53 | 53 | ||
54 | pub fn parse(text: &str) -> ast::File { | 54 | pub fn parse(text: &str) -> ast::ParsedFile { |
55 | ast::File::parse(text) | 55 | ast::ParsedFile::parse(text) |
56 | } | 56 | } |
57 | 57 | ||
58 | pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option<TextUnit> { | 58 | pub fn matching_brace(file: &ast::ParsedFile, offset: TextUnit) -> Option<TextUnit> { |
59 | const BRACES: &[SyntaxKind] = &[ | 59 | const BRACES: &[SyntaxKind] = &[ |
60 | L_CURLY, R_CURLY, | 60 | L_CURLY, R_CURLY, |
61 | L_BRACK, R_BRACK, | 61 | L_BRACK, R_BRACK, |
62 | L_PAREN, R_PAREN, | 62 | L_PAREN, R_PAREN, |
63 | L_ANGLE, R_ANGLE, | 63 | L_ANGLE, R_ANGLE, |
64 | ]; | 64 | ]; |
65 | let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax_ref(), offset) | 65 | let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset) |
66 | .filter_map(|node| { | 66 | .filter_map(|node| { |
67 | let idx = BRACES.iter().position(|&brace| brace == node.kind())?; | 67 | let idx = BRACES.iter().position(|&brace| brace == node.kind())?; |
68 | Some((node, idx)) | 68 | Some((node, idx)) |
@@ -75,9 +75,9 @@ pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option<TextUnit> { | |||
75 | Some(matching_node.range().start()) | 75 | Some(matching_node.range().start()) |
76 | } | 76 | } |
77 | 77 | ||
78 | pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> { | 78 | pub fn highlight(file: &ast::ParsedFile) -> Vec<HighlightedRange> { |
79 | let mut res = Vec::new(); | 79 | let mut res = Vec::new(); |
80 | for node in walk::preorder(file.syntax_ref()) { | 80 | for node in walk::preorder(file.syntax()) { |
81 | let tag = match node.kind() { | 81 | let tag = match node.kind() { |
82 | ERROR => "error", | 82 | ERROR => "error", |
83 | COMMENT | DOC_COMMENT => "comment", | 83 | COMMENT | DOC_COMMENT => "comment", |
@@ -98,10 +98,10 @@ pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> { | |||
98 | res | 98 | res |
99 | } | 99 | } |
100 | 100 | ||
101 | pub fn diagnostics(file: &ast::File) -> Vec<Diagnostic> { | 101 | pub fn diagnostics(file: &ast::ParsedFile) -> Vec<Diagnostic> { |
102 | let mut res = Vec::new(); | 102 | let mut res = Vec::new(); |
103 | 103 | ||
104 | for node in walk::preorder(file.syntax_ref()) { | 104 | for node in walk::preorder(file.syntax()) { |
105 | if node.kind() == ERROR { | 105 | if node.kind() == ERROR { |
106 | res.push(Diagnostic { | 106 | res.push(Diagnostic { |
107 | range: node.range(), | 107 | range: node.range(), |
@@ -116,12 +116,12 @@ pub fn diagnostics(file: &ast::File) -> Vec<Diagnostic> { | |||
116 | res | 116 | res |
117 | } | 117 | } |
118 | 118 | ||
119 | pub fn syntax_tree(file: &ast::File) -> String { | 119 | pub fn syntax_tree(file: &ast::ParsedFile) -> String { |
120 | ::libsyntax2::utils::dump_tree(&file.syntax()) | 120 | ::libsyntax2::utils::dump_tree(file.syntax()) |
121 | } | 121 | } |
122 | 122 | ||
123 | pub fn runnables(file: &ast::File) -> Vec<Runnable> { | 123 | pub fn runnables(file: &ast::ParsedFile) -> Vec<Runnable> { |
124 | file | 124 | file.ast() |
125 | .functions() | 125 | .functions() |
126 | .filter_map(|f| { | 126 | .filter_map(|f| { |
127 | let name = f.name()?.text(); | 127 | let name = f.name()?.text(); |
diff --git a/crates/libeditor/src/symbols.rs b/crates/libeditor/src/symbols.rs index cf5bd2a41..d7bd111e6 100644 --- a/crates/libeditor/src/symbols.rs +++ b/crates/libeditor/src/symbols.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use smol_str::SmolStr; | 1 | use smol_str::SmolStr; |
2 | use libsyntax2::{ | 2 | use libsyntax2::{ |
3 | SyntaxKind, SyntaxNodeRef, AstNode, RefRoot, | 3 | SyntaxKind, SyntaxNodeRef, AstNode, ParsedFile, |
4 | ast::{self, NameOwner}, | 4 | ast::{self, NameOwner}, |
5 | algo::{ | 5 | algo::{ |
6 | visit::{visitor, Visitor}, | 6 | visit::{visitor, Visitor}, |
@@ -25,14 +25,14 @@ pub struct FileSymbol { | |||
25 | pub kind: SyntaxKind, | 25 | pub kind: SyntaxKind, |
26 | } | 26 | } |
27 | 27 | ||
28 | pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> { | 28 | pub fn file_symbols(file: &ParsedFile) -> Vec<FileSymbol> { |
29 | preorder(file.syntax_ref()) | 29 | preorder(file.syntax()) |
30 | .filter_map(to_symbol) | 30 | .filter_map(to_symbol) |
31 | .collect() | 31 | .collect() |
32 | } | 32 | } |
33 | 33 | ||
34 | fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> { | 34 | fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> { |
35 | fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<FileSymbol> { | 35 | fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<FileSymbol> { |
36 | let name = node.name()?; | 36 | let name = node.name()?; |
37 | Some(FileSymbol { | 37 | Some(FileSymbol { |
38 | name: name.text(), | 38 | name: name.text(), |
@@ -41,23 +41,23 @@ fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> { | |||
41 | }) | 41 | }) |
42 | } | 42 | } |
43 | visitor() | 43 | visitor() |
44 | .visit(decl::<ast::FnDef<_>>) | 44 | .visit(decl::<ast::FnDef>) |
45 | .visit(decl::<ast::StructDef<_>>) | 45 | .visit(decl::<ast::StructDef>) |
46 | .visit(decl::<ast::EnumDef<_>>) | 46 | .visit(decl::<ast::EnumDef>) |
47 | .visit(decl::<ast::TraitDef<_>>) | 47 | .visit(decl::<ast::TraitDef>) |
48 | .visit(decl::<ast::Module<_>>) | 48 | .visit(decl::<ast::Module>) |
49 | .visit(decl::<ast::TypeDef<_>>) | 49 | .visit(decl::<ast::TypeDef>) |
50 | .visit(decl::<ast::ConstDef<_>>) | 50 | .visit(decl::<ast::ConstDef>) |
51 | .visit(decl::<ast::StaticDef<_>>) | 51 | .visit(decl::<ast::StaticDef>) |
52 | .accept(node)? | 52 | .accept(node)? |
53 | } | 53 | } |
54 | 54 | ||
55 | 55 | ||
56 | pub fn file_structure(file: &ast::File) -> Vec<StructureNode> { | 56 | pub fn file_structure(file: &ParsedFile) -> Vec<StructureNode> { |
57 | let mut res = Vec::new(); | 57 | let mut res = Vec::new(); |
58 | let mut stack = Vec::new(); | 58 | let mut stack = Vec::new(); |
59 | 59 | ||
60 | for event in walk(file.syntax_ref()) { | 60 | for event in walk(file.syntax()) { |
61 | match event { | 61 | match event { |
62 | WalkEvent::Enter(node) => { | 62 | WalkEvent::Enter(node) => { |
63 | match structure_node(node) { | 63 | match structure_node(node) { |
@@ -80,7 +80,7 @@ pub fn file_structure(file: &ast::File) -> Vec<StructureNode> { | |||
80 | } | 80 | } |
81 | 81 | ||
82 | fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> { | 82 | fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> { |
83 | fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<StructureNode> { | 83 | fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<StructureNode> { |
84 | let name = node.name()?; | 84 | let name = node.name()?; |
85 | Some(StructureNode { | 85 | Some(StructureNode { |
86 | parent: None, | 86 | parent: None, |
@@ -92,16 +92,16 @@ fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> { | |||
92 | } | 92 | } |
93 | 93 | ||
94 | visitor() | 94 | visitor() |
95 | .visit(decl::<ast::FnDef<_>>) | 95 | .visit(decl::<ast::FnDef>) |
96 | .visit(decl::<ast::StructDef<_>>) | 96 | .visit(decl::<ast::StructDef>) |
97 | .visit(decl::<ast::NamedField<_>>) | 97 | .visit(decl::<ast::NamedField>) |
98 | .visit(decl::<ast::EnumDef<_>>) | 98 | .visit(decl::<ast::EnumDef>) |
99 | .visit(decl::<ast::TraitDef<_>>) | 99 | .visit(decl::<ast::TraitDef>) |
100 | .visit(decl::<ast::Module<_>>) | 100 | .visit(decl::<ast::Module>) |
101 | .visit(decl::<ast::TypeDef<_>>) | 101 | .visit(decl::<ast::TypeDef>) |
102 | .visit(decl::<ast::ConstDef<_>>) | 102 | .visit(decl::<ast::ConstDef>) |
103 | .visit(decl::<ast::StaticDef<_>>) | 103 | .visit(decl::<ast::StaticDef>) |
104 | .visit(|im: ast::ImplItem<_>| { | 104 | .visit(|im: ast::ImplItem| { |
105 | let target_type = im.target_type()?; | 105 | let target_type = im.target_type()?; |
106 | let target_trait = im.target_trait(); | 106 | let target_trait = im.target_trait(); |
107 | let label = match target_trait { | 107 | let label = match target_trait { |