aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/typing.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-07-19 12:15:55 +0100
committerbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-07-19 12:15:55 +0100
commitf209843e31af7f0e0212aa28ffec2efad2a70c6f (patch)
tree548227da78a3bea644f57714d075410c0bdf7469 /crates/ra_ide_api/src/typing.rs
parent58d4983ba5745975446d60f2886d96f8d2adf0f2 (diff)
parentd4a66166c002f0a49e41d856a49cb5685ac93202 (diff)
Merge #1545
1545: migrate ra_syntax to the new rowan API r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/typing.rs')
-rw-r--r--crates/ra_ide_api/src/typing.rs19
1 files changed, 10 insertions, 9 deletions
diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs
index 01eb32b2f..ad0ababcc 100644
--- a/crates/ra_ide_api/src/typing.rs
+++ b/crates/ra_ide_api/src/typing.rs
@@ -1,15 +1,16 @@
1use crate::{db::RootDatabase, SourceChange, SourceFileEdit};
2use ra_db::{FilePosition, SourceDatabase}; 1use ra_db::{FilePosition, SourceDatabase};
3use ra_fmt::leading_indent; 2use ra_fmt::leading_indent;
4use ra_syntax::{ 3use ra_syntax::{
5 algo::{find_node_at_offset, find_token_at_offset, TokenAtOffset}, 4 algo::{find_node_at_offset, find_token_at_offset, TokenAtOffset},
6 ast::{self, AstToken}, 5 ast::{self, AstToken},
7 AstNode, SourceFile, 6 AstNode, SmolStr, SourceFile,
8 SyntaxKind::*, 7 SyntaxKind::*,
9 SyntaxToken, TextRange, TextUnit, 8 SyntaxToken, TextRange, TextUnit,
10}; 9};
11use ra_text_edit::{TextEdit, TextEditBuilder}; 10use ra_text_edit::{TextEdit, TextEditBuilder};
12 11
12use crate::{db::RootDatabase, SourceChange, SourceFileEdit};
13
13pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> { 14pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
14 let parse = db.parse(position.file_id); 15 let parse = db.parse(position.file_id);
15 let file = parse.tree(); 16 let file = parse.tree();
@@ -43,15 +44,15 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
43 ) 44 )
44} 45}
45 46
46fn node_indent<'a>(file: &'a SourceFile, token: SyntaxToken) -> Option<&'a str> { 47fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
47 let ws = match find_token_at_offset(file.syntax(), token.range().start()) { 48 let ws = match find_token_at_offset(file.syntax(), token.range().start()) {
48 TokenAtOffset::Between(l, r) => { 49 TokenAtOffset::Between(l, r) => {
49 assert!(r == token); 50 assert!(r == *token);
50 l 51 l
51 } 52 }
52 TokenAtOffset::Single(n) => { 53 TokenAtOffset::Single(n) => {
53 assert!(n == token); 54 assert!(n == *token);
54 return Some(""); 55 return Some("".into());
55 } 56 }
56 TokenAtOffset::None => unreachable!(), 57 TokenAtOffset::None => unreachable!(),
57 }; 58 };
@@ -60,12 +61,12 @@ fn node_indent<'a>(file: &'a SourceFile, token: SyntaxToken) -> Option<&'a str>
60 } 61 }
61 let text = ws.text(); 62 let text = ws.text();
62 let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0); 63 let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0);
63 Some(&text[pos..]) 64 Some(text[pos..].into())
64} 65}
65 66
66pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option<TextEdit> { 67pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option<TextEdit> {
67 assert_eq!(file.syntax().text().char_at(eq_offset), Some('=')); 68 assert_eq!(file.syntax().text().char_at(eq_offset), Some('='));
68 let let_stmt: &ast::LetStmt = find_node_at_offset(file.syntax(), eq_offset)?; 69 let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), eq_offset)?;
69 if let_stmt.has_semi() { 70 if let_stmt.has_semi() {
70 return None; 71 return None;
71 } 72 }
@@ -141,7 +142,7 @@ mod tests {
141 edit.insert(offset, "=".to_string()); 142 edit.insert(offset, "=".to_string());
142 let before = edit.finish().apply(&before); 143 let before = edit.finish().apply(&before);
143 let parse = SourceFile::parse(&before); 144 let parse = SourceFile::parse(&before);
144 if let Some(result) = on_eq_typed(parse.tree(), offset) { 145 if let Some(result) = on_eq_typed(&parse.tree(), offset) {
145 let actual = result.apply(&before); 146 let actual = result.apply(&before);
146 assert_eq_text!(after, &actual); 147 assert_eq_text!(after, &actual);
147 } else { 148 } else {