Move renames into ra_analysis

This commit is contained in:
DJMcNab 2018-12-30 18:14:55 +00:00
parent 0f95d8523e
commit c402b007a3
3 changed files with 39 additions and 15 deletions

View file

@ -288,7 +288,11 @@ impl AnalysisImpl {
Some(it) => it, Some(it) => it,
}; };
let mut ret = vec![(position.file_id, binding.syntax().range())]; let mut ret = binding
.name()
.into_iter()
.map(|name| (position.file_id, name.syntax().range()))
.collect::<Vec<_>>();
ret.extend( ret.extend(
descr descr
.scopes(&*self.db) .scopes(&*self.db)
@ -505,7 +509,25 @@ impl AnalysisImpl {
let infer = function.infer(&*self.db)?; let infer = function.infer(&*self.db)?;
Ok(infer.type_of_node(node).map(|t| t.to_string())) Ok(infer.type_of_node(node).map(|t| t.to_string()))
} }
pub fn rename(
&self,
position: FilePosition,
new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> {
let res = self
.find_all_refs(position)?
.iter()
.map(|(file_id, text_range)| SourceFileEdit {
file_id: *file_id,
edit: {
let mut builder = ra_text_edit::TextEditBuilder::new();
builder.replace(*text_range, new_name.into());
builder.finish()
},
})
.collect::<Vec<_>>();
Ok(res)
}
fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> { fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> {
let name = name_ref.text(); let name = name_ref.text();
let mut query = Query::new(name.to_string()); let mut query = Query::new(name.to_string());

View file

@ -367,6 +367,13 @@ impl Analysis {
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> { pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
self.imp.type_of(frange) self.imp.type_of(frange)
} }
pub fn rename(
&self,
position: FilePosition,
new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> {
self.imp.rename(position, new_name)
}
} }
pub struct LibraryData { pub struct LibraryData {

View file

@ -557,24 +557,19 @@ pub fn handle_rename(world: ServerWorld, params: RenameParams) -> Result<Option<
.into()); .into());
} }
let refs = world let renames = world
.analysis() .analysis()
.find_all_refs(FilePosition { file_id, offset })?; .rename(FilePosition { file_id, offset }, &*params.new_name)?;
if refs.is_empty() { if renames.is_empty() {
return Ok(None); return Ok(None);
} }
let mut changes = HashMap::new(); let mut changes = HashMap::new();
for r in refs { for edit in renames {
if let Ok(loc) = to_location(r.0, r.1, &world, &line_index) { changes
changes .entry(file_id.try_conv_with(&world)?)
.entry(loc.uri) .or_insert_with(Vec::new)
.or_insert_with(Vec::new) .extend(edit.edit.conv_with(&line_index));
.push(TextEdit {
range: loc.range,
new_text: params.new_name.clone(),
});
}
} }
Ok(Some(WorkspaceEdit { Ok(Some(WorkspaceEdit {