diff options
Diffstat (limited to 'crates/ra_ide/src/references')
-rw-r--r-- | crates/ra_ide/src/references/classify.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide/src/references/rename.rs | 54 |
2 files changed, 55 insertions, 5 deletions
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 5cea805ec..b716d32e5 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! Functions that are used to classify an element from its definition or reference. | 1 | //! Functions that are used to classify an element from its definition or reference. |
2 | 2 | ||
3 | use hir::{FromSource, Module, ModuleSource, PathResolution, Source, SourceAnalyzer}; | 3 | use hir::{FromSource, InFile, Module, ModuleSource, PathResolution, SourceAnalyzer}; |
4 | use ra_prof::profile; | 4 | use ra_prof::profile; |
5 | use ra_syntax::{ast, match_ast, AstNode}; | 5 | use ra_syntax::{ast, match_ast, AstNode}; |
6 | use test_utils::tested_by; | 6 | use test_utils::tested_by; |
@@ -11,7 +11,7 @@ use super::{ | |||
11 | }; | 11 | }; |
12 | use crate::db::RootDatabase; | 12 | use crate::db::RootDatabase; |
13 | 13 | ||
14 | pub(crate) fn classify_name(db: &RootDatabase, name: Source<&ast::Name>) -> Option<NameDefinition> { | 14 | pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Option<NameDefinition> { |
15 | let _p = profile("classify_name"); | 15 | let _p = profile("classify_name"); |
16 | let parent = name.value.syntax().parent()?; | 16 | let parent = name.value.syntax().parent()?; |
17 | 17 | ||
@@ -117,7 +117,7 @@ pub(crate) fn classify_name(db: &RootDatabase, name: Source<&ast::Name>) -> Opti | |||
117 | 117 | ||
118 | pub(crate) fn classify_name_ref( | 118 | pub(crate) fn classify_name_ref( |
119 | db: &RootDatabase, | 119 | db: &RootDatabase, |
120 | name_ref: Source<&ast::NameRef>, | 120 | name_ref: InFile<&ast::NameRef>, |
121 | ) -> Option<NameDefinition> { | 121 | ) -> Option<NameDefinition> { |
122 | let _p = profile("classify_name_ref"); | 122 | let _p = profile("classify_name_ref"); |
123 | 123 | ||
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index d58496049..b804d5f6d 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use hir::ModuleSource; | 3 | use hir::ModuleSource; |
4 | use ra_db::{RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt}; | 4 | use ra_db::{RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt}; |
5 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; | 5 | use ra_syntax::{algo::find_node_at_offset, ast, tokenize, AstNode, SyntaxKind, SyntaxNode}; |
6 | use ra_text_edit::TextEdit; | 6 | use ra_text_edit::TextEdit; |
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
@@ -17,6 +17,13 @@ pub(crate) fn rename( | |||
17 | position: FilePosition, | 17 | position: FilePosition, |
18 | new_name: &str, | 18 | new_name: &str, |
19 | ) -> Option<RangeInfo<SourceChange>> { | 19 | ) -> Option<RangeInfo<SourceChange>> { |
20 | let tokens = tokenize(new_name); | ||
21 | if tokens.len() != 1 | ||
22 | || (tokens[0].kind != SyntaxKind::IDENT && tokens[0].kind != SyntaxKind::UNDERSCORE) | ||
23 | { | ||
24 | return None; | ||
25 | } | ||
26 | |||
20 | let parse = db.parse(position.file_id); | 27 | let parse = db.parse(position.file_id); |
21 | if let Some((ast_name, ast_module)) = | 28 | if let Some((ast_name, ast_module)) = |
22 | find_name_and_module_at_offset(parse.tree().syntax(), position) | 29 | find_name_and_module_at_offset(parse.tree().syntax(), position) |
@@ -55,7 +62,7 @@ fn rename_mod( | |||
55 | ) -> Option<SourceChange> { | 62 | ) -> Option<SourceChange> { |
56 | let mut source_file_edits = Vec::new(); | 63 | let mut source_file_edits = Vec::new(); |
57 | let mut file_system_edits = Vec::new(); | 64 | let mut file_system_edits = Vec::new(); |
58 | let module_src = hir::Source { file_id: position.file_id.into(), value: ast_module.clone() }; | 65 | let module_src = hir::InFile { file_id: position.file_id.into(), value: ast_module.clone() }; |
59 | if let Some(module) = hir::Module::from_declaration(db, module_src) { | 66 | if let Some(module) = hir::Module::from_declaration(db, module_src) { |
60 | let src = module.definition_source(db); | 67 | let src = module.definition_source(db); |
61 | let file_id = src.file_id.original_file(db); | 68 | let file_id = src.file_id.original_file(db); |
@@ -124,6 +131,49 @@ mod tests { | |||
124 | }; | 131 | }; |
125 | 132 | ||
126 | #[test] | 133 | #[test] |
134 | fn test_rename_to_underscore() { | ||
135 | test_rename( | ||
136 | r#" | ||
137 | fn main() { | ||
138 | let i<|> = 1; | ||
139 | }"#, | ||
140 | "_", | ||
141 | r#" | ||
142 | fn main() { | ||
143 | let _ = 1; | ||
144 | }"#, | ||
145 | ); | ||
146 | } | ||
147 | |||
148 | #[test] | ||
149 | fn test_rename_to_raw_identifier() { | ||
150 | test_rename( | ||
151 | r#" | ||
152 | fn main() { | ||
153 | let i<|> = 1; | ||
154 | }"#, | ||
155 | "r#fn", | ||
156 | r#" | ||
157 | fn main() { | ||
158 | let r#fn = 1; | ||
159 | }"#, | ||
160 | ); | ||
161 | } | ||
162 | |||
163 | #[test] | ||
164 | fn test_rename_to_invalid_identifier() { | ||
165 | let (analysis, position) = single_file_with_position( | ||
166 | " | ||
167 | fn main() { | ||
168 | let i<|> = 1; | ||
169 | }", | ||
170 | ); | ||
171 | let new_name = "invalid!"; | ||
172 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
173 | assert!(source_change.is_none()); | ||
174 | } | ||
175 | |||
176 | #[test] | ||
127 | fn test_rename_for_local() { | 177 | fn test_rename_for_local() { |
128 | test_rename( | 178 | test_rename( |
129 | r#" | 179 | r#" |