aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor')
-rw-r--r--crates/libeditor/src/code_actions.rs15
-rw-r--r--crates/libeditor/src/extend_selection.rs5
-rw-r--r--crates/libeditor/src/lib.rs26
-rw-r--r--crates/libeditor/src/symbols.rs50
-rw-r--r--crates/libeditor/tests/test.rs8
5 files changed, 51 insertions, 53 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 @@
1use {TextUnit, File, EditBuilder, Edit}; 1use {TextUnit, EditBuilder, Edit};
2use libsyntax2::{ 2use 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
22pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { 22pub 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
40pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { 39pub 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
73pub fn find_node<'a, N: AstNode<RefRoot<'a>>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> { 72pub 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 @@
1use libsyntax2::{ 1use 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
8pub fn extend_selection(file: &ast::File, range: TextRange) -> Option<TextRange> { 7pub 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};
18pub use libsyntax2::{File, TextRange, TextUnit}; 18pub use libsyntax2::{ParsedFile, TextRange, TextUnit};
19pub use self::{ 19pub 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
54pub fn parse(text: &str) -> ast::File { 54pub fn parse(text: &str) -> ast::ParsedFile {
55 ast::File::parse(text) 55 ast::ParsedFile::parse(text)
56} 56}
57 57
58pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option<TextUnit> { 58pub 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
78pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> { 78pub 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
101pub fn diagnostics(file: &ast::File) -> Vec<Diagnostic> { 101pub 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
119pub fn syntax_tree(file: &ast::File) -> String { 119pub 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
123pub fn runnables(file: &ast::File) -> Vec<Runnable> { 123pub 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 @@
1use smol_str::SmolStr; 1use smol_str::SmolStr;
2use libsyntax2::{ 2use 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
28pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> { 28pub 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
34fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> { 34fn 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
56pub fn file_structure(file: &ast::File) -> Vec<StructureNode> { 56pub 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
82fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> { 82fn 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 {
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
index cc4226710..a2e26003a 100644
--- a/crates/libeditor/tests/test.rs
+++ b/crates/libeditor/tests/test.rs
@@ -5,7 +5,7 @@ extern crate assert_eq_text;
5 5
6use assert_eq_text::{assert_eq_dbg}; 6use assert_eq_text::{assert_eq_dbg};
7use libeditor::{ 7use libeditor::{
8 File, TextUnit, TextRange, ActionResult, CursorPosition, 8 ParsedFile, TextUnit, TextRange, ActionResult, CursorPosition,
9 highlight, runnables, extend_selection, file_structure, 9 highlight, runnables, extend_selection, file_structure,
10 flip_comma, add_derive, matching_brace, 10 flip_comma, add_derive, matching_brace,
11}; 11};
@@ -146,11 +146,11 @@ fn test_matching_brace() {
146 ); 146 );
147} 147}
148 148
149fn file(text: &str) -> File { 149fn file(text: &str) -> ParsedFile {
150 File::parse(text) 150 ParsedFile::parse(text)
151} 151}
152 152
153fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>( 153fn check_action<F: Fn(&ParsedFile, TextUnit) -> Option<ActionResult>>(
154 before: &str, 154 before: &str,
155 after: &str, 155 after: &str,
156 f: F, 156 f: F,