aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/extend_selection.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-26 12:04:22 +0000
committerGitHub <[email protected]>2020-02-26 12:04:22 +0000
commit5c64ad27e041bcdb281c0a751720ceb3a6369d04 (patch)
tree12d89798f61b276f8bd640db07276a7d4e92b1c2 /crates/ra_ide/src/extend_selection.rs
parent04deae3dba7c9b7054f7a1d64e4b93a05aecc132 (diff)
parentc3a4c4429de83450654795534e64e878a774a088 (diff)
Merge pull request #3222 from matklad/identity
Introduce Semantics API
Diffstat (limited to 'crates/ra_ide/src/extend_selection.rs')
-rw-r--r--crates/ra_ide/src/extend_selection.rs51
1 files changed, 23 insertions, 28 deletions
diff --git a/crates/ra_ide/src/extend_selection.rs b/crates/ra_ide/src/extend_selection.rs
index 1e7d0621a..86e6f12d7 100644
--- a/crates/ra_ide/src/extend_selection.rs
+++ b/crates/ra_ide/src/extend_selection.rs
@@ -2,26 +2,26 @@
2 2
3use std::iter::successors; 3use std::iter::successors;
4 4
5use hir::db::AstDatabase; 5use hir::Semantics;
6use ra_db::SourceDatabase;
7use ra_ide_db::RootDatabase; 6use ra_ide_db::RootDatabase;
8use ra_syntax::{ 7use ra_syntax::{
9 algo::find_covering_element, 8 algo::{self, find_covering_element},
10 ast::{self, AstNode, AstToken}, 9 ast::{self, AstNode, AstToken},
11 Direction, NodeOrToken, SyntaxElement, 10 Direction, NodeOrToken,
12 SyntaxKind::{self, *}, 11 SyntaxKind::{self, *},
13 SyntaxNode, SyntaxToken, TextRange, TextUnit, TokenAtOffset, T, 12 SyntaxNode, SyntaxToken, TextRange, TextUnit, TokenAtOffset, T,
14}; 13};
15 14
16use crate::{expand::descend_into_macros, FileId, FileRange}; 15use crate::FileRange;
17 16
18pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { 17pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange {
19 let src = db.parse(frange.file_id).tree(); 18 let sema = Semantics::new(db);
20 try_extend_selection(db, src.syntax(), frange).unwrap_or(frange.range) 19 let src = sema.parse(frange.file_id);
20 try_extend_selection(&sema, src.syntax(), frange).unwrap_or(frange.range)
21} 21}
22 22
23fn try_extend_selection( 23fn try_extend_selection(
24 db: &RootDatabase, 24 sema: &Semantics<RootDatabase>,
25 root: &SyntaxNode, 25 root: &SyntaxNode,
26 frange: FileRange, 26 frange: FileRange,
27) -> Option<TextRange> { 27) -> Option<TextRange> {
@@ -86,7 +86,7 @@ fn try_extend_selection(
86 // if we are in single token_tree, we maybe live in macro or attr 86 // if we are in single token_tree, we maybe live in macro or attr
87 if node.kind() == TOKEN_TREE { 87 if node.kind() == TOKEN_TREE {
88 if let Some(macro_call) = node.ancestors().find_map(ast::MacroCall::cast) { 88 if let Some(macro_call) = node.ancestors().find_map(ast::MacroCall::cast) {
89 if let Some(range) = extend_tokens_from_range(db, frange.file_id, macro_call, range) { 89 if let Some(range) = extend_tokens_from_range(sema, macro_call, range) {
90 return Some(range); 90 return Some(range);
91 } 91 }
92 } 92 }
@@ -96,7 +96,7 @@ fn try_extend_selection(
96 return Some(node.text_range()); 96 return Some(node.text_range());
97 } 97 }
98 98
99 let node = shallowest_node(&node.into()).unwrap(); 99 let node = shallowest_node(&node.into());
100 100
101 if node.parent().map(|n| list_kinds.contains(&n.kind())) == Some(true) { 101 if node.parent().map(|n| list_kinds.contains(&n.kind())) == Some(true) {
102 if let Some(range) = extend_list_item(&node) { 102 if let Some(range) = extend_list_item(&node) {
@@ -108,8 +108,7 @@ fn try_extend_selection(
108} 108}
109 109
110fn extend_tokens_from_range( 110fn extend_tokens_from_range(
111 db: &RootDatabase, 111 sema: &Semantics<RootDatabase>,
112 file_id: FileId,
113 macro_call: ast::MacroCall, 112 macro_call: ast::MacroCall,
114 original_range: TextRange, 113 original_range: TextRange,
115) -> Option<TextRange> { 114) -> Option<TextRange> {
@@ -130,25 +129,21 @@ fn extend_tokens_from_range(
130 } 129 }
131 130
132 // compute original mapped token range 131 // compute original mapped token range
133 let expanded = { 132 let extended = {
134 let first_node = descend_into_macros(db, file_id, first_token.clone()); 133 let fst_expanded = sema.descend_into_macros(first_token.clone());
135 let first_node = first_node.map(|it| it.text_range()); 134 let lst_expanded = sema.descend_into_macros(last_token.clone());
136 135 let mut lca = algo::least_common_ancestor(&fst_expanded.parent(), &lst_expanded.parent())?;
137 let last_node = descend_into_macros(db, file_id, last_token.clone()); 136 lca = shallowest_node(&lca);
138 if last_node.file_id == file_id.into() || first_node.file_id != last_node.file_id { 137 if lca.first_token() == Some(fst_expanded) && lca.last_token() == Some(lst_expanded) {
139 return None; 138 lca = lca.parent()?;
140 } 139 }
141 first_node.map(|it| union_range(it, last_node.value.text_range())) 140 lca
142 }; 141 };
143 142
144 // Compute parent node range 143 // Compute parent node range
145 let src = db.parse_or_expand(expanded.file_id)?;
146 let parent = shallowest_node(&find_covering_element(&src, expanded.value))?.parent()?;
147
148 let validate = |token: &SyntaxToken| { 144 let validate = |token: &SyntaxToken| {
149 let node = descend_into_macros(db, file_id, token.clone()); 145 let expanded = sema.descend_into_macros(token.clone());
150 node.file_id == expanded.file_id 146 algo::least_common_ancestor(&extended, &expanded.parent()).as_ref() == Some(&extended)
151 && node.value.text_range().is_subrange(&parent.text_range())
152 }; 147 };
153 148
154 // Find the first and last text range under expanded parent 149 // Find the first and last text range under expanded parent
@@ -191,8 +186,8 @@ fn union_range(range: TextRange, r: TextRange) -> TextRange {
191} 186}
192 187
193/// Find the shallowest node with same range, which allows us to traverse siblings. 188/// Find the shallowest node with same range, which allows us to traverse siblings.
194fn shallowest_node(node: &SyntaxElement) -> Option<SyntaxNode> { 189fn shallowest_node(node: &SyntaxNode) -> SyntaxNode {
195 node.ancestors().take_while(|n| n.text_range() == node.text_range()).last() 190 node.ancestors().take_while(|n| n.text_range() == node.text_range()).last().unwrap()
196} 191}
197 192
198fn extend_single_word_in_comment_or_string( 193fn extend_single_word_in_comment_or_string(