aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:05:55 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:05:55 +0000
commit3f4be819125ce4a22edd86721fa56b5caba99c2e (patch)
treebe93895ddc08c911585d9f7bc64623a3741f32c6 /crates/ra_editor
parent4e444d2bc24d16284401444fd2154f63e0f96070 (diff)
parent122410d7aa34a32d468a3173858cbc8a2bbc68f5 (diff)
Merge #449
449: switch to new rowan API r=matklad a=matklad closes https://github.com/rust-analyzer/rust-analyzer/issues/448 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_editor')
-rw-r--r--crates/ra_editor/src/assists.rs16
-rw-r--r--crates/ra_editor/src/assists/add_derive.rs2
-rw-r--r--crates/ra_editor/src/assists/change_visibility.rs2
-rw-r--r--crates/ra_editor/src/assists/introduce_variable.rs4
-rw-r--r--crates/ra_editor/src/diagnostics.rs32
-rw-r--r--crates/ra_editor/src/extend_selection.rs24
-rw-r--r--crates/ra_editor/src/folding_ranges.rs17
-rw-r--r--crates/ra_editor/src/lib.rs24
-rw-r--r--crates/ra_editor/src/structure.rs12
-rw-r--r--crates/ra_editor/src/test_utils.rs10
-rw-r--r--crates/ra_editor/src/typing.rs36
11 files changed, 83 insertions, 96 deletions
diff --git a/crates/ra_editor/src/assists.rs b/crates/ra_editor/src/assists.rs
index 57b78342a..a320caabf 100644
--- a/crates/ra_editor/src/assists.rs
+++ b/crates/ra_editor/src/assists.rs
@@ -12,7 +12,7 @@ mod split_import;
12 12
13use ra_text_edit::{TextEdit, TextEditBuilder}; 13use ra_text_edit::{TextEdit, TextEditBuilder};
14use ra_syntax::{ 14use ra_syntax::{
15 Direction, SyntaxNodeRef, TextUnit, TextRange,SourceFileNode, AstNode, 15 Direction, SyntaxNode, TextUnit, TextRange, SourceFile, AstNode,
16 algo::{find_leaf_at_offset, find_covering_node, LeafAtOffset}, 16 algo::{find_leaf_at_offset, find_covering_node, LeafAtOffset},
17}; 17};
18 18
@@ -28,7 +28,7 @@ pub use self::{
28}; 28};
29 29
30/// Return all the assists applicable at the given position. 30/// Return all the assists applicable at the given position.
31pub fn assists(file: &SourceFileNode, range: TextRange) -> Vec<LocalEdit> { 31pub fn assists(file: &SourceFile, range: TextRange) -> Vec<LocalEdit> {
32 let ctx = AssistCtx::new(file, range); 32 let ctx = AssistCtx::new(file, range);
33 [ 33 [
34 flip_comma, 34 flip_comma,
@@ -50,7 +50,7 @@ pub struct LocalEdit {
50 pub cursor_position: Option<TextUnit>, 50 pub cursor_position: Option<TextUnit>,
51} 51}
52 52
53fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> { 53fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> {
54 node.siblings(direction) 54 node.siblings(direction)
55 .skip(1) 55 .skip(1)
56 .find(|node| !node.kind().is_trivia()) 56 .find(|node| !node.kind().is_trivia())
@@ -88,7 +88,7 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta
88/// easier to just compute the edit eagarly :-) 88/// easier to just compute the edit eagarly :-)
89#[derive(Debug, Clone)] 89#[derive(Debug, Clone)]
90pub struct AssistCtx<'a> { 90pub struct AssistCtx<'a> {
91 source_file: &'a SourceFileNode, 91 source_file: &'a SourceFile,
92 range: TextRange, 92 range: TextRange,
93 should_compute_edit: bool, 93 should_compute_edit: bool,
94} 94}
@@ -106,7 +106,7 @@ struct AssistBuilder {
106} 106}
107 107
108impl<'a> AssistCtx<'a> { 108impl<'a> AssistCtx<'a> {
109 pub fn new(source_file: &'a SourceFileNode, range: TextRange) -> AssistCtx { 109 pub fn new(source_file: &'a SourceFile, range: TextRange) -> AssistCtx {
110 AssistCtx { 110 AssistCtx {
111 source_file, 111 source_file,
112 range, 112 range,
@@ -145,13 +145,13 @@ impl<'a> AssistCtx<'a> {
145 })) 145 }))
146 } 146 }
147 147
148 pub(crate) fn leaf_at_offset(&self) -> LeafAtOffset<SyntaxNodeRef<'a>> { 148 pub(crate) fn leaf_at_offset(&self) -> LeafAtOffset<&'a SyntaxNode> {
149 find_leaf_at_offset(self.source_file.syntax(), self.range.start()) 149 find_leaf_at_offset(self.source_file.syntax(), self.range.start())
150 } 150 }
151 pub(crate) fn node_at_offset<N: AstNode<'a>>(&self) -> Option<N> { 151 pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<&'a N> {
152 find_node_at_offset(self.source_file.syntax(), self.range.start()) 152 find_node_at_offset(self.source_file.syntax(), self.range.start())
153 } 153 }
154 pub(crate) fn covering_node(&self) -> SyntaxNodeRef<'a> { 154 pub(crate) fn covering_node(&self) -> &'a SyntaxNode {
155 find_covering_node(self.source_file.syntax(), self.range) 155 find_covering_node(self.source_file.syntax(), self.range)
156 } 156 }
157} 157}
diff --git a/crates/ra_editor/src/assists/add_derive.rs b/crates/ra_editor/src/assists/add_derive.rs
index 1e2cd4f30..6e964d011 100644
--- a/crates/ra_editor/src/assists/add_derive.rs
+++ b/crates/ra_editor/src/assists/add_derive.rs
@@ -28,7 +28,7 @@ pub fn add_derive(ctx: AssistCtx) -> Option<Assist> {
28} 28}
29 29
30// Insert `derive` after doc comments. 30// Insert `derive` after doc comments.
31fn derive_insertion_offset(nominal: ast::NominalDef) -> Option<TextUnit> { 31fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextUnit> {
32 let non_ws_child = nominal 32 let non_ws_child = nominal
33 .syntax() 33 .syntax()
34 .children() 34 .children()
diff --git a/crates/ra_editor/src/assists/change_visibility.rs b/crates/ra_editor/src/assists/change_visibility.rs
index 6c8466394..89729e2c2 100644
--- a/crates/ra_editor/src/assists/change_visibility.rs
+++ b/crates/ra_editor/src/assists/change_visibility.rs
@@ -46,7 +46,7 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
46 }) 46 })
47} 47}
48 48
49fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option<Assist> { 49fn change_vis(ctx: AssistCtx, vis: &ast::Visibility) -> Option<Assist> {
50 if vis.syntax().text() != "pub" { 50 if vis.syntax().text() != "pub" {
51 return None; 51 return None;
52 } 52 }
diff --git a/crates/ra_editor/src/assists/introduce_variable.rs b/crates/ra_editor/src/assists/introduce_variable.rs
index 782861023..523ec7034 100644
--- a/crates/ra_editor/src/assists/introduce_variable.rs
+++ b/crates/ra_editor/src/assists/introduce_variable.rs
@@ -1,7 +1,7 @@
1use ra_syntax::{ 1use ra_syntax::{
2 ast::{self, AstNode}, 2 ast::{self, AstNode},
3 SyntaxKind::WHITESPACE, 3 SyntaxKind::WHITESPACE,
4 SyntaxNodeRef, TextUnit, 4 SyntaxNode, TextUnit,
5}; 5};
6 6
7use crate::assists::{AssistCtx, Assist}; 7use crate::assists::{AssistCtx, Assist};
@@ -39,7 +39,7 @@ pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option<Assist> {
39 39
40/// Statement or last in the block expression, which will follow 40/// Statement or last in the block expression, which will follow
41/// the freshly introduced var. 41/// the freshly introduced var.
42fn anchor_stmt(expr: ast::Expr) -> Option<SyntaxNodeRef> { 42fn anchor_stmt(expr: &ast::Expr) -> Option<&SyntaxNode> {
43 expr.syntax().ancestors().find(|&node| { 43 expr.syntax().ancestors().find(|&node| {
44 if ast::Stmt::cast(node).is_some() { 44 if ast::Stmt::cast(node).is_some() {
45 return true; 45 return true;
diff --git a/crates/ra_editor/src/diagnostics.rs b/crates/ra_editor/src/diagnostics.rs
index 199b0e502..2b695dfdf 100644
--- a/crates/ra_editor/src/diagnostics.rs
+++ b/crates/ra_editor/src/diagnostics.rs
@@ -1,25 +1,15 @@
1use itertools::Itertools; 1use itertools::Itertools;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 Location, SourceFile, SyntaxKind, TextRange, SyntaxNode,
4 ast::{self, AstNode}, 5 ast::{self, AstNode},
5 Location,
6 SourceFileNode,
7 SyntaxKind,
8 TextRange,
9};
10use ra_syntax::SyntaxNodeRef;
11use ra_text_edit::{
12 TextEdit,
13 TextEditBuilder,
14};
15 6
16use crate::{
17 Diagnostic,
18 LocalEdit,
19 Severity,
20}; 7};
8use ra_text_edit::{TextEdit, TextEditBuilder};
9
10use crate::{Diagnostic, LocalEdit, Severity};
21 11
22pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> { 12pub fn diagnostics(file: &SourceFile) -> Vec<Diagnostic> {
23 fn location_to_range(location: Location) -> TextRange { 13 fn location_to_range(location: Location) -> TextRange {
24 match location { 14 match location {
25 Location::Offset(offset) => TextRange::offset_len(offset, 1.into()), 15 Location::Offset(offset) => TextRange::offset_len(offset, 1.into()),
@@ -48,7 +38,7 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> {
48 38
49fn check_unnecessary_braces_in_use_statement( 39fn check_unnecessary_braces_in_use_statement(
50 acc: &mut Vec<Diagnostic>, 40 acc: &mut Vec<Diagnostic>,
51 node: SyntaxNodeRef, 41 node: &SyntaxNode,
52) -> Option<()> { 42) -> Option<()> {
53 let use_tree_list = ast::UseTreeList::cast(node)?; 43 let use_tree_list = ast::UseTreeList::cast(node)?;
54 if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() { 44 if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
@@ -79,7 +69,7 @@ fn check_unnecessary_braces_in_use_statement(
79} 69}
80 70
81fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( 71fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
82 single_use_tree: ast::UseTree, 72 single_use_tree: &ast::UseTree,
83) -> Option<TextEdit> { 73) -> Option<TextEdit> {
84 let use_tree_list_node = single_use_tree.syntax().parent()?; 74 let use_tree_list_node = single_use_tree.syntax().parent()?;
85 if single_use_tree 75 if single_use_tree
@@ -102,7 +92,7 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
102 92
103fn check_struct_shorthand_initialization( 93fn check_struct_shorthand_initialization(
104 acc: &mut Vec<Diagnostic>, 94 acc: &mut Vec<Diagnostic>,
105 node: SyntaxNodeRef, 95 node: &SyntaxNode,
106) -> Option<()> { 96) -> Option<()> {
107 let struct_lit = ast::StructLit::cast(node)?; 97 let struct_lit = ast::StructLit::cast(node)?;
108 let named_field_list = struct_lit.named_field_list()?; 98 let named_field_list = struct_lit.named_field_list()?;
@@ -138,10 +128,10 @@ mod tests {
138 128
139 use super::*; 129 use super::*;
140 130
141 type DiagnosticChecker = fn(&mut Vec<Diagnostic>, SyntaxNodeRef) -> Option<()>; 131 type DiagnosticChecker = fn(&mut Vec<Diagnostic>, &SyntaxNode) -> Option<()>;
142 132
143 fn check_not_applicable(code: &str, func: DiagnosticChecker) { 133 fn check_not_applicable(code: &str, func: DiagnosticChecker) {
144 let file = SourceFileNode::parse(code); 134 let file = SourceFile::parse(code);
145 let mut diagnostics = Vec::new(); 135 let mut diagnostics = Vec::new();
146 for node in file.syntax().descendants() { 136 for node in file.syntax().descendants() {
147 func(&mut diagnostics, node); 137 func(&mut diagnostics, node);
@@ -150,7 +140,7 @@ mod tests {
150 } 140 }
151 141
152 fn check_apply(before: &str, after: &str, func: DiagnosticChecker) { 142 fn check_apply(before: &str, after: &str, func: DiagnosticChecker) {
153 let file = SourceFileNode::parse(before); 143 let file = SourceFile::parse(before);
154 let mut diagnostics = Vec::new(); 144 let mut diagnostics = Vec::new();
155 for node in file.syntax().descendants() { 145 for node in file.syntax().descendants() {
156 func(&mut diagnostics, node); 146 func(&mut diagnostics, node);
diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs
index 7a423852b..08cae5a51 100644
--- a/crates/ra_editor/src/extend_selection.rs
+++ b/crates/ra_editor/src/extend_selection.rs
@@ -1,11 +1,10 @@
1use ra_syntax::{ 1use ra_syntax::{
2 Direction, SyntaxNode, TextRange, TextUnit,
2 algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, 3 algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset},
3 Direction,
4 SyntaxKind::*, 4 SyntaxKind::*,
5 SyntaxNodeRef, TextRange, TextUnit,
6}; 5};
7 6
8pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { 7pub fn extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> {
9 let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING]; 8 let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING];
10 if range.is_empty() { 9 if range.is_empty() {
11 let offset = range.start(); 10 let offset = range.start();
@@ -40,7 +39,7 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan
40} 39}
41 40
42fn extend_single_word_in_comment_or_string( 41fn extend_single_word_in_comment_or_string(
43 leaf: SyntaxNodeRef, 42 leaf: &SyntaxNode,
44 offset: TextUnit, 43 offset: TextUnit,
45) -> Option<TextRange> { 44) -> Option<TextRange> {
46 let text: &str = leaf.leaf_text()?; 45 let text: &str = leaf.leaf_text()?;
@@ -66,7 +65,7 @@ fn extend_single_word_in_comment_or_string(
66 } 65 }
67} 66}
68 67
69fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRange { 68fn extend_ws(root: &SyntaxNode, ws: &SyntaxNode, offset: TextUnit) -> TextRange {
70 let ws_text = ws.leaf_text().unwrap(); 69 let ws_text = ws.leaf_text().unwrap();
71 let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start(); 70 let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start();
72 let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start(); 71 let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start();
@@ -89,9 +88,9 @@ fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRa
89 ws.range() 88 ws.range()
90} 89}
91 90
92fn pick_best<'a>(l: SyntaxNodeRef<'a>, r: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { 91fn pick_best<'a>(l: &'a SyntaxNode, r: &'a SyntaxNode) -> &'a SyntaxNode {
93 return if priority(r) > priority(l) { r } else { l }; 92 return if priority(r) > priority(l) { r } else { l };
94 fn priority(n: SyntaxNodeRef) -> usize { 93 fn priority(n: &SyntaxNode) -> usize {
95 match n.kind() { 94 match n.kind() {
96 WHITESPACE => 0, 95 WHITESPACE => 0,
97 IDENT | SELF_KW | SUPER_KW | CRATE_KW | LIFETIME => 2, 96 IDENT | SELF_KW | SUPER_KW | CRATE_KW | LIFETIME => 2,
@@ -100,7 +99,7 @@ fn pick_best<'a>(l: SyntaxNodeRef<'a>, r: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a
100 } 99 }
101} 100}
102 101
103fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> { 102fn extend_comments(node: &SyntaxNode) -> Option<TextRange> {
104 let prev = adj_comments(node, Direction::Prev); 103 let prev = adj_comments(node, Direction::Prev);
105 let next = adj_comments(node, Direction::Next); 104 let next = adj_comments(node, Direction::Next);
106 if prev != next { 105 if prev != next {
@@ -110,7 +109,7 @@ fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> {
110 } 109 }
111} 110}
112 111
113fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef { 112fn adj_comments(node: &SyntaxNode, dir: Direction) -> &SyntaxNode {
114 let mut res = node; 113 let mut res = node;
115 for node in node.siblings(dir) { 114 for node in node.siblings(dir) {
116 match node.kind() { 115 match node.kind() {
@@ -124,13 +123,14 @@ fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef {
124 123
125#[cfg(test)] 124#[cfg(test)]
126mod tests { 125mod tests {
127 use super::*; 126 use ra_syntax::{SourceFile, AstNode};
128 use ra_syntax::SourceFileNode;
129 use test_utils::extract_offset; 127 use test_utils::extract_offset;
130 128
129 use super::*;
130
131 fn do_check(before: &str, afters: &[&str]) { 131 fn do_check(before: &str, afters: &[&str]) {
132 let (cursor, before) = extract_offset(before); 132 let (cursor, before) = extract_offset(before);
133 let file = SourceFileNode::parse(&before); 133 let file = SourceFile::parse(&before);
134 let mut range = TextRange::offset_len(cursor, 0.into()); 134 let mut range = TextRange::offset_len(cursor, 0.into());
135 for &after in afters { 135 for &after in afters {
136 range = extend_selection(file.syntax(), range).unwrap(); 136 range = extend_selection(file.syntax(), range).unwrap();
diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs
index da542ecf0..6f3106889 100644
--- a/crates/ra_editor/src/folding_ranges.rs
+++ b/crates/ra_editor/src/folding_ranges.rs
@@ -1,9 +1,8 @@
1use rustc_hash::FxHashSet; 1use rustc_hash::FxHashSet;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 ast, AstNode, Direction, SourceFileNode, 4 ast, AstNode, Direction, SourceFile, SyntaxNode, TextRange,
5 SyntaxKind::{self, *}, 5 SyntaxKind::{self, *},
6 SyntaxNodeRef, TextRange,
7}; 6};
8 7
9#[derive(Debug, PartialEq, Eq)] 8#[derive(Debug, PartialEq, Eq)]
@@ -19,7 +18,7 @@ pub struct Fold {
19 pub kind: FoldKind, 18 pub kind: FoldKind,
20} 19}
21 20
22pub fn folding_ranges(file: &SourceFileNode) -> Vec<Fold> { 21pub fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
23 let mut res = vec![]; 22 let mut res = vec![];
24 let mut visited_comments = FxHashSet::default(); 23 let mut visited_comments = FxHashSet::default();
25 let mut visited_imports = FxHashSet::default(); 24 let mut visited_imports = FxHashSet::default();
@@ -69,7 +68,7 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
69 } 68 }
70} 69}
71 70
72fn has_newline(node: SyntaxNodeRef) -> bool { 71fn has_newline(node: &SyntaxNode) -> bool {
73 for descendant in node.descendants() { 72 for descendant in node.descendants() {
74 if let Some(ws) = ast::Whitespace::cast(descendant) { 73 if let Some(ws) = ast::Whitespace::cast(descendant) {
75 if ws.has_newlines() { 74 if ws.has_newlines() {
@@ -86,8 +85,8 @@ fn has_newline(node: SyntaxNodeRef) -> bool {
86} 85}
87 86
88fn contiguous_range_for_group<'a>( 87fn contiguous_range_for_group<'a>(
89 first: SyntaxNodeRef<'a>, 88 first: &'a SyntaxNode,
90 visited: &mut FxHashSet<SyntaxNodeRef<'a>>, 89 visited: &mut FxHashSet<&'a SyntaxNode>,
91) -> Option<TextRange> { 90) -> Option<TextRange> {
92 visited.insert(first); 91 visited.insert(first);
93 92
@@ -124,8 +123,8 @@ fn contiguous_range_for_group<'a>(
124} 123}
125 124
126fn contiguous_range_for_comment<'a>( 125fn contiguous_range_for_comment<'a>(
127 first: SyntaxNodeRef<'a>, 126 first: &'a SyntaxNode,
128 visited: &mut FxHashSet<SyntaxNodeRef<'a>>, 127 visited: &mut FxHashSet<&'a SyntaxNode>,
129) -> Option<TextRange> { 128) -> Option<TextRange> {
130 visited.insert(first); 129 visited.insert(first);
131 130
@@ -174,7 +173,7 @@ mod tests {
174 173
175 fn do_check(text: &str, fold_kinds: &[FoldKind]) { 174 fn do_check(text: &str, fold_kinds: &[FoldKind]) {
176 let (ranges, text) = extract_ranges(text, "fold"); 175 let (ranges, text) = extract_ranges(text, "fold");
177 let file = SourceFileNode::parse(&text); 176 let file = SourceFile::parse(&text);
178 let folds = folding_ranges(&file); 177 let folds = folding_ranges(&file);
179 178
180 assert_eq!( 179 assert_eq!(
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index a3c85ed5d..6731260a3 100644
--- a/crates/ra_editor/src/lib.rs
+++ b/crates/ra_editor/src/lib.rs
@@ -21,11 +21,10 @@ pub use self::{
21}; 21};
22use ra_text_edit::TextEditBuilder; 22use ra_text_edit::TextEditBuilder;
23use ra_syntax::{ 23use ra_syntax::{
24 algo::find_leaf_at_offset, 24 SourceFile, SyntaxNode, TextRange, TextUnit, Direction,
25 ast::{self, AstNode},
26 SourceFileNode,
27 SyntaxKind::{self, *}, 25 SyntaxKind::{self, *},
28 SyntaxNodeRef, TextRange, TextUnit, Direction, 26 ast::{self, AstNode},
27 algo::find_leaf_at_offset,
29}; 28};
30use rustc_hash::FxHashSet; 29use rustc_hash::FxHashSet;
31 30
@@ -49,7 +48,7 @@ pub struct Diagnostic {
49 pub fix: Option<LocalEdit>, 48 pub fix: Option<LocalEdit>,
50} 49}
51 50
52pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> { 51pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
53 const BRACES: &[SyntaxKind] = &[ 52 const BRACES: &[SyntaxKind] = &[
54 L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE, 53 L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE,
55 ]; 54 ];
@@ -67,7 +66,7 @@ pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option<TextUni
67 Some(matching_node.range().start()) 66 Some(matching_node.range().start())
68} 67}
69 68
70pub fn highlight(root: SyntaxNodeRef) -> Vec<HighlightedRange> { 69pub fn highlight(root: &SyntaxNode) -> Vec<HighlightedRange> {
71 // Visited nodes to handle highlighting priorities 70 // Visited nodes to handle highlighting priorities
72 let mut highlighted = FxHashSet::default(); 71 let mut highlighted = FxHashSet::default();
73 let mut res = Vec::new(); 72 let mut res = Vec::new();
@@ -117,26 +116,25 @@ pub fn highlight(root: SyntaxNodeRef) -> Vec<HighlightedRange> {
117 res 116 res
118} 117}
119 118
120pub fn syntax_tree(file: &SourceFileNode) -> String { 119pub fn syntax_tree(file: &SourceFile) -> String {
121 ::ra_syntax::utils::dump_tree(file.syntax()) 120 ::ra_syntax::utils::dump_tree(file.syntax())
122} 121}
123 122
124pub fn find_node_at_offset<'a, N: AstNode<'a>>( 123pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
125 syntax: SyntaxNodeRef<'a>,
126 offset: TextUnit,
127) -> Option<N> {
128 find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) 124 find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
129} 125}
130 126
131#[cfg(test)] 127#[cfg(test)]
132mod tests { 128mod tests {
129 use ra_syntax::AstNode;
130
133 use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset}; 131 use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset};
134 132
135 use super::*; 133 use super::*;
136 134
137 #[test] 135 #[test]
138 fn test_highlighting() { 136 fn test_highlighting() {
139 let file = SourceFileNode::parse( 137 let file = SourceFile::parse(
140 r#" 138 r#"
141// comment 139// comment
142fn main() {} 140fn main() {}
@@ -159,7 +157,7 @@ fn main() {}
159 fn test_matching_brace() { 157 fn test_matching_brace() {
160 fn do_check(before: &str, after: &str) { 158 fn do_check(before: &str, after: &str) {
161 let (pos, before) = extract_offset(before); 159 let (pos, before) = extract_offset(before);
162 let file = SourceFileNode::parse(&before); 160 let file = SourceFile::parse(&before);
163 let new_pos = match matching_brace(&file, pos) { 161 let new_pos = match matching_brace(&file, pos) {
164 None => pos, 162 None => pos,
165 Some(pos) => pos, 163 Some(pos) => pos,
diff --git a/crates/ra_editor/src/structure.rs b/crates/ra_editor/src/structure.rs
index 32d59e335..8bd57555f 100644
--- a/crates/ra_editor/src/structure.rs
+++ b/crates/ra_editor/src/structure.rs
@@ -3,7 +3,7 @@ use crate::TextRange;
3use ra_syntax::{ 3use ra_syntax::{
4 algo::visit::{visitor, Visitor}, 4 algo::visit::{visitor, Visitor},
5 ast::{self, NameOwner}, 5 ast::{self, NameOwner},
6 AstNode, SourceFileNode, SyntaxKind, SyntaxNodeRef, WalkEvent, 6 AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent,
7}; 7};
8 8
9#[derive(Debug, Clone)] 9#[derive(Debug, Clone)]
@@ -15,7 +15,7 @@ pub struct StructureNode {
15 pub kind: SyntaxKind, 15 pub kind: SyntaxKind,
16} 16}
17 17
18pub fn file_structure(file: &SourceFileNode) -> Vec<StructureNode> { 18pub fn file_structure(file: &SourceFile) -> Vec<StructureNode> {
19 let mut res = Vec::new(); 19 let mut res = Vec::new();
20 let mut stack = Vec::new(); 20 let mut stack = Vec::new();
21 21
@@ -38,8 +38,8 @@ pub fn file_structure(file: &SourceFileNode) -> Vec<StructureNode> {
38 res 38 res
39} 39}
40 40
41fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> { 41fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
42 fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<StructureNode> { 42 fn decl<N: NameOwner>(node: &N) -> Option<StructureNode> {
43 let name = node.name()?; 43 let name = node.name()?;
44 Some(StructureNode { 44 Some(StructureNode {
45 parent: None, 45 parent: None,
@@ -60,7 +60,7 @@ fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
60 .visit(decl::<ast::TypeDef>) 60 .visit(decl::<ast::TypeDef>)
61 .visit(decl::<ast::ConstDef>) 61 .visit(decl::<ast::ConstDef>)
62 .visit(decl::<ast::StaticDef>) 62 .visit(decl::<ast::StaticDef>)
63 .visit(|im: ast::ImplBlock| { 63 .visit(|im: &ast::ImplBlock| {
64 let target_type = im.target_type()?; 64 let target_type = im.target_type()?;
65 let target_trait = im.target_trait(); 65 let target_trait = im.target_trait();
66 let label = match target_trait { 66 let label = match target_trait {
@@ -91,7 +91,7 @@ mod tests {
91 91
92 #[test] 92 #[test]
93 fn test_file_structure() { 93 fn test_file_structure() {
94 let file = SourceFileNode::parse( 94 let file = SourceFile::parse(
95 r#" 95 r#"
96struct Foo { 96struct Foo {
97 x: i32 97 x: i32
diff --git a/crates/ra_editor/src/test_utils.rs b/crates/ra_editor/src/test_utils.rs
index f0a4f250a..bf40c92c0 100644
--- a/crates/ra_editor/src/test_utils.rs
+++ b/crates/ra_editor/src/test_utils.rs
@@ -1,15 +1,15 @@
1use ra_syntax::{SourceFileNode, TextRange, TextUnit}; 1use ra_syntax::{SourceFile, TextRange, TextUnit};
2 2
3use crate::LocalEdit; 3use crate::LocalEdit;
4pub use test_utils::*; 4pub use test_utils::*;
5 5
6pub fn check_action<F: Fn(&SourceFileNode, TextUnit) -> Option<LocalEdit>>( 6pub fn check_action<F: Fn(&SourceFile, TextUnit) -> Option<LocalEdit>>(
7 before: &str, 7 before: &str,
8 after: &str, 8 after: &str,
9 f: F, 9 f: F,
10) { 10) {
11 let (before_cursor_pos, before) = extract_offset(before); 11 let (before_cursor_pos, before) = extract_offset(before);
12 let file = SourceFileNode::parse(&before); 12 let file = SourceFile::parse(&before);
13 let result = f(&file, before_cursor_pos).expect("code action is not applicable"); 13 let result = f(&file, before_cursor_pos).expect("code action is not applicable");
14 let actual = result.edit.apply(&before); 14 let actual = result.edit.apply(&before);
15 let actual_cursor_pos = match result.cursor_position { 15 let actual_cursor_pos = match result.cursor_position {
@@ -20,13 +20,13 @@ pub fn check_action<F: Fn(&SourceFileNode, TextUnit) -> Option<LocalEdit>>(
20 assert_eq_text!(after, &actual); 20 assert_eq_text!(after, &actual);
21} 21}
22 22
23pub fn check_action_range<F: Fn(&SourceFileNode, TextRange) -> Option<LocalEdit>>( 23pub fn check_action_range<F: Fn(&SourceFile, TextRange) -> Option<LocalEdit>>(
24 before: &str, 24 before: &str,
25 after: &str, 25 after: &str,
26 f: F, 26 f: F,
27) { 27) {
28 let (range, before) = extract_range(before); 28 let (range, before) = extract_range(before);
29 let file = SourceFileNode::parse(&before); 29 let file = SourceFile::parse(&before);
30 let result = f(&file, range).expect("code action is not applicable"); 30 let result = f(&file, range).expect("code action is not applicable");
31 let actual = result.edit.apply(&before); 31 let actual = result.edit.apply(&before);
32 let actual_cursor_pos = match result.cursor_position { 32 let actual_cursor_pos = match result.cursor_position {
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs
index 12500854c..5b260d2ac 100644
--- a/crates/ra_editor/src/typing.rs
+++ b/crates/ra_editor/src/typing.rs
@@ -5,15 +5,15 @@ use ra_syntax::{
5 algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, 5 algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset},
6 ast, 6 ast,
7 text_utils::intersect, 7 text_utils::intersect,
8 AstNode, Direction, SourceFileNode, SyntaxKind, 8 AstNode, Direction, SourceFile, SyntaxKind,
9 SyntaxKind::*, 9 SyntaxKind::*,
10 SyntaxNodeRef, TextRange, TextUnit, 10 SyntaxNode, TextRange, TextUnit,
11}; 11};
12use ra_text_edit::text_utils::contains_offset_nonstrict; 12use ra_text_edit::text_utils::contains_offset_nonstrict;
13 13
14use crate::{find_node_at_offset, LocalEdit, TextEditBuilder}; 14use crate::{find_node_at_offset, LocalEdit, TextEditBuilder};
15 15
16pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { 16pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
17 let range = if range.is_empty() { 17 let range = if range.is_empty() {
18 let syntax = file.syntax(); 18 let syntax = file.syntax();
19 let text = syntax.text().slice(range.start()..); 19 let text = syntax.text().slice(range.start()..);
@@ -59,7 +59,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
59 } 59 }
60} 60}
61 61
62pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> { 62pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
63 let comment = find_leaf_at_offset(file.syntax(), offset) 63 let comment = find_leaf_at_offset(file.syntax(), offset)
64 .left_biased() 64 .left_biased()
65 .and_then(ast::Comment::cast)?; 65 .and_then(ast::Comment::cast)?;
@@ -85,7 +85,7 @@ pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> {
85 }) 85 })
86} 86}
87 87
88fn node_indent<'a>(file: &'a SourceFileNode, node: SyntaxNodeRef) -> Option<&'a str> { 88fn node_indent<'a>(file: &'a SourceFile, node: &SyntaxNode) -> Option<&'a str> {
89 let ws = match find_leaf_at_offset(file.syntax(), node.range().start()) { 89 let ws = match find_leaf_at_offset(file.syntax(), node.range().start()) {
90 LeafAtOffset::Between(l, r) => { 90 LeafAtOffset::Between(l, r) => {
91 assert!(r == node); 91 assert!(r == node);
@@ -105,8 +105,8 @@ fn node_indent<'a>(file: &'a SourceFileNode, node: SyntaxNodeRef) -> Option<&'a
105 Some(&text[pos..]) 105 Some(&text[pos..])
106} 106}
107 107
108pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> { 108pub fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
109 let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?; 109 let let_stmt: &ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
110 if let_stmt.has_semi() { 110 if let_stmt.has_semi() {
111 return None; 111 return None;
112 } 112 }
@@ -136,7 +136,7 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit>
136 }) 136 })
137} 137}
138 138
139pub fn on_dot_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> { 139pub fn on_dot_typed(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
140 let before_dot_offset = offset - TextUnit::of_char('.'); 140 let before_dot_offset = offset - TextUnit::of_char('.');
141 141
142 let whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset).left_biased()?; 142 let whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset).left_biased()?;
@@ -151,7 +151,7 @@ pub fn on_dot_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit
151 .skip(1) 151 .skip(1)
152 .next()?; 152 .next()?;
153 153
154 ast::MethodCallExprNode::cast(method_call)?; 154 ast::MethodCallExpr::cast(method_call)?;
155 155
156 // find how much the _method call is indented 156 // find how much the _method call is indented
157 let method_chain_indent = method_call 157 let method_chain_indent = method_call
@@ -188,7 +188,7 @@ fn last_line_indent_in_whitespace(ws: &str) -> &str {
188 188
189fn remove_newline( 189fn remove_newline(
190 edit: &mut TextEditBuilder, 190 edit: &mut TextEditBuilder,
191 node: SyntaxNodeRef, 191 node: &SyntaxNode,
192 node_text: &str, 192 node_text: &str,
193 offset: TextUnit, 193 offset: TextUnit,
194) { 194) {
@@ -266,7 +266,7 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
266 } 266 }
267} 267}
268 268
269fn join_single_expr_block(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> { 269fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
270 let block = ast::Block::cast(node.parent()?)?; 270 let block = ast::Block::cast(node.parent()?)?;
271 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; 271 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
272 let expr = single_expr(block)?; 272 let expr = single_expr(block)?;
@@ -277,7 +277,7 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Op
277 Some(()) 277 Some(())
278} 278}
279 279
280fn single_expr(block: ast::Block) -> Option<ast::Expr> { 280fn single_expr(block: &ast::Block) -> Option<&ast::Expr> {
281 let mut res = None; 281 let mut res = None;
282 for child in block.syntax().children() { 282 for child in block.syntax().children() {
283 if let Some(expr) = ast::Expr::cast(child) { 283 if let Some(expr) = ast::Expr::cast(child) {
@@ -297,7 +297,7 @@ fn single_expr(block: ast::Block) -> Option<ast::Expr> {
297 res 297 res
298} 298}
299 299
300fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> { 300fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
301 let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; 301 let use_tree_list = ast::UseTreeList::cast(node.parent()?)?;
302 let (tree,) = use_tree_list.use_trees().collect_tuple()?; 302 let (tree,) = use_tree_list.use_trees().collect_tuple()?;
303 edit.replace( 303 edit.replace(
@@ -307,7 +307,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Opti
307 Some(()) 307 Some(())
308} 308}
309 309
310fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { 310fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
311 match left.kind() { 311 match left.kind() {
312 L_PAREN | L_BRACK => return "", 312 L_PAREN | L_BRACK => return "",
313 L_CURLY => { 313 L_CURLY => {
@@ -547,7 +547,7 @@ fn foo() {
547 547
548 fn check_join_lines_sel(before: &str, after: &str) { 548 fn check_join_lines_sel(before: &str, after: &str) {
549 let (sel, before) = extract_range(before); 549 let (sel, before) = extract_range(before);
550 let file = SourceFileNode::parse(&before); 550 let file = SourceFile::parse(&before);
551 let result = join_lines(&file, sel); 551 let result = join_lines(&file, sel);
552 let actual = result.edit.apply(&before); 552 let actual = result.edit.apply(&before);
553 assert_eq_text!(after, &actual); 553 assert_eq_text!(after, &actual);
@@ -626,7 +626,7 @@ pub fn handle_find_matching_brace() {
626 fn test_on_eq_typed() { 626 fn test_on_eq_typed() {
627 fn do_check(before: &str, after: &str) { 627 fn do_check(before: &str, after: &str) {
628 let (offset, before) = extract_offset(before); 628 let (offset, before) = extract_offset(before);
629 let file = SourceFileNode::parse(&before); 629 let file = SourceFile::parse(&before);
630 let result = on_eq_typed(&file, offset).unwrap(); 630 let result = on_eq_typed(&file, offset).unwrap();
631 let actual = result.edit.apply(&before); 631 let actual = result.edit.apply(&before);
632 assert_eq_text!(after, &actual); 632 assert_eq_text!(after, &actual);
@@ -670,7 +670,7 @@ fn foo() {
670 fn test_on_dot_typed() { 670 fn test_on_dot_typed() {
671 fn do_check(before: &str, after: &str) { 671 fn do_check(before: &str, after: &str) {
672 let (offset, before) = extract_offset(before); 672 let (offset, before) = extract_offset(before);
673 let file = SourceFileNode::parse(&before); 673 let file = SourceFile::parse(&before);
674 if let Some(result) = on_eq_typed(&file, offset) { 674 if let Some(result) = on_eq_typed(&file, offset) {
675 let actual = result.edit.apply(&before); 675 let actual = result.edit.apply(&before);
676 assert_eq_text!(after, &actual); 676 assert_eq_text!(after, &actual);
@@ -779,7 +779,7 @@ fn foo() {
779 fn test_on_enter() { 779 fn test_on_enter() {
780 fn apply_on_enter(before: &str) -> Option<String> { 780 fn apply_on_enter(before: &str) -> Option<String> {
781 let (offset, before) = extract_offset(before); 781 let (offset, before) = extract_offset(before);
782 let file = SourceFileNode::parse(&before); 782 let file = SourceFile::parse(&before);
783 let result = on_enter(&file, offset)?; 783 let result = on_enter(&file, offset)?;
784 let actual = result.edit.apply(&before); 784 let actual = result.edit.apply(&before);
785 let actual = add_cursor(&actual, result.cursor_position.unwrap()); 785 let actual = add_cursor(&actual, result.cursor_position.unwrap());