aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assist_ctx.rs5
-rw-r--r--crates/ra_hir/src/ty/tests.rs6
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs4
-rw-r--r--crates/ra_ide_api/src/extend_selection.rs6
-rw-r--r--crates/ra_ide_api/src/goto_type_definition.rs4
-rw-r--r--crates/ra_ide_api/src/matching_brace.rs6
-rw-r--r--crates/ra_ide_api/src/typing.rs15
-rw-r--r--crates/ra_syntax/src/algo.rs17
-rw-r--r--crates/ra_syntax/src/lib.rs6
-rw-r--r--crates/ra_syntax/src/syntax_node.rs1
10 files changed, 33 insertions, 37 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index 4d5a76de6..a12c3ed54 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -2,8 +2,9 @@ use hir::db::HirDatabase;
2use ra_db::FileRange; 2use ra_db::FileRange;
3use ra_fmt::{leading_indent, reindent}; 3use ra_fmt::{leading_indent, reindent};
4use ra_syntax::{ 4use ra_syntax::{
5 algo::{find_covering_element, find_node_at_offset, find_token_at_offset, TokenAtOffset}, 5 algo::{find_covering_element, find_node_at_offset},
6 AstNode, SourceFile, SyntaxElement, SyntaxNode, SyntaxToken, TextRange, TextUnit, 6 AstNode, SourceFile, SyntaxElement, SyntaxNode, SyntaxToken, TextRange, TextUnit,
7 TokenAtOffset,
7}; 8};
8use ra_text_edit::TextEditBuilder; 9use ra_text_edit::TextEditBuilder;
9 10
@@ -105,7 +106,7 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
105 } 106 }
106 107
107 pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> { 108 pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
108 find_token_at_offset(self.source_file.syntax(), self.frange.range.start()) 109 self.source_file.syntax().token_at_offset(self.frange.range.start())
109 } 110 }
110 111
111 pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<N> { 112 pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<N> {
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 706500484..676711d0a 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -3211,8 +3211,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
3211 ); 3211 );
3212 { 3212 {
3213 let file = db.parse(pos.file_id).ok().unwrap(); 3213 let file = db.parse(pos.file_id).ok().unwrap();
3214 let node = 3214 let node = file.syntax().token_at_offset(pos.offset).right_biased().unwrap().parent();
3215 algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
3216 let events = db.log_executed(|| { 3215 let events = db.log_executed(|| {
3217 SourceAnalyzer::new(&db, pos.file_id, &node, None); 3216 SourceAnalyzer::new(&db, pos.file_id, &node, None);
3218 }); 3217 });
@@ -3232,8 +3231,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
3232 3231
3233 { 3232 {
3234 let file = db.parse(pos.file_id).ok().unwrap(); 3233 let file = db.parse(pos.file_id).ok().unwrap();
3235 let node = 3234 let node = file.syntax().token_at_offset(pos.offset).right_biased().unwrap().parent();
3236 algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
3237 let events = db.log_executed(|| { 3235 let events = db.log_executed(|| {
3238 SourceAnalyzer::new(&db, pos.file_id, &node, None); 3236 SourceAnalyzer::new(&db, pos.file_id, &node, None);
3239 }); 3237 });
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index 2f78d5409..968f5694b 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -1,6 +1,6 @@
1use hir::source_binder; 1use hir::source_binder;
2use ra_syntax::{ 2use ra_syntax::{
3 algo::{find_covering_element, find_node_at_offset, find_token_at_offset}, 3 algo::{find_covering_element, find_node_at_offset},
4 ast, AstNode, Parse, SourceFile, 4 ast, AstNode, Parse, SourceFile,
5 SyntaxKind::*, 5 SyntaxKind::*,
6 SyntaxNode, SyntaxToken, TextRange, TextUnit, 6 SyntaxNode, SyntaxToken, TextRange, TextUnit,
@@ -48,7 +48,7 @@ impl<'a> CompletionContext<'a> {
48 ) -> Option<CompletionContext<'a>> { 48 ) -> Option<CompletionContext<'a>> {
49 let module = source_binder::module_from_position(db, position); 49 let module = source_binder::module_from_position(db, position);
50 let token = 50 let token =
51 find_token_at_offset(original_parse.tree().syntax(), position.offset).left_biased()?; 51 original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?;
52 let analyzer = 52 let analyzer =
53 hir::SourceAnalyzer::new(db, position.file_id, &token.parent(), Some(position.offset)); 53 hir::SourceAnalyzer::new(db, position.file_id, &token.parent(), Some(position.offset));
54 let mut ctx = CompletionContext { 54 let mut ctx = CompletionContext {
diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs
index f78c562af..edbf622c1 100644
--- a/crates/ra_ide_api/src/extend_selection.rs
+++ b/crates/ra_ide_api/src/extend_selection.rs
@@ -1,10 +1,10 @@
1use ra_db::SourceDatabase; 1use ra_db::SourceDatabase;
2use ra_syntax::{ 2use ra_syntax::{
3 algo::{find_covering_element, find_token_at_offset, TokenAtOffset}, 3 algo::find_covering_element,
4 ast::{self, AstNode, AstToken}, 4 ast::{self, AstNode, AstToken},
5 Direction, NodeOrToken, 5 Direction, NodeOrToken,
6 SyntaxKind::*, 6 SyntaxKind::*,
7 SyntaxNode, SyntaxToken, TextRange, TextUnit, T, 7 SyntaxNode, SyntaxToken, TextRange, TextUnit, TokenAtOffset, T,
8}; 8};
9 9
10use crate::{db::RootDatabase, FileRange}; 10use crate::{db::RootDatabase, FileRange};
@@ -34,7 +34,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange
34 34
35 if range.is_empty() { 35 if range.is_empty() {
36 let offset = range.start(); 36 let offset = range.start();
37 let mut leaves = find_token_at_offset(root, offset); 37 let mut leaves = root.token_at_offset(offset);
38 if leaves.clone().all(|it| it.kind() == WHITESPACE) { 38 if leaves.clone().all(|it| it.kind() == WHITESPACE) {
39 return Some(extend_ws(root, leaves.next()?, offset)); 39 return Some(extend_ws(root, leaves.next()?, offset));
40 } 40 }
diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs
index 007259d9e..72884e5ca 100644
--- a/crates/ra_ide_api/src/goto_type_definition.rs
+++ b/crates/ra_ide_api/src/goto_type_definition.rs
@@ -1,5 +1,5 @@
1use ra_db::SourceDatabase; 1use ra_db::SourceDatabase;
2use ra_syntax::{algo::find_token_at_offset, ast, AstNode}; 2use ra_syntax::{ast, AstNode};
3 3
4use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo}; 4use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo};
5 5
@@ -9,7 +9,7 @@ pub(crate) fn goto_type_definition(
9) -> Option<RangeInfo<Vec<NavigationTarget>>> { 9) -> Option<RangeInfo<Vec<NavigationTarget>>> {
10 let parse = db.parse(position.file_id); 10 let parse = db.parse(position.file_id);
11 11
12 let node = find_token_at_offset(parse.tree().syntax(), position.offset).find_map(|token| { 12 let node = parse.tree().syntax().token_at_offset(position.offset).find_map(|token| {
13 token 13 token
14 .parent() 14 .parent()
15 .ancestors() 15 .ancestors()
diff --git a/crates/ra_ide_api/src/matching_brace.rs b/crates/ra_ide_api/src/matching_brace.rs
index 1e2fac848..e802d01e4 100644
--- a/crates/ra_ide_api/src/matching_brace.rs
+++ b/crates/ra_ide_api/src/matching_brace.rs
@@ -1,9 +1,11 @@
1use ra_syntax::{algo::find_token_at_offset, ast::AstNode, SourceFile, SyntaxKind, TextUnit, T}; 1use ra_syntax::{ast::AstNode, SourceFile, SyntaxKind, TextUnit, T};
2 2
3pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> { 3pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
4 const BRACES: &[SyntaxKind] = 4 const BRACES: &[SyntaxKind] =
5 &[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>]]; 5 &[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>]];
6 let (brace_node, brace_idx) = find_token_at_offset(file.syntax(), offset) 6 let (brace_node, brace_idx) = file
7 .syntax()
8 .token_at_offset(offset)
7 .filter_map(|node| { 9 .filter_map(|node| {
8 let idx = BRACES.iter().position(|&brace| brace == node.kind())?; 10 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
9 Some((node, idx)) 11 Some((node, idx))
diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs
index 5a1cbcc49..6b3fd5904 100644
--- a/crates/ra_ide_api/src/typing.rs
+++ b/crates/ra_ide_api/src/typing.rs
@@ -1,11 +1,11 @@
1use ra_db::{FilePosition, SourceDatabase}; 1use ra_db::{FilePosition, SourceDatabase};
2use ra_fmt::leading_indent; 2use ra_fmt::leading_indent;
3use ra_syntax::{ 3use ra_syntax::{
4 algo::{find_node_at_offset, find_token_at_offset, TokenAtOffset}, 4 algo::find_node_at_offset,
5 ast::{self, AstToken}, 5 ast::{self, AstToken},
6 AstNode, SmolStr, SourceFile, 6 AstNode, SmolStr, SourceFile,
7 SyntaxKind::*, 7 SyntaxKind::*,
8 SyntaxToken, TextRange, TextUnit, 8 SyntaxToken, TextRange, TextUnit, TokenAtOffset,
9}; 9};
10use ra_text_edit::{TextEdit, TextEditBuilder}; 10use ra_text_edit::{TextEdit, TextEditBuilder};
11 11
@@ -14,7 +14,9 @@ use crate::{db::RootDatabase, SourceChange, SourceFileEdit};
14pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> { 14pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
15 let parse = db.parse(position.file_id); 15 let parse = db.parse(position.file_id);
16 let file = parse.tree(); 16 let file = parse.tree();
17 let comment = find_token_at_offset(file.syntax(), position.offset) 17 let comment = file
18 .syntax()
19 .token_at_offset(position.offset)
18 .left_biased() 20 .left_biased()
19 .and_then(ast::Comment::cast)?; 21 .and_then(ast::Comment::cast)?;
20 22
@@ -45,7 +47,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
45} 47}
46 48
47fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> { 49fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
48 let ws = match find_token_at_offset(file.syntax(), token.text_range().start()) { 50 let ws = match file.syntax().token_at_offset(token.text_range().start()) {
49 TokenAtOffset::Between(l, r) => { 51 TokenAtOffset::Between(l, r) => {
50 assert!(r == *token); 52 assert!(r == *token);
51 l 53 l
@@ -91,7 +93,10 @@ pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option<
91 let parse = db.parse(position.file_id); 93 let parse = db.parse(position.file_id);
92 assert_eq!(parse.tree().syntax().text().char_at(position.offset), Some('.')); 94 assert_eq!(parse.tree().syntax().text().char_at(position.offset), Some('.'));
93 95
94 let whitespace = find_token_at_offset(parse.tree().syntax(), position.offset) 96 let whitespace = parse
97 .tree()
98 .syntax()
99 .token_at_offset(position.offset)
95 .left_biased() 100 .left_biased()
96 .and_then(ast::Whitespace::cast)?; 101 .and_then(ast::Whitespace::cast)?;
97 102
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index ecd42c133..45f624810 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -5,16 +5,9 @@ use std::ops::RangeInclusive;
5use itertools::Itertools; 5use itertools::Itertools;
6 6
7use crate::{ 7use crate::{
8 AstNode, Direction, NodeOrToken, SourceFile, SyntaxElement, SyntaxNode, SyntaxNodePtr, 8 AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
9 SyntaxToken, TextRange, TextUnit,
10}; 9};
11 10
12pub use rowan::TokenAtOffset;
13
14pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffset<SyntaxToken> {
15 node.token_at_offset(offset)
16}
17
18/// Returns ancestors of the node at the offset, sorted by length. This should 11/// Returns ancestors of the node at the offset, sorted by length. This should
19/// do the right thing at an edge, e.g. when searching for expressions at `{ 12/// do the right thing at an edge, e.g. when searching for expressions at `{
20/// <|>foo }` we will get the name reference instead of the whole block, which 13/// <|>foo }` we will get the name reference instead of the whole block, which
@@ -24,7 +17,7 @@ pub fn ancestors_at_offset(
24 node: &SyntaxNode, 17 node: &SyntaxNode,
25 offset: TextUnit, 18 offset: TextUnit,
26) -> impl Iterator<Item = SyntaxNode> { 19) -> impl Iterator<Item = SyntaxNode> {
27 find_token_at_offset(node, offset) 20 node.token_at_offset(offset)
28 .map(|token| token.parent().ancestors()) 21 .map(|token| token.parent().ancestors())
29 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) 22 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
30} 23}
@@ -137,14 +130,14 @@ fn with_children(
137 let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>(); 130 let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>();
138 let new_node = 131 let new_node =
139 rowan::GreenNode::new(rowan::cursor::SyntaxKind(parent.kind() as u16), new_children); 132 rowan::GreenNode::new(rowan::cursor::SyntaxKind(parent.kind() as u16), new_children);
140 let new_file_node = parent.replace_with(new_node); 133 let new_root_node = parent.replace_with(new_node);
141 let file = SourceFile::new(new_file_node); 134 let new_root_node = SyntaxNode::new_root(new_root_node);
142 135
143 // FIXME: use a more elegant way to re-fetch the node (#1185), make 136 // FIXME: use a more elegant way to re-fetch the node (#1185), make
144 // `range` private afterwards 137 // `range` private afterwards
145 let mut ptr = SyntaxNodePtr::new(parent); 138 let mut ptr = SyntaxNodePtr::new(parent);
146 ptr.range = TextRange::offset_len(ptr.range().start(), len); 139 ptr.range = TextRange::offset_len(ptr.range().start(), len);
147 ptr.to_node(file.syntax()).to_owned() 140 ptr.to_node(&new_root_node)
148} 141}
149 142
150fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize { 143fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize {
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 7b778f38c..d02078256 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -44,12 +44,10 @@ pub use crate::{
44 syntax_error::{Location, SyntaxError, SyntaxErrorKind}, 44 syntax_error::{Location, SyntaxError, SyntaxErrorKind},
45 syntax_node::{ 45 syntax_node::{
46 Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder, 46 Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder,
47 WalkEvent,
48 }, 47 },
49}; 48};
50pub use ra_parser::SyntaxKind; 49pub use ra_parser::{SyntaxKind, T};
51pub use ra_parser::T; 50pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit, TokenAtOffset, WalkEvent};
52pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit};
53 51
54/// `Parse` is the result of the parsing: a syntax tree and a collection of 52/// `Parse` is the result of the parsing: a syntax tree and a collection of
55/// errors. 53/// errors.
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index 95795a27a..b2f5b8c64 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -14,7 +14,6 @@ use crate::{
14 Parse, SmolStr, SyntaxKind, TextUnit, 14 Parse, SmolStr, SyntaxKind, TextUnit,
15}; 15};
16 16
17pub use rowan::WalkEvent;
18pub(crate) use rowan::{GreenNode, GreenToken}; 17pub(crate) use rowan::{GreenNode, GreenToken};
19 18
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 19#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]